xref: /linux/drivers/staging/media/atomisp/pci/sh_css.c (revision 189f164e573e18d9f8876dbd3ad8fcbe11f93037)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Support for Intel Camera Imaging ISP subsystem.
4  * Copyright (c) 2015, Intel Corporation.
5  */
6 
7 /*! \file */
8 #include <linux/mm.h>
9 #include <linux/slab.h>
10 #include <linux/vmalloc.h>
11 
12 #include "hmm.h"
13 
14 #include "atomisp_internal.h"
15 
16 #include "ia_css.h"
17 #include "sh_css_hrt.h"		/* only for file 2 MIPI */
18 #include "ia_css_buffer.h"
19 #include "ia_css_binary.h"
20 #include "sh_css_internal.h"
21 #include "sh_css_mipi.h"
22 #include "sh_css_sp.h"		/* sh_css_sp_group */
23 #include "ia_css_isys.h"
24 #include "ia_css_frame.h"
25 #include "sh_css_defs.h"
26 #include "sh_css_firmware.h"
27 #include "sh_css_params.h"
28 #include "sh_css_params_internal.h"
29 #include "sh_css_param_shading.h"
30 #include "ia_css_refcount.h"
31 #include "ia_css_rmgr.h"
32 #include "ia_css_debug.h"
33 #include "ia_css_debug_pipe.h"
34 #include "ia_css_device_access.h"
35 #include "device_access.h"
36 #include "sh_css_legacy.h"
37 #include "ia_css_pipeline.h"
38 #include "ia_css_stream.h"
39 #include "sh_css_stream_format.h"
40 #include "ia_css_pipe.h"
41 #include "ia_css_util.h"
42 #include "ia_css_pipe_util.h"
43 #include "ia_css_pipe_binarydesc.h"
44 #include "ia_css_pipe_stagedesc.h"
45 
46 #include "tag.h"
47 #include "assert_support.h"
48 #include "math_support.h"
49 #include "sw_event_global.h"			/* Event IDs.*/
50 #include "ia_css_ifmtr.h"
51 #include "input_system.h"
52 #include "mmu_device.h"		/* mmu_set_page_table_base_index(), ... */
53 #include "ia_css_mmu_private.h" /* sh_css_mmu_set_page_table_base_index() */
54 #include "gdc_device.h"		/* HRT_GDC_N */
55 #include "dma.h"		/* dma_set_max_burst_size() */
56 #include "irq.h"		/* virq */
57 #include "sp.h"			/* cnd_sp_irq_enable() */
58 #include "isp.h"		/* cnd_isp_irq_enable, ISP_VEC_NELEMS */
59 #include "gp_device.h"		/* gp_device_reg_store() */
60 #include <gpio_global.h>
61 #include <gpio_private.h>
62 #include "timed_ctrl.h"
63 #include "ia_css_inputfifo.h"
64 #define WITH_PC_MONITORING  0
65 
66 #define SH_CSS_VIDEO_BUFFER_ALIGNMENT 0
67 
68 
69 #include "ia_css_spctrl.h"
70 #include "ia_css_version_data.h"
71 #include "sh_css_struct.h"
72 #include "ia_css_bufq.h"
73 #include "ia_css_timer.h" /* clock_value_t */
74 
75 #include "isp/modes/interface/input_buf.isp.h"
76 
77 /* Name of the sp program: should not be built-in */
78 #define SP_PROG_NAME "sp"
79 /* Size of Refcount List */
80 #define REFCOUNT_SIZE 1000
81 
82 /*
83  * for JPEG, we don't know the length of the image upfront,
84  * but since we support sensor up to 16MP, we take this as
85  * upper limit.
86  */
87 #define JPEG_BYTES (16 * 1024 * 1024)
88 
89 struct sh_css my_css;
90 
91 int  __printf(1, 0) (*sh_css_printf)(const char *fmt, va_list args) = NULL;
92 
93 /*
94  * modes of work: stream_create and stream_destroy will update the save/restore
95  * data only when in working mode, not suspend/resume
96  */
97 enum ia_sh_css_modes {
98 	sh_css_mode_none = 0,
99 	sh_css_mode_working,
100 	sh_css_mode_suspend,
101 	sh_css_mode_resume
102 };
103 
104 /**
105  * struct sh_css_stream_seed - a stream seed, to save and restore the
106  * stream data.
107  *
108  * @orig_stream:	pointer to restore the original handle
109  * @stream:		handle, used as ID too.
110  * @stream_config:	stream config struct
111  * @num_pipes:		number of pipes
112  * @pipes:		pipe handles
113  * @orig_pipes:		pointer to restore original handle
114  * @pipe_config:	pipe config structs
115  *
116  * the stream seed contains all the data required to "grow" the seed again
117  * after it was closed.
118 */
119 struct sh_css_stream_seed {
120 	struct ia_css_stream		**orig_stream;
121 	struct ia_css_stream		*stream;
122 	struct ia_css_stream_config	stream_config;
123 	int				num_pipes;
124 	struct ia_css_pipe		*pipes[IA_CSS_PIPE_ID_NUM];
125 	struct ia_css_pipe		**orig_pipes[IA_CSS_PIPE_ID_NUM];
126 	struct ia_css_pipe_config	pipe_config[IA_CSS_PIPE_ID_NUM];
127 };
128 
129 #define MAX_ACTIVE_STREAMS	5
130 /*
131  * A global struct for save/restore to hold all the data that should
132  * sustain power-down: MMU base, IRQ type, env for routines, binary loaded FW
133  * and the stream seeds.
134  */
135 struct sh_css_save {
136 	enum ia_sh_css_modes		mode;
137 	u32		       mmu_base;		/* the last mmu_base */
138 	enum ia_css_irq_type           irq_type;
139 	struct sh_css_stream_seed      stream_seeds[MAX_ACTIVE_STREAMS];
140 	struct ia_css_fw	       *loaded_fw;	/* fw struct previously loaded */
141 	struct ia_css_env	       driver_env;	/* driver-supplied env copy */
142 };
143 
144 static bool my_css_save_initialized;	/* if my_css_save was initialized */
145 static struct sh_css_save my_css_save;
146 
147 /*
148  * pqiao NOTICE: this is for css internal buffer recycling when stopping
149  * pipeline,
150  * this array is temporary and will be replaced by resource manager
151  */
152 
153 /* Taking the biggest Size for number of Elements */
154 #define MAX_HMM_BUFFER_NUM	\
155 	(SH_CSS_MAX_NUM_QUEUES * (IA_CSS_NUM_ELEMS_SP2HOST_BUFFER_QUEUE + 2))
156 
157 struct sh_css_hmm_buffer_record {
158 	bool in_use;
159 	enum ia_css_buffer_type type;
160 	struct ia_css_rmgr_vbuf_handle *h_vbuf;
161 	hrt_address kernel_ptr;
162 };
163 
164 static struct sh_css_hmm_buffer_record hmm_buffer_record[MAX_HMM_BUFFER_NUM];
165 
166 #define GPIO_FLASH_PIN_MASK BIT(HIVE_GPIO_STROBE_TRIGGER_PIN)
167 
168 /*
169  * Local prototypes
170  */
171 
172 static int
173 allocate_delay_frames(struct ia_css_pipe *pipe);
174 
175 static int
176 sh_css_pipe_start(struct ia_css_stream *stream);
177 
178 /*
179  * @brief Check if all "ia_css_pipe" instances in the target
180  * "ia_css_stream" instance have stopped.
181  *
182  * @param[in] stream	Point to the target "ia_css_stream" instance.
183  *
184  * @return
185  * - true, if all "ia_css_pipe" instances in the target "ia_css_stream"
186  *   instance have ben stopped.
187  * - false, otherwise.
188  */
189 
190 /* ISP 2401 */
191 static int
192 ia_css_pipe_check_format(struct ia_css_pipe *pipe,
193 			 enum ia_css_frame_format format);
194 
195 /* ISP 2401 */
196 static void
197 ia_css_reset_defaults(struct sh_css *css);
198 
199 static void
200 sh_css_init_host_sp_control_vars(void);
201 
202 static int
203 set_num_primary_stages(unsigned int *num, enum ia_css_pipe_version version);
204 
205 static bool
206 need_capture_pp(const struct ia_css_pipe *pipe);
207 
208 static bool
209 need_yuv_scaler_stage(const struct ia_css_pipe *pipe);
210 
211 static int ia_css_pipe_create_cas_scaler_desc_single_output(
212     struct ia_css_frame_info *cas_scaler_in_info,
213     struct ia_css_frame_info *cas_scaler_out_info,
214     struct ia_css_frame_info *cas_scaler_vf_info,
215     struct ia_css_cas_binary_descr *descr);
216 
217 static void ia_css_pipe_destroy_cas_scaler_desc(struct ia_css_cas_binary_descr
218 	*descr);
219 
220 static bool
221 need_downscaling(const struct ia_css_resolution in_res,
222 		 const struct ia_css_resolution out_res);
223 
224 static bool need_capt_ldc(const struct ia_css_pipe *pipe);
225 
226 static int
227 sh_css_pipe_load_binaries(struct ia_css_pipe *pipe);
228 
229 static
230 int sh_css_pipe_get_viewfinder_frame_info(
231     struct ia_css_pipe *pipe,
232     struct ia_css_frame_info *info,
233     unsigned int idx);
234 
235 static int
236 sh_css_pipe_get_output_frame_info(struct ia_css_pipe *pipe,
237 				  struct ia_css_frame_info *info,
238 				  unsigned int idx);
239 
240 static int
241 capture_start(struct ia_css_pipe *pipe);
242 
243 static int
244 video_start(struct ia_css_pipe *pipe);
245 
246 static int
247 preview_start(struct ia_css_pipe *pipe);
248 
249 static int
250 yuvpp_start(struct ia_css_pipe *pipe);
251 
252 static bool copy_on_sp(struct ia_css_pipe *pipe);
253 
254 static int
255 init_vf_frameinfo_defaults(struct ia_css_pipe *pipe,
256 			   struct ia_css_frame *vf_frame, unsigned int idx);
257 
258 static int
259 init_in_frameinfo_memory_defaults(struct ia_css_pipe *pipe,
260 				  struct ia_css_frame *frame, enum ia_css_frame_format format);
261 
262 static int
263 init_out_frameinfo_defaults(struct ia_css_pipe *pipe,
264 			    struct ia_css_frame *out_frame, unsigned int idx);
265 
266 static int
267 alloc_continuous_frames(struct ia_css_pipe *pipe, bool init_time);
268 
269 static void
270 pipe_global_init(void);
271 
272 static int
273 pipe_generate_pipe_num(const struct ia_css_pipe *pipe,
274 		       unsigned int *pipe_number);
275 
276 static void
277 pipe_release_pipe_num(unsigned int pipe_num);
278 
279 static int
280 create_host_pipeline_structure(struct ia_css_stream *stream);
281 
282 static int
283 create_host_pipeline(struct ia_css_stream *stream);
284 
285 static int
286 create_host_preview_pipeline(struct ia_css_pipe *pipe);
287 
288 static int
289 create_host_video_pipeline(struct ia_css_pipe *pipe);
290 
291 static int
292 create_host_copy_pipeline(struct ia_css_pipe *pipe,
293 			  unsigned int max_input_width,
294 			  struct ia_css_frame *out_frame);
295 
296 static int
297 create_host_isyscopy_capture_pipeline(struct ia_css_pipe *pipe);
298 
299 static int
300 create_host_capture_pipeline(struct ia_css_pipe *pipe);
301 
302 static int
303 create_host_yuvpp_pipeline(struct ia_css_pipe *pipe);
304 
305 static unsigned int
306 sh_css_get_sw_interrupt_value(unsigned int irq);
307 
308 static struct ia_css_binary *ia_css_pipe_get_shading_correction_binary(
309     const struct ia_css_pipe *pipe);
310 
311 static struct ia_css_binary *
312 ia_css_pipe_get_s3a_binary(const struct ia_css_pipe *pipe);
313 
314 static struct ia_css_binary *
315 ia_css_pipe_get_sdis_binary(const struct ia_css_pipe *pipe);
316 
317 static void
318 sh_css_hmm_buffer_record_init(void);
319 
320 static void
321 sh_css_hmm_buffer_record_uninit(void);
322 
323 static void
324 sh_css_hmm_buffer_record_reset(struct sh_css_hmm_buffer_record *buffer_record);
325 
326 static struct sh_css_hmm_buffer_record
327 *sh_css_hmm_buffer_record_acquire(struct ia_css_rmgr_vbuf_handle *h_vbuf,
328 				  enum ia_css_buffer_type type,
329 				  hrt_address kernel_ptr);
330 
331 static struct sh_css_hmm_buffer_record
332 *sh_css_hmm_buffer_record_validate(ia_css_ptr ddr_buffer_addr,
333 				   enum ia_css_buffer_type type);
334 
335 static unsigned int get_crop_lines_for_bayer_order(const struct
336 	ia_css_stream_config *config);
337 static unsigned int get_crop_columns_for_bayer_order(const struct
338 	ia_css_stream_config *config);
339 static void get_pipe_extra_pixel(struct ia_css_pipe *pipe,
340 				 unsigned int *extra_row, unsigned int *extra_column);
341 
342 static void
sh_css_pipe_free_shading_table(struct ia_css_pipe * pipe)343 sh_css_pipe_free_shading_table(struct ia_css_pipe *pipe)
344 {
345 	if (!pipe) {
346 		IA_CSS_ERROR("NULL input parameter");
347 		return;
348 	}
349 
350 	if (pipe->shading_table)
351 		ia_css_shading_table_free(pipe->shading_table);
352 	pipe->shading_table = NULL;
353 }
354 
355 static enum ia_css_frame_format yuv420_copy_formats[] = {
356 	IA_CSS_FRAME_FORMAT_NV12,
357 	IA_CSS_FRAME_FORMAT_NV21,
358 	IA_CSS_FRAME_FORMAT_YV12,
359 	IA_CSS_FRAME_FORMAT_YUV420,
360 	IA_CSS_FRAME_FORMAT_YUV420_16,
361 	IA_CSS_FRAME_FORMAT_CSI_MIPI_YUV420_8,
362 	IA_CSS_FRAME_FORMAT_CSI_MIPI_LEGACY_YUV420_8
363 };
364 
365 static enum ia_css_frame_format yuv422_copy_formats[] = {
366 	IA_CSS_FRAME_FORMAT_NV12,
367 	IA_CSS_FRAME_FORMAT_NV16,
368 	IA_CSS_FRAME_FORMAT_NV21,
369 	IA_CSS_FRAME_FORMAT_NV61,
370 	IA_CSS_FRAME_FORMAT_YV12,
371 	IA_CSS_FRAME_FORMAT_YV16,
372 	IA_CSS_FRAME_FORMAT_YUV420,
373 	IA_CSS_FRAME_FORMAT_YUV420_16,
374 	IA_CSS_FRAME_FORMAT_YUV422,
375 	IA_CSS_FRAME_FORMAT_YUV422_16,
376 	IA_CSS_FRAME_FORMAT_UYVY,
377 	IA_CSS_FRAME_FORMAT_YUYV
378 };
379 
380 /*
381  * Verify whether the selected output format is can be produced
382  * by the copy binary given the stream format.
383  */
384 static int
verify_copy_out_frame_format(struct ia_css_pipe * pipe)385 verify_copy_out_frame_format(struct ia_css_pipe *pipe)
386 {
387 	enum ia_css_frame_format out_fmt = pipe->output_info[0].format;
388 	unsigned int i, found = 0;
389 
390 	assert(pipe);
391 	assert(pipe->stream);
392 
393 	switch (pipe->stream->config.input_config.format) {
394 	case ATOMISP_INPUT_FORMAT_YUV420_8_LEGACY:
395 	case ATOMISP_INPUT_FORMAT_YUV420_8:
396 		for (i = 0; i < ARRAY_SIZE(yuv420_copy_formats) && !found; i++)
397 			found = (out_fmt == yuv420_copy_formats[i]);
398 		break;
399 	case ATOMISP_INPUT_FORMAT_YUV420_10:
400 	case ATOMISP_INPUT_FORMAT_YUV420_16:
401 		found = (out_fmt == IA_CSS_FRAME_FORMAT_YUV420_16);
402 		break;
403 	case ATOMISP_INPUT_FORMAT_YUV422_8:
404 		for (i = 0; i < ARRAY_SIZE(yuv422_copy_formats) && !found; i++)
405 			found = (out_fmt == yuv422_copy_formats[i]);
406 		break;
407 	case ATOMISP_INPUT_FORMAT_YUV422_10:
408 	case ATOMISP_INPUT_FORMAT_YUV422_16:
409 		found = (out_fmt == IA_CSS_FRAME_FORMAT_YUV422_16 ||
410 			 out_fmt == IA_CSS_FRAME_FORMAT_YUV420_16);
411 		break;
412 	case ATOMISP_INPUT_FORMAT_RGB_444:
413 	case ATOMISP_INPUT_FORMAT_RGB_555:
414 	case ATOMISP_INPUT_FORMAT_RGB_565:
415 		found = (out_fmt == IA_CSS_FRAME_FORMAT_RGBA888 ||
416 			 out_fmt == IA_CSS_FRAME_FORMAT_RGB565);
417 		break;
418 	case ATOMISP_INPUT_FORMAT_RGB_666:
419 	case ATOMISP_INPUT_FORMAT_RGB_888:
420 		found = (out_fmt == IA_CSS_FRAME_FORMAT_RGBA888 ||
421 			 out_fmt == IA_CSS_FRAME_FORMAT_YUV420);
422 		break;
423 	case ATOMISP_INPUT_FORMAT_RAW_6:
424 	case ATOMISP_INPUT_FORMAT_RAW_7:
425 	case ATOMISP_INPUT_FORMAT_RAW_8:
426 	case ATOMISP_INPUT_FORMAT_RAW_10:
427 	case ATOMISP_INPUT_FORMAT_RAW_12:
428 	case ATOMISP_INPUT_FORMAT_RAW_14:
429 	case ATOMISP_INPUT_FORMAT_RAW_16:
430 		found = (out_fmt == IA_CSS_FRAME_FORMAT_RAW) ||
431 		(out_fmt == IA_CSS_FRAME_FORMAT_RAW_PACKED);
432 		break;
433 	case ATOMISP_INPUT_FORMAT_BINARY_8:
434 		found = (out_fmt == IA_CSS_FRAME_FORMAT_BINARY_8);
435 		break;
436 	default:
437 		break;
438 	}
439 	if (!found)
440 		return -EINVAL;
441 	return 0;
442 }
443 
444 unsigned int
ia_css_stream_input_format_bits_per_pixel(struct ia_css_stream * stream)445 ia_css_stream_input_format_bits_per_pixel(struct ia_css_stream *stream)
446 {
447 	int bpp = 0;
448 
449 	if (stream)
450 		bpp = ia_css_util_input_format_bpp(stream->config.input_config.format,
451 						   stream->config.pixels_per_clock == 2);
452 
453 	return bpp;
454 }
455 
456 static int
sh_css_config_input_network_2400(struct ia_css_stream * stream)457 sh_css_config_input_network_2400(struct ia_css_stream *stream)
458 {
459 	unsigned int fmt_type;
460 	struct ia_css_pipe *pipe = stream->last_pipe;
461 	struct ia_css_binary *binary = NULL;
462 	int err = 0;
463 
464 	assert(stream);
465 	assert(pipe);
466 
467 	ia_css_debug_dtrace(IA_CSS_DEBUG_TRACE_PRIVATE,
468 			    "sh_css_config_input_network() enter:\n");
469 
470 	if (pipe->pipeline.stages)
471 		binary = pipe->pipeline.stages->binary;
472 
473 	err = ia_css_isys_convert_stream_format_to_mipi_format(
474 	    stream->config.input_config.format,
475 	    stream->csi_rx_config.comp,
476 	    &fmt_type);
477 	if (err)
478 		return err;
479 	sh_css_sp_program_input_circuit(fmt_type,
480 					stream->config.channel_id,
481 					stream->config.mode);
482 
483 	if ((binary && (binary->online || stream->config.continuous)) ||
484 	    pipe->config.mode == IA_CSS_PIPE_MODE_COPY) {
485 		err = ia_css_ifmtr_configure(&stream->config,
486 					     binary);
487 		if (err)
488 			return err;
489 	}
490 
491 	if (stream->config.mode == IA_CSS_INPUT_MODE_PRBS) {
492 		unsigned int width, height, vblank_cycles;
493 		const unsigned int hblank_cycles = 100;
494 		const unsigned int vblank_lines = 6;
495 
496 		width = (stream->config.input_config.input_res.width) /
497 			(1 + (stream->config.pixels_per_clock == 2));
498 		height = stream->config.input_config.input_res.height;
499 		vblank_cycles = vblank_lines * (width + hblank_cycles);
500 		sh_css_sp_configure_sync_gen(width, height, hblank_cycles, vblank_cycles);
501 	}
502 	ia_css_debug_dtrace(IA_CSS_DEBUG_TRACE_PRIVATE,
503 			    "sh_css_config_input_network() leave:\n");
504 	return 0;
505 }
506 
csi2_protocol_calculate_max_subpixels_per_line(enum atomisp_input_format format,unsigned int pixels_per_line)507 static unsigned int csi2_protocol_calculate_max_subpixels_per_line(
508     enum atomisp_input_format	format,
509     unsigned int			pixels_per_line)
510 {
511 	unsigned int rval;
512 
513 	switch (format) {
514 	case ATOMISP_INPUT_FORMAT_YUV420_8_LEGACY:
515 		/*
516 		 * The frame format layout is shown below.
517 		 *
518 		 *		Line	0:	UYY0 UYY0 ... UYY0
519 		 *		Line	1:	VYY0 VYY0 ... VYY0
520 		 *		Line	2:	UYY0 UYY0 ... UYY0
521 		 *		Line	3:	VYY0 VYY0 ... VYY0
522 		 *		...
523 		 *		Line (n-2):	UYY0 UYY0 ... UYY0
524 		 *		Line (n-1):	VYY0 VYY0 ... VYY0
525 		 *
526 		 *	In this frame format, the even-line is
527 		 *	as wide as the odd-line.
528 		 *	The 0 is introduced by the input system
529 		 *	(mipi backend).
530 		 */
531 		rval = pixels_per_line * 2;
532 		break;
533 	case ATOMISP_INPUT_FORMAT_YUV420_8:
534 	case ATOMISP_INPUT_FORMAT_YUV420_10:
535 	case ATOMISP_INPUT_FORMAT_YUV420_16:
536 		/*
537 		 * The frame format layout is shown below.
538 		 *
539 		 *		Line	0:	YYYY YYYY ... YYYY
540 		 *		Line	1:	UYVY UYVY ... UYVY UYVY
541 		 *		Line	2:	YYYY YYYY ... YYYY
542 		 *		Line	3:	UYVY UYVY ... UYVY UYVY
543 		 *		...
544 		 *		Line (n-2):	YYYY YYYY ... YYYY
545 		 *		Line (n-1):	UYVY UYVY ... UYVY UYVY
546 		 *
547 		 * In this frame format, the odd-line is twice
548 		 * wider than the even-line.
549 		 */
550 		rval = pixels_per_line * 2;
551 		break;
552 	case ATOMISP_INPUT_FORMAT_YUV422_8:
553 	case ATOMISP_INPUT_FORMAT_YUV422_10:
554 	case ATOMISP_INPUT_FORMAT_YUV422_16:
555 		/*
556 		 * The frame format layout is shown below.
557 		 *
558 		 *		Line	0:	UYVY UYVY ... UYVY
559 		 *		Line	1:	UYVY UYVY ... UYVY
560 		 *		Line	2:	UYVY UYVY ... UYVY
561 		 *		Line	3:	UYVY UYVY ... UYVY
562 		 *		...
563 		 *		Line (n-2):	UYVY UYVY ... UYVY
564 		 *		Line (n-1):	UYVY UYVY ... UYVY
565 		 *
566 		 * In this frame format, the even-line is
567 		 * as wide as the odd-line.
568 		 */
569 		rval = pixels_per_line * 2;
570 		break;
571 	case ATOMISP_INPUT_FORMAT_RGB_444:
572 	case ATOMISP_INPUT_FORMAT_RGB_555:
573 	case ATOMISP_INPUT_FORMAT_RGB_565:
574 	case ATOMISP_INPUT_FORMAT_RGB_666:
575 	case ATOMISP_INPUT_FORMAT_RGB_888:
576 		/*
577 		 * The frame format layout is shown below.
578 		 *
579 		 *		Line	0:	ABGR ABGR ... ABGR
580 		 *		Line	1:	ABGR ABGR ... ABGR
581 		 *		Line	2:	ABGR ABGR ... ABGR
582 		 *		Line	3:	ABGR ABGR ... ABGR
583 		 *		...
584 		 *		Line (n-2):	ABGR ABGR ... ABGR
585 		 *		Line (n-1):	ABGR ABGR ... ABGR
586 		 *
587 		 * In this frame format, the even-line is
588 		 * as wide as the odd-line.
589 		 */
590 		rval = pixels_per_line * 4;
591 		break;
592 	case ATOMISP_INPUT_FORMAT_RAW_6:
593 	case ATOMISP_INPUT_FORMAT_RAW_7:
594 	case ATOMISP_INPUT_FORMAT_RAW_8:
595 	case ATOMISP_INPUT_FORMAT_RAW_10:
596 	case ATOMISP_INPUT_FORMAT_RAW_12:
597 	case ATOMISP_INPUT_FORMAT_RAW_14:
598 	case ATOMISP_INPUT_FORMAT_RAW_16:
599 	case ATOMISP_INPUT_FORMAT_BINARY_8:
600 	case ATOMISP_INPUT_FORMAT_USER_DEF1:
601 	case ATOMISP_INPUT_FORMAT_USER_DEF2:
602 	case ATOMISP_INPUT_FORMAT_USER_DEF3:
603 	case ATOMISP_INPUT_FORMAT_USER_DEF4:
604 	case ATOMISP_INPUT_FORMAT_USER_DEF5:
605 	case ATOMISP_INPUT_FORMAT_USER_DEF6:
606 	case ATOMISP_INPUT_FORMAT_USER_DEF7:
607 	case ATOMISP_INPUT_FORMAT_USER_DEF8:
608 		/*
609 		 * The frame format layout is shown below.
610 		 *
611 		 *		Line	0:	Pixel ... Pixel
612 		 *		Line	1:	Pixel ... Pixel
613 		 *		Line	2:	Pixel ... Pixel
614 		 *		Line	3:	Pixel ... Pixel
615 		 *		...
616 		 *		Line (n-2):	Pixel ... Pixel
617 		 *		Line (n-1):	Pixel ... Pixel
618 		 *
619 		 * In this frame format, the even-line is
620 		 * as wide as the odd-line.
621 		 */
622 		rval = pixels_per_line;
623 		break;
624 	default:
625 		rval = 0;
626 		break;
627 	}
628 
629 	return rval;
630 }
631 
sh_css_translate_stream_cfg_to_input_system_input_port_id(struct ia_css_stream_config * stream_cfg,ia_css_isys_descr_t * isys_stream_descr)632 static bool sh_css_translate_stream_cfg_to_input_system_input_port_id(
633     struct ia_css_stream_config *stream_cfg,
634     ia_css_isys_descr_t	*isys_stream_descr)
635 {
636 	bool rc;
637 
638 	rc = true;
639 	switch (stream_cfg->mode) {
640 	case IA_CSS_INPUT_MODE_PRBS:
641 
642 		if (stream_cfg->source.prbs.id == IA_CSS_PRBS_ID0)
643 			isys_stream_descr->input_port_id = INPUT_SYSTEM_PIXELGEN_PORT0_ID;
644 		else if (stream_cfg->source.prbs.id == IA_CSS_PRBS_ID1)
645 			isys_stream_descr->input_port_id = INPUT_SYSTEM_PIXELGEN_PORT1_ID;
646 		else if (stream_cfg->source.prbs.id == IA_CSS_PRBS_ID2)
647 			isys_stream_descr->input_port_id = INPUT_SYSTEM_PIXELGEN_PORT2_ID;
648 
649 		break;
650 	case IA_CSS_INPUT_MODE_BUFFERED_SENSOR:
651 
652 		if (stream_cfg->source.port.port == MIPI_PORT0_ID)
653 			isys_stream_descr->input_port_id = INPUT_SYSTEM_CSI_PORT0_ID;
654 		else if (stream_cfg->source.port.port == MIPI_PORT1_ID)
655 			isys_stream_descr->input_port_id = INPUT_SYSTEM_CSI_PORT1_ID;
656 		else if (stream_cfg->source.port.port == MIPI_PORT2_ID)
657 			isys_stream_descr->input_port_id = INPUT_SYSTEM_CSI_PORT2_ID;
658 
659 		break;
660 	default:
661 		rc = false;
662 		break;
663 	}
664 
665 	return rc;
666 }
667 
sh_css_translate_stream_cfg_to_input_system_input_port_type(struct ia_css_stream_config * stream_cfg,ia_css_isys_descr_t * isys_stream_descr)668 static bool sh_css_translate_stream_cfg_to_input_system_input_port_type(
669     struct ia_css_stream_config *stream_cfg,
670     ia_css_isys_descr_t	*isys_stream_descr)
671 {
672 	bool rc;
673 
674 	rc = true;
675 	switch (stream_cfg->mode) {
676 	case IA_CSS_INPUT_MODE_PRBS:
677 
678 		isys_stream_descr->mode = INPUT_SYSTEM_SOURCE_TYPE_PRBS;
679 
680 		break;
681 	case IA_CSS_INPUT_MODE_SENSOR:
682 	case IA_CSS_INPUT_MODE_BUFFERED_SENSOR:
683 
684 		isys_stream_descr->mode = INPUT_SYSTEM_SOURCE_TYPE_SENSOR;
685 		break;
686 
687 	default:
688 		rc = false;
689 		break;
690 	}
691 
692 	return rc;
693 }
694 
sh_css_translate_stream_cfg_to_input_system_input_port_attr(struct ia_css_stream_config * stream_cfg,ia_css_isys_descr_t * isys_stream_descr,int isys_stream_idx)695 static bool sh_css_translate_stream_cfg_to_input_system_input_port_attr(
696     struct ia_css_stream_config *stream_cfg,
697     ia_css_isys_descr_t	*isys_stream_descr,
698     int isys_stream_idx)
699 {
700 	bool rc;
701 
702 	rc = true;
703 	switch (stream_cfg->mode) {
704 	case IA_CSS_INPUT_MODE_PRBS:
705 
706 		isys_stream_descr->prbs_port_attr.seed0 = stream_cfg->source.prbs.seed;
707 		isys_stream_descr->prbs_port_attr.seed1 = stream_cfg->source.prbs.seed1;
708 
709 		/*
710 		 * TODO
711 		 * - Make "sync_gen_cfg" as part of "ia_css_prbs_config".
712 		 */
713 		isys_stream_descr->prbs_port_attr.sync_gen_cfg.hblank_cycles = 100;
714 		isys_stream_descr->prbs_port_attr.sync_gen_cfg.vblank_cycles = 100;
715 		isys_stream_descr->prbs_port_attr.sync_gen_cfg.pixels_per_clock =
716 		    stream_cfg->pixels_per_clock;
717 		isys_stream_descr->prbs_port_attr.sync_gen_cfg.nr_of_frames = (uint32_t)~(0x0);
718 		isys_stream_descr->prbs_port_attr.sync_gen_cfg.pixels_per_line =
719 		    stream_cfg->isys_config[IA_CSS_STREAM_DEFAULT_ISYS_STREAM_IDX].input_res.width;
720 		isys_stream_descr->prbs_port_attr.sync_gen_cfg.lines_per_frame =
721 		    stream_cfg->isys_config[IA_CSS_STREAM_DEFAULT_ISYS_STREAM_IDX].input_res.height;
722 
723 		break;
724 	case IA_CSS_INPUT_MODE_BUFFERED_SENSOR: {
725 		int err;
726 		unsigned int fmt_type;
727 
728 		err = ia_css_isys_convert_stream_format_to_mipi_format(
729 			  stream_cfg->isys_config[isys_stream_idx].format,
730 			  MIPI_PREDICTOR_NONE,
731 			  &fmt_type);
732 		if (err)
733 			rc = false;
734 
735 		isys_stream_descr->csi_port_attr.active_lanes =
736 		    stream_cfg->source.port.num_lanes;
737 		isys_stream_descr->csi_port_attr.fmt_type = fmt_type;
738 		isys_stream_descr->csi_port_attr.ch_id = stream_cfg->channel_id;
739 
740 		if (IS_ISP2401)
741 			isys_stream_descr->online = stream_cfg->online;
742 
743 		err |= ia_css_isys_convert_compressed_format(
744 			   &stream_cfg->source.port.compression,
745 			   isys_stream_descr);
746 		if (err)
747 			rc = false;
748 
749 		/* metadata */
750 		isys_stream_descr->metadata.enable = false;
751 		if (stream_cfg->metadata_config.resolution.height > 0) {
752 			err = ia_css_isys_convert_stream_format_to_mipi_format(
753 				  stream_cfg->metadata_config.data_type,
754 				  MIPI_PREDICTOR_NONE,
755 				  &fmt_type);
756 			if (err)
757 				rc = false;
758 			isys_stream_descr->metadata.fmt_type = fmt_type;
759 			isys_stream_descr->metadata.bits_per_pixel =
760 			    ia_css_util_input_format_bpp(stream_cfg->metadata_config.data_type, true);
761 			isys_stream_descr->metadata.pixels_per_line =
762 			    stream_cfg->metadata_config.resolution.width;
763 			isys_stream_descr->metadata.lines_per_frame =
764 			    stream_cfg->metadata_config.resolution.height;
765 
766 			/*
767 			 * For new input system, number of str2mmio requests must be even.
768 			 * So we round up number of metadata lines to be even.
769 			 */
770 			if (IS_ISP2401 && isys_stream_descr->metadata.lines_per_frame > 0)
771 				isys_stream_descr->metadata.lines_per_frame +=
772 				    (isys_stream_descr->metadata.lines_per_frame & 1);
773 
774 			isys_stream_descr->metadata.align_req_in_bytes =
775 			    ia_css_csi2_calculate_input_system_alignment(
776 				stream_cfg->metadata_config.data_type);
777 			isys_stream_descr->metadata.enable = true;
778 		}
779 
780 		break;
781 	}
782 	default:
783 		rc = false;
784 		break;
785 	}
786 
787 	return rc;
788 }
789 
sh_css_translate_stream_cfg_to_input_system_input_port_resolution(struct ia_css_stream_config * stream_cfg,ia_css_isys_descr_t * isys_stream_descr,int isys_stream_idx)790 static bool sh_css_translate_stream_cfg_to_input_system_input_port_resolution(
791     struct ia_css_stream_config *stream_cfg,
792     ia_css_isys_descr_t	*isys_stream_descr,
793     int isys_stream_idx)
794 {
795 	unsigned int bits_per_subpixel;
796 	unsigned int max_subpixels_per_line;
797 	unsigned int lines_per_frame;
798 	unsigned int align_req_in_bytes;
799 	enum atomisp_input_format fmt_type;
800 
801 	fmt_type = stream_cfg->isys_config[isys_stream_idx].format;
802 	if ((stream_cfg->mode == IA_CSS_INPUT_MODE_SENSOR ||
803 	     stream_cfg->mode == IA_CSS_INPUT_MODE_BUFFERED_SENSOR) &&
804 	    stream_cfg->source.port.compression.type != IA_CSS_CSI2_COMPRESSION_TYPE_NONE) {
805 		if (stream_cfg->source.port.compression.uncompressed_bits_per_pixel ==
806 		    UNCOMPRESSED_BITS_PER_PIXEL_10)
807 			fmt_type = ATOMISP_INPUT_FORMAT_RAW_10;
808 		else if (stream_cfg->source.port.compression.uncompressed_bits_per_pixel ==
809 			   UNCOMPRESSED_BITS_PER_PIXEL_12)
810 			fmt_type = ATOMISP_INPUT_FORMAT_RAW_12;
811 		else
812 			return false;
813 	}
814 
815 	bits_per_subpixel =
816 	    sh_css_stream_format_2_bits_per_subpixel(fmt_type);
817 	if (bits_per_subpixel == 0)
818 		return false;
819 
820 	max_subpixels_per_line =
821 	    csi2_protocol_calculate_max_subpixels_per_line(fmt_type,
822 		    stream_cfg->isys_config[isys_stream_idx].input_res.width);
823 	if (max_subpixels_per_line == 0)
824 		return false;
825 
826 	lines_per_frame = stream_cfg->isys_config[isys_stream_idx].input_res.height;
827 	if (lines_per_frame == 0)
828 		return false;
829 
830 	align_req_in_bytes = ia_css_csi2_calculate_input_system_alignment(fmt_type);
831 
832 	/* HW needs subpixel info for their settings */
833 	isys_stream_descr->input_port_resolution.bits_per_pixel = bits_per_subpixel;
834 	isys_stream_descr->input_port_resolution.pixels_per_line =
835 	    max_subpixels_per_line;
836 	isys_stream_descr->input_port_resolution.lines_per_frame = lines_per_frame;
837 	isys_stream_descr->input_port_resolution.align_req_in_bytes =
838 	    align_req_in_bytes;
839 
840 	return true;
841 }
842 
sh_css_translate_stream_cfg_to_isys_stream_descr(struct ia_css_stream_config * stream_cfg,bool early_polling,ia_css_isys_descr_t * isys_stream_descr,int isys_stream_idx)843 static bool sh_css_translate_stream_cfg_to_isys_stream_descr(
844     struct ia_css_stream_config *stream_cfg,
845     bool early_polling,
846     ia_css_isys_descr_t	*isys_stream_descr,
847     int isys_stream_idx)
848 {
849 	bool rc;
850 
851 	ia_css_debug_dtrace(IA_CSS_DEBUG_TRACE_PRIVATE,
852 			    "sh_css_translate_stream_cfg_to_isys_stream_descr() enter:\n");
853 	rc  = sh_css_translate_stream_cfg_to_input_system_input_port_id(stream_cfg,
854 		isys_stream_descr);
855 	rc &= sh_css_translate_stream_cfg_to_input_system_input_port_type(stream_cfg,
856 		isys_stream_descr);
857 	rc &= sh_css_translate_stream_cfg_to_input_system_input_port_attr(stream_cfg,
858 		isys_stream_descr, isys_stream_idx);
859 	rc &= sh_css_translate_stream_cfg_to_input_system_input_port_resolution(
860 		  stream_cfg, isys_stream_descr, isys_stream_idx);
861 
862 	isys_stream_descr->raw_packed = stream_cfg->pack_raw_pixels;
863 	isys_stream_descr->linked_isys_stream_id = (int8_t)
864 		stream_cfg->isys_config[isys_stream_idx].linked_isys_stream_id;
865 
866 	if (IS_ISP2401)
867 		ia_css_debug_dtrace(IA_CSS_DEBUG_TRACE_PRIVATE,
868 				    "sh_css_translate_stream_cfg_to_isys_stream_descr() leave:\n");
869 
870 	return rc;
871 }
872 
sh_css_translate_binary_info_to_input_system_output_port_attr(struct ia_css_binary * binary,ia_css_isys_descr_t * isys_stream_descr)873 static bool sh_css_translate_binary_info_to_input_system_output_port_attr(
874     struct ia_css_binary *binary,
875     ia_css_isys_descr_t     *isys_stream_descr)
876 {
877 	if (!binary)
878 		return false;
879 
880 	isys_stream_descr->output_port_attr.left_padding = binary->left_padding;
881 	isys_stream_descr->output_port_attr.max_isp_input_width =
882 	    binary->info->sp.input.max_width;
883 
884 	return true;
885 }
886 
887 static int
sh_css_config_input_network_2401(struct ia_css_stream * stream)888 sh_css_config_input_network_2401(struct ia_css_stream *stream)
889 {
890 	bool					rc;
891 	ia_css_isys_descr_t			isys_stream_descr;
892 	unsigned int				sp_thread_id;
893 	struct sh_css_sp_pipeline_terminal	*sp_pipeline_input_terminal;
894 	struct ia_css_pipe *pipe = NULL;
895 	struct ia_css_binary *binary = NULL;
896 	int i;
897 	u32 isys_stream_id;
898 	bool early_polling = false;
899 
900 	assert(stream);
901 	ia_css_debug_dtrace(IA_CSS_DEBUG_TRACE_PRIVATE,
902 			    "sh_css_config_input_network() enter 0x%p:\n", stream);
903 
904 	if (stream->config.continuous) {
905 		if (stream->last_pipe->config.mode == IA_CSS_PIPE_MODE_CAPTURE)
906 			pipe = stream->last_pipe;
907 		else if (stream->last_pipe->config.mode == IA_CSS_PIPE_MODE_YUVPP)
908 			pipe = stream->last_pipe;
909 		else if (stream->last_pipe->config.mode == IA_CSS_PIPE_MODE_PREVIEW)
910 			pipe = stream->last_pipe->pipe_settings.preview.copy_pipe;
911 		else if (stream->last_pipe->config.mode == IA_CSS_PIPE_MODE_VIDEO)
912 			pipe = stream->last_pipe->pipe_settings.video.copy_pipe;
913 	} else {
914 		pipe = stream->last_pipe;
915 	}
916 
917 	if (!pipe)
918 		return -EINVAL;
919 
920 	if (pipe->pipeline.stages)
921 		if (pipe->pipeline.stages->binary)
922 			binary = pipe->pipeline.stages->binary;
923 
924 	if (binary) {
925 		/*
926 		 * this was being done in ifmtr in 2400.
927 		 * online and cont bypass the init_in_frameinfo_memory_defaults
928 		 * so need to do it here
929 		 */
930 		ia_css_get_crop_offsets(pipe, &binary->in_frame_info);
931 	}
932 
933 	/* get the SP thread id */
934 	rc = ia_css_pipeline_get_sp_thread_id(ia_css_pipe_get_pipe_num(pipe), &sp_thread_id);
935 	if (!rc)
936 		return -EINVAL;
937 	/* get the target input terminal */
938 	sp_pipeline_input_terminal = &sh_css_sp_group.pipe_io[sp_thread_id].input;
939 
940 	for (i = 0; i < IA_CSS_STREAM_MAX_ISYS_STREAM_PER_CH; i++) {
941 		/* initialization */
942 		memset((void *)(&isys_stream_descr), 0, sizeof(ia_css_isys_descr_t));
943 		sp_pipeline_input_terminal->context.virtual_input_system_stream[i].valid = 0;
944 		sp_pipeline_input_terminal->ctrl.virtual_input_system_stream_cfg[i].valid = 0;
945 
946 		if (!stream->config.isys_config[i].valid)
947 			continue;
948 
949 		/* translate the stream configuration to the Input System (2401) configuration */
950 		rc = sh_css_translate_stream_cfg_to_isys_stream_descr(
951 			 &stream->config,
952 			 early_polling,
953 			 &(isys_stream_descr), i);
954 
955 		if (stream->config.online) {
956 			rc &= sh_css_translate_binary_info_to_input_system_output_port_attr(
957 				  binary,
958 				  &(isys_stream_descr));
959 		}
960 
961 		if (!rc)
962 			return -EINVAL;
963 
964 		isys_stream_id = ia_css_isys_generate_stream_id(sp_thread_id, i);
965 
966 		/* create the virtual Input System (2401) */
967 		rc =  ia_css_isys_stream_create(
968 			  &(isys_stream_descr),
969 			  &sp_pipeline_input_terminal->context.virtual_input_system_stream[i],
970 			  isys_stream_id);
971 		if (!rc)
972 			return -EINVAL;
973 
974 		/* calculate the configuration of the virtual Input System (2401) */
975 		rc = ia_css_isys_stream_calculate_cfg(
976 			 &sp_pipeline_input_terminal->context.virtual_input_system_stream[i],
977 			 &(isys_stream_descr),
978 			 &sp_pipeline_input_terminal->ctrl.virtual_input_system_stream_cfg[i]);
979 		if (!rc) {
980 			ia_css_isys_stream_destroy(
981 			    &sp_pipeline_input_terminal->context.virtual_input_system_stream[i]);
982 			return -EINVAL;
983 		}
984 	}
985 
986 	ia_css_debug_dtrace(IA_CSS_DEBUG_TRACE_PRIVATE,
987 			    "sh_css_config_input_network() leave:\n");
988 
989 	return 0;
990 }
991 
stream_get_last_pipe(struct ia_css_stream * stream)992 static inline struct ia_css_pipe *stream_get_last_pipe(
993     struct ia_css_stream *stream)
994 {
995 	struct ia_css_pipe *last_pipe = NULL;
996 
997 	if (stream)
998 		last_pipe = stream->last_pipe;
999 
1000 	return last_pipe;
1001 }
1002 
stream_get_copy_pipe(struct ia_css_stream * stream)1003 static inline struct ia_css_pipe *stream_get_copy_pipe(
1004     struct ia_css_stream *stream)
1005 {
1006 	struct ia_css_pipe *copy_pipe = NULL;
1007 	struct ia_css_pipe *last_pipe = NULL;
1008 	enum ia_css_pipe_id pipe_id;
1009 
1010 	last_pipe = stream_get_last_pipe(stream);
1011 
1012 	if ((stream) &&
1013 	    (last_pipe) &&
1014 	    (stream->config.continuous)) {
1015 		pipe_id = last_pipe->mode;
1016 		switch (pipe_id) {
1017 		case IA_CSS_PIPE_ID_PREVIEW:
1018 			copy_pipe = last_pipe->pipe_settings.preview.copy_pipe;
1019 			break;
1020 		case IA_CSS_PIPE_ID_VIDEO:
1021 			copy_pipe = last_pipe->pipe_settings.video.copy_pipe;
1022 			break;
1023 		default:
1024 			copy_pipe = NULL;
1025 			break;
1026 		}
1027 	}
1028 
1029 	return copy_pipe;
1030 }
1031 
stream_get_target_pipe(struct ia_css_stream * stream)1032 static inline struct ia_css_pipe *stream_get_target_pipe(
1033     struct ia_css_stream *stream)
1034 {
1035 	struct ia_css_pipe *target_pipe;
1036 
1037 	/* get the pipe that consumes the stream */
1038 	if (stream->config.continuous)
1039 		target_pipe = stream_get_copy_pipe(stream);
1040 	else
1041 		target_pipe = stream_get_last_pipe(stream);
1042 
1043 	return target_pipe;
1044 }
1045 
stream_csi_rx_helper(struct ia_css_stream * stream,int (* func)(enum mipi_port_id,uint32_t))1046 static int stream_csi_rx_helper(
1047     struct ia_css_stream *stream,
1048     int (*func)(enum mipi_port_id, uint32_t))
1049 {
1050 	int retval = -EINVAL;
1051 	u32 sp_thread_id, stream_id;
1052 	bool rc;
1053 	struct ia_css_pipe *target_pipe = NULL;
1054 
1055 	if ((!stream) || (stream->config.mode != IA_CSS_INPUT_MODE_BUFFERED_SENSOR))
1056 		goto exit;
1057 
1058 	target_pipe = stream_get_target_pipe(stream);
1059 
1060 	if (!target_pipe)
1061 		goto exit;
1062 
1063 	rc = ia_css_pipeline_get_sp_thread_id(
1064 		 ia_css_pipe_get_pipe_num(target_pipe),
1065 		 &sp_thread_id);
1066 
1067 	if (!rc)
1068 		goto exit;
1069 
1070 	/* (un)register all valid "virtual isys streams" within the ia_css_stream */
1071 	stream_id = 0;
1072 	do {
1073 		if (stream->config.isys_config[stream_id].valid) {
1074 			u32 isys_stream_id = ia_css_isys_generate_stream_id(sp_thread_id, stream_id);
1075 
1076 			retval = func(stream->config.source.port.port, isys_stream_id);
1077 		}
1078 		stream_id++;
1079 	} while ((retval == 0) &&
1080 		 (stream_id < IA_CSS_STREAM_MAX_ISYS_STREAM_PER_CH));
1081 
1082 exit:
1083 	return retval;
1084 }
1085 
stream_register_with_csi_rx(struct ia_css_stream * stream)1086 static inline int stream_register_with_csi_rx(
1087     struct ia_css_stream *stream)
1088 {
1089 	return stream_csi_rx_helper(stream, ia_css_isys_csi_rx_register_stream);
1090 }
1091 
stream_unregister_with_csi_rx(struct ia_css_stream * stream)1092 static inline int stream_unregister_with_csi_rx(
1093     struct ia_css_stream *stream)
1094 {
1095 	return stream_csi_rx_helper(stream, ia_css_isys_csi_rx_unregister_stream);
1096 }
1097 
1098 
1099 static void
start_binary(struct ia_css_pipe * pipe,struct ia_css_binary * binary)1100 start_binary(struct ia_css_pipe *pipe,
1101 	     struct ia_css_binary *binary)
1102 {
1103 	assert(pipe);
1104 	/* Acceleration uses firmware, the binary thus can be NULL */
1105 
1106 	if (binary)
1107 		sh_css_metrics_start_binary(&binary->metrics);
1108 
1109 	if (!IS_ISP2401 && pipe->stream->reconfigure_css_rx) {
1110 		ia_css_isys_rx_configure(&pipe->stream->csi_rx_config,
1111 					 pipe->stream->config.mode);
1112 		pipe->stream->reconfigure_css_rx = false;
1113 	}
1114 }
1115 
1116 /* start the copy function on the SP */
1117 static int
start_copy_on_sp(struct ia_css_pipe * pipe,struct ia_css_frame * out_frame)1118 start_copy_on_sp(struct ia_css_pipe *pipe,
1119 		 struct ia_css_frame *out_frame)
1120 {
1121 	(void)out_frame;
1122 
1123 	if ((!pipe) || (!pipe->stream))
1124 		return -EINVAL;
1125 
1126 	if (!IS_ISP2401 && pipe->stream->reconfigure_css_rx)
1127 		ia_css_isys_rx_disable();
1128 
1129 	if (pipe->stream->config.input_config.format != ATOMISP_INPUT_FORMAT_BINARY_8)
1130 		return -EINVAL;
1131 	sh_css_sp_start_binary_copy(ia_css_pipe_get_pipe_num(pipe), out_frame, pipe->stream->config.pixels_per_clock == 2);
1132 
1133 	if (!IS_ISP2401 && pipe->stream->reconfigure_css_rx) {
1134 		ia_css_isys_rx_configure(&pipe->stream->csi_rx_config,
1135 					 pipe->stream->config.mode);
1136 		pipe->stream->reconfigure_css_rx = false;
1137 	}
1138 
1139 	return 0;
1140 }
1141 
sh_css_binary_args_reset(struct sh_css_binary_args * args)1142 void sh_css_binary_args_reset(struct sh_css_binary_args *args)
1143 {
1144 	unsigned int i;
1145 
1146 	for (i = 0; i < NUM_VIDEO_TNR_FRAMES; i++)
1147 		args->tnr_frames[i] = NULL;
1148 	for (i = 0; i < MAX_NUM_VIDEO_DELAY_FRAMES; i++)
1149 		args->delay_frames[i] = NULL;
1150 	args->in_frame      = NULL;
1151 	for (i = 0; i < IA_CSS_BINARY_MAX_OUTPUT_PORTS; i++)
1152 		args->out_frame[i] = NULL;
1153 	args->out_vf_frame  = NULL;
1154 	args->copy_vf       = false;
1155 	args->copy_output   = true;
1156 	args->vf_downscale_log2 = 0;
1157 }
1158 
start_pipe(struct ia_css_pipe * me,enum sh_css_pipe_config_override copy_ovrd,enum ia_css_input_mode input_mode)1159 static void start_pipe(
1160     struct ia_css_pipe *me,
1161     enum sh_css_pipe_config_override copy_ovrd,
1162     enum ia_css_input_mode input_mode)
1163 {
1164 	IA_CSS_ENTER_PRIVATE("me = %p, copy_ovrd = %d, input_mode = %d",
1165 			     me, copy_ovrd, input_mode);
1166 
1167 	assert(me); /* all callers are in this file and call with non null argument */
1168 
1169 	sh_css_sp_init_pipeline(&me->pipeline,
1170 				me->mode,
1171 				(uint8_t)ia_css_pipe_get_pipe_num(me),
1172 				me->config.default_capture_config.enable_xnr != 0,
1173 				me->stream->config.pixels_per_clock == 2,
1174 				me->stream->config.continuous,
1175 				false,
1176 				me->required_bds_factor,
1177 				copy_ovrd,
1178 				input_mode,
1179 				&me->stream->config.metadata_config,
1180 				&me->stream->info.metadata_info
1181 				, (input_mode == IA_CSS_INPUT_MODE_MEMORY) ?
1182 				(enum mipi_port_id)0 :
1183 				me->stream->config.source.port.port);
1184 
1185 	if (me->config.mode != IA_CSS_PIPE_MODE_COPY) {
1186 		struct ia_css_pipeline_stage *stage;
1187 
1188 		stage = me->pipeline.stages;
1189 		if (stage) {
1190 			me->pipeline.current_stage = stage;
1191 			start_binary(me, stage->binary);
1192 		}
1193 	}
1194 	IA_CSS_LEAVE_PRIVATE("void");
1195 }
1196 
1197 void
sh_css_invalidate_shading_tables(struct ia_css_stream * stream)1198 sh_css_invalidate_shading_tables(struct ia_css_stream *stream)
1199 {
1200 	int i;
1201 
1202 	assert(stream);
1203 
1204 	ia_css_debug_dtrace(IA_CSS_DEBUG_TRACE,
1205 			    "sh_css_invalidate_shading_tables() enter:\n");
1206 
1207 	for (i = 0; i < stream->num_pipes; i++) {
1208 		assert(stream->pipes[i]);
1209 		sh_css_pipe_free_shading_table(stream->pipes[i]);
1210 	}
1211 
1212 	ia_css_debug_dtrace(IA_CSS_DEBUG_TRACE,
1213 			    "sh_css_invalidate_shading_tables() leave: return_void\n");
1214 }
1215 
1216 static void
enable_interrupts(enum ia_css_irq_type irq_type)1217 enable_interrupts(enum ia_css_irq_type irq_type)
1218 {
1219 	enum mipi_port_id port;
1220 	bool enable_pulse = irq_type != IA_CSS_IRQ_TYPE_EDGE;
1221 
1222 	IA_CSS_ENTER_PRIVATE("");
1223 	/* Enable IRQ on the SP which signals that SP goes to idle
1224 	 * (aka ready state) */
1225 	cnd_sp_irq_enable(SP0_ID, true);
1226 	/* Set the IRQ device 0 to either level or pulse */
1227 	irq_enable_pulse(IRQ0_ID, enable_pulse);
1228 
1229 	cnd_virq_enable_channel(virq_sp, true);
1230 
1231 	/* Enable SW interrupt 0, this is used to signal ISYS events */
1232 	cnd_virq_enable_channel(
1233 	    (enum virq_id)(IRQ_SW_CHANNEL0_ID + IRQ_SW_CHANNEL_OFFSET),
1234 	    true);
1235 	/* Enable SW interrupt 1, this is used to signal PSYS events */
1236 	cnd_virq_enable_channel(
1237 	    (enum virq_id)(IRQ_SW_CHANNEL1_ID + IRQ_SW_CHANNEL_OFFSET),
1238 	    true);
1239 
1240 	if (!IS_ISP2401) {
1241 		for (port = 0; port < N_MIPI_PORT_ID; port++)
1242 			ia_css_isys_rx_enable_all_interrupts(port);
1243 	}
1244 
1245 	IA_CSS_LEAVE_PRIVATE("");
1246 }
1247 
sh_css_setup_spctrl_config(const struct ia_css_fw_info * fw,const char * program,ia_css_spctrl_cfg * spctrl_cfg)1248 static bool sh_css_setup_spctrl_config(const struct ia_css_fw_info *fw,
1249 				       const char *program,
1250 				       ia_css_spctrl_cfg  *spctrl_cfg)
1251 {
1252 	if ((!fw) || (!spctrl_cfg))
1253 		return false;
1254 	spctrl_cfg->sp_entry = 0;
1255 	spctrl_cfg->program_name = (char *)(program);
1256 
1257 	spctrl_cfg->ddr_data_offset =  fw->blob.data_source;
1258 	spctrl_cfg->dmem_data_addr = fw->blob.data_target;
1259 	spctrl_cfg->dmem_bss_addr = fw->blob.bss_target;
1260 	spctrl_cfg->data_size = fw->blob.data_size;
1261 	spctrl_cfg->bss_size = fw->blob.bss_size;
1262 
1263 	spctrl_cfg->spctrl_config_dmem_addr = fw->info.sp.init_dmem_data;
1264 	spctrl_cfg->spctrl_state_dmem_addr = fw->info.sp.sw_state;
1265 
1266 	spctrl_cfg->code_size = fw->blob.size;
1267 	spctrl_cfg->code      = fw->blob.code;
1268 	spctrl_cfg->sp_entry  = fw->info.sp.sp_entry; /* entry function ptr on SP */
1269 
1270 	return true;
1271 }
1272 
1273 void
ia_css_unload_firmware(void)1274 ia_css_unload_firmware(void)
1275 {
1276 	if (sh_css_num_binaries) {
1277 		/* we have already loaded before so get rid of the old stuff */
1278 		ia_css_binary_uninit();
1279 		sh_css_unload_firmware();
1280 	}
1281 }
1282 
1283 static void
ia_css_reset_defaults(struct sh_css * css)1284 ia_css_reset_defaults(struct sh_css *css)
1285 {
1286 	struct sh_css default_css;
1287 
1288 	/* Reset everything to zero */
1289 	memset(&default_css, 0, sizeof(default_css));
1290 
1291 	/* Initialize the non zero values */
1292 	default_css.check_system_idle = true;
1293 	default_css.num_cont_raw_frames = NUM_CONTINUOUS_FRAMES;
1294 
1295 	/*
1296 	 * All should be 0: but memset does it already.
1297 	 * default_css.num_mipi_frames[N_CSI_PORTS] = 0;
1298 	 */
1299 
1300 	default_css.irq_type = IA_CSS_IRQ_TYPE_EDGE;
1301 
1302 	/* Set the defaults to the output */
1303 	*css = default_css;
1304 }
1305 
1306 int
ia_css_load_firmware(struct device * dev,const struct ia_css_env * env,const struct ia_css_fw * fw)1307 ia_css_load_firmware(struct device *dev, const struct ia_css_env *env,
1308 		     const struct ia_css_fw  *fw)
1309 {
1310 	int err;
1311 
1312 	if (!env)
1313 		return -EINVAL;
1314 	if (!fw)
1315 		return -EINVAL;
1316 
1317 	ia_css_debug_dtrace(IA_CSS_DEBUG_TRACE, "ia_css_load_firmware() enter\n");
1318 
1319 	/* make sure we initialize my_css */
1320 	if (my_css.flush != env->cpu_mem_env.flush) {
1321 		ia_css_reset_defaults(&my_css);
1322 		my_css.flush = env->cpu_mem_env.flush;
1323 	}
1324 
1325 	err = sh_css_load_firmware(dev, fw->data, fw->bytes);
1326 	if (!err)
1327 		err = ia_css_binary_init_infos();
1328 
1329 	ia_css_debug_dtrace(IA_CSS_DEBUG_TRACE, "ia_css_load_firmware() leave\n");
1330 	return err;
1331 }
1332 
1333 int
ia_css_init(struct device * dev,const struct ia_css_env * env,u32 mmu_l1_base,enum ia_css_irq_type irq_type)1334 ia_css_init(struct device *dev, const struct ia_css_env *env,
1335 	    u32 mmu_l1_base, enum ia_css_irq_type irq_type)
1336 {
1337 	int err;
1338 	ia_css_spctrl_cfg spctrl_cfg;
1339 	void (*flush_func)(struct ia_css_acc_fw *fw);
1340 	hrt_data select, enable;
1341 
1342 	if (!env)
1343 		return -EINVAL;
1344 
1345 	sh_css_printf = env->print_env.debug_print;
1346 
1347 	IA_CSS_ENTER("void");
1348 
1349 	flush_func     = env->cpu_mem_env.flush;
1350 
1351 	pipe_global_init();
1352 	ia_css_pipeline_init();
1353 	ia_css_queue_map_init();
1354 
1355 	ia_css_device_access_init(&env->hw_access_env);
1356 
1357 	select = gpio_reg_load(GPIO0_ID, _gpio_block_reg_do_select) & ~GPIO_FLASH_PIN_MASK;
1358 	enable = gpio_reg_load(GPIO0_ID, _gpio_block_reg_do_e) | GPIO_FLASH_PIN_MASK;
1359 	sh_css_mmu_set_page_table_base_index(mmu_l1_base);
1360 
1361 	my_css_save.mmu_base = mmu_l1_base;
1362 
1363 	ia_css_reset_defaults(&my_css);
1364 
1365 	my_css_save.driver_env = *env;
1366 	my_css.flush     = flush_func;
1367 
1368 	err = ia_css_rmgr_init();
1369 	if (err) {
1370 		IA_CSS_LEAVE_ERR(err);
1371 		return err;
1372 	}
1373 
1374 	IA_CSS_LOG("init: %d", my_css_save_initialized);
1375 
1376 	if (!my_css_save_initialized) {
1377 		my_css_save_initialized = true;
1378 		my_css_save.mode = sh_css_mode_working;
1379 		memset(my_css_save.stream_seeds, 0,
1380 		       sizeof(struct sh_css_stream_seed) * MAX_ACTIVE_STREAMS);
1381 		IA_CSS_LOG("init: %d mode=%d", my_css_save_initialized, my_css_save.mode);
1382 	}
1383 
1384 	mipi_init();
1385 
1386 	/*
1387 	 * In case this has been programmed already, update internal
1388 	 * data structure ...
1389 	 * DEPRECATED
1390 	 */
1391 	if (!IS_ISP2401)
1392 		my_css.page_table_base_index = mmu_get_page_table_base_index(MMU0_ID);
1393 
1394 	my_css.irq_type = irq_type;
1395 
1396 	my_css_save.irq_type = irq_type;
1397 
1398 	enable_interrupts(my_css.irq_type);
1399 
1400 	/* configure GPIO to output mode */
1401 	gpio_reg_store(GPIO0_ID, _gpio_block_reg_do_select, select);
1402 	gpio_reg_store(GPIO0_ID, _gpio_block_reg_do_e, enable);
1403 	gpio_reg_store(GPIO0_ID, _gpio_block_reg_do_0, 0);
1404 
1405 	err = ia_css_refcount_init(REFCOUNT_SIZE);
1406 	if (err) {
1407 		IA_CSS_LEAVE_ERR(err);
1408 		return err;
1409 	}
1410 	err = sh_css_params_init();
1411 	if (err) {
1412 		IA_CSS_LEAVE_ERR(err);
1413 		return err;
1414 	}
1415 
1416 	if (!sh_css_setup_spctrl_config(&sh_css_sp_fw, SP_PROG_NAME, &spctrl_cfg))
1417 		return -EINVAL;
1418 
1419 	err = ia_css_spctrl_load_fw(SP0_ID, &spctrl_cfg);
1420 	if (err) {
1421 		IA_CSS_LEAVE_ERR(err);
1422 		return err;
1423 	}
1424 
1425 	if (!sh_css_hrt_system_is_idle()) {
1426 		IA_CSS_LEAVE_ERR(-EBUSY);
1427 		return -EBUSY;
1428 	}
1429 	/*
1430 	 * can be called here, queuing works, but:
1431 	 * - when sp is started later, it will wipe queued items
1432 	 * so for now we leave it for later and make sure
1433 	 * updates are not called to frequently.
1434 	 * sh_css_init_buffer_queues();
1435 	 */
1436 
1437 	if (IS_ISP2401)
1438 		gp_device_reg_store(GP_DEVICE0_ID, _REG_GP_SWITCH_ISYS2401_ADDR, 1);
1439 
1440 	if (!IS_ISP2401)
1441 		dma_set_max_burst_size(DMA0_ID, HIVE_DMA_BUS_DDR_CONN,
1442 				       ISP2400_DMA_MAX_BURST_LENGTH);
1443 	else
1444 		dma_set_max_burst_size(DMA0_ID, HIVE_DMA_BUS_DDR_CONN,
1445 				       ISP2401_DMA_MAX_BURST_LENGTH);
1446 
1447 	if (ia_css_isys_init() != INPUT_SYSTEM_ERR_NO_ERROR)
1448 		err = -EINVAL;
1449 
1450 	sh_css_params_map_and_store_default_gdc_lut();
1451 
1452 	IA_CSS_LEAVE_ERR(err);
1453 	return err;
1454 }
1455 
1456 int
ia_css_enable_isys_event_queue(bool enable)1457 ia_css_enable_isys_event_queue(bool enable)
1458 {
1459 	if (sh_css_sp_is_running())
1460 		return -EBUSY;
1461 	sh_css_sp_enable_isys_event_queue(enable);
1462 	return 0;
1463 }
1464 
1465 /*
1466  * Mapping sp threads. Currently, this is done when a stream is created and
1467  * pipelines are ready to be converted to sp pipelines. Be careful if you are
1468  * doing it from stream_create since we could run out of sp threads due to
1469  * allocation on inactive pipelines.
1470  */
1471 static int
map_sp_threads(struct ia_css_stream * stream,bool map)1472 map_sp_threads(struct ia_css_stream *stream, bool map)
1473 {
1474 	struct ia_css_pipe *main_pipe = NULL;
1475 	struct ia_css_pipe *copy_pipe = NULL;
1476 	struct ia_css_pipe *capture_pipe = NULL;
1477 	int err = 0;
1478 	enum ia_css_pipe_id pipe_id;
1479 
1480 	IA_CSS_ENTER_PRIVATE("stream = %p, map = %s",
1481 			     stream, map ? "true" : "false");
1482 
1483 	if (!stream) {
1484 		IA_CSS_LEAVE_ERR_PRIVATE(-EINVAL);
1485 		return -EINVAL;
1486 	}
1487 
1488 	main_pipe = stream->last_pipe;
1489 	pipe_id	= main_pipe->mode;
1490 
1491 	ia_css_pipeline_map(main_pipe->pipe_num, map);
1492 
1493 	switch (pipe_id) {
1494 	case IA_CSS_PIPE_ID_PREVIEW:
1495 		copy_pipe    = main_pipe->pipe_settings.preview.copy_pipe;
1496 		capture_pipe = main_pipe->pipe_settings.preview.capture_pipe;
1497 		break;
1498 
1499 	case IA_CSS_PIPE_ID_VIDEO:
1500 		copy_pipe    = main_pipe->pipe_settings.video.copy_pipe;
1501 		capture_pipe = main_pipe->pipe_settings.video.capture_pipe;
1502 		break;
1503 
1504 	case IA_CSS_PIPE_ID_CAPTURE:
1505 	default:
1506 		break;
1507 	}
1508 
1509 	if (capture_pipe)
1510 		ia_css_pipeline_map(capture_pipe->pipe_num, map);
1511 
1512 	/* Firmware expects copy pipe to be the last pipe mapped. (if needed) */
1513 	if (copy_pipe)
1514 		ia_css_pipeline_map(copy_pipe->pipe_num, map);
1515 
1516 	/* DH regular multi pipe - not continuous mode: map the next pipes too */
1517 	if (!stream->config.continuous) {
1518 		int i;
1519 
1520 		for (i = 1; i < stream->num_pipes; i++)
1521 			ia_css_pipeline_map(stream->pipes[i]->pipe_num, map);
1522 	}
1523 
1524 	IA_CSS_LEAVE_ERR_PRIVATE(err);
1525 	return err;
1526 }
1527 
1528 /*
1529  * creates a host pipeline skeleton for all pipes in a stream. Called during
1530  * stream_create.
1531  */
1532 static int
create_host_pipeline_structure(struct ia_css_stream * stream)1533 create_host_pipeline_structure(struct ia_css_stream *stream)
1534 {
1535 	struct ia_css_pipe *copy_pipe = NULL, *capture_pipe = NULL;
1536 	enum ia_css_pipe_id pipe_id;
1537 	struct ia_css_pipe *main_pipe = NULL;
1538 	int err = 0;
1539 	unsigned int copy_pipe_delay = 0,
1540 	capture_pipe_delay = 0;
1541 
1542 	IA_CSS_ENTER_PRIVATE("stream = %p", stream);
1543 
1544 	if (!stream) {
1545 		IA_CSS_LEAVE_ERR_PRIVATE(-EINVAL);
1546 		return -EINVAL;
1547 	}
1548 
1549 	main_pipe	= stream->last_pipe;
1550 	if (!main_pipe) {
1551 		IA_CSS_LEAVE_ERR_PRIVATE(-EINVAL);
1552 		return -EINVAL;
1553 	}
1554 
1555 	pipe_id	= main_pipe->mode;
1556 
1557 	switch (pipe_id) {
1558 	case IA_CSS_PIPE_ID_PREVIEW:
1559 		copy_pipe    = main_pipe->pipe_settings.preview.copy_pipe;
1560 		copy_pipe_delay = main_pipe->dvs_frame_delay;
1561 		capture_pipe = main_pipe->pipe_settings.preview.capture_pipe;
1562 		capture_pipe_delay = IA_CSS_FRAME_DELAY_0;
1563 		err = ia_css_pipeline_create(&main_pipe->pipeline, main_pipe->mode,
1564 					     main_pipe->pipe_num, main_pipe->dvs_frame_delay);
1565 		break;
1566 
1567 	case IA_CSS_PIPE_ID_VIDEO:
1568 		copy_pipe    = main_pipe->pipe_settings.video.copy_pipe;
1569 		copy_pipe_delay = main_pipe->dvs_frame_delay;
1570 		capture_pipe = main_pipe->pipe_settings.video.capture_pipe;
1571 		capture_pipe_delay = IA_CSS_FRAME_DELAY_0;
1572 		err = ia_css_pipeline_create(&main_pipe->pipeline, main_pipe->mode,
1573 					     main_pipe->pipe_num, main_pipe->dvs_frame_delay);
1574 		break;
1575 
1576 	case IA_CSS_PIPE_ID_CAPTURE:
1577 		capture_pipe = main_pipe;
1578 		capture_pipe_delay = main_pipe->dvs_frame_delay;
1579 		break;
1580 
1581 	case IA_CSS_PIPE_ID_YUVPP:
1582 		err = ia_css_pipeline_create(&main_pipe->pipeline, main_pipe->mode,
1583 					     main_pipe->pipe_num, main_pipe->dvs_frame_delay);
1584 		break;
1585 
1586 	default:
1587 		err = -EINVAL;
1588 	}
1589 
1590 	if (!(err) && copy_pipe)
1591 		err = ia_css_pipeline_create(&copy_pipe->pipeline,
1592 					     copy_pipe->mode,
1593 					     copy_pipe->pipe_num,
1594 					     copy_pipe_delay);
1595 
1596 	if (!(err) && capture_pipe)
1597 		err = ia_css_pipeline_create(&capture_pipe->pipeline,
1598 					     capture_pipe->mode,
1599 					     capture_pipe->pipe_num,
1600 					     capture_pipe_delay);
1601 
1602 	/* DH regular multi pipe - not continuous mode: create the next pipelines too */
1603 	if (!stream->config.continuous) {
1604 		int i;
1605 
1606 		for (i = 1; i < stream->num_pipes && 0 == err; i++) {
1607 			main_pipe = stream->pipes[i];
1608 			err = ia_css_pipeline_create(&main_pipe->pipeline,
1609 						     main_pipe->mode,
1610 						     main_pipe->pipe_num,
1611 						     main_pipe->dvs_frame_delay);
1612 		}
1613 	}
1614 
1615 	IA_CSS_LEAVE_ERR_PRIVATE(err);
1616 	return err;
1617 }
1618 
1619 /*
1620  * creates a host pipeline for all pipes in a stream. Called during
1621  * stream_start.
1622  */
1623 static int
create_host_pipeline(struct ia_css_stream * stream)1624 create_host_pipeline(struct ia_css_stream *stream)
1625 {
1626 	struct ia_css_pipe *copy_pipe = NULL, *capture_pipe = NULL;
1627 	enum ia_css_pipe_id pipe_id;
1628 	struct ia_css_pipe *main_pipe = NULL;
1629 	int err = 0;
1630 	unsigned int max_input_width = 0;
1631 
1632 	IA_CSS_ENTER_PRIVATE("stream = %p", stream);
1633 	if (!stream) {
1634 		IA_CSS_LEAVE_ERR_PRIVATE(-EINVAL);
1635 		return -EINVAL;
1636 	}
1637 
1638 	main_pipe	= stream->last_pipe;
1639 	pipe_id	= main_pipe->mode;
1640 
1641 	/*
1642 	 * No continuous frame allocation for capture pipe. It uses the
1643 	 * "main" pipe's frames.
1644 	 */
1645 	if ((pipe_id == IA_CSS_PIPE_ID_PREVIEW) ||
1646 	    (pipe_id == IA_CSS_PIPE_ID_VIDEO)) {
1647 		/*
1648 		 * About
1649 		 *    pipe_id == IA_CSS_PIPE_ID_PREVIEW &&
1650 		 *    stream->config.mode != IA_CSS_INPUT_MODE_MEMORY:
1651 		 *
1652 		 * The original condition pipe_id == IA_CSS_PIPE_ID_PREVIEW is
1653 		 * too strong. E.g. in SkyCam (with memory based input frames)
1654 		 * there is no continuous mode and thus no need for allocated
1655 		 * continuous frames.
1656 		 * This is not only for SkyCam but for all preview cases that
1657 		 * use DDR based input frames. For this reason the
1658 		 * stream->config.mode != IA_CSS_INPUT_MODE_MEMORY has beed
1659 		 * added.
1660 		 */
1661 		if (stream->config.continuous ||
1662 		    (pipe_id == IA_CSS_PIPE_ID_PREVIEW &&
1663 		     stream->config.mode != IA_CSS_INPUT_MODE_MEMORY)) {
1664 			err = alloc_continuous_frames(main_pipe, true);
1665 			if (err)
1666 				goto ERR;
1667 		}
1668 	}
1669 
1670 	/* old isys: need to allocate_mipi_frames() even in IA_CSS_PIPE_MODE_COPY */
1671 	if (!IS_ISP2401 || main_pipe->config.mode != IA_CSS_PIPE_MODE_COPY) {
1672 		err = allocate_mipi_frames(main_pipe, &stream->info);
1673 		if (err)
1674 			goto ERR;
1675 	}
1676 
1677 	switch (pipe_id) {
1678 	case IA_CSS_PIPE_ID_PREVIEW:
1679 		copy_pipe    = main_pipe->pipe_settings.preview.copy_pipe;
1680 		capture_pipe = main_pipe->pipe_settings.preview.capture_pipe;
1681 		max_input_width =
1682 		    main_pipe->pipe_settings.preview.preview_binary.info->sp.input.max_width;
1683 
1684 		err = create_host_preview_pipeline(main_pipe);
1685 		if (err)
1686 			goto ERR;
1687 
1688 		break;
1689 
1690 	case IA_CSS_PIPE_ID_VIDEO:
1691 		copy_pipe    = main_pipe->pipe_settings.video.copy_pipe;
1692 		capture_pipe = main_pipe->pipe_settings.video.capture_pipe;
1693 		max_input_width =
1694 		    main_pipe->pipe_settings.video.video_binary.info->sp.input.max_width;
1695 
1696 		err = create_host_video_pipeline(main_pipe);
1697 		if (err)
1698 			goto ERR;
1699 
1700 		break;
1701 
1702 	case IA_CSS_PIPE_ID_CAPTURE:
1703 		capture_pipe = main_pipe;
1704 
1705 		break;
1706 
1707 	case IA_CSS_PIPE_ID_YUVPP:
1708 		err = create_host_yuvpp_pipeline(main_pipe);
1709 		if (err)
1710 			goto ERR;
1711 
1712 		break;
1713 
1714 	default:
1715 		err = -EINVAL;
1716 	}
1717 	if (err)
1718 		goto ERR;
1719 
1720 	if (copy_pipe) {
1721 		err = create_host_copy_pipeline(copy_pipe, max_input_width,
1722 						main_pipe->continuous_frames[0]);
1723 		if (err)
1724 			goto ERR;
1725 	}
1726 
1727 	if (capture_pipe) {
1728 		err = create_host_capture_pipeline(capture_pipe);
1729 		if (err)
1730 			goto ERR;
1731 	}
1732 
1733 	/* DH regular multi pipe - not continuous mode: create the next pipelines too */
1734 	if (!stream->config.continuous) {
1735 		int i;
1736 
1737 		for (i = 1; i < stream->num_pipes && 0 == err; i++) {
1738 			switch (stream->pipes[i]->mode) {
1739 			case IA_CSS_PIPE_ID_PREVIEW:
1740 				err = create_host_preview_pipeline(stream->pipes[i]);
1741 				break;
1742 			case IA_CSS_PIPE_ID_VIDEO:
1743 				err = create_host_video_pipeline(stream->pipes[i]);
1744 				break;
1745 			case IA_CSS_PIPE_ID_CAPTURE:
1746 				err = create_host_capture_pipeline(stream->pipes[i]);
1747 				break;
1748 			case IA_CSS_PIPE_ID_YUVPP:
1749 				err = create_host_yuvpp_pipeline(stream->pipes[i]);
1750 				break;
1751 			default:
1752 				err = -EINVAL;
1753 			}
1754 			if (err)
1755 				goto ERR;
1756 		}
1757 	}
1758 
1759 ERR:
1760 	IA_CSS_LEAVE_ERR_PRIVATE(err);
1761 	return err;
1762 }
1763 
1764 static const struct ia_css_pipe default_pipe = IA_CSS_DEFAULT_PIPE;
1765 static const struct ia_css_preview_settings preview = IA_CSS_DEFAULT_PREVIEW_SETTINGS;
1766 static const struct ia_css_capture_settings capture = IA_CSS_DEFAULT_CAPTURE_SETTINGS;
1767 static const struct ia_css_video_settings video = IA_CSS_DEFAULT_VIDEO_SETTINGS;
1768 static const struct ia_css_yuvpp_settings yuvpp = IA_CSS_DEFAULT_YUVPP_SETTINGS;
1769 
1770 static int
init_pipe_defaults(enum ia_css_pipe_mode mode,struct ia_css_pipe * pipe,bool copy_pipe)1771 init_pipe_defaults(enum ia_css_pipe_mode mode,
1772 		   struct ia_css_pipe *pipe,
1773 		   bool copy_pipe)
1774 {
1775 	if (!pipe) {
1776 		IA_CSS_ERROR("NULL pipe parameter");
1777 		return -EINVAL;
1778 	}
1779 
1780 	/* Initialize pipe to pre-defined defaults */
1781 	memcpy(pipe, &default_pipe, sizeof(default_pipe));
1782 
1783 	/* TODO: JB should not be needed, but temporary backward reference */
1784 	switch (mode) {
1785 	case IA_CSS_PIPE_MODE_PREVIEW:
1786 		pipe->mode = IA_CSS_PIPE_ID_PREVIEW;
1787 		memcpy(&pipe->pipe_settings.preview, &preview, sizeof(preview));
1788 		break;
1789 	case IA_CSS_PIPE_MODE_CAPTURE:
1790 		if (copy_pipe)
1791 			pipe->mode = IA_CSS_PIPE_ID_COPY;
1792 		else
1793 			pipe->mode = IA_CSS_PIPE_ID_CAPTURE;
1794 
1795 		memcpy(&pipe->pipe_settings.capture, &capture, sizeof(capture));
1796 		break;
1797 	case IA_CSS_PIPE_MODE_VIDEO:
1798 		pipe->mode = IA_CSS_PIPE_ID_VIDEO;
1799 		memcpy(&pipe->pipe_settings.video, &video, sizeof(video));
1800 		break;
1801 	case IA_CSS_PIPE_MODE_COPY:
1802 		pipe->mode = IA_CSS_PIPE_ID_CAPTURE;
1803 		break;
1804 	case IA_CSS_PIPE_MODE_YUVPP:
1805 		pipe->mode = IA_CSS_PIPE_ID_YUVPP;
1806 		memcpy(&pipe->pipe_settings.yuvpp, &yuvpp, sizeof(yuvpp));
1807 		break;
1808 	default:
1809 		return -EINVAL;
1810 	}
1811 
1812 	return 0;
1813 }
1814 
1815 static void
pipe_global_init(void)1816 pipe_global_init(void)
1817 {
1818 	u8 i;
1819 
1820 	my_css.pipe_counter = 0;
1821 	for (i = 0; i < IA_CSS_PIPELINE_NUM_MAX; i++)
1822 		my_css.all_pipes[i] = NULL;
1823 }
1824 
1825 static int
pipe_generate_pipe_num(const struct ia_css_pipe * pipe,unsigned int * pipe_number)1826 pipe_generate_pipe_num(const struct ia_css_pipe *pipe,
1827 		       unsigned int *pipe_number)
1828 {
1829 	const u8 INVALID_PIPE_NUM = (uint8_t)~(0);
1830 	u8 pipe_num = INVALID_PIPE_NUM;
1831 	u8 i;
1832 
1833 	if (!pipe) {
1834 		IA_CSS_ERROR("NULL pipe parameter");
1835 		return -EINVAL;
1836 	}
1837 
1838 	/* Assign a new pipe_num .... search for empty place */
1839 	for (i = 0; i < IA_CSS_PIPELINE_NUM_MAX; i++) {
1840 		if (!my_css.all_pipes[i]) {
1841 			/* position is reserved */
1842 			my_css.all_pipes[i] = (struct ia_css_pipe *)pipe;
1843 			pipe_num = i;
1844 			break;
1845 		}
1846 	}
1847 	if (pipe_num == INVALID_PIPE_NUM) {
1848 		/* Max number of pipes already allocated */
1849 		IA_CSS_ERROR("Max number of pipes already created");
1850 		return -ENOSPC;
1851 	}
1852 
1853 	my_css.pipe_counter++;
1854 
1855 	IA_CSS_LOG("pipe_num (%d)", pipe_num);
1856 
1857 	*pipe_number = pipe_num;
1858 	return 0;
1859 }
1860 
1861 static void
pipe_release_pipe_num(unsigned int pipe_num)1862 pipe_release_pipe_num(unsigned int pipe_num)
1863 {
1864 	my_css.all_pipes[pipe_num] = NULL;
1865 	my_css.pipe_counter--;
1866 	ia_css_debug_dtrace(IA_CSS_DEBUG_TRACE,
1867 			    "pipe_release_pipe_num (%d)\n", pipe_num);
1868 }
1869 
1870 static int
create_pipe(enum ia_css_pipe_mode mode,struct ia_css_pipe ** pipe,bool copy_pipe)1871 create_pipe(enum ia_css_pipe_mode mode,
1872 	    struct ia_css_pipe **pipe,
1873 	    bool copy_pipe)
1874 {
1875 	int err = 0;
1876 	struct ia_css_pipe *me;
1877 
1878 	if (!pipe) {
1879 		IA_CSS_ERROR("NULL pipe parameter");
1880 		return -EINVAL;
1881 	}
1882 
1883 	me = kmalloc_obj(*me);
1884 	if (!me)
1885 		return -ENOMEM;
1886 
1887 	err = init_pipe_defaults(mode, me, copy_pipe);
1888 	if (err) {
1889 		kfree(me);
1890 		return err;
1891 	}
1892 
1893 	err = pipe_generate_pipe_num(me, &me->pipe_num);
1894 	if (err) {
1895 		kfree(me);
1896 		return err;
1897 	}
1898 
1899 	*pipe = me;
1900 	return 0;
1901 }
1902 
1903 struct ia_css_pipe *
find_pipe_by_num(uint32_t pipe_num)1904 find_pipe_by_num(uint32_t pipe_num)
1905 {
1906 	unsigned int i;
1907 
1908 	for (i = 0; i < IA_CSS_PIPELINE_NUM_MAX; i++) {
1909 		if (my_css.all_pipes[i] &&
1910 		    ia_css_pipe_get_pipe_num(my_css.all_pipes[i]) == pipe_num) {
1911 			return my_css.all_pipes[i];
1912 		}
1913 	}
1914 	return NULL;
1915 }
1916 
1917 int
ia_css_pipe_destroy(struct ia_css_pipe * pipe)1918 ia_css_pipe_destroy(struct ia_css_pipe *pipe)
1919 {
1920 	int err = 0;
1921 
1922 	IA_CSS_ENTER("pipe = %p", pipe);
1923 
1924 	if (!pipe) {
1925 		IA_CSS_LEAVE_ERR(-EINVAL);
1926 		return -EINVAL;
1927 	}
1928 
1929 	if (pipe->stream) {
1930 		IA_CSS_LOG("ia_css_stream_destroy not called!");
1931 		IA_CSS_LEAVE_ERR(-EINVAL);
1932 		return -EINVAL;
1933 	}
1934 
1935 	switch (pipe->config.mode) {
1936 	case IA_CSS_PIPE_MODE_PREVIEW:
1937 		/*
1938 		 * need to take into account that this function is also called
1939 		 * on the internal copy pipe
1940 		 */
1941 		if (pipe->mode == IA_CSS_PIPE_ID_PREVIEW) {
1942 			ia_css_frame_free_multiple(NUM_CONTINUOUS_FRAMES,
1943 						   pipe->continuous_frames);
1944 			ia_css_metadata_free_multiple(NUM_CONTINUOUS_FRAMES,
1945 						      pipe->cont_md_buffers);
1946 			if (pipe->pipe_settings.preview.copy_pipe) {
1947 				err = ia_css_pipe_destroy(pipe->pipe_settings.preview.copy_pipe);
1948 				ia_css_debug_dtrace(IA_CSS_DEBUG_TRACE,
1949 						    "ia_css_pipe_destroy(): destroyed internal copy pipe err=%d\n",
1950 						    err);
1951 			}
1952 		}
1953 		break;
1954 	case IA_CSS_PIPE_MODE_VIDEO:
1955 		if (pipe->mode == IA_CSS_PIPE_ID_VIDEO) {
1956 			ia_css_frame_free_multiple(NUM_CONTINUOUS_FRAMES,
1957 						   pipe->continuous_frames);
1958 			ia_css_metadata_free_multiple(NUM_CONTINUOUS_FRAMES,
1959 						      pipe->cont_md_buffers);
1960 			if (pipe->pipe_settings.video.copy_pipe) {
1961 				err = ia_css_pipe_destroy(pipe->pipe_settings.video.copy_pipe);
1962 				ia_css_debug_dtrace(IA_CSS_DEBUG_TRACE,
1963 						    "ia_css_pipe_destroy(): destroyed internal copy pipe err=%d\n",
1964 						    err);
1965 			}
1966 		}
1967 		ia_css_frame_free_multiple(NUM_VIDEO_TNR_FRAMES,
1968 					   pipe->pipe_settings.video.tnr_frames);
1969 		ia_css_frame_free_multiple(MAX_NUM_VIDEO_DELAY_FRAMES,
1970 					   pipe->pipe_settings.video.delay_frames);
1971 		break;
1972 	case IA_CSS_PIPE_MODE_CAPTURE:
1973 		ia_css_frame_free_multiple(MAX_NUM_VIDEO_DELAY_FRAMES,
1974 					   pipe->pipe_settings.capture.delay_frames);
1975 		break;
1976 	case IA_CSS_PIPE_MODE_COPY:
1977 		break;
1978 	case IA_CSS_PIPE_MODE_YUVPP:
1979 		break;
1980 	}
1981 
1982 	if (pipe->scaler_pp_lut != mmgr_NULL) {
1983 		hmm_free(pipe->scaler_pp_lut);
1984 		pipe->scaler_pp_lut = mmgr_NULL;
1985 	}
1986 
1987 	my_css.active_pipes[ia_css_pipe_get_pipe_num(pipe)] = NULL;
1988 	sh_css_pipe_free_shading_table(pipe);
1989 
1990 	ia_css_pipeline_destroy(&pipe->pipeline);
1991 	pipe_release_pipe_num(ia_css_pipe_get_pipe_num(pipe));
1992 
1993 	kfree(pipe);
1994 	IA_CSS_LEAVE("err = %d", err);
1995 	return err;
1996 }
1997 
1998 void
ia_css_uninit(void)1999 ia_css_uninit(void)
2000 {
2001 	ia_css_debug_dtrace(IA_CSS_DEBUG_TRACE, "ia_css_uninit() enter: void\n");
2002 
2003 	sh_css_params_free_default_gdc_lut();
2004 
2005 	/* cleanup generic data */
2006 	sh_css_params_uninit();
2007 	ia_css_refcount_uninit();
2008 
2009 	ia_css_rmgr_uninit();
2010 
2011 	if (!IS_ISP2401) {
2012 		/* needed for reprogramming the inputformatter after power cycle of css */
2013 		ifmtr_set_if_blocking_mode_reset = true;
2014 	}
2015 
2016 	ia_css_spctrl_unload_fw(SP0_ID);
2017 	sh_css_sp_set_sp_running(false);
2018 	/* check and free any remaining mipi frames */
2019 	free_mipi_frames(NULL);
2020 
2021 	sh_css_sp_reset_global_vars();
2022 
2023 	ia_css_isys_uninit();
2024 
2025 	ia_css_debug_dtrace(IA_CSS_DEBUG_TRACE, "ia_css_uninit() leave: return_void\n");
2026 }
2027 
ia_css_irq_translate(unsigned int * irq_infos)2028 int ia_css_irq_translate(
2029     unsigned int *irq_infos)
2030 {
2031 	enum virq_id	irq;
2032 	enum hrt_isp_css_irq_status status = hrt_isp_css_irq_status_more_irqs;
2033 	unsigned int infos = 0;
2034 
2035 	/* irq_infos can be NULL, but that would make the function useless */
2036 	/* assert(irq_infos != NULL); */
2037 	ia_css_debug_dtrace(IA_CSS_DEBUG_TRACE,
2038 			    "ia_css_irq_translate() enter: irq_infos=%p\n", irq_infos);
2039 
2040 	while (status == hrt_isp_css_irq_status_more_irqs) {
2041 		status = virq_get_channel_id(&irq);
2042 		if (status == hrt_isp_css_irq_status_error)
2043 			return -EINVAL;
2044 
2045 
2046 		switch (irq) {
2047 		case virq_sp:
2048 			/*
2049 			 * When SP goes to idle, info is available in the
2050 			 * event queue.
2051 			 */
2052 			infos |= IA_CSS_IRQ_INFO_EVENTS_READY;
2053 			break;
2054 		case virq_isp:
2055 			break;
2056 		case virq_isys_sof:
2057 			infos |= IA_CSS_IRQ_INFO_CSS_RECEIVER_SOF;
2058 			break;
2059 		case virq_isys_eof:
2060 			infos |= IA_CSS_IRQ_INFO_CSS_RECEIVER_EOF;
2061 			break;
2062 		case virq_isys_csi:
2063 			infos |= IA_CSS_IRQ_INFO_INPUT_SYSTEM_ERROR;
2064 			break;
2065 		case virq_ifmt0_id:
2066 			if (!IS_ISP2401)
2067 				infos |= IA_CSS_IRQ_INFO_IF_ERROR;
2068 			break;
2069 		case virq_dma:
2070 			infos |= IA_CSS_IRQ_INFO_DMA_ERROR;
2071 			break;
2072 		case virq_sw_pin_0:
2073 			infos |= sh_css_get_sw_interrupt_value(0);
2074 			break;
2075 		case virq_sw_pin_1:
2076 			infos |= sh_css_get_sw_interrupt_value(1);
2077 			/* pqiao TODO: also assumption here */
2078 			break;
2079 		default:
2080 			break;
2081 		}
2082 	}
2083 
2084 	if (irq_infos)
2085 		*irq_infos = infos;
2086 
2087 	ia_css_debug_dtrace(IA_CSS_DEBUG_TRACE,
2088 			    "ia_css_irq_translate() leave: irq_infos=%u\n",
2089 			    infos);
2090 
2091 	return 0;
2092 }
2093 
ia_css_irq_enable(enum ia_css_irq_info info,bool enable)2094 int ia_css_irq_enable(
2095     enum ia_css_irq_info info,
2096     bool enable)
2097 {
2098 	enum virq_id	irq = N_virq_id;
2099 
2100 	IA_CSS_ENTER("info=%d, enable=%d", info, enable);
2101 
2102 	switch (info) {
2103 	case IA_CSS_IRQ_INFO_CSS_RECEIVER_SOF:
2104 		if (IS_ISP2401)
2105 			/* Just ignore those unused IRQs without printing errors */
2106 			return 0;
2107 
2108 		irq = virq_isys_sof;
2109 		break;
2110 	case IA_CSS_IRQ_INFO_CSS_RECEIVER_EOF:
2111 		if (IS_ISP2401)
2112 			/* Just ignore those unused IRQs without printing errors */
2113 			return 0;
2114 
2115 		irq = virq_isys_eof;
2116 		break;
2117 	case IA_CSS_IRQ_INFO_INPUT_SYSTEM_ERROR:
2118 		if (IS_ISP2401)
2119 			/* Just ignore those unused IRQs without printing errors */
2120 			return 0;
2121 
2122 		irq = virq_isys_csi;
2123 		break;
2124 	case IA_CSS_IRQ_INFO_IF_ERROR:
2125 		if (IS_ISP2401)
2126 			/* Just ignore those unused IRQs without printing errors */
2127 			return 0;
2128 
2129 		irq = virq_ifmt0_id;
2130 		break;
2131 	case IA_CSS_IRQ_INFO_DMA_ERROR:
2132 		irq = virq_dma;
2133 		break;
2134 	case IA_CSS_IRQ_INFO_SW_0:
2135 		irq = virq_sw_pin_0;
2136 		break;
2137 	case IA_CSS_IRQ_INFO_SW_1:
2138 		irq = virq_sw_pin_1;
2139 		break;
2140 	default:
2141 		IA_CSS_LEAVE_ERR(-EINVAL);
2142 		return -EINVAL;
2143 	}
2144 
2145 	cnd_virq_enable_channel(irq, enable);
2146 
2147 	IA_CSS_LEAVE_ERR(0);
2148 	return 0;
2149 }
2150 
2151 
2152 static unsigned int
sh_css_get_sw_interrupt_value(unsigned int irq)2153 sh_css_get_sw_interrupt_value(unsigned int irq)
2154 {
2155 	unsigned int irq_value;
2156 
2157 	ia_css_debug_dtrace(IA_CSS_DEBUG_TRACE,
2158 			    "sh_css_get_sw_interrupt_value() enter: irq=%d\n", irq);
2159 	irq_value = sh_css_sp_get_sw_interrupt_value(irq);
2160 	ia_css_debug_dtrace(IA_CSS_DEBUG_TRACE,
2161 			    "sh_css_get_sw_interrupt_value() leave: irq_value=%d\n", irq_value);
2162 	return irq_value;
2163 }
2164 
2165 /*
2166  * configure and load the copy binary, the next binary is used to
2167  * determine whether the copy binary needs to do left padding.
2168  */
load_copy_binary(struct ia_css_pipe * pipe,struct ia_css_binary * copy_binary,struct ia_css_binary * next_binary)2169 static int load_copy_binary(
2170     struct ia_css_pipe *pipe,
2171     struct ia_css_binary *copy_binary,
2172     struct ia_css_binary *next_binary)
2173 {
2174 	struct ia_css_frame_info copy_out_info, copy_in_info, copy_vf_info;
2175 	unsigned int left_padding;
2176 	int err;
2177 	struct ia_css_binary_descr copy_descr;
2178 
2179 	/* next_binary can be NULL */
2180 	assert(pipe);
2181 	assert(copy_binary);
2182 	ia_css_debug_dtrace(IA_CSS_DEBUG_TRACE_PRIVATE,
2183 			    "load_copy_binary() enter:\n");
2184 
2185 	if (next_binary) {
2186 		copy_out_info = next_binary->in_frame_info;
2187 		left_padding = next_binary->left_padding;
2188 	} else {
2189 		copy_out_info = pipe->output_info[0];
2190 		copy_vf_info = pipe->vf_output_info[0];
2191 		ia_css_frame_info_set_format(&copy_vf_info, IA_CSS_FRAME_FORMAT_YUV_LINE);
2192 		left_padding = 0;
2193 	}
2194 
2195 	ia_css_pipe_get_copy_binarydesc(pipe, &copy_descr,
2196 					&copy_in_info, &copy_out_info,
2197 					(next_binary) ? NULL : NULL/*TODO: &copy_vf_info*/);
2198 	err = ia_css_binary_find(&copy_descr, copy_binary);
2199 	if (err)
2200 		return err;
2201 	copy_binary->left_padding = left_padding;
2202 	return 0;
2203 }
2204 
2205 static int
alloc_continuous_frames(struct ia_css_pipe * pipe,bool init_time)2206 alloc_continuous_frames(struct ia_css_pipe *pipe, bool init_time)
2207 {
2208 	int err = 0;
2209 	struct ia_css_frame_info ref_info;
2210 	enum ia_css_pipe_id pipe_id;
2211 	bool continuous;
2212 	unsigned int i, idx;
2213 	unsigned int num_frames;
2214 
2215 	IA_CSS_ENTER_PRIVATE("pipe = %p, init_time = %d", pipe, init_time);
2216 
2217 	if ((!pipe) || (!pipe->stream)) {
2218 		IA_CSS_LEAVE_ERR_PRIVATE(-EINVAL);
2219 		return -EINVAL;
2220 	}
2221 
2222 	pipe_id = pipe->mode;
2223 	continuous = pipe->stream->config.continuous;
2224 
2225 	if (continuous) {
2226 		if (init_time) {
2227 			num_frames = pipe->stream->config.init_num_cont_raw_buf;
2228 			pipe->stream->continuous_pipe = pipe;
2229 		} else {
2230 			num_frames = pipe->stream->config.target_num_cont_raw_buf;
2231 		}
2232 	} else {
2233 		num_frames = NUM_ONLINE_INIT_CONTINUOUS_FRAMES;
2234 	}
2235 
2236 	if (pipe_id == IA_CSS_PIPE_ID_PREVIEW) {
2237 		ref_info = pipe->pipe_settings.preview.preview_binary.in_frame_info;
2238 	} else if (pipe_id == IA_CSS_PIPE_ID_VIDEO) {
2239 		ref_info = pipe->pipe_settings.video.video_binary.in_frame_info;
2240 	} else {
2241 		/* should not happen */
2242 		IA_CSS_LEAVE_ERR_PRIVATE(-EINVAL);
2243 		return -EINVAL;
2244 	}
2245 
2246 	if (IS_ISP2401) {
2247 		/* For CSI2+, the continuous frame will hold the full input frame */
2248 		ref_info.res.width = pipe->stream->config.input_config.input_res.width;
2249 		ref_info.res.height = pipe->stream->config.input_config.input_res.height;
2250 
2251 		/* Ensure padded width is aligned for 2401 */
2252 		ref_info.padded_width = CEIL_MUL(ref_info.res.width, 2 * ISP_VEC_NELEMS);
2253 	}
2254 
2255 	if (pipe->stream->config.pack_raw_pixels) {
2256 		ia_css_debug_dtrace(IA_CSS_DEBUG_TRACE_PRIVATE,
2257 				    "alloc_continuous_frames() IA_CSS_FRAME_FORMAT_RAW_PACKED\n");
2258 		ref_info.format = IA_CSS_FRAME_FORMAT_RAW_PACKED;
2259 	} else
2260 	{
2261 		ia_css_debug_dtrace(IA_CSS_DEBUG_TRACE_PRIVATE,
2262 				    "alloc_continuous_frames() IA_CSS_FRAME_FORMAT_RAW\n");
2263 		ref_info.format = IA_CSS_FRAME_FORMAT_RAW;
2264 	}
2265 
2266 	/* Write format back to binary */
2267 	if (pipe_id == IA_CSS_PIPE_ID_PREVIEW) {
2268 		pipe->pipe_settings.preview.preview_binary.in_frame_info.format =
2269 		    ref_info.format;
2270 	} else if (pipe_id == IA_CSS_PIPE_ID_VIDEO) {
2271 		pipe->pipe_settings.video.video_binary.in_frame_info.format = ref_info.format;
2272 	} else {
2273 		/* should not happen */
2274 		IA_CSS_LEAVE_ERR_PRIVATE(-EINVAL);
2275 		return -EINVAL;
2276 	}
2277 
2278 	if (init_time)
2279 		idx = 0;
2280 	else
2281 		idx = pipe->stream->config.init_num_cont_raw_buf;
2282 
2283 	for (i = idx; i < NUM_CONTINUOUS_FRAMES; i++) {
2284 		/* free previous frame */
2285 		if (pipe->continuous_frames[i]) {
2286 			ia_css_frame_free(pipe->continuous_frames[i]);
2287 			pipe->continuous_frames[i] = NULL;
2288 		}
2289 		/* free previous metadata buffer */
2290 		ia_css_metadata_free(pipe->cont_md_buffers[i]);
2291 		pipe->cont_md_buffers[i] = NULL;
2292 
2293 		/* check if new frame needed */
2294 		if (i < num_frames) {
2295 			/* allocate new frame */
2296 			err = ia_css_frame_allocate_from_info(
2297 				  &pipe->continuous_frames[i],
2298 				  &ref_info);
2299 			if (err) {
2300 				IA_CSS_LEAVE_ERR_PRIVATE(err);
2301 				return err;
2302 			}
2303 			/* allocate metadata buffer */
2304 			pipe->cont_md_buffers[i] = ia_css_metadata_allocate(
2305 						       &pipe->stream->info.metadata_info);
2306 		}
2307 	}
2308 	IA_CSS_LEAVE_ERR_PRIVATE(0);
2309 	return 0;
2310 }
2311 
2312 int
ia_css_alloc_continuous_frame_remain(struct ia_css_stream * stream)2313 ia_css_alloc_continuous_frame_remain(struct ia_css_stream *stream)
2314 {
2315 	if (!stream)
2316 		return -EINVAL;
2317 	return alloc_continuous_frames(stream->continuous_pipe, false);
2318 }
2319 
2320 static int
load_preview_binaries(struct ia_css_pipe * pipe)2321 load_preview_binaries(struct ia_css_pipe *pipe)
2322 {
2323 	struct ia_css_frame_info prev_in_info,
2324 		prev_bds_out_info,
2325 		prev_out_info,
2326 		prev_vf_info;
2327 	struct ia_css_binary_descr preview_descr;
2328 	bool online;
2329 	int err = 0;
2330 	bool need_vf_pp = false;
2331 	bool need_isp_copy_binary = false;
2332 	bool sensor = false;
2333 	bool continuous;
2334 
2335 	/* preview only have 1 output pin now */
2336 	struct ia_css_frame_info *pipe_out_info = &pipe->output_info[0];
2337 	struct ia_css_preview_settings *mycs  = &pipe->pipe_settings.preview;
2338 
2339 	IA_CSS_ENTER_PRIVATE("");
2340 	assert(pipe);
2341 	assert(pipe->stream);
2342 	assert(pipe->mode == IA_CSS_PIPE_ID_PREVIEW);
2343 
2344 	online = pipe->stream->config.online;
2345 
2346 	sensor = pipe->stream->config.mode == IA_CSS_INPUT_MODE_SENSOR;
2347 	continuous = pipe->stream->config.continuous;
2348 
2349 	if (mycs->preview_binary.info)
2350 		return 0;
2351 
2352 	err = ia_css_util_check_input(&pipe->stream->config, false, false);
2353 	if (err)
2354 		return err;
2355 	err = ia_css_frame_check_info(pipe_out_info);
2356 	if (err)
2357 		return err;
2358 
2359 	/*
2360 	 * Note: the current selection of vf_pp binary and
2361 	 * parameterization of the preview binary contains a few pieces
2362 	 * of hardcoded knowledge. This needs to be cleaned up such that
2363 	 * the binary selection becomes more generic.
2364 	 * The vf_pp binary is needed if one or more of the following features
2365 	 * are required:
2366 	 * 1. YUV downscaling.
2367 	 * 2. Digital zoom.
2368 	 * 3. An output format that is not supported by the preview binary.
2369 	 *    In practice this means something other than yuv_line or nv12.
2370 	 * The decision if the vf_pp binary is needed for YUV downscaling is
2371 	 * made after the preview binary selection, since some preview binaries
2372 	 * can perform the requested YUV downscaling.
2373 	 */
2374 	need_vf_pp = pipe->config.enable_dz;
2375 	need_vf_pp |= pipe_out_info->format != IA_CSS_FRAME_FORMAT_YUV_LINE &&
2376 	!(pipe_out_info->format == IA_CSS_FRAME_FORMAT_NV12 ||
2377 	  pipe_out_info->format == IA_CSS_FRAME_FORMAT_NV12_16 ||
2378 	  pipe_out_info->format == IA_CSS_FRAME_FORMAT_NV12_TILEY);
2379 
2380 	/* Preview step 1 */
2381 	if (pipe->vf_yuv_ds_input_info.res.width)
2382 		prev_vf_info = pipe->vf_yuv_ds_input_info;
2383 	else
2384 		prev_vf_info = *pipe_out_info;
2385 	/*
2386 	 * If vf_pp is needed, then preview must output yuv_line.
2387 	 * The exception is when vf_pp is manually disabled, that is only
2388 	 * used in combination with a pipeline extension that requires
2389 	 * yuv_line as input.
2390 	 */
2391 	if (need_vf_pp)
2392 		ia_css_frame_info_set_format(&prev_vf_info,
2393 					     IA_CSS_FRAME_FORMAT_YUV_LINE);
2394 
2395 	err = ia_css_pipe_get_preview_binarydesc(
2396 	    pipe,
2397 	    &preview_descr,
2398 	    &prev_in_info,
2399 	    &prev_bds_out_info,
2400 	    &prev_out_info,
2401 	    &prev_vf_info);
2402 	if (err)
2403 		return err;
2404 	err = ia_css_binary_find(&preview_descr, &mycs->preview_binary);
2405 	if (err)
2406 		return err;
2407 
2408 	/* The vf_pp binary is needed when (further) YUV downscaling is required */
2409 	need_vf_pp |= mycs->preview_binary.out_frame_info[0].res.width != pipe_out_info->res.width;
2410 	need_vf_pp |= mycs->preview_binary.out_frame_info[0].res.height != pipe_out_info->res.height;
2411 
2412 	/*
2413 	 * When vf_pp is needed, then the output format of the selected
2414 	 * preview binary must be yuv_line. If this is not the case,
2415 	 * then the preview binary selection is done again.
2416 	 */
2417 	if (need_vf_pp &&
2418 	    (mycs->preview_binary.out_frame_info[0].format != IA_CSS_FRAME_FORMAT_YUV_LINE)) {
2419 		/* Preview step 2 */
2420 		if (pipe->vf_yuv_ds_input_info.res.width)
2421 			prev_vf_info = pipe->vf_yuv_ds_input_info;
2422 		else
2423 			prev_vf_info = *pipe_out_info;
2424 
2425 		ia_css_frame_info_set_format(&prev_vf_info,
2426 					     IA_CSS_FRAME_FORMAT_YUV_LINE);
2427 
2428 		err = ia_css_pipe_get_preview_binarydesc(
2429 		    pipe,
2430 		    &preview_descr,
2431 		    &prev_in_info,
2432 		    &prev_bds_out_info,
2433 		    &prev_out_info,
2434 		    &prev_vf_info);
2435 		if (err)
2436 			return err;
2437 		err = ia_css_binary_find(&preview_descr,
2438 					 &mycs->preview_binary);
2439 		if (err)
2440 			return err;
2441 	}
2442 
2443 	if (need_vf_pp) {
2444 		struct ia_css_binary_descr vf_pp_descr;
2445 
2446 		/* Viewfinder post-processing */
2447 		ia_css_pipe_get_vfpp_binarydesc(pipe, &vf_pp_descr,
2448 						&mycs->preview_binary.out_frame_info[0],
2449 						pipe_out_info);
2450 		err = ia_css_binary_find(&vf_pp_descr,
2451 					 &mycs->vf_pp_binary);
2452 		if (err)
2453 			return err;
2454 	}
2455 
2456 	if (IS_ISP2401) {
2457 		/*
2458 		 * When the input system is 2401, only the Direct Sensor Mode
2459 		 * Offline Preview uses the ISP copy binary.
2460 		 */
2461 		need_isp_copy_binary = !online && sensor;
2462 	} else {
2463 		/*
2464 		 * About pipe->stream->config.mode == IA_CSS_INPUT_MODE_MEMORY:
2465 		 * This is typical the case with SkyCam (which has no input system) but it also
2466 		 * applies to all cases where the driver chooses for memory based input frames.
2467 		 * In these cases, a copy binary (which typical copies sensor data to DDR) does
2468 		 * not have much use.
2469 		 */
2470 		need_isp_copy_binary = !online && !continuous;
2471 	}
2472 
2473 	/* Copy */
2474 	if (need_isp_copy_binary) {
2475 		err = load_copy_binary(pipe,
2476 				       &mycs->copy_binary,
2477 				       &mycs->preview_binary);
2478 		if (err)
2479 			return err;
2480 	}
2481 
2482 	if (pipe->shading_table) {
2483 		ia_css_shading_table_free(pipe->shading_table);
2484 		pipe->shading_table = NULL;
2485 	}
2486 
2487 	return 0;
2488 }
2489 
2490 static void
ia_css_binary_unload(struct ia_css_binary * binary)2491 ia_css_binary_unload(struct ia_css_binary *binary)
2492 {
2493 	ia_css_binary_destroy_isp_parameters(binary);
2494 }
2495 
2496 static int
unload_preview_binaries(struct ia_css_pipe * pipe)2497 unload_preview_binaries(struct ia_css_pipe *pipe)
2498 {
2499 	IA_CSS_ENTER_PRIVATE("pipe = %p", pipe);
2500 
2501 	if ((!pipe) || (pipe->mode != IA_CSS_PIPE_ID_PREVIEW)) {
2502 		IA_CSS_LEAVE_ERR_PRIVATE(-EINVAL);
2503 		return -EINVAL;
2504 	}
2505 	ia_css_binary_unload(&pipe->pipe_settings.preview.copy_binary);
2506 	ia_css_binary_unload(&pipe->pipe_settings.preview.preview_binary);
2507 	ia_css_binary_unload(&pipe->pipe_settings.preview.vf_pp_binary);
2508 
2509 	IA_CSS_LEAVE_ERR_PRIVATE(0);
2510 	return 0;
2511 }
2512 
last_output_firmware(const struct ia_css_fw_info * fw)2513 static const struct ia_css_fw_info *last_output_firmware(
2514     const struct ia_css_fw_info *fw)
2515 {
2516 	const struct ia_css_fw_info *last_fw = NULL;
2517 	/* fw can be NULL */
2518 	IA_CSS_ENTER_LEAVE_PRIVATE("");
2519 
2520 	for (; fw; fw = fw->next) {
2521 		const struct ia_css_fw_info *info = fw;
2522 
2523 		if (info->info.isp.sp.enable.output)
2524 			last_fw = fw;
2525 	}
2526 	return last_fw;
2527 }
2528 
add_firmwares(struct ia_css_pipeline * me,struct ia_css_binary * binary,const struct ia_css_fw_info * fw,const struct ia_css_fw_info * last_fw,unsigned int binary_mode,struct ia_css_frame * in_frame,struct ia_css_frame * out_frame,struct ia_css_frame * vf_frame,struct ia_css_pipeline_stage ** my_stage,struct ia_css_pipeline_stage ** vf_stage)2529 static int add_firmwares(
2530     struct ia_css_pipeline *me,
2531     struct ia_css_binary *binary,
2532     const struct ia_css_fw_info *fw,
2533     const struct ia_css_fw_info *last_fw,
2534     unsigned int binary_mode,
2535     struct ia_css_frame *in_frame,
2536     struct ia_css_frame *out_frame,
2537     struct ia_css_frame *vf_frame,
2538     struct ia_css_pipeline_stage **my_stage,
2539     struct ia_css_pipeline_stage **vf_stage)
2540 {
2541 	int err = 0;
2542 	struct ia_css_pipeline_stage *extra_stage = NULL;
2543 	struct ia_css_pipeline_stage_desc stage_desc;
2544 
2545 	/* all args can be NULL ??? */
2546 	ia_css_debug_dtrace(IA_CSS_DEBUG_TRACE_PRIVATE,
2547 			    "add_firmwares() enter:\n");
2548 
2549 	for (; fw; fw = fw->next) {
2550 		struct ia_css_frame *out[IA_CSS_BINARY_MAX_OUTPUT_PORTS] = {NULL};
2551 		struct ia_css_frame *in = NULL;
2552 		struct ia_css_frame *vf = NULL;
2553 
2554 		if ((fw == last_fw) && (fw->info.isp.sp.enable.out_frame  != 0))
2555 			out[0] = out_frame;
2556 
2557 		if (fw->info.isp.sp.enable.in_frame != 0)
2558 			in = in_frame;
2559 
2560 		if (fw->info.isp.sp.enable.out_frame != 0)
2561 			vf = vf_frame;
2562 
2563 		ia_css_pipe_get_firmwares_stage_desc(&stage_desc, binary,
2564 						     out, in, vf, fw, binary_mode);
2565 		err = ia_css_pipeline_create_and_add_stage(me, &stage_desc,
2566 							   &extra_stage);
2567 		if (err)
2568 			return err;
2569 		if (fw->info.isp.sp.enable.output != 0)
2570 			in_frame = extra_stage->args.out_frame[0];
2571 		if (my_stage && !*my_stage && extra_stage)
2572 			*my_stage = extra_stage;
2573 		if (vf_stage && !*vf_stage && extra_stage &&
2574 		    fw->info.isp.sp.enable.vf_veceven)
2575 			*vf_stage = extra_stage;
2576 	}
2577 	return err;
2578 }
2579 
add_vf_pp_stage(struct ia_css_pipe * pipe,struct ia_css_frame * in_frame,struct ia_css_frame * out_frame,struct ia_css_binary * vf_pp_binary,struct ia_css_pipeline_stage ** vf_pp_stage)2580 static int add_vf_pp_stage(
2581     struct ia_css_pipe *pipe,
2582     struct ia_css_frame *in_frame,
2583     struct ia_css_frame *out_frame,
2584     struct ia_css_binary *vf_pp_binary,
2585     struct ia_css_pipeline_stage **vf_pp_stage)
2586 {
2587 	struct ia_css_pipeline *me = NULL;
2588 	const struct ia_css_fw_info *last_fw = NULL;
2589 	int err = 0;
2590 	struct ia_css_frame *out_frames[IA_CSS_BINARY_MAX_OUTPUT_PORTS];
2591 	struct ia_css_pipeline_stage_desc stage_desc;
2592 
2593 	/* out_frame can be NULL ??? */
2594 
2595 	if (!pipe)
2596 		return -EINVAL;
2597 	if (!in_frame)
2598 		return -EINVAL;
2599 	if (!vf_pp_binary)
2600 		return -EINVAL;
2601 	if (!vf_pp_stage)
2602 		return -EINVAL;
2603 
2604 	ia_css_pipe_util_create_output_frames(out_frames);
2605 	me = &pipe->pipeline;
2606 
2607 	ia_css_debug_dtrace(IA_CSS_DEBUG_TRACE_PRIVATE,
2608 			    "add_vf_pp_stage() enter:\n");
2609 
2610 	*vf_pp_stage = NULL;
2611 
2612 	last_fw = last_output_firmware(pipe->vf_stage);
2613 	if (!pipe->extra_config.disable_vf_pp) {
2614 		if (last_fw) {
2615 			ia_css_pipe_util_set_output_frames(out_frames, 0, NULL);
2616 			ia_css_pipe_get_generic_stage_desc(&stage_desc, vf_pp_binary,
2617 							   out_frames, in_frame, NULL);
2618 		} else {
2619 			ia_css_pipe_util_set_output_frames(out_frames, 0, out_frame);
2620 			ia_css_pipe_get_generic_stage_desc(&stage_desc, vf_pp_binary,
2621 							   out_frames, in_frame, NULL);
2622 		}
2623 		err = ia_css_pipeline_create_and_add_stage(me, &stage_desc, vf_pp_stage);
2624 		if (err)
2625 			return err;
2626 		in_frame = (*vf_pp_stage)->args.out_frame[0];
2627 	}
2628 	err = add_firmwares(me, vf_pp_binary, pipe->vf_stage, last_fw,
2629 			    IA_CSS_BINARY_MODE_VF_PP,
2630 			    in_frame, out_frame, NULL,
2631 			    vf_pp_stage, NULL);
2632 	return err;
2633 }
2634 
add_yuv_scaler_stage(struct ia_css_pipe * pipe,struct ia_css_pipeline * me,struct ia_css_frame * in_frame,struct ia_css_frame * out_frame,struct ia_css_frame * internal_out_frame,struct ia_css_binary * yuv_scaler_binary,struct ia_css_pipeline_stage ** pre_vf_pp_stage)2635 static int add_yuv_scaler_stage(
2636     struct ia_css_pipe *pipe,
2637     struct ia_css_pipeline *me,
2638     struct ia_css_frame *in_frame,
2639     struct ia_css_frame *out_frame,
2640     struct ia_css_frame *internal_out_frame,
2641     struct ia_css_binary *yuv_scaler_binary,
2642     struct ia_css_pipeline_stage **pre_vf_pp_stage)
2643 {
2644 	const struct ia_css_fw_info *last_fw;
2645 	int err = 0;
2646 	struct ia_css_frame *vf_frame = NULL;
2647 	struct ia_css_frame *out_frames[IA_CSS_BINARY_MAX_OUTPUT_PORTS];
2648 	struct ia_css_pipeline_stage_desc stage_desc;
2649 
2650 	/* out_frame can be NULL ??? */
2651 	assert(in_frame);
2652 	assert(pipe);
2653 	assert(me);
2654 	assert(yuv_scaler_binary);
2655 	assert(pre_vf_pp_stage);
2656 	ia_css_debug_dtrace(IA_CSS_DEBUG_TRACE_PRIVATE,
2657 			    "add_yuv_scaler_stage() enter:\n");
2658 
2659 	*pre_vf_pp_stage = NULL;
2660 	ia_css_pipe_util_create_output_frames(out_frames);
2661 
2662 	last_fw = last_output_firmware(pipe->output_stage);
2663 
2664 	if (last_fw) {
2665 		ia_css_pipe_util_set_output_frames(out_frames, 0, NULL);
2666 		ia_css_pipe_get_generic_stage_desc(&stage_desc,
2667 						   yuv_scaler_binary, out_frames, in_frame, vf_frame);
2668 	} else {
2669 		ia_css_pipe_util_set_output_frames(out_frames, 0, out_frame);
2670 		ia_css_pipe_util_set_output_frames(out_frames, 1, internal_out_frame);
2671 		ia_css_pipe_get_generic_stage_desc(&stage_desc,
2672 						   yuv_scaler_binary, out_frames, in_frame, vf_frame);
2673 	}
2674 	err = ia_css_pipeline_create_and_add_stage(me, &stage_desc,
2675 						   pre_vf_pp_stage);
2676 	if (err)
2677 		return err;
2678 	in_frame = (*pre_vf_pp_stage)->args.out_frame[0];
2679 
2680 	err = add_firmwares(me, yuv_scaler_binary, pipe->output_stage, last_fw,
2681 			    IA_CSS_BINARY_MODE_CAPTURE_PP,
2682 			    in_frame, out_frame, vf_frame,
2683 			    NULL, pre_vf_pp_stage);
2684 	/* If a firmware produce vf_pp output, we set that as vf_pp input */
2685 	(*pre_vf_pp_stage)->args.vf_downscale_log2 =
2686 	    yuv_scaler_binary->vf_downscale_log2;
2687 
2688 	ia_css_debug_dtrace(IA_CSS_DEBUG_TRACE_PRIVATE,
2689 			    "add_yuv_scaler_stage() leave:\n");
2690 	return err;
2691 }
2692 
add_capture_pp_stage(struct ia_css_pipe * pipe,struct ia_css_pipeline * me,struct ia_css_frame * in_frame,struct ia_css_frame * out_frame,struct ia_css_binary * capture_pp_binary,struct ia_css_pipeline_stage ** capture_pp_stage)2693 static int add_capture_pp_stage(
2694     struct ia_css_pipe *pipe,
2695     struct ia_css_pipeline *me,
2696     struct ia_css_frame *in_frame,
2697     struct ia_css_frame *out_frame,
2698     struct ia_css_binary *capture_pp_binary,
2699     struct ia_css_pipeline_stage **capture_pp_stage)
2700 {
2701 	const struct ia_css_fw_info *last_fw = NULL;
2702 	int err = 0;
2703 	struct ia_css_frame *vf_frame = NULL;
2704 	struct ia_css_frame *out_frames[IA_CSS_BINARY_MAX_OUTPUT_PORTS];
2705 	struct ia_css_pipeline_stage_desc stage_desc;
2706 
2707 	/* out_frame can be NULL ??? */
2708 	assert(in_frame);
2709 	assert(pipe);
2710 	assert(me);
2711 	assert(capture_pp_binary);
2712 	assert(capture_pp_stage);
2713 	ia_css_debug_dtrace(IA_CSS_DEBUG_TRACE_PRIVATE,
2714 			    "add_capture_pp_stage() enter:\n");
2715 
2716 	*capture_pp_stage = NULL;
2717 	ia_css_pipe_util_create_output_frames(out_frames);
2718 
2719 	last_fw = last_output_firmware(pipe->output_stage);
2720 	err = ia_css_frame_allocate_from_info(&vf_frame,
2721 					      &capture_pp_binary->vf_frame_info);
2722 	if (err)
2723 		return err;
2724 	if (last_fw)	{
2725 		ia_css_pipe_util_set_output_frames(out_frames, 0, NULL);
2726 		ia_css_pipe_get_generic_stage_desc(&stage_desc,
2727 						   capture_pp_binary, out_frames, NULL, vf_frame);
2728 	} else {
2729 		ia_css_pipe_util_set_output_frames(out_frames, 0, out_frame);
2730 		ia_css_pipe_get_generic_stage_desc(&stage_desc,
2731 						   capture_pp_binary, out_frames, NULL, vf_frame);
2732 	}
2733 	err = ia_css_pipeline_create_and_add_stage(me, &stage_desc,
2734 						   capture_pp_stage);
2735 	if (err)
2736 		return err;
2737 	err = add_firmwares(me, capture_pp_binary, pipe->output_stage, last_fw,
2738 			    IA_CSS_BINARY_MODE_CAPTURE_PP,
2739 			    in_frame, out_frame, vf_frame,
2740 			    NULL, capture_pp_stage);
2741 	/* If a firmware produce vf_pp output, we set that as vf_pp input */
2742 	if (*capture_pp_stage) {
2743 		(*capture_pp_stage)->args.vf_downscale_log2 =
2744 		    capture_pp_binary->vf_downscale_log2;
2745 	}
2746 	return err;
2747 }
2748 
sh_css_setup_queues(void)2749 static void sh_css_setup_queues(void)
2750 {
2751 	const struct ia_css_fw_info *fw;
2752 	unsigned int HIVE_ADDR_host_sp_queues_initialized;
2753 
2754 	sh_css_hmm_buffer_record_init();
2755 
2756 	sh_css_event_init_irq_mask();
2757 
2758 	fw = &sh_css_sp_fw;
2759 	HIVE_ADDR_host_sp_queues_initialized =
2760 	    fw->info.sp.host_sp_queues_initialized;
2761 
2762 	ia_css_bufq_init();
2763 
2764 	/* set "host_sp_queues_initialized" to "true" */
2765 	sp_dmem_store_uint32(SP0_ID,
2766 			     (unsigned int)sp_address_of(host_sp_queues_initialized),
2767 			     (uint32_t)(1));
2768 	ia_css_debug_dtrace(IA_CSS_DEBUG_TRACE, "sh_css_setup_queues() leave:\n");
2769 }
2770 
2771 static int
init_vf_frameinfo_defaults(struct ia_css_pipe * pipe,struct ia_css_frame * vf_frame,unsigned int idx)2772 init_vf_frameinfo_defaults(struct ia_css_pipe *pipe,
2773 			   struct ia_css_frame *vf_frame, unsigned int idx)
2774 {
2775 	int err = 0;
2776 	unsigned int thread_id;
2777 	enum sh_css_queue_id queue_id;
2778 
2779 	assert(vf_frame);
2780 
2781 	sh_css_pipe_get_viewfinder_frame_info(pipe, &vf_frame->frame_info, idx);
2782 	ia_css_pipeline_get_sp_thread_id(ia_css_pipe_get_pipe_num(pipe), &thread_id);
2783 	ia_css_query_internal_queue_id(IA_CSS_BUFFER_TYPE_VF_OUTPUT_FRAME + idx, thread_id, &queue_id);
2784 	vf_frame->dynamic_queue_id = queue_id;
2785 	vf_frame->buf_type = IA_CSS_BUFFER_TYPE_VF_OUTPUT_FRAME + idx;
2786 
2787 	err = ia_css_frame_init_planes(vf_frame);
2788 	return err;
2789 }
2790 
2791 static unsigned int
get_crop_lines_for_bayer_order(const struct ia_css_stream_config * config)2792 get_crop_lines_for_bayer_order(const struct ia_css_stream_config *config)
2793 {
2794 	assert(config);
2795 	if ((config->input_config.bayer_order == IA_CSS_BAYER_ORDER_BGGR) ||
2796 	    (config->input_config.bayer_order == IA_CSS_BAYER_ORDER_GBRG))
2797 		return 1;
2798 
2799 	return 0;
2800 }
2801 
2802 static unsigned int
get_crop_columns_for_bayer_order(const struct ia_css_stream_config * config)2803 get_crop_columns_for_bayer_order(const struct ia_css_stream_config *config)
2804 {
2805 	assert(config);
2806 	if ((config->input_config.bayer_order == IA_CSS_BAYER_ORDER_RGGB) ||
2807 	    (config->input_config.bayer_order == IA_CSS_BAYER_ORDER_GBRG))
2808 		return 1;
2809 
2810 	return 0;
2811 }
2812 
2813 /*
2814  * This function is to get the sum of all extra pixels in addition to the effective
2815  * input, it includes dvs envelop and filter run-in
2816  */
get_pipe_extra_pixel(struct ia_css_pipe * pipe,unsigned int * extra_row,unsigned int * extra_column)2817 static void get_pipe_extra_pixel(struct ia_css_pipe *pipe,
2818 				 unsigned int *extra_row, unsigned int *extra_column)
2819 {
2820 	enum ia_css_pipe_id pipe_id = pipe->mode;
2821 	unsigned int left_cropping = 0, top_cropping = 0;
2822 	unsigned int i;
2823 	struct ia_css_resolution dvs_env = pipe->config.dvs_envelope;
2824 
2825 	/*
2826 	 * The dvs envelope info may not be correctly sent down via pipe config
2827 	 * The check is made and the correct value is populated in the binary info
2828 	 * Use this value when computing crop, else excess lines may get trimmed
2829 	 */
2830 	switch (pipe_id) {
2831 	case IA_CSS_PIPE_ID_PREVIEW:
2832 		if (pipe->pipe_settings.preview.preview_binary.info) {
2833 			left_cropping =
2834 			    pipe->pipe_settings.preview.preview_binary.info->sp.pipeline.left_cropping;
2835 			top_cropping =
2836 			    pipe->pipe_settings.preview.preview_binary.info->sp.pipeline.top_cropping;
2837 		}
2838 		dvs_env = pipe->pipe_settings.preview.preview_binary.dvs_envelope;
2839 		break;
2840 	case IA_CSS_PIPE_ID_VIDEO:
2841 		if (pipe->pipe_settings.video.video_binary.info) {
2842 			left_cropping =
2843 			    pipe->pipe_settings.video.video_binary.info->sp.pipeline.left_cropping;
2844 			top_cropping =
2845 			    pipe->pipe_settings.video.video_binary.info->sp.pipeline.top_cropping;
2846 		}
2847 		dvs_env = pipe->pipe_settings.video.video_binary.dvs_envelope;
2848 		break;
2849 	case IA_CSS_PIPE_ID_CAPTURE:
2850 		for (i = 0; i < pipe->pipe_settings.capture.num_primary_stage; i++) {
2851 			if (pipe->pipe_settings.capture.primary_binary[i].info) {
2852 				left_cropping +=
2853 				    pipe->pipe_settings.capture.primary_binary[i].info->sp.pipeline.left_cropping;
2854 				top_cropping +=
2855 				    pipe->pipe_settings.capture.primary_binary[i].info->sp.pipeline.top_cropping;
2856 			}
2857 			dvs_env.width +=
2858 			    pipe->pipe_settings.capture.primary_binary[i].dvs_envelope.width;
2859 			dvs_env.height +=
2860 			    pipe->pipe_settings.capture.primary_binary[i].dvs_envelope.height;
2861 		}
2862 		break;
2863 	default:
2864 		break;
2865 	}
2866 
2867 	*extra_row = top_cropping + dvs_env.height;
2868 	*extra_column = left_cropping + dvs_env.width;
2869 }
2870 
2871 void
ia_css_get_crop_offsets(struct ia_css_pipe * pipe,struct ia_css_frame_info * in_frame)2872 ia_css_get_crop_offsets(
2873     struct ia_css_pipe *pipe,
2874     struct ia_css_frame_info *in_frame)
2875 {
2876 	unsigned int row = 0;
2877 	unsigned int column = 0;
2878 	struct ia_css_resolution *input_res;
2879 	struct ia_css_resolution *effective_res;
2880 	unsigned int extra_row = 0, extra_col = 0;
2881 	unsigned int min_reqd_height, min_reqd_width;
2882 
2883 	assert(pipe);
2884 	assert(pipe->stream);
2885 	assert(in_frame);
2886 
2887 	IA_CSS_ENTER_PRIVATE("pipe = %p effective_wd = %u effective_ht = %u",
2888 			     pipe, pipe->config.input_effective_res.width,
2889 			     pipe->config.input_effective_res.height);
2890 
2891 	input_res = &pipe->stream->config.input_config.input_res;
2892 
2893 	if (IS_ISP2401)
2894 		effective_res = &pipe->config.input_effective_res;
2895 	else
2896 		effective_res = &pipe->stream->config.input_config.effective_res;
2897 
2898 	get_pipe_extra_pixel(pipe, &extra_row, &extra_col);
2899 
2900 	in_frame->raw_bayer_order = pipe->stream->config.input_config.bayer_order;
2901 
2902 	min_reqd_height = effective_res->height + extra_row;
2903 	min_reqd_width = effective_res->width + extra_col;
2904 
2905 	if (input_res->height > min_reqd_height) {
2906 		row = (input_res->height - min_reqd_height) / 2;
2907 		row &= ~0x1;
2908 	}
2909 	if (input_res->width > min_reqd_width) {
2910 		column = (input_res->width - min_reqd_width) / 2;
2911 		column &= ~0x1;
2912 	}
2913 
2914 	/*
2915 	 * TODO:
2916 	 * 1. Require the special support for RAW10 packed mode.
2917 	 * 2. Require the special support for the online use cases.
2918 	 */
2919 
2920 	/*
2921 	 * ISP expects GRBG bayer order, we skip one line and/or one row
2922 	 * to correct in case the input bayer order is different.
2923 	 */
2924 	column += get_crop_columns_for_bayer_order(&pipe->stream->config);
2925 	row += get_crop_lines_for_bayer_order(&pipe->stream->config);
2926 
2927 	in_frame->crop_info.start_column = column;
2928 	in_frame->crop_info.start_line = row;
2929 
2930 	IA_CSS_LEAVE_PRIVATE("void start_col: %u start_row: %u", column, row);
2931 
2932 	return;
2933 }
2934 
2935 static int
init_in_frameinfo_memory_defaults(struct ia_css_pipe * pipe,struct ia_css_frame * frame,enum ia_css_frame_format format)2936 init_in_frameinfo_memory_defaults(struct ia_css_pipe *pipe,
2937 				  struct ia_css_frame *frame, enum ia_css_frame_format format)
2938 {
2939 	struct ia_css_frame *in_frame;
2940 	int err = 0;
2941 	unsigned int thread_id;
2942 	enum sh_css_queue_id queue_id;
2943 
2944 	assert(frame);
2945 	in_frame = frame;
2946 
2947 	in_frame->frame_info.format = format;
2948 
2949 	if (IS_ISP2401 && format == IA_CSS_FRAME_FORMAT_RAW) {
2950 		in_frame->frame_info.format = (pipe->stream->config.pack_raw_pixels) ?
2951 		IA_CSS_FRAME_FORMAT_RAW_PACKED : IA_CSS_FRAME_FORMAT_RAW;
2952 	}
2953 
2954 	in_frame->frame_info.res.width = pipe->stream->config.input_config.input_res.width;
2955 	in_frame->frame_info.res.height = pipe->stream->config.input_config.input_res.height;
2956 	in_frame->frame_info.raw_bit_depth = ia_css_pipe_util_pipe_input_format_bpp(pipe);
2957 	ia_css_frame_info_set_width(&in_frame->frame_info,
2958 				    pipe->stream->config.input_config.input_res.width, 0);
2959 	ia_css_pipeline_get_sp_thread_id(ia_css_pipe_get_pipe_num(pipe), &thread_id);
2960 	ia_css_query_internal_queue_id(IA_CSS_BUFFER_TYPE_INPUT_FRAME, thread_id, &queue_id);
2961 	in_frame->dynamic_queue_id = queue_id;
2962 	in_frame->buf_type = IA_CSS_BUFFER_TYPE_INPUT_FRAME;
2963 
2964 	if (IS_ISP2401)
2965 		ia_css_get_crop_offsets(pipe, &in_frame->frame_info);
2966 
2967 	err = ia_css_frame_init_planes(in_frame);
2968 
2969 	ia_css_debug_dtrace(IA_CSS_DEBUG_TRACE_PRIVATE, "%s() bayer_order = %d\n",
2970 			    __func__, in_frame->frame_info.raw_bayer_order);
2971 
2972 	return err;
2973 }
2974 
2975 static int
init_out_frameinfo_defaults(struct ia_css_pipe * pipe,struct ia_css_frame * out_frame,unsigned int idx)2976 init_out_frameinfo_defaults(struct ia_css_pipe *pipe,
2977 			    struct ia_css_frame *out_frame, unsigned int idx)
2978 {
2979 	int err = 0;
2980 	unsigned int thread_id;
2981 	enum sh_css_queue_id queue_id;
2982 
2983 	assert(out_frame);
2984 
2985 	sh_css_pipe_get_output_frame_info(pipe, &out_frame->frame_info, idx);
2986 	ia_css_pipeline_get_sp_thread_id(ia_css_pipe_get_pipe_num(pipe), &thread_id);
2987 	ia_css_query_internal_queue_id(IA_CSS_BUFFER_TYPE_OUTPUT_FRAME + idx, thread_id, &queue_id);
2988 	out_frame->dynamic_queue_id = queue_id;
2989 	out_frame->buf_type = IA_CSS_BUFFER_TYPE_OUTPUT_FRAME + idx;
2990 	err = ia_css_frame_init_planes(out_frame);
2991 
2992 	return err;
2993 }
2994 
2995 /* Create stages for video pipe */
create_host_video_pipeline(struct ia_css_pipe * pipe)2996 static int create_host_video_pipeline(struct ia_css_pipe *pipe)
2997 {
2998 	struct ia_css_pipeline_stage_desc stage_desc;
2999 	struct ia_css_binary *copy_binary, *video_binary,
3000 		       *yuv_scaler_binary, *vf_pp_binary;
3001 	struct ia_css_pipeline_stage *copy_stage  = NULL;
3002 	struct ia_css_pipeline_stage *video_stage = NULL;
3003 	struct ia_css_pipeline_stage *yuv_scaler_stage  = NULL;
3004 	struct ia_css_pipeline_stage *vf_pp_stage = NULL;
3005 	struct ia_css_pipeline *me;
3006 	struct ia_css_frame *in_frame = NULL;
3007 	struct ia_css_frame *out_frame;
3008 	struct ia_css_frame *out_frames[IA_CSS_BINARY_MAX_OUTPUT_PORTS];
3009 	struct ia_css_frame *vf_frame = NULL;
3010 	int err = 0;
3011 	bool need_copy   = false;
3012 	bool need_vf_pp  = false;
3013 	bool need_yuv_pp = false;
3014 	bool need_in_frameinfo_memory = false;
3015 
3016 	unsigned int i, num_yuv_scaler;
3017 	bool *is_output_stage = NULL;
3018 
3019 	IA_CSS_ENTER_PRIVATE("pipe = %p", pipe);
3020 	if ((!pipe) || (!pipe->stream) || (pipe->mode != IA_CSS_PIPE_ID_VIDEO)) {
3021 		IA_CSS_LEAVE_ERR_PRIVATE(-EINVAL);
3022 		return -EINVAL;
3023 	}
3024 	ia_css_pipe_util_create_output_frames(out_frames);
3025 	out_frame = &pipe->out_frame_struct;
3026 
3027 	/* pipeline already created as part of create_host_pipeline_structure */
3028 	me = &pipe->pipeline;
3029 	ia_css_pipeline_clean(me);
3030 
3031 	me->dvs_frame_delay = pipe->dvs_frame_delay;
3032 
3033 	if (IS_ISP2401) {
3034 		/*
3035 		 * When the input system is 2401, always enable 'in_frameinfo_memory'
3036 		 * except for the following: online or continuous
3037 		 */
3038 		need_in_frameinfo_memory = !(pipe->stream->config.online ||
3039 					     pipe->stream->config.continuous);
3040 	} else {
3041 		/* Construct in_frame info (only in case we have dynamic input */
3042 		need_in_frameinfo_memory = pipe->stream->config.mode ==
3043 					   IA_CSS_INPUT_MODE_MEMORY;
3044 	}
3045 
3046 	/* Construct in_frame info (only in case we have dynamic input */
3047 	if (need_in_frameinfo_memory) {
3048 		in_frame = &pipe->in_frame_struct;
3049 		err = init_in_frameinfo_memory_defaults(pipe, in_frame,
3050 							IA_CSS_FRAME_FORMAT_RAW);
3051 		if (err)
3052 			goto ERR;
3053 	}
3054 
3055 	out_frame->data = 0;
3056 	err = init_out_frameinfo_defaults(pipe, out_frame, 0);
3057 	if (err)
3058 		goto ERR;
3059 
3060 	if (pipe->enable_viewfinder[IA_CSS_PIPE_OUTPUT_STAGE_0]) {
3061 		vf_frame = &pipe->vf_frame_struct;
3062 		vf_frame->data = 0;
3063 		err = init_vf_frameinfo_defaults(pipe, vf_frame, 0);
3064 		if (err)
3065 			goto ERR;
3066 	}
3067 
3068 	copy_binary  = &pipe->pipe_settings.video.copy_binary;
3069 	video_binary = &pipe->pipe_settings.video.video_binary;
3070 	vf_pp_binary = &pipe->pipe_settings.video.vf_pp_binary;
3071 
3072 	yuv_scaler_binary = pipe->pipe_settings.video.yuv_scaler_binary;
3073 	num_yuv_scaler  = pipe->pipe_settings.video.num_yuv_scaler;
3074 	is_output_stage = pipe->pipe_settings.video.is_output_stage;
3075 
3076 	need_copy   = (copy_binary && copy_binary->info);
3077 	need_vf_pp  = (vf_pp_binary && vf_pp_binary->info);
3078 	need_yuv_pp = (yuv_scaler_binary && yuv_scaler_binary->info);
3079 
3080 	if (need_copy) {
3081 		ia_css_pipe_util_set_output_frames(out_frames, 0, NULL);
3082 		ia_css_pipe_get_generic_stage_desc(&stage_desc, copy_binary,
3083 						   out_frames, NULL, NULL);
3084 		err = ia_css_pipeline_create_and_add_stage(me, &stage_desc,
3085 							   &copy_stage);
3086 		if (err)
3087 			goto ERR;
3088 		in_frame = me->stages->args.out_frame[0];
3089 	} else if (pipe->stream->config.continuous) {
3090 		if (IS_ISP2401)
3091 			/*
3092 			 * When continuous is enabled, configure in_frame with the
3093 			 * last pipe, which is the copy pipe.
3094 			 */
3095 			in_frame = pipe->stream->last_pipe->continuous_frames[0];
3096 		else
3097 			in_frame = pipe->continuous_frames[0];
3098 	}
3099 
3100 	ia_css_pipe_util_set_output_frames(out_frames, 0,
3101 					   need_yuv_pp ? NULL : out_frame);
3102 
3103 	/*
3104 	 * when the video binary supports a second output pin,
3105 	 * it can directly produce the vf_frame.
3106 	 */
3107 	if (need_vf_pp) {
3108 		ia_css_pipe_get_generic_stage_desc(&stage_desc, video_binary,
3109 						   out_frames, in_frame, NULL);
3110 	} else {
3111 		ia_css_pipe_get_generic_stage_desc(&stage_desc, video_binary,
3112 						   out_frames, in_frame, vf_frame);
3113 	}
3114 	err = ia_css_pipeline_create_and_add_stage(me, &stage_desc,
3115 						   &video_stage);
3116 	if (err)
3117 		goto ERR;
3118 
3119 	/* If we use copy iso video, the input must be yuv iso raw */
3120 	if (video_stage) {
3121 		video_stage->args.copy_vf =
3122 		    video_binary->info->sp.pipeline.mode == IA_CSS_BINARY_MODE_COPY;
3123 		video_stage->args.copy_output = video_stage->args.copy_vf;
3124 	}
3125 
3126 	/* when the video binary supports only 1 output pin, vf_pp is needed to
3127 	produce the vf_frame.*/
3128 	if (need_vf_pp && video_stage) {
3129 		in_frame = video_stage->args.out_vf_frame;
3130 		err = add_vf_pp_stage(pipe, in_frame, vf_frame, vf_pp_binary,
3131 				      &vf_pp_stage);
3132 		if (err)
3133 			goto ERR;
3134 	}
3135 	if (video_stage) {
3136 		int frm;
3137 
3138 		for (frm = 0; frm < NUM_VIDEO_TNR_FRAMES; frm++) {
3139 			video_stage->args.tnr_frames[frm] =
3140 			    pipe->pipe_settings.video.tnr_frames[frm];
3141 		}
3142 		for (frm = 0; frm < MAX_NUM_VIDEO_DELAY_FRAMES; frm++) {
3143 			video_stage->args.delay_frames[frm] =
3144 			    pipe->pipe_settings.video.delay_frames[frm];
3145 		}
3146 	}
3147 
3148 	if (need_yuv_pp && video_stage) {
3149 		struct ia_css_frame *tmp_in_frame = video_stage->args.out_frame[0];
3150 		struct ia_css_frame *tmp_out_frame = NULL;
3151 
3152 		for (i = 0; i < num_yuv_scaler; i++) {
3153 			tmp_out_frame = is_output_stage[i] ? out_frame : NULL;
3154 
3155 			err = add_yuv_scaler_stage(pipe, me, tmp_in_frame,
3156 						   tmp_out_frame, NULL,
3157 						   &yuv_scaler_binary[i],
3158 						   &yuv_scaler_stage);
3159 
3160 			if (err) {
3161 				IA_CSS_LEAVE_ERR_PRIVATE(err);
3162 				return err;
3163 			}
3164 			/* we use output port 1 as internal output port */
3165 			if (yuv_scaler_stage)
3166 				tmp_in_frame = yuv_scaler_stage->args.out_frame[1];
3167 		}
3168 	}
3169 
3170 	pipe->pipeline.acquire_isp_each_stage = false;
3171 	ia_css_pipeline_finalize_stages(&pipe->pipeline,
3172 					pipe->stream->config.continuous);
3173 
3174 ERR:
3175 	IA_CSS_LEAVE_ERR_PRIVATE(err);
3176 	return err;
3177 }
3178 
3179 /* Create stages for preview */
3180 static int
create_host_preview_pipeline(struct ia_css_pipe * pipe)3181 create_host_preview_pipeline(struct ia_css_pipe *pipe)
3182 {
3183 	struct ia_css_pipeline_stage *copy_stage = NULL;
3184 	struct ia_css_pipeline_stage *preview_stage = NULL;
3185 	struct ia_css_pipeline_stage *vf_pp_stage = NULL;
3186 	struct ia_css_pipeline_stage_desc stage_desc;
3187 	struct ia_css_pipeline *me = NULL;
3188 	struct ia_css_binary *copy_binary, *preview_binary, *vf_pp_binary = NULL;
3189 	struct ia_css_frame *in_frame = NULL;
3190 	int err = 0;
3191 	struct ia_css_frame *out_frame;
3192 	struct ia_css_frame *out_frames[IA_CSS_BINARY_MAX_OUTPUT_PORTS];
3193 	bool need_in_frameinfo_memory = false;
3194 	bool sensor = false;
3195 	bool buffered_sensor = false;
3196 	bool online = false;
3197 	bool continuous = false;
3198 
3199 	IA_CSS_ENTER_PRIVATE("pipe = %p", pipe);
3200 	if ((!pipe) || (!pipe->stream) || (pipe->mode != IA_CSS_PIPE_ID_PREVIEW)) {
3201 		IA_CSS_LEAVE_ERR_PRIVATE(-EINVAL);
3202 		return -EINVAL;
3203 	}
3204 
3205 	ia_css_pipe_util_create_output_frames(out_frames);
3206 	/* pipeline already created as part of create_host_pipeline_structure */
3207 	me = &pipe->pipeline;
3208 	ia_css_pipeline_clean(me);
3209 
3210 	if (IS_ISP2401) {
3211 		/*
3212 		 * When the input system is 2401, always enable 'in_frameinfo_memory'
3213 		 * except for the following:
3214 		 * - Direct Sensor Mode Online Preview
3215 		 * - Buffered Sensor Mode Online Preview
3216 		 * - Direct Sensor Mode Continuous Preview
3217 		 * - Buffered Sensor Mode Continuous Preview
3218 		 */
3219 		sensor = (pipe->stream->config.mode == IA_CSS_INPUT_MODE_SENSOR);
3220 		buffered_sensor = (pipe->stream->config.mode == IA_CSS_INPUT_MODE_BUFFERED_SENSOR);
3221 		online = pipe->stream->config.online;
3222 		continuous = pipe->stream->config.continuous;
3223 		need_in_frameinfo_memory =
3224 		!((sensor && (online || continuous)) || (buffered_sensor &&
3225 							(online || continuous)));
3226 	} else {
3227 		/* Construct in_frame info (only in case we have dynamic input */
3228 		need_in_frameinfo_memory = pipe->stream->config.mode == IA_CSS_INPUT_MODE_MEMORY;
3229 	}
3230 	if (need_in_frameinfo_memory) {
3231 		err = init_in_frameinfo_memory_defaults(pipe, &me->in_frame,
3232 							IA_CSS_FRAME_FORMAT_RAW);
3233 		if (err)
3234 			goto ERR;
3235 
3236 		in_frame = &me->in_frame;
3237 	} else {
3238 		in_frame = NULL;
3239 	}
3240 	err = init_out_frameinfo_defaults(pipe, &me->out_frame[0], 0);
3241 	if (err)
3242 		goto ERR;
3243 	out_frame = &me->out_frame[0];
3244 
3245 	copy_binary    = &pipe->pipe_settings.preview.copy_binary;
3246 	preview_binary = &pipe->pipe_settings.preview.preview_binary;
3247 	if (pipe->pipe_settings.preview.vf_pp_binary.info)
3248 		vf_pp_binary = &pipe->pipe_settings.preview.vf_pp_binary;
3249 
3250 	if (pipe->pipe_settings.preview.copy_binary.info) {
3251 		ia_css_pipe_util_set_output_frames(out_frames, 0, NULL);
3252 		ia_css_pipe_get_generic_stage_desc(&stage_desc, copy_binary,
3253 						   out_frames, NULL, NULL);
3254 		err = ia_css_pipeline_create_and_add_stage(me, &stage_desc,
3255 							   &copy_stage);
3256 		if (err)
3257 			goto ERR;
3258 		in_frame = me->stages->args.out_frame[0];
3259 	} else if (pipe->stream->config.continuous) {
3260 		if (IS_ISP2401) {
3261 			/*
3262 			 * When continuous is enabled, configure in_frame with the
3263 			 * last pipe, which is the copy pipe.
3264 			 */
3265 			if (continuous || !online)
3266 				in_frame = pipe->stream->last_pipe->continuous_frames[0];
3267 		} else {
3268 			in_frame = pipe->continuous_frames[0];
3269 		}
3270 	}
3271 
3272 	if (vf_pp_binary) {
3273 		ia_css_pipe_util_set_output_frames(out_frames, 0, NULL);
3274 		ia_css_pipe_get_generic_stage_desc(&stage_desc, preview_binary,
3275 						   out_frames, in_frame, NULL);
3276 	} else {
3277 		ia_css_pipe_util_set_output_frames(out_frames, 0, out_frame);
3278 		ia_css_pipe_get_generic_stage_desc(&stage_desc, preview_binary,
3279 						   out_frames, in_frame, NULL);
3280 	}
3281 	err = ia_css_pipeline_create_and_add_stage(me, &stage_desc,
3282 						   &preview_stage);
3283 	if (err)
3284 		goto ERR;
3285 	/* If we use copy iso preview, the input must be yuv iso raw */
3286 	preview_stage->args.copy_vf =
3287 	    preview_binary->info->sp.pipeline.mode == IA_CSS_BINARY_MODE_COPY;
3288 	preview_stage->args.copy_output = !preview_stage->args.copy_vf;
3289 	if (preview_stage->args.copy_vf && !preview_stage->args.out_vf_frame) {
3290 		/* in case of copy, use the vf frame as output frame */
3291 		preview_stage->args.out_vf_frame =
3292 		    preview_stage->args.out_frame[0];
3293 	}
3294 	if (vf_pp_binary) {
3295 		if (preview_binary->info->sp.pipeline.mode == IA_CSS_BINARY_MODE_COPY)
3296 			in_frame = preview_stage->args.out_vf_frame;
3297 		else
3298 			in_frame = preview_stage->args.out_frame[0];
3299 		err = add_vf_pp_stage(pipe, in_frame, out_frame, vf_pp_binary,
3300 				      &vf_pp_stage);
3301 		if (err)
3302 			goto ERR;
3303 	}
3304 
3305 	pipe->pipeline.acquire_isp_each_stage = false;
3306 	ia_css_pipeline_finalize_stages(&pipe->pipeline, pipe->stream->config.continuous);
3307 
3308 ERR:
3309 	IA_CSS_LEAVE_ERR_PRIVATE(err);
3310 	return err;
3311 }
3312 
send_raw_frames(struct ia_css_pipe * pipe)3313 static void send_raw_frames(struct ia_css_pipe *pipe)
3314 {
3315 	if (pipe->stream->config.continuous) {
3316 		unsigned int i;
3317 
3318 		sh_css_update_host2sp_cont_num_raw_frames
3319 		(pipe->stream->config.init_num_cont_raw_buf, true);
3320 		sh_css_update_host2sp_cont_num_raw_frames
3321 		(pipe->stream->config.target_num_cont_raw_buf, false);
3322 
3323 		/* Hand-over all the SP-internal buffers */
3324 		for (i = 0; i < pipe->stream->config.init_num_cont_raw_buf; i++) {
3325 			sh_css_update_host2sp_offline_frame(i,
3326 							    pipe->continuous_frames[i], pipe->cont_md_buffers[i]);
3327 		}
3328 	}
3329 
3330 	return;
3331 }
3332 
3333 static int
preview_start(struct ia_css_pipe * pipe)3334 preview_start(struct ia_css_pipe *pipe)
3335 {
3336 	int err = 0;
3337 	struct ia_css_pipe *copy_pipe, *capture_pipe;
3338 	enum sh_css_pipe_config_override copy_ovrd;
3339 	enum ia_css_input_mode preview_pipe_input_mode;
3340 	unsigned int thread_id;
3341 
3342 	IA_CSS_ENTER_PRIVATE("pipe = %p", pipe);
3343 	if ((!pipe) || (!pipe->stream) || (pipe->mode != IA_CSS_PIPE_ID_PREVIEW)) {
3344 		IA_CSS_LEAVE_ERR_PRIVATE(-EINVAL);
3345 		return -EINVAL;
3346 	}
3347 
3348 	preview_pipe_input_mode = pipe->stream->config.mode;
3349 
3350 	copy_pipe    = pipe->pipe_settings.preview.copy_pipe;
3351 	capture_pipe = pipe->pipe_settings.preview.capture_pipe;
3352 
3353 	sh_css_metrics_start_frame();
3354 
3355 	/* multi stream video needs mipi buffers */
3356 	err = send_mipi_frames(pipe);
3357 	if (err) {
3358 		IA_CSS_LEAVE_ERR_PRIVATE(err);
3359 		return err;
3360 	}
3361 	send_raw_frames(pipe);
3362 
3363 	ia_css_pipeline_get_sp_thread_id(ia_css_pipe_get_pipe_num(pipe), &thread_id);
3364 	copy_ovrd = 1 << thread_id;
3365 
3366 	if (pipe->stream->cont_capt) {
3367 		ia_css_pipeline_get_sp_thread_id(ia_css_pipe_get_pipe_num(capture_pipe),
3368 						 &thread_id);
3369 		copy_ovrd |= 1 << thread_id;
3370 	}
3371 
3372 	/* Construct and load the copy pipe */
3373 	if (pipe->stream->config.continuous) {
3374 		sh_css_sp_init_pipeline(&copy_pipe->pipeline,
3375 					IA_CSS_PIPE_ID_COPY,
3376 					(uint8_t)ia_css_pipe_get_pipe_num(copy_pipe),
3377 					false,
3378 					pipe->stream->config.pixels_per_clock == 2, false,
3379 					false, pipe->required_bds_factor,
3380 					copy_ovrd,
3381 					pipe->stream->config.mode,
3382 					&pipe->stream->config.metadata_config,
3383 					&pipe->stream->info.metadata_info,
3384 					pipe->stream->config.source.port.port);
3385 
3386 		/*
3387 		 * make the preview pipe start with mem mode input, copy handles
3388 		 * the actual mode
3389 		 */
3390 		preview_pipe_input_mode = IA_CSS_INPUT_MODE_MEMORY;
3391 	}
3392 
3393 	/* Construct and load the capture pipe */
3394 	if (pipe->stream->cont_capt) {
3395 		sh_css_sp_init_pipeline(&capture_pipe->pipeline,
3396 					IA_CSS_PIPE_ID_CAPTURE,
3397 					(uint8_t)ia_css_pipe_get_pipe_num(capture_pipe),
3398 					capture_pipe->config.default_capture_config.enable_xnr != 0,
3399 					capture_pipe->stream->config.pixels_per_clock == 2,
3400 					true, /* continuous */
3401 					false, /* offline */
3402 					capture_pipe->required_bds_factor,
3403 					0,
3404 					IA_CSS_INPUT_MODE_MEMORY,
3405 					&pipe->stream->config.metadata_config,
3406 					&pipe->stream->info.metadata_info,
3407 					(enum mipi_port_id)0);
3408 	}
3409 
3410 	start_pipe(pipe, copy_ovrd, preview_pipe_input_mode);
3411 
3412 	IA_CSS_LEAVE_ERR_PRIVATE(err);
3413 	return err;
3414 }
3415 
3416 int
ia_css_pipe_enqueue_buffer(struct ia_css_pipe * pipe,const struct ia_css_buffer * buffer)3417 ia_css_pipe_enqueue_buffer(struct ia_css_pipe *pipe,
3418 			   const struct ia_css_buffer *buffer)
3419 {
3420 	int return_err = 0;
3421 	unsigned int thread_id;
3422 	enum sh_css_queue_id queue_id;
3423 	struct ia_css_pipeline *pipeline;
3424 	struct ia_css_pipeline_stage *stage;
3425 	struct ia_css_rmgr_vbuf_handle p_vbuf;
3426 	struct ia_css_rmgr_vbuf_handle *h_vbuf;
3427 	struct sh_css_hmm_buffer ddr_buffer;
3428 	enum ia_css_buffer_type buf_type;
3429 	enum ia_css_pipe_id pipe_id;
3430 	bool ret_err;
3431 
3432 	IA_CSS_ENTER("pipe=%p, buffer=%p", pipe, buffer);
3433 
3434 	if ((!pipe) || (!buffer)) {
3435 		IA_CSS_LEAVE_ERR(-EINVAL);
3436 		return -EINVAL;
3437 	}
3438 
3439 	buf_type = buffer->type;
3440 
3441 	pipe_id = pipe->mode;
3442 
3443 	IA_CSS_LOG("pipe_id=%d, buf_type=%d", pipe_id, buf_type);
3444 
3445 	assert(pipe_id < IA_CSS_PIPE_ID_NUM);
3446 	assert(buf_type < IA_CSS_NUM_DYNAMIC_BUFFER_TYPE);
3447 	if (buf_type == IA_CSS_BUFFER_TYPE_INVALID ||
3448 	    buf_type >= IA_CSS_NUM_DYNAMIC_BUFFER_TYPE ||
3449 	    pipe_id >= IA_CSS_PIPE_ID_NUM) {
3450 		IA_CSS_LEAVE_ERR(-EINVAL);
3451 		return -EINVAL;
3452 	}
3453 
3454 	ret_err = ia_css_pipeline_get_sp_thread_id(ia_css_pipe_get_pipe_num(pipe), &thread_id);
3455 	if (!ret_err) {
3456 		IA_CSS_LEAVE_ERR(-EINVAL);
3457 		return -EINVAL;
3458 	}
3459 
3460 	ret_err = ia_css_query_internal_queue_id(buf_type, thread_id, &queue_id);
3461 	if (!ret_err) {
3462 		IA_CSS_LEAVE_ERR(-EINVAL);
3463 		return -EINVAL;
3464 	}
3465 
3466 	if ((queue_id <= SH_CSS_INVALID_QUEUE_ID) || (queue_id >= SH_CSS_MAX_NUM_QUEUES)) {
3467 		IA_CSS_LEAVE_ERR(-EINVAL);
3468 		return -EINVAL;
3469 	}
3470 
3471 	if (!sh_css_sp_is_running()) {
3472 		IA_CSS_LOG("SP is not running!");
3473 		IA_CSS_LEAVE_ERR(-EBUSY);
3474 		/* SP is not running. The queues are not valid */
3475 		return -EBUSY;
3476 	}
3477 
3478 	pipeline = &pipe->pipeline;
3479 
3480 	assert(pipeline || pipe_id == IA_CSS_PIPE_ID_COPY);
3481 
3482 	assert(sizeof(void *) <= sizeof(ddr_buffer.kernel_ptr));
3483 	ddr_buffer.kernel_ptr = HOST_ADDRESS(NULL);
3484 	ddr_buffer.cookie_ptr = buffer->driver_cookie;
3485 	ddr_buffer.timing_data = buffer->timing_data;
3486 
3487 	if (buf_type == IA_CSS_BUFFER_TYPE_3A_STATISTICS) {
3488 		if (!buffer->data.stats_3a) {
3489 			IA_CSS_LEAVE_ERR(-EINVAL);
3490 			return -EINVAL;
3491 		}
3492 		ddr_buffer.kernel_ptr = HOST_ADDRESS(buffer->data.stats_3a);
3493 		ddr_buffer.payload.s3a = *buffer->data.stats_3a;
3494 	} else if (buf_type == IA_CSS_BUFFER_TYPE_DIS_STATISTICS) {
3495 		if (!buffer->data.stats_dvs) {
3496 			IA_CSS_LEAVE_ERR(-EINVAL);
3497 			return -EINVAL;
3498 		}
3499 		ddr_buffer.kernel_ptr = HOST_ADDRESS(buffer->data.stats_dvs);
3500 		ddr_buffer.payload.dis = *buffer->data.stats_dvs;
3501 	} else if (buf_type == IA_CSS_BUFFER_TYPE_METADATA) {
3502 		if (!buffer->data.metadata) {
3503 			IA_CSS_LEAVE_ERR(-EINVAL);
3504 			return -EINVAL;
3505 		}
3506 		ddr_buffer.kernel_ptr = HOST_ADDRESS(buffer->data.metadata);
3507 		ddr_buffer.payload.metadata = *buffer->data.metadata;
3508 	} else if (buf_type == IA_CSS_BUFFER_TYPE_INPUT_FRAME ||
3509 		   buf_type == IA_CSS_BUFFER_TYPE_OUTPUT_FRAME ||
3510 		   buf_type == IA_CSS_BUFFER_TYPE_VF_OUTPUT_FRAME ||
3511 		   buf_type == IA_CSS_BUFFER_TYPE_SEC_OUTPUT_FRAME ||
3512 		   buf_type == IA_CSS_BUFFER_TYPE_SEC_VF_OUTPUT_FRAME) {
3513 		if (!buffer->data.frame) {
3514 			IA_CSS_LEAVE_ERR(-EINVAL);
3515 			return -EINVAL;
3516 		}
3517 		ddr_buffer.kernel_ptr = HOST_ADDRESS(buffer->data.frame);
3518 		ddr_buffer.payload.frame.frame_data = buffer->data.frame->data;
3519 		ddr_buffer.payload.frame.flashed = 0;
3520 
3521 		ia_css_debug_dtrace(IA_CSS_DEBUG_TRACE,
3522 				    "ia_css_pipe_enqueue_buffer() buf_type=%d, data(DDR address)=0x%x\n",
3523 				    buf_type, buffer->data.frame->data);
3524 
3525 	}
3526 
3527 	/* start of test for using rmgr for acq/rel memory */
3528 	p_vbuf.vptr = 0;
3529 	p_vbuf.count = 0;
3530 	p_vbuf.size = sizeof(struct sh_css_hmm_buffer);
3531 	h_vbuf = &p_vbuf;
3532 	/* TODO: change next to correct pool for optimization */
3533 	ia_css_rmgr_acq_vbuf(hmm_buffer_pool, &h_vbuf);
3534 
3535 	if ((!h_vbuf) || (h_vbuf->vptr == 0x0)) {
3536 		IA_CSS_LEAVE_ERR(-EINVAL);
3537 		return -EINVAL;
3538 	}
3539 
3540 	hmm_store(h_vbuf->vptr,
3541 		  (void *)(&ddr_buffer),
3542 		  sizeof(struct sh_css_hmm_buffer));
3543 	if (buf_type == IA_CSS_BUFFER_TYPE_3A_STATISTICS ||
3544 	    buf_type == IA_CSS_BUFFER_TYPE_DIS_STATISTICS ||
3545 	    buf_type == IA_CSS_BUFFER_TYPE_LACE_STATISTICS) {
3546 		if (!pipeline) {
3547 			ia_css_rmgr_rel_vbuf(hmm_buffer_pool, &h_vbuf);
3548 			IA_CSS_LOG("pipeline is empty!");
3549 			IA_CSS_LEAVE_ERR(-EINVAL);
3550 			return -EINVAL;
3551 		}
3552 
3553 		for (stage = pipeline->stages; stage; stage = stage->next) {
3554 			/*
3555 			 * The SP will read the params after it got
3556 			 * empty 3a and dis
3557 			 */
3558 			if (stage->binary && stage->binary->info &&
3559 			    (stage->binary->info->sp.enable.s3a ||
3560 			     stage->binary->info->sp.enable.dis)) {
3561 				/* there is a stage that needs it */
3562 				return_err = ia_css_bufq_enqueue_buffer(thread_id,
3563 									queue_id,
3564 									(uint32_t)h_vbuf->vptr);
3565 			}
3566 		}
3567 	} else if (buf_type == IA_CSS_BUFFER_TYPE_INPUT_FRAME ||
3568 		   buf_type == IA_CSS_BUFFER_TYPE_OUTPUT_FRAME ||
3569 		   buf_type == IA_CSS_BUFFER_TYPE_VF_OUTPUT_FRAME ||
3570 		   buf_type == IA_CSS_BUFFER_TYPE_SEC_OUTPUT_FRAME ||
3571 		   buf_type == IA_CSS_BUFFER_TYPE_SEC_VF_OUTPUT_FRAME ||
3572 		   buf_type == IA_CSS_BUFFER_TYPE_METADATA) {
3573 		return_err = ia_css_bufq_enqueue_buffer(thread_id,
3574 							queue_id,
3575 							(uint32_t)h_vbuf->vptr);
3576 		if (!return_err &&
3577 		    buf_type == IA_CSS_BUFFER_TYPE_OUTPUT_FRAME) {
3578 			IA_CSS_LOG("pfp: enqueued OF %d to q %d thread %d",
3579 				   ddr_buffer.payload.frame.frame_data,
3580 				   queue_id, thread_id);
3581 		}
3582 	}
3583 
3584 	if (!return_err) {
3585 		if (sh_css_hmm_buffer_record_acquire(
3586 			h_vbuf, buf_type,
3587 			HOST_ADDRESS(ddr_buffer.kernel_ptr))) {
3588 			IA_CSS_LOG("send vbuf=%p", h_vbuf);
3589 		} else {
3590 			return_err = -EINVAL;
3591 			IA_CSS_ERROR("hmm_buffer_record[]: no available slots\n");
3592 		}
3593 	}
3594 
3595 	/*
3596 	 * Tell the SP which queues are not empty,
3597 	 * by sending the software event.
3598 	 */
3599 	if (!return_err) {
3600 		if (!sh_css_sp_is_running()) {
3601 			/* SP is not running. The queues are not valid */
3602 			IA_CSS_LOG("SP is not running!");
3603 			IA_CSS_LEAVE_ERR(-EBUSY);
3604 			return -EBUSY;
3605 		}
3606 		return_err = ia_css_bufq_enqueue_psys_event(
3607 				 IA_CSS_PSYS_SW_EVENT_BUFFER_ENQUEUED,
3608 				 (uint8_t)thread_id,
3609 				 queue_id,
3610 				 0);
3611 	} else {
3612 		ia_css_rmgr_rel_vbuf(hmm_buffer_pool, &h_vbuf);
3613 		IA_CSS_ERROR("buffer not enqueued");
3614 	}
3615 
3616 	IA_CSS_LEAVE("return value = %d", return_err);
3617 
3618 	return return_err;
3619 }
3620 
3621 /*
3622  * TODO: Free up the hmm memory space.
3623  */
3624 int
ia_css_pipe_dequeue_buffer(struct ia_css_pipe * pipe,struct ia_css_buffer * buffer)3625 ia_css_pipe_dequeue_buffer(struct ia_css_pipe *pipe,
3626 			   struct ia_css_buffer *buffer)
3627 {
3628 	int return_err;
3629 	enum sh_css_queue_id queue_id;
3630 	ia_css_ptr ddr_buffer_addr = (ia_css_ptr)0;
3631 	struct sh_css_hmm_buffer ddr_buffer;
3632 	enum ia_css_buffer_type buf_type;
3633 	enum ia_css_pipe_id pipe_id;
3634 	unsigned int thread_id;
3635 	hrt_address kernel_ptr = 0;
3636 	bool ret_err;
3637 
3638 	IA_CSS_ENTER("pipe=%p, buffer=%p", pipe, buffer);
3639 
3640 	if ((!pipe) || (!buffer)) {
3641 		IA_CSS_LEAVE_ERR(-EINVAL);
3642 		return -EINVAL;
3643 	}
3644 
3645 	pipe_id = pipe->mode;
3646 
3647 	buf_type = buffer->type;
3648 
3649 	IA_CSS_LOG("pipe_id=%d, buf_type=%d", pipe_id, buf_type);
3650 
3651 	ddr_buffer.kernel_ptr = 0;
3652 
3653 	ret_err = ia_css_pipeline_get_sp_thread_id(ia_css_pipe_get_pipe_num(pipe), &thread_id);
3654 	if (!ret_err) {
3655 		IA_CSS_LEAVE_ERR(-EINVAL);
3656 		return -EINVAL;
3657 	}
3658 
3659 	ret_err = ia_css_query_internal_queue_id(buf_type, thread_id, &queue_id);
3660 	if (!ret_err) {
3661 		IA_CSS_LEAVE_ERR(-EINVAL);
3662 		return -EINVAL;
3663 	}
3664 
3665 	if ((queue_id <= SH_CSS_INVALID_QUEUE_ID) || (queue_id >= SH_CSS_MAX_NUM_QUEUES)) {
3666 		IA_CSS_LEAVE_ERR(-EINVAL);
3667 		return -EINVAL;
3668 	}
3669 
3670 	if (!sh_css_sp_is_running()) {
3671 		IA_CSS_LOG("SP is not running!");
3672 		IA_CSS_LEAVE_ERR(-EBUSY);
3673 		/* SP is not running. The queues are not valid */
3674 		return -EBUSY;
3675 	}
3676 
3677 	return_err = ia_css_bufq_dequeue_buffer(queue_id,
3678 						(uint32_t *)&ddr_buffer_addr);
3679 
3680 	if (!return_err) {
3681 		struct ia_css_frame *frame;
3682 		struct sh_css_hmm_buffer_record *hmm_buffer_record = NULL;
3683 
3684 		IA_CSS_LOG("receive vbuf=%x", (int)ddr_buffer_addr);
3685 
3686 		/* Validate the ddr_buffer_addr and buf_type */
3687 		hmm_buffer_record = sh_css_hmm_buffer_record_validate(
3688 		    ddr_buffer_addr, buf_type);
3689 		if (hmm_buffer_record) {
3690 			/*
3691 			 * valid hmm_buffer_record found. Save the kernel_ptr
3692 			 * for validation after performing hmm_load.  The
3693 			 * vbuf handle and buffer_record can be released.
3694 			 */
3695 			kernel_ptr = hmm_buffer_record->kernel_ptr;
3696 			ia_css_rmgr_rel_vbuf(hmm_buffer_pool, &hmm_buffer_record->h_vbuf);
3697 			sh_css_hmm_buffer_record_reset(hmm_buffer_record);
3698 		} else {
3699 			IA_CSS_ERROR("hmm_buffer_record not found (0x%x) buf_type(%d)",
3700 				     ddr_buffer_addr, buf_type);
3701 			IA_CSS_LEAVE_ERR(-EINVAL);
3702 			return -EINVAL;
3703 		}
3704 
3705 		hmm_load(ddr_buffer_addr,
3706 			 &ddr_buffer,
3707 			 sizeof(struct sh_css_hmm_buffer));
3708 
3709 		/*
3710 		 * if the kernel_ptr is 0 or an invalid, return an error.
3711 		 * do not access the buffer via the kernal_ptr.
3712 		 */
3713 		if ((ddr_buffer.kernel_ptr == 0) ||
3714 		    (kernel_ptr != HOST_ADDRESS(ddr_buffer.kernel_ptr))) {
3715 			IA_CSS_ERROR("kernel_ptr invalid");
3716 			IA_CSS_ERROR("expected: (0x%llx)", (u64)kernel_ptr);
3717 			IA_CSS_ERROR("actual: (0x%llx)", (u64)HOST_ADDRESS(ddr_buffer.kernel_ptr));
3718 			IA_CSS_ERROR("buf_type: %d\n", buf_type);
3719 			IA_CSS_LEAVE_ERR(-EINVAL);
3720 			return -EINVAL;
3721 		}
3722 
3723 		if (ddr_buffer.kernel_ptr != 0) {
3724 			/*
3725 			 * buffer->exp_id : all instances to be removed later
3726 			 * once the driver change is completed. See patch #5758
3727 			 * for reference
3728 			 */
3729 			buffer->exp_id = 0;
3730 			buffer->driver_cookie = ddr_buffer.cookie_ptr;
3731 			buffer->timing_data = ddr_buffer.timing_data;
3732 
3733 			if (buf_type == IA_CSS_BUFFER_TYPE_OUTPUT_FRAME ||
3734 			    buf_type == IA_CSS_BUFFER_TYPE_VF_OUTPUT_FRAME) {
3735 				buffer->isys_eof_clock_tick.ticks = ddr_buffer.isys_eof_clock_tick;
3736 			}
3737 
3738 			switch (buf_type) {
3739 			case IA_CSS_BUFFER_TYPE_INPUT_FRAME:
3740 			case IA_CSS_BUFFER_TYPE_OUTPUT_FRAME:
3741 			case IA_CSS_BUFFER_TYPE_SEC_OUTPUT_FRAME:
3742 			case IA_CSS_BUFFER_TYPE_VF_OUTPUT_FRAME:
3743 			case IA_CSS_BUFFER_TYPE_SEC_VF_OUTPUT_FRAME:
3744 				frame = (struct ia_css_frame *)HOST_ADDRESS(ddr_buffer.kernel_ptr);
3745 				buffer->data.frame = frame;
3746 				buffer->exp_id = ddr_buffer.payload.frame.exp_id;
3747 				frame->exp_id = ddr_buffer.payload.frame.exp_id;
3748 				frame->isp_config_id = ddr_buffer.payload.frame.isp_parameters_id;
3749 				frame->valid = pipe->num_invalid_frames == 0;
3750 				if (!frame->valid)
3751 					pipe->num_invalid_frames--;
3752 
3753 				if (frame->frame_info.format == IA_CSS_FRAME_FORMAT_BINARY_8) {
3754 					if (IS_ISP2401)
3755 						frame->planes.binary.size = frame->data_bytes;
3756 					else
3757 						frame->planes.binary.size =
3758 						    sh_css_sp_get_binary_copy_size();
3759 				}
3760 				if (buf_type == IA_CSS_BUFFER_TYPE_OUTPUT_FRAME) {
3761 					IA_CSS_LOG("pfp: dequeued OF %d with config id %d thread %d",
3762 						   frame->data, frame->isp_config_id, thread_id);
3763 				}
3764 
3765 				ia_css_debug_dtrace(IA_CSS_DEBUG_TRACE,
3766 						    "ia_css_pipe_dequeue_buffer() buf_type=%d, data(DDR address)=0x%x\n",
3767 						    buf_type, buffer->data.frame->data);
3768 
3769 				break;
3770 			case IA_CSS_BUFFER_TYPE_3A_STATISTICS:
3771 				buffer->data.stats_3a =
3772 				    (struct ia_css_isp_3a_statistics *)HOST_ADDRESS(ddr_buffer.kernel_ptr);
3773 				buffer->exp_id = ddr_buffer.payload.s3a.exp_id;
3774 				buffer->data.stats_3a->exp_id = ddr_buffer.payload.s3a.exp_id;
3775 				buffer->data.stats_3a->isp_config_id = ddr_buffer.payload.s3a.isp_config_id;
3776 				break;
3777 			case IA_CSS_BUFFER_TYPE_DIS_STATISTICS:
3778 				buffer->data.stats_dvs =
3779 				    (struct ia_css_isp_dvs_statistics *)
3780 				    HOST_ADDRESS(ddr_buffer.kernel_ptr);
3781 				buffer->exp_id = ddr_buffer.payload.dis.exp_id;
3782 				buffer->data.stats_dvs->exp_id = ddr_buffer.payload.dis.exp_id;
3783 				break;
3784 			case IA_CSS_BUFFER_TYPE_LACE_STATISTICS:
3785 				break;
3786 			case IA_CSS_BUFFER_TYPE_METADATA:
3787 				buffer->data.metadata =
3788 				    (struct ia_css_metadata *)HOST_ADDRESS(ddr_buffer.kernel_ptr);
3789 				buffer->exp_id = ddr_buffer.payload.metadata.exp_id;
3790 				buffer->data.metadata->exp_id = ddr_buffer.payload.metadata.exp_id;
3791 				break;
3792 			default:
3793 				return_err = -EINVAL;
3794 				break;
3795 			}
3796 		}
3797 	}
3798 
3799 	/*
3800 	 * Tell the SP which queues are not full,
3801 	 * by sending the software event.
3802 	 */
3803 	if (!return_err) {
3804 		if (!sh_css_sp_is_running()) {
3805 			IA_CSS_LOG("SP is not running!");
3806 			IA_CSS_LEAVE_ERR(-EBUSY);
3807 			/* SP is not running. The queues are not valid */
3808 			return -EBUSY;
3809 		}
3810 		ia_css_bufq_enqueue_psys_event(
3811 		    IA_CSS_PSYS_SW_EVENT_BUFFER_DEQUEUED,
3812 		    0,
3813 		    queue_id,
3814 		    0);
3815 	}
3816 	IA_CSS_LEAVE("buffer=%p", buffer);
3817 
3818 	return return_err;
3819 }
3820 
3821 /*
3822  * Cannot Move this to event module as it is of ia_css_event_type which is declared in ia_css.h
3823  * TODO: modify and move it if possible.
3824  *
3825  * !!!IMPORTANT!!! KEEP THE FOLLOWING IN SYNC:
3826  * 1) "enum ia_css_event_type"					(ia_css_event_public.h)
3827  * 2) "enum sh_css_sp_event_type"				(sh_css_internal.h)
3828  * 3) "enum ia_css_event_type event_id_2_event_mask"		(event_handler.sp.c)
3829  * 4) "enum ia_css_event_type convert_event_sp_to_host_domain"	(sh_css.c)
3830  */
3831 static enum ia_css_event_type convert_event_sp_to_host_domain[] = {
3832 	IA_CSS_EVENT_TYPE_OUTPUT_FRAME_DONE,	/* Output frame ready. */
3833 	IA_CSS_EVENT_TYPE_SECOND_OUTPUT_FRAME_DONE,	/* Second output frame ready. */
3834 	IA_CSS_EVENT_TYPE_VF_OUTPUT_FRAME_DONE,	/* Viewfinder Output frame ready. */
3835 	IA_CSS_EVENT_TYPE_SECOND_VF_OUTPUT_FRAME_DONE,	/* Second viewfinder Output frame ready. */
3836 	IA_CSS_EVENT_TYPE_3A_STATISTICS_DONE,	/* Indication that 3A statistics are available. */
3837 	IA_CSS_EVENT_TYPE_DIS_STATISTICS_DONE,	/* Indication that DIS statistics are available. */
3838 	IA_CSS_EVENT_TYPE_PIPELINE_DONE,	/* Pipeline Done event, sent after last pipeline stage. */
3839 	IA_CSS_EVENT_TYPE_FRAME_TAGGED,		/* Frame tagged. */
3840 	IA_CSS_EVENT_TYPE_INPUT_FRAME_DONE,	/* Input frame ready. */
3841 	IA_CSS_EVENT_TYPE_METADATA_DONE,	/* Metadata ready. */
3842 	IA_CSS_EVENT_TYPE_LACE_STATISTICS_DONE,	/* Indication that LACE statistics are available. */
3843 	IA_CSS_EVENT_TYPE_ACC_STAGE_COMPLETE,	/* Extension stage executed. */
3844 	IA_CSS_EVENT_TYPE_TIMER,		/* Timing measurement data. */
3845 	IA_CSS_EVENT_TYPE_PORT_EOF,		/* End Of Frame event, sent when in buffered sensor mode. */
3846 	IA_CSS_EVENT_TYPE_FW_WARNING,		/* Performance warning encountered by FW */
3847 	IA_CSS_EVENT_TYPE_FW_ASSERT,		/* Assertion hit by FW */
3848 	0,					/* error if sp passes  SH_CSS_SP_EVENT_NR_OF_TYPES as a valid event. */
3849 };
3850 
3851 int
ia_css_dequeue_psys_event(struct ia_css_event * event)3852 ia_css_dequeue_psys_event(struct ia_css_event *event)
3853 {
3854 	enum ia_css_pipe_id pipe_id = 0;
3855 	u8 payload[4] = {0, 0, 0, 0};
3856 	int ret_err;
3857 
3858 	/*
3859 	 * TODO:
3860 	 * a) use generic decoding function , same as the one used by sp.
3861 	 * b) group decode and dequeue into eventQueue module
3862 	 *
3863 	 * We skip the IA_CSS_ENTER logging call
3864 	 * to avoid flooding the logs when the host application
3865 	 * uses polling.
3866 	 */
3867 	if (!event)
3868 		return -EINVAL;
3869 
3870 	/* SP is not running. The queues are not valid */
3871 	if (!sh_css_sp_is_running())
3872 		return -EBUSY;
3873 
3874 	/* dequeue the event (if any) from the psys event queue */
3875 	ret_err = ia_css_bufq_dequeue_psys_event(payload);
3876 	if (ret_err)
3877 		return ret_err;
3878 
3879 	IA_CSS_LOG("event dequeued from psys event queue");
3880 
3881 	/* Tell the SP that we dequeued an event from the event queue. */
3882 	ia_css_bufq_enqueue_psys_event(
3883 	    IA_CSS_PSYS_SW_EVENT_EVENT_DEQUEUED, 0, 0, 0);
3884 
3885 	/*
3886 	 * Events are decoded into 4 bytes of payload, the first byte
3887 	 * contains the sp event type. This is converted to a host enum.
3888 	 * TODO: can this enum conversion be eliminated
3889 	 */
3890 	event->type = convert_event_sp_to_host_domain[payload[0]];
3891 	/* Some sane default values since not all events use all fields. */
3892 	event->pipe = NULL;
3893 	event->port = MIPI_PORT0_ID;
3894 	event->exp_id = 0;
3895 	event->fw_warning = IA_CSS_FW_WARNING_NONE;
3896 	event->fw_handle = 0;
3897 	event->timer_data = 0;
3898 	event->timer_code = 0;
3899 	event->timer_subcode = 0;
3900 
3901 	if (event->type == IA_CSS_EVENT_TYPE_TIMER) {
3902 		/*
3903 		 * timer event ??? get the 2nd event and decode the data
3904 		 * into the event struct
3905 		 */
3906 		u32 tmp_data;
3907 		/* 1st event: LSB 16-bit timer data and code */
3908 		event->timer_data = ((payload[1] & 0xFF) | ((payload[3] & 0xFF) << 8));
3909 		event->timer_code = payload[2];
3910 		payload[0] = payload[1] = payload[2] = payload[3] = 0;
3911 		ret_err = ia_css_bufq_dequeue_psys_event(payload);
3912 		if (ret_err) {
3913 			/* no 2nd event ??? an error */
3914 			/*
3915 			 * Putting IA_CSS_ERROR is resulting in failures in
3916 			 * Merrifield smoke testing
3917 			 */
3918 			IA_CSS_WARNING("Timer: Error de-queuing the 2nd TIMER event!!!\n");
3919 			return ret_err;
3920 		}
3921 		ia_css_bufq_enqueue_psys_event(
3922 		    IA_CSS_PSYS_SW_EVENT_EVENT_DEQUEUED, 0, 0, 0);
3923 		event->type = convert_event_sp_to_host_domain[payload[0]];
3924 		/* It's a timer */
3925 		if (event->type == IA_CSS_EVENT_TYPE_TIMER) {
3926 			/* 2nd event data: MSB 16-bit timer and subcode */
3927 			tmp_data = ((payload[1] & 0xFF) | ((payload[3] & 0xFF) << 8));
3928 			event->timer_data |= (tmp_data << 16);
3929 			event->timer_subcode = payload[2];
3930 		} else {
3931 			/*
3932 			 * It's a non timer event. So clear first half of the
3933 			 * timer event data.
3934 			 * If the second part of the TIMER event is not
3935 			 * received, we discard the first half of the timer
3936 			 * data and process the non timer event without
3937 			 * affecting the flow. So the non timer event falls
3938 			 * through the code.
3939 			 */
3940 			event->timer_data = 0;
3941 			event->timer_code = 0;
3942 			event->timer_subcode = 0;
3943 			IA_CSS_ERROR("Missing 2nd timer event. Timer event discarded");
3944 		}
3945 	}
3946 	if (event->type == IA_CSS_EVENT_TYPE_PORT_EOF) {
3947 		event->port = (enum mipi_port_id)payload[1];
3948 		event->exp_id = payload[3];
3949 	} else if (event->type == IA_CSS_EVENT_TYPE_FW_WARNING) {
3950 		event->fw_warning = (enum ia_css_fw_warning)payload[1];
3951 		/* exp_id is only available in these warning types */
3952 		if (event->fw_warning == IA_CSS_FW_WARNING_EXP_ID_LOCKED ||
3953 		    event->fw_warning == IA_CSS_FW_WARNING_TAG_EXP_ID_FAILED)
3954 			event->exp_id = payload[3];
3955 	} else if (event->type == IA_CSS_EVENT_TYPE_FW_ASSERT) {
3956 		event->fw_assert_module_id = payload[1]; /* module */
3957 		event->fw_assert_line_no = (payload[2] << 8) + payload[3];
3958 		/* payload[2] is line_no>>8, payload[3] is line_no&0xff */
3959 	} else if (event->type != IA_CSS_EVENT_TYPE_TIMER) {
3960 		/*
3961 		 * pipe related events.
3962 		 * payload[1] contains the pipe_num,
3963 		 * payload[2] contains the pipe_id. These are different.
3964 		 */
3965 		event->pipe = find_pipe_by_num(payload[1]);
3966 		pipe_id = (enum ia_css_pipe_id)payload[2];
3967 		/* Check to see if pipe still exists */
3968 		if (!event->pipe)
3969 			return -EBUSY;
3970 
3971 		if (event->type == IA_CSS_EVENT_TYPE_FRAME_TAGGED) {
3972 			/* find the capture pipe that goes with this */
3973 			int i, n;
3974 
3975 			n = event->pipe->stream->num_pipes;
3976 			for (i = 0; i < n; i++) {
3977 				struct ia_css_pipe *p =
3978 					    event->pipe->stream->pipes[i];
3979 				if (p->config.mode == IA_CSS_PIPE_MODE_CAPTURE) {
3980 					event->pipe = p;
3981 					break;
3982 				}
3983 			}
3984 			event->exp_id = payload[3];
3985 		}
3986 		if (event->type == IA_CSS_EVENT_TYPE_ACC_STAGE_COMPLETE) {
3987 			/* payload[3] contains the acc fw handle. */
3988 			u32 stage_num = (uint32_t)payload[3];
3989 
3990 			ret_err = ia_css_pipeline_get_fw_from_stage(
3991 				      &event->pipe->pipeline,
3992 				      stage_num,
3993 				      &event->fw_handle);
3994 			if (ret_err) {
3995 				IA_CSS_ERROR("Invalid stage num received for ACC event. stage_num:%u",
3996 					     stage_num);
3997 				return ret_err;
3998 			}
3999 		}
4000 	}
4001 
4002 	if (event->pipe)
4003 		IA_CSS_LEAVE("event_id=%d, pipe_id=%d", event->type, pipe_id);
4004 	else
4005 		IA_CSS_LEAVE("event_id=%d", event->type);
4006 
4007 	return 0;
4008 }
4009 
4010 int
ia_css_dequeue_isys_event(struct ia_css_event * event)4011 ia_css_dequeue_isys_event(struct ia_css_event *event)
4012 {
4013 	u8 payload[4] = {0, 0, 0, 0};
4014 	int err = 0;
4015 
4016 	/*
4017 	 * We skip the IA_CSS_ENTER logging call
4018 	 * to avoid flooding the logs when the host application
4019 	 * uses polling.
4020 	 */
4021 	if (!event)
4022 		return -EINVAL;
4023 
4024 	/* SP is not running. The queues are not valid */
4025 	if (!sh_css_sp_is_running())
4026 		return -EBUSY;
4027 
4028 	err = ia_css_bufq_dequeue_isys_event(payload);
4029 	if (err)
4030 		return err;
4031 
4032 	IA_CSS_LOG("event dequeued from isys event queue");
4033 
4034 	/* Update SP state to indicate that element was dequeued. */
4035 	ia_css_bufq_enqueue_isys_event(IA_CSS_ISYS_SW_EVENT_EVENT_DEQUEUED);
4036 
4037 	/* Fill return struct with appropriate info */
4038 	event->type = IA_CSS_EVENT_TYPE_PORT_EOF;
4039 	/* EOF events are associated with a CSI port, not with a pipe */
4040 	event->pipe = NULL;
4041 	event->port = payload[1];
4042 	event->exp_id = payload[3];
4043 
4044 	IA_CSS_LEAVE_ERR(err);
4045 	return err;
4046 }
4047 
4048 static int
sh_css_pipe_start(struct ia_css_stream * stream)4049 sh_css_pipe_start(struct ia_css_stream *stream)
4050 {
4051 	int err = 0;
4052 
4053 	struct ia_css_pipe *pipe;
4054 	enum ia_css_pipe_id pipe_id;
4055 	unsigned int thread_id;
4056 
4057 	IA_CSS_ENTER_PRIVATE("stream = %p", stream);
4058 
4059 	if (!stream) {
4060 		IA_CSS_LEAVE_ERR(-EINVAL);
4061 		return -EINVAL;
4062 	}
4063 	pipe = stream->last_pipe;
4064 	if (!pipe) {
4065 		IA_CSS_LEAVE_ERR(-EINVAL);
4066 		return -EINVAL;
4067 	}
4068 
4069 	pipe_id = pipe->mode;
4070 
4071 	if (stream->started) {
4072 		IA_CSS_WARNING("Cannot start stream that is already started");
4073 		IA_CSS_LEAVE_ERR(err);
4074 		return err;
4075 	}
4076 
4077 	switch (pipe_id) {
4078 	case IA_CSS_PIPE_ID_PREVIEW:
4079 		err = preview_start(pipe);
4080 		break;
4081 	case IA_CSS_PIPE_ID_VIDEO:
4082 		err = video_start(pipe);
4083 		break;
4084 	case IA_CSS_PIPE_ID_CAPTURE:
4085 		err = capture_start(pipe);
4086 		break;
4087 	case IA_CSS_PIPE_ID_YUVPP:
4088 		err = yuvpp_start(pipe);
4089 		break;
4090 	default:
4091 		err = -EINVAL;
4092 	}
4093 	/* DH regular multi pipe - not continuous mode: start the next pipes too */
4094 	if (!stream->config.continuous) {
4095 		int i;
4096 
4097 		for (i = 1; i < stream->num_pipes && 0 == err ; i++) {
4098 			switch (stream->pipes[i]->mode) {
4099 			case IA_CSS_PIPE_ID_PREVIEW:
4100 				err = preview_start(stream->pipes[i]);
4101 				break;
4102 			case IA_CSS_PIPE_ID_VIDEO:
4103 				err = video_start(stream->pipes[i]);
4104 				break;
4105 			case IA_CSS_PIPE_ID_CAPTURE:
4106 				err = capture_start(stream->pipes[i]);
4107 				break;
4108 			case IA_CSS_PIPE_ID_YUVPP:
4109 				err = yuvpp_start(stream->pipes[i]);
4110 				break;
4111 			default:
4112 				err = -EINVAL;
4113 			}
4114 		}
4115 	}
4116 	if (err) {
4117 		IA_CSS_LEAVE_ERR_PRIVATE(err);
4118 		return err;
4119 	}
4120 
4121 	/*
4122 	 * Force ISP parameter calculation after a mode change
4123 	 * Acceleration API examples pass NULL for stream but they
4124 	 * don't use ISP parameters anyway. So this should be okay.
4125 	 * The SP binary (jpeg) copy does not use any parameters.
4126 	 */
4127 	if (!copy_on_sp(pipe)) {
4128 		sh_css_invalidate_params(stream);
4129 		err = sh_css_param_update_isp_params(pipe,
4130 						     stream->isp_params_configs, true, NULL);
4131 		if (err) {
4132 			IA_CSS_LEAVE_ERR_PRIVATE(err);
4133 			return err;
4134 		}
4135 	}
4136 
4137 	ia_css_debug_pipe_graph_dump_epilogue();
4138 
4139 	ia_css_pipeline_get_sp_thread_id(ia_css_pipe_get_pipe_num(pipe), &thread_id);
4140 
4141 	if (!sh_css_sp_is_running()) {
4142 		IA_CSS_LEAVE_ERR_PRIVATE(-EBUSY);
4143 		/* SP is not running. The queues are not valid */
4144 		return -EBUSY;
4145 	}
4146 	ia_css_bufq_enqueue_psys_event(IA_CSS_PSYS_SW_EVENT_START_STREAM,
4147 				       (uint8_t)thread_id, 0, 0);
4148 
4149 	/* DH regular multi pipe - not continuous mode: enqueue event to the next pipes too */
4150 	if (!stream->config.continuous) {
4151 		int i;
4152 
4153 		for (i = 1; i < stream->num_pipes; i++) {
4154 			ia_css_pipeline_get_sp_thread_id(
4155 			    ia_css_pipe_get_pipe_num(stream->pipes[i]),
4156 			    &thread_id);
4157 			ia_css_bufq_enqueue_psys_event(
4158 			    IA_CSS_PSYS_SW_EVENT_START_STREAM,
4159 			    (uint8_t)thread_id, 0, 0);
4160 		}
4161 	}
4162 
4163 	/* in case of continuous capture mode, we also start capture thread and copy thread*/
4164 	if (pipe->stream->config.continuous) {
4165 		struct ia_css_pipe *copy_pipe = NULL;
4166 
4167 		if (pipe_id == IA_CSS_PIPE_ID_PREVIEW)
4168 			copy_pipe = pipe->pipe_settings.preview.copy_pipe;
4169 		else if (pipe_id == IA_CSS_PIPE_ID_VIDEO)
4170 			copy_pipe = pipe->pipe_settings.video.copy_pipe;
4171 
4172 		if (!copy_pipe) {
4173 			IA_CSS_LEAVE_ERR_PRIVATE(-EINVAL);
4174 			return -EINVAL;
4175 		}
4176 		ia_css_pipeline_get_sp_thread_id(ia_css_pipe_get_pipe_num(copy_pipe),
4177 						 &thread_id);
4178 		/* by the time we reach here q is initialized and handle is available.*/
4179 		ia_css_bufq_enqueue_psys_event(
4180 		    IA_CSS_PSYS_SW_EVENT_START_STREAM,
4181 		    (uint8_t)thread_id, 0,  0);
4182 	}
4183 	if (pipe->stream->cont_capt) {
4184 		struct ia_css_pipe *capture_pipe = NULL;
4185 
4186 		if (pipe_id == IA_CSS_PIPE_ID_PREVIEW)
4187 			capture_pipe = pipe->pipe_settings.preview.capture_pipe;
4188 		else if (pipe_id == IA_CSS_PIPE_ID_VIDEO)
4189 			capture_pipe = pipe->pipe_settings.video.capture_pipe;
4190 
4191 		if (!capture_pipe) {
4192 			IA_CSS_LEAVE_ERR_PRIVATE(-EINVAL);
4193 			return -EINVAL;
4194 		}
4195 		ia_css_pipeline_get_sp_thread_id(ia_css_pipe_get_pipe_num(capture_pipe),
4196 						 &thread_id);
4197 		/* by the time we reach here q is initialized and handle is available.*/
4198 		ia_css_bufq_enqueue_psys_event(
4199 		    IA_CSS_PSYS_SW_EVENT_START_STREAM,
4200 		    (uint8_t)thread_id, 0,  0);
4201 	}
4202 
4203 	stream->started = true;
4204 
4205 	IA_CSS_LEAVE_ERR_PRIVATE(err);
4206 	return err;
4207 }
4208 
4209 /* ISP2400 */
4210 void
sh_css_enable_cont_capt(bool enable,bool stop_copy_preview)4211 sh_css_enable_cont_capt(bool enable, bool stop_copy_preview)
4212 {
4213 	ia_css_debug_dtrace(IA_CSS_DEBUG_TRACE,
4214 			    "sh_css_enable_cont_capt() enter: enable=%d\n", enable);
4215 //my_css.cont_capt = enable;
4216 	my_css.stop_copy_preview = stop_copy_preview;
4217 }
4218 
4219 bool
sh_css_continuous_is_enabled(uint8_t pipe_num)4220 sh_css_continuous_is_enabled(uint8_t pipe_num)
4221 {
4222 	struct ia_css_pipe *pipe;
4223 	bool continuous;
4224 
4225 	ia_css_debug_dtrace(IA_CSS_DEBUG_TRACE,
4226 			    "sh_css_continuous_is_enabled() enter: pipe_num=%d\n", pipe_num);
4227 
4228 	pipe = find_pipe_by_num(pipe_num);
4229 	continuous = pipe && pipe->stream->config.continuous;
4230 	ia_css_debug_dtrace(IA_CSS_DEBUG_TRACE,
4231 			    "sh_css_continuous_is_enabled() leave: enable=%d\n",
4232 			    continuous);
4233 	return continuous;
4234 }
4235 
4236 /* ISP2400 */
4237 int
ia_css_stream_get_max_buffer_depth(struct ia_css_stream * stream,int * buffer_depth)4238 ia_css_stream_get_max_buffer_depth(struct ia_css_stream *stream,
4239 				   int *buffer_depth)
4240 {
4241 	if (!buffer_depth)
4242 		return -EINVAL;
4243 	ia_css_debug_dtrace(IA_CSS_DEBUG_TRACE, "ia_css_stream_get_max_buffer_depth() enter: void\n");
4244 	(void)stream;
4245 	*buffer_depth = NUM_CONTINUOUS_FRAMES;
4246 	return 0;
4247 }
4248 
4249 int
ia_css_stream_set_buffer_depth(struct ia_css_stream * stream,int buffer_depth)4250 ia_css_stream_set_buffer_depth(struct ia_css_stream *stream, int buffer_depth)
4251 {
4252 	ia_css_debug_dtrace(IA_CSS_DEBUG_TRACE, "ia_css_stream_set_buffer_depth() enter: num_frames=%d\n", buffer_depth);
4253 	(void)stream;
4254 	if (buffer_depth > NUM_CONTINUOUS_FRAMES || buffer_depth < 1)
4255 		return -EINVAL;
4256 	/* ok, value allowed */
4257 	stream->config.target_num_cont_raw_buf = buffer_depth;
4258 	/* TODO: check what to regarding initialization */
4259 	return 0;
4260 }
4261 
4262 /* ISP2401 */
4263 int
ia_css_stream_get_buffer_depth(struct ia_css_stream * stream,int * buffer_depth)4264 ia_css_stream_get_buffer_depth(struct ia_css_stream *stream,
4265 			       int *buffer_depth)
4266 {
4267 	if (!buffer_depth)
4268 		return -EINVAL;
4269 	ia_css_debug_dtrace(IA_CSS_DEBUG_TRACE, "ia_css_stream_get_buffer_depth() enter: void\n");
4270 	(void)stream;
4271 	*buffer_depth = stream->config.target_num_cont_raw_buf;
4272 	return 0;
4273 }
4274 
4275 unsigned int
sh_css_get_mipi_sizes_for_check(const unsigned int port,const unsigned int idx)4276 sh_css_get_mipi_sizes_for_check(const unsigned int port, const unsigned int idx)
4277 {
4278 	OP___assert(port < N_CSI_PORTS);
4279 	OP___assert(idx  < IA_CSS_MIPI_SIZE_CHECK_MAX_NOF_ENTRIES_PER_PORT);
4280 	ia_css_debug_dtrace(IA_CSS_DEBUG_TRACE_PRIVATE,
4281 			    "sh_css_get_mipi_sizes_for_check(port %d, idx %d): %d\n",
4282 			    port, idx, my_css.mipi_sizes_for_check[port][idx]);
4283 	return my_css.mipi_sizes_for_check[port][idx];
4284 }
4285 
sh_css_pipe_configure_output(struct ia_css_pipe * pipe,unsigned int width,unsigned int height,unsigned int padded_width,enum ia_css_frame_format format,unsigned int idx)4286 static int sh_css_pipe_configure_output(
4287     struct ia_css_pipe *pipe,
4288     unsigned int width,
4289     unsigned int height,
4290     unsigned int padded_width,
4291     enum ia_css_frame_format format,
4292     unsigned int idx)
4293 {
4294 	int err = 0;
4295 
4296 	IA_CSS_ENTER_PRIVATE("pipe = %p, width = %d, height = %d, padded width = %d, format = %d, idx = %d",
4297 			     pipe, width, height, padded_width, format, idx);
4298 	if (!pipe) {
4299 		IA_CSS_LEAVE_ERR_PRIVATE(-EINVAL);
4300 		return -EINVAL;
4301 	}
4302 
4303 	err = ia_css_util_check_res(width, height);
4304 	if (err) {
4305 		IA_CSS_LEAVE_ERR_PRIVATE(err);
4306 		return err;
4307 	}
4308 	if (pipe->output_info[idx].res.width != width ||
4309 	    pipe->output_info[idx].res.height != height ||
4310 	    pipe->output_info[idx].format != format) {
4311 		ia_css_frame_info_init(
4312 		    &pipe->output_info[idx],
4313 		    width,
4314 		    height,
4315 		    format,
4316 		    padded_width);
4317 	}
4318 	IA_CSS_LEAVE_ERR_PRIVATE(0);
4319 	return 0;
4320 }
4321 
4322 static int
sh_css_pipe_get_shading_info(struct ia_css_pipe * pipe,struct ia_css_shading_info * shading_info,struct ia_css_pipe_config * pipe_config)4323 sh_css_pipe_get_shading_info(struct ia_css_pipe *pipe,
4324 			     struct ia_css_shading_info *shading_info,
4325 			     struct ia_css_pipe_config *pipe_config)
4326 {
4327 	int err = 0;
4328 	struct ia_css_binary *binary = NULL;
4329 
4330 	ia_css_debug_dtrace(IA_CSS_DEBUG_TRACE_PRIVATE,
4331 			    "sh_css_pipe_get_shading_info() enter:\n");
4332 
4333 	binary = ia_css_pipe_get_shading_correction_binary(pipe);
4334 
4335 	if (binary) {
4336 		err = ia_css_binary_get_shading_info(binary,
4337 						     IA_CSS_SHADING_CORRECTION_TYPE_1,
4338 						     pipe->required_bds_factor,
4339 						     (const struct ia_css_stream_config *)&pipe->stream->config,
4340 						     shading_info, pipe_config);
4341 
4342 		/*
4343 		 * Other function calls can be added here when other shading
4344 		 * correction types will be added in the future.
4345 		 */
4346 	} else {
4347 		/*
4348 		 * When the pipe does not have a binary which has the shading
4349 		 * correction, this function does not need to fill the shading
4350 		 * information. It is not a error case, and then
4351 		 * this function should return 0.
4352 		 */
4353 		memset(shading_info, 0, sizeof(*shading_info));
4354 	}
4355 	return err;
4356 }
4357 
4358 static int
sh_css_pipe_get_grid_info(struct ia_css_pipe * pipe,struct ia_css_grid_info * info)4359 sh_css_pipe_get_grid_info(struct ia_css_pipe *pipe,
4360 			  struct ia_css_grid_info *info)
4361 {
4362 	int err = 0;
4363 	struct ia_css_binary *binary = NULL;
4364 
4365 	assert(pipe);
4366 	assert(info);
4367 
4368 	IA_CSS_ENTER_PRIVATE("");
4369 
4370 	binary = ia_css_pipe_get_s3a_binary(pipe);
4371 
4372 	if (binary) {
4373 		err = ia_css_binary_3a_grid_info(binary, info, pipe);
4374 		if (err)
4375 			goto err;
4376 	} else {
4377 		memset(&info->s3a_grid, 0, sizeof(info->s3a_grid));
4378 	}
4379 
4380 	binary = ia_css_pipe_get_sdis_binary(pipe);
4381 
4382 	if (binary) {
4383 		ia_css_binary_dvs_grid_info(binary, info, pipe);
4384 		ia_css_binary_dvs_stat_grid_info(binary, info, pipe);
4385 	} else {
4386 		memset(&info->dvs_grid, 0, sizeof(info->dvs_grid));
4387 		memset(&info->dvs_grid.dvs_stat_grid_info, 0,
4388 			   sizeof(info->dvs_grid.dvs_stat_grid_info));
4389 	}
4390 
4391 	if (binary) {
4392 		/* copy pipe does not have ISP binary*/
4393 		info->isp_in_width = binary->internal_frame_info.res.width;
4394 		info->isp_in_height = binary->internal_frame_info.res.height;
4395 	}
4396 
4397 	info->vamem_type = IA_CSS_VAMEM_TYPE_2;
4398 
4399 err:
4400 	IA_CSS_LEAVE_ERR_PRIVATE(err);
4401 	return err;
4402 }
4403 
4404 /* ISP2401 */
4405 /*
4406  * @brief Check if a format is supported by the pipe.
4407  *
4408  */
4409 static int
ia_css_pipe_check_format(struct ia_css_pipe * pipe,enum ia_css_frame_format format)4410 ia_css_pipe_check_format(struct ia_css_pipe *pipe,
4411 			 enum ia_css_frame_format format)
4412 {
4413 	const enum ia_css_frame_format *supported_formats;
4414 	int number_of_formats;
4415 	int found = 0;
4416 	int i;
4417 
4418 	IA_CSS_ENTER_PRIVATE("");
4419 
4420 	if (NULL == pipe || NULL == pipe->pipe_settings.video.video_binary.info) {
4421 		IA_CSS_ERROR("Pipe or binary info is not set");
4422 		IA_CSS_LEAVE_ERR_PRIVATE(-EINVAL);
4423 		return -EINVAL;
4424 	}
4425 
4426 	supported_formats = pipe->pipe_settings.video.video_binary.info->output_formats;
4427 	number_of_formats = sizeof(pipe->pipe_settings.video.video_binary.info->output_formats) / sizeof(enum ia_css_frame_format);
4428 
4429 	for (i = 0; i < number_of_formats && !found; i++) {
4430 		if (supported_formats[i] == format) {
4431 			found = 1;
4432 			break;
4433 		}
4434 	}
4435 	if (!found) {
4436 		IA_CSS_ERROR("Requested format is not supported by binary");
4437 		IA_CSS_LEAVE_ERR_PRIVATE(-EINVAL);
4438 		return -EINVAL;
4439 	}
4440 	IA_CSS_LEAVE_ERR_PRIVATE(0);
4441 	return 0;
4442 }
4443 
load_video_binaries(struct ia_css_pipe * pipe)4444 static int load_video_binaries(struct ia_css_pipe *pipe)
4445 {
4446 	struct ia_css_frame_info video_in_info, tnr_info,
4447 		       *video_vf_info, video_bds_out_info, *pipe_out_info, *pipe_vf_out_info;
4448 	bool online;
4449 	int err = 0;
4450 	bool continuous = pipe->stream->config.continuous;
4451 	unsigned int i;
4452 	unsigned int num_output_pins;
4453 	struct ia_css_frame_info video_bin_out_info;
4454 	bool need_scaler = false;
4455 	bool vf_res_different_than_output = false;
4456 	bool need_vf_pp = false;
4457 	int vf_ds_log2;
4458 	struct ia_css_video_settings *mycs  = &pipe->pipe_settings.video;
4459 
4460 	IA_CSS_ENTER_PRIVATE("");
4461 	assert(pipe);
4462 	assert(pipe->mode == IA_CSS_PIPE_ID_VIDEO);
4463 	/*
4464 	 * we only test the video_binary because offline video doesn't need a
4465 	 * vf_pp binary and online does not (always use) the copy_binary.
4466 	 * All are always reset at the same time anyway.
4467 	 */
4468 	if (mycs->video_binary.info)
4469 		return 0;
4470 
4471 	online = pipe->stream->config.online;
4472 	pipe_out_info = &pipe->output_info[0];
4473 	pipe_vf_out_info = &pipe->vf_output_info[0];
4474 
4475 	assert(pipe_out_info);
4476 
4477 	/*
4478 	 * There is no explicit input format requirement for raw or yuv
4479 	 * What matters is that there is a binary that supports the stream format.
4480 	 * This is checked in the binary_find(), so no need to check it here
4481 	 */
4482 	err = ia_css_util_check_input(&pipe->stream->config, false, false);
4483 	if (err)
4484 		return err;
4485 	/* cannot have online video and input_mode memory */
4486 	if (online && pipe->stream->config.mode == IA_CSS_INPUT_MODE_MEMORY)
4487 		return -EINVAL;
4488 	if (pipe->enable_viewfinder[IA_CSS_PIPE_OUTPUT_STAGE_0]) {
4489 		err = ia_css_util_check_vf_out_info(pipe_out_info,
4490 						    pipe_vf_out_info);
4491 		if (err)
4492 			return err;
4493 	} else {
4494 		err = ia_css_frame_check_info(pipe_out_info);
4495 		if (err)
4496 			return err;
4497 	}
4498 
4499 	if (pipe->out_yuv_ds_input_info.res.width)
4500 		video_bin_out_info = pipe->out_yuv_ds_input_info;
4501 	else
4502 		video_bin_out_info = *pipe_out_info;
4503 
4504 	/* Video */
4505 	if (pipe->enable_viewfinder[IA_CSS_PIPE_OUTPUT_STAGE_0]) {
4506 		video_vf_info = pipe_vf_out_info;
4507 		vf_res_different_than_output = (video_vf_info->res.width !=
4508 						video_bin_out_info.res.width) ||
4509 					       (video_vf_info->res.height != video_bin_out_info.res.height);
4510 	} else {
4511 		video_vf_info = NULL;
4512 	}
4513 
4514 	need_scaler = need_downscaling(video_bin_out_info.res, pipe_out_info->res);
4515 
4516 	/* we build up the pipeline starting at the end */
4517 	/* YUV post-processing if needed */
4518 	if (need_scaler) {
4519 		struct ia_css_cas_binary_descr cas_scaler_descr = { };
4520 
4521 		/* NV12 is the common format that is supported by both */
4522 		/* yuv_scaler and the video_xx_isp2_min binaries. */
4523 		video_bin_out_info.format = IA_CSS_FRAME_FORMAT_NV12;
4524 
4525 		err = ia_css_pipe_create_cas_scaler_desc_single_output(
4526 			  &video_bin_out_info,
4527 			  pipe_out_info,
4528 			  NULL,
4529 			  &cas_scaler_descr);
4530 		if (err)
4531 			return err;
4532 		mycs->num_yuv_scaler = cas_scaler_descr.num_stage;
4533 		mycs->yuv_scaler_binary = kzalloc_objs(struct ia_css_binary,
4534 						       cas_scaler_descr.num_stage);
4535 		if (!mycs->yuv_scaler_binary) {
4536 			mycs->num_yuv_scaler = 0;
4537 			err = -ENOMEM;
4538 			return err;
4539 		}
4540 		mycs->is_output_stage = kzalloc_objs(bool,
4541 						     cas_scaler_descr.num_stage);
4542 		if (!mycs->is_output_stage) {
4543 			err = -ENOMEM;
4544 			return err;
4545 		}
4546 		for (i = 0; i < cas_scaler_descr.num_stage; i++) {
4547 			struct ia_css_binary_descr yuv_scaler_descr;
4548 
4549 			mycs->is_output_stage[i] = cas_scaler_descr.is_output_stage[i];
4550 			ia_css_pipe_get_yuvscaler_binarydesc(pipe,
4551 							     &yuv_scaler_descr, &cas_scaler_descr.in_info[i],
4552 							     &cas_scaler_descr.out_info[i],
4553 							     &cas_scaler_descr.internal_out_info[i],
4554 							     &cas_scaler_descr.vf_info[i]);
4555 			err = ia_css_binary_find(&yuv_scaler_descr,
4556 						 &mycs->yuv_scaler_binary[i]);
4557 			if (err) {
4558 				kfree(mycs->is_output_stage);
4559 				mycs->is_output_stage = NULL;
4560 				return err;
4561 			}
4562 		}
4563 		ia_css_pipe_destroy_cas_scaler_desc(&cas_scaler_descr);
4564 	}
4565 
4566 	{
4567 		struct ia_css_binary_descr video_descr;
4568 		enum ia_css_frame_format vf_info_format;
4569 
4570 		err = ia_css_pipe_get_video_binarydesc(pipe,
4571 						       &video_descr, &video_in_info, &video_bds_out_info, &video_bin_out_info,
4572 						       video_vf_info,
4573 						       pipe->stream->config.left_padding);
4574 		if (err)
4575 			return err;
4576 
4577 		/*
4578 		 * In the case where video_vf_info is not NULL, this allows
4579 		 * us to find a potential video library with desired vf format.
4580 		 * If success, no vf_pp binary is needed.
4581 		 * If failed, we will look up video binary with YUV_LINE vf format
4582 		 */
4583 		err = ia_css_binary_find(&video_descr,
4584 					 &mycs->video_binary);
4585 
4586 		if (err) {
4587 			/* This will do another video binary lookup later for YUV_LINE format*/
4588 			if (video_vf_info)
4589 				need_vf_pp = true;
4590 			else
4591 				return err;
4592 		} else if (video_vf_info) {
4593 			/*
4594 			 * The first video binary lookup is successful, but we
4595 			 * may still need vf_pp binary based on additional check
4596 			 */
4597 			num_output_pins = mycs->video_binary.info->num_output_pins;
4598 			vf_ds_log2 = mycs->video_binary.vf_downscale_log2;
4599 
4600 			/*
4601 			 * If the binary has dual output pins, we need vf_pp
4602 			 * if the resolution is different.
4603 			 */
4604 			need_vf_pp |= ((num_output_pins == 2) && vf_res_different_than_output);
4605 
4606 			/*
4607 			 * If the binary has single output pin, we need vf_pp
4608 			 * if additional scaling is needed for vf
4609 			 */
4610 			need_vf_pp |= ((num_output_pins == 1) &&
4611 				       ((video_vf_info->res.width << vf_ds_log2 != pipe_out_info->res.width) ||
4612 					(video_vf_info->res.height << vf_ds_log2 != pipe_out_info->res.height)));
4613 		}
4614 
4615 		if (need_vf_pp) {
4616 			/* save the current vf_info format for restoration later */
4617 			ia_css_debug_dtrace(IA_CSS_DEBUG_TRACE,
4618 					    "load_video_binaries() need_vf_pp; find video binary with YUV_LINE again\n");
4619 
4620 			vf_info_format = video_vf_info->format;
4621 
4622 			if (!pipe->config.enable_vfpp_bci)
4623 				ia_css_frame_info_set_format(video_vf_info,
4624 							     IA_CSS_FRAME_FORMAT_YUV_LINE);
4625 
4626 			ia_css_binary_destroy_isp_parameters(&mycs->video_binary);
4627 
4628 			err = ia_css_binary_find(&video_descr,
4629 						 &mycs->video_binary);
4630 
4631 			/* restore original vf_info format */
4632 			ia_css_frame_info_set_format(video_vf_info,
4633 						     vf_info_format);
4634 			if (err)
4635 				return err;
4636 		}
4637 	}
4638 
4639 	/*
4640 	 * If a video binary does not use a ref_frame, we set the frame delay
4641 	 * to 0. This is the case for the 1-stage low-power video binary.
4642 	 */
4643 	if (!mycs->video_binary.info->sp.enable.ref_frame)
4644 		pipe->dvs_frame_delay = 0;
4645 
4646 	/*
4647 	 * The delay latency determines the number of invalid frames after
4648 	 * a stream is started.
4649 	 */
4650 	pipe->num_invalid_frames = pipe->dvs_frame_delay;
4651 	pipe->info.num_invalid_frames = pipe->num_invalid_frames;
4652 
4653 	/*
4654 	 * Viewfinder frames also decrement num_invalid_frames. If the pipe
4655 	 * outputs a viewfinder output, then we need double the number of
4656 	 * invalid frames
4657 	 */
4658 	if (video_vf_info)
4659 		pipe->num_invalid_frames *= 2;
4660 
4661 	ia_css_debug_dtrace(IA_CSS_DEBUG_TRACE,
4662 			    "load_video_binaries() num_invalid_frames=%d dvs_frame_delay=%d\n",
4663 			    pipe->num_invalid_frames, pipe->dvs_frame_delay);
4664 
4665 	/* pqiao TODO: temp hack for PO, should be removed after offline YUVPP is enabled */
4666 	if (!IS_ISP2401) {
4667 		/* Copy */
4668 		if (!online && !continuous) {
4669 			/*
4670 			 * TODO: what exactly needs doing, prepend the copy binary to
4671 			 *	 video base this only on !online?
4672 			 */
4673 			err = load_copy_binary(pipe,
4674 					       &mycs->copy_binary,
4675 					       &mycs->video_binary);
4676 			if (err)
4677 				return err;
4678 		}
4679 	}
4680 
4681 	if (pipe->enable_viewfinder[IA_CSS_PIPE_OUTPUT_STAGE_0] && need_vf_pp) {
4682 		struct ia_css_binary_descr vf_pp_descr;
4683 
4684 		if (mycs->video_binary.vf_frame_info.format
4685 		    == IA_CSS_FRAME_FORMAT_YUV_LINE) {
4686 			ia_css_pipe_get_vfpp_binarydesc(pipe, &vf_pp_descr,
4687 							&mycs->video_binary.vf_frame_info,
4688 							pipe_vf_out_info);
4689 		} else {
4690 			/*
4691 			 * output from main binary is not yuv line. currently
4692 			 * this is possible only when bci is enabled on vfpp
4693 			 * output
4694 			 */
4695 			assert(pipe->config.enable_vfpp_bci);
4696 			ia_css_pipe_get_yuvscaler_binarydesc(pipe, &vf_pp_descr,
4697 							     &mycs->video_binary.vf_frame_info,
4698 							     pipe_vf_out_info, NULL, NULL);
4699 		}
4700 
4701 		err = ia_css_binary_find(&vf_pp_descr,
4702 					 &mycs->vf_pp_binary);
4703 		if (err)
4704 			return err;
4705 	}
4706 
4707 	err = allocate_delay_frames(pipe);
4708 
4709 	if (err)
4710 		return err;
4711 
4712 	if (mycs->video_binary.info->sp.enable.block_output) {
4713 		tnr_info = mycs->video_binary.out_frame_info[0];
4714 
4715 		/* Make tnr reference buffers output block height align */
4716 		tnr_info.res.height = CEIL_MUL(tnr_info.res.height,
4717 					       mycs->video_binary.info->sp.block.output_block_height);
4718 	} else {
4719 		tnr_info = mycs->video_binary.internal_frame_info;
4720 	}
4721 	tnr_info.format = IA_CSS_FRAME_FORMAT_YUV_LINE;
4722 	tnr_info.raw_bit_depth = SH_CSS_TNR_BIT_DEPTH;
4723 
4724 	for (i = 0; i < NUM_VIDEO_TNR_FRAMES; i++) {
4725 		if (mycs->tnr_frames[i]) {
4726 			ia_css_frame_free(mycs->tnr_frames[i]);
4727 			mycs->tnr_frames[i] = NULL;
4728 		}
4729 		err = ia_css_frame_allocate_from_info(
4730 			  &mycs->tnr_frames[i],
4731 			  &tnr_info);
4732 		if (err)
4733 			return err;
4734 	}
4735 	IA_CSS_LEAVE_PRIVATE("");
4736 	return 0;
4737 }
4738 
4739 static int
unload_video_binaries(struct ia_css_pipe * pipe)4740 unload_video_binaries(struct ia_css_pipe *pipe)
4741 {
4742 	unsigned int i;
4743 
4744 	IA_CSS_ENTER_PRIVATE("pipe = %p", pipe);
4745 
4746 	if ((!pipe) || (pipe->mode != IA_CSS_PIPE_ID_VIDEO)) {
4747 		IA_CSS_LEAVE_ERR_PRIVATE(-EINVAL);
4748 		return -EINVAL;
4749 	}
4750 	ia_css_binary_unload(&pipe->pipe_settings.video.copy_binary);
4751 	ia_css_binary_unload(&pipe->pipe_settings.video.video_binary);
4752 	ia_css_binary_unload(&pipe->pipe_settings.video.vf_pp_binary);
4753 
4754 	for (i = 0; i < pipe->pipe_settings.video.num_yuv_scaler; i++)
4755 		ia_css_binary_unload(&pipe->pipe_settings.video.yuv_scaler_binary[i]);
4756 
4757 	kfree(pipe->pipe_settings.video.is_output_stage);
4758 	pipe->pipe_settings.video.is_output_stage = NULL;
4759 	kfree(pipe->pipe_settings.video.yuv_scaler_binary);
4760 	pipe->pipe_settings.video.yuv_scaler_binary = NULL;
4761 
4762 	IA_CSS_LEAVE_ERR_PRIVATE(0);
4763 	return 0;
4764 }
4765 
video_start(struct ia_css_pipe * pipe)4766 static int video_start(struct ia_css_pipe *pipe)
4767 {
4768 	int err = 0;
4769 	struct ia_css_pipe *copy_pipe, *capture_pipe;
4770 	enum sh_css_pipe_config_override copy_ovrd;
4771 	enum ia_css_input_mode video_pipe_input_mode;
4772 	unsigned int thread_id;
4773 
4774 	IA_CSS_ENTER_PRIVATE("pipe = %p", pipe);
4775 	if ((!pipe) || (pipe->mode != IA_CSS_PIPE_ID_VIDEO)) {
4776 		IA_CSS_LEAVE_ERR_PRIVATE(-EINVAL);
4777 		return -EINVAL;
4778 	}
4779 
4780 	video_pipe_input_mode = pipe->stream->config.mode;
4781 
4782 	copy_pipe    = pipe->pipe_settings.video.copy_pipe;
4783 	capture_pipe = pipe->pipe_settings.video.capture_pipe;
4784 
4785 	sh_css_metrics_start_frame();
4786 
4787 	/* multi stream video needs mipi buffers */
4788 
4789 	err = send_mipi_frames(pipe);
4790 	if (err)
4791 		return err;
4792 
4793 	send_raw_frames(pipe);
4794 
4795 	ia_css_pipeline_get_sp_thread_id(ia_css_pipe_get_pipe_num(pipe), &thread_id);
4796 	copy_ovrd = 1 << thread_id;
4797 
4798 	if (pipe->stream->cont_capt) {
4799 		ia_css_pipeline_get_sp_thread_id(ia_css_pipe_get_pipe_num(capture_pipe),
4800 						    &thread_id);
4801 		copy_ovrd |= 1 << thread_id;
4802 	}
4803 
4804 	/* Construct and load the copy pipe */
4805 	if (pipe->stream->config.continuous) {
4806 		sh_css_sp_init_pipeline(&copy_pipe->pipeline,
4807 					IA_CSS_PIPE_ID_COPY,
4808 					(uint8_t)ia_css_pipe_get_pipe_num(copy_pipe),
4809 					false,
4810 					pipe->stream->config.pixels_per_clock == 2, false,
4811 					false, pipe->required_bds_factor,
4812 					copy_ovrd,
4813 					pipe->stream->config.mode,
4814 					&pipe->stream->config.metadata_config,
4815 					&pipe->stream->info.metadata_info,
4816 					pipe->stream->config.source.port.port);
4817 
4818 		/*
4819 		 * make the video pipe start with mem mode input, copy handles
4820 		 * the actual mode
4821 		 */
4822 		video_pipe_input_mode = IA_CSS_INPUT_MODE_MEMORY;
4823 	}
4824 
4825 	/* Construct and load the capture pipe */
4826 	if (pipe->stream->cont_capt) {
4827 		sh_css_sp_init_pipeline(&capture_pipe->pipeline,
4828 					IA_CSS_PIPE_ID_CAPTURE,
4829 					(uint8_t)ia_css_pipe_get_pipe_num(capture_pipe),
4830 					capture_pipe->config.default_capture_config.enable_xnr != 0,
4831 					capture_pipe->stream->config.pixels_per_clock == 2,
4832 					true, /* continuous */
4833 					false, /* offline */
4834 					capture_pipe->required_bds_factor,
4835 					0,
4836 					IA_CSS_INPUT_MODE_MEMORY,
4837 					&pipe->stream->config.metadata_config,
4838 					&pipe->stream->info.metadata_info,
4839 					(enum mipi_port_id)0);
4840 	}
4841 
4842 	start_pipe(pipe, copy_ovrd, video_pipe_input_mode);
4843 
4844 	IA_CSS_LEAVE_ERR_PRIVATE(err);
4845 	return err;
4846 }
4847 
4848 static
sh_css_pipe_get_viewfinder_frame_info(struct ia_css_pipe * pipe,struct ia_css_frame_info * info,unsigned int idx)4849 int sh_css_pipe_get_viewfinder_frame_info(
4850     struct ia_css_pipe *pipe,
4851     struct ia_css_frame_info *info,
4852     unsigned int idx)
4853 {
4854 	assert(pipe);
4855 	assert(info);
4856 
4857 	/* We could print the pointer as input arg, and the values as output */
4858 	ia_css_debug_dtrace(IA_CSS_DEBUG_TRACE_PRIVATE,
4859 			    "sh_css_pipe_get_viewfinder_frame_info() enter: void\n");
4860 
4861 	if (pipe->mode == IA_CSS_PIPE_ID_CAPTURE &&
4862 	    (pipe->config.default_capture_config.mode == IA_CSS_CAPTURE_MODE_RAW ||
4863 	     pipe->config.default_capture_config.mode == IA_CSS_CAPTURE_MODE_BAYER))
4864 		return -EINVAL;
4865 	/* offline video does not generate viewfinder output */
4866 	*info = pipe->vf_output_info[idx];
4867 
4868 	ia_css_debug_dtrace(IA_CSS_DEBUG_TRACE_PRIVATE,
4869 			    "sh_css_pipe_get_viewfinder_frame_info() leave: \
4870 		info.res.width=%d, info.res.height=%d, \
4871 		info.padded_width=%d, info.format=%d, \
4872 		info.raw_bit_depth=%d, info.raw_bayer_order=%d\n",
4873 			    info->res.width, info->res.height,
4874 			    info->padded_width, info->format,
4875 			    info->raw_bit_depth, info->raw_bayer_order);
4876 
4877 	return 0;
4878 }
4879 
4880 static int
sh_css_pipe_configure_viewfinder(struct ia_css_pipe * pipe,unsigned int width,unsigned int height,unsigned int min_width,enum ia_css_frame_format format,unsigned int idx)4881 sh_css_pipe_configure_viewfinder(struct ia_css_pipe *pipe, unsigned int width,
4882 				 unsigned int height, unsigned int min_width,
4883 				 enum ia_css_frame_format format,
4884 				 unsigned int idx)
4885 {
4886 	int err = 0;
4887 
4888 	IA_CSS_ENTER_PRIVATE("pipe = %p, width = %d, height = %d, min_width = %d, format = %d, idx = %d\n",
4889 			     pipe, width, height, min_width, format, idx);
4890 
4891 	if (!pipe) {
4892 		IA_CSS_LEAVE_ERR_PRIVATE(-EINVAL);
4893 		return -EINVAL;
4894 	}
4895 
4896 	err = ia_css_util_check_res(width, height);
4897 	if (err) {
4898 		IA_CSS_LEAVE_ERR_PRIVATE(err);
4899 		return err;
4900 	}
4901 	if (pipe->vf_output_info[idx].res.width != width ||
4902 	    pipe->vf_output_info[idx].res.height != height ||
4903 	    pipe->vf_output_info[idx].format != format)
4904 		ia_css_frame_info_init(&pipe->vf_output_info[idx], width, height,
4905 				       format, min_width);
4906 
4907 	IA_CSS_LEAVE_ERR_PRIVATE(0);
4908 	return 0;
4909 }
4910 
load_copy_binaries(struct ia_css_pipe * pipe)4911 static int load_copy_binaries(struct ia_css_pipe *pipe)
4912 {
4913 	int err = 0;
4914 
4915 	assert(pipe);
4916 	IA_CSS_ENTER_PRIVATE("");
4917 
4918 	assert(pipe->mode == IA_CSS_PIPE_ID_CAPTURE ||
4919 	       pipe->mode == IA_CSS_PIPE_ID_COPY);
4920 	if (pipe->pipe_settings.capture.copy_binary.info)
4921 		return 0;
4922 
4923 	err = ia_css_frame_check_info(&pipe->output_info[0]);
4924 	if (err)
4925 		goto ERR;
4926 
4927 	err = verify_copy_out_frame_format(pipe);
4928 	if (err)
4929 		goto ERR;
4930 
4931 	err = load_copy_binary(pipe,
4932 			       &pipe->pipe_settings.capture.copy_binary,
4933 			       NULL);
4934 
4935 ERR:
4936 	IA_CSS_LEAVE_ERR_PRIVATE(err);
4937 	return err;
4938 }
4939 
need_capture_pp(const struct ia_css_pipe * pipe)4940 static bool need_capture_pp(
4941     const struct ia_css_pipe *pipe)
4942 {
4943 	const struct ia_css_frame_info *out_info = &pipe->output_info[0];
4944 
4945 	IA_CSS_ENTER_LEAVE_PRIVATE("");
4946 	assert(pipe);
4947 	assert(pipe->mode == IA_CSS_PIPE_ID_CAPTURE);
4948 
4949 	/* determine whether we need to use the capture_pp binary.
4950 	 * This is needed for:
4951 	 *   1. XNR or
4952 	 *   2. Digital Zoom or
4953 	 *   3. YUV downscaling
4954 	 */
4955 	if (pipe->out_yuv_ds_input_info.res.width &&
4956 	    ((pipe->out_yuv_ds_input_info.res.width != out_info->res.width) ||
4957 	     (pipe->out_yuv_ds_input_info.res.height != out_info->res.height)))
4958 		return true;
4959 
4960 	if (pipe->config.default_capture_config.enable_xnr != 0)
4961 		return true;
4962 
4963 	if ((pipe->stream->isp_params_configs->dz_config.dx < HRT_GDC_N) ||
4964 	    (pipe->stream->isp_params_configs->dz_config.dy < HRT_GDC_N) ||
4965 	    pipe->config.enable_dz)
4966 		return true;
4967 
4968 	return false;
4969 }
4970 
need_capt_ldc(const struct ia_css_pipe * pipe)4971 static bool need_capt_ldc(
4972     const struct ia_css_pipe *pipe)
4973 {
4974 	IA_CSS_ENTER_LEAVE_PRIVATE("");
4975 	assert(pipe);
4976 	assert(pipe->mode == IA_CSS_PIPE_ID_CAPTURE);
4977 	return (pipe->extra_config.enable_dvs_6axis) ? true : false;
4978 }
4979 
set_num_primary_stages(unsigned int * num,enum ia_css_pipe_version version)4980 static int set_num_primary_stages(unsigned int *num,
4981 				  enum ia_css_pipe_version version)
4982 {
4983 	int err = 0;
4984 
4985 	if (!num)
4986 		return -EINVAL;
4987 
4988 	switch (version) {
4989 	case IA_CSS_PIPE_VERSION_2_6_1:
4990 		*num = NUM_PRIMARY_HQ_STAGES;
4991 		break;
4992 	case IA_CSS_PIPE_VERSION_2_2:
4993 	case IA_CSS_PIPE_VERSION_1:
4994 		*num = NUM_PRIMARY_STAGES;
4995 		break;
4996 	default:
4997 		err = -EINVAL;
4998 		break;
4999 	}
5000 
5001 	return err;
5002 }
5003 
load_primary_binaries(struct ia_css_pipe * pipe)5004 static int load_primary_binaries(
5005     struct ia_css_pipe *pipe)
5006 {
5007 	bool online = false;
5008 	bool need_pp = false;
5009 	bool need_isp_copy_binary = false;
5010 	bool need_ldc = false;
5011 	bool sensor = false;
5012 	bool memory, continuous;
5013 	struct ia_css_frame_info prim_in_info,
5014 		       prim_out_info,
5015 		       capt_pp_out_info, vf_info,
5016 		       *vf_pp_in_info, *pipe_out_info,
5017 		       *pipe_vf_out_info, *capt_pp_in_info,
5018 		       capt_ldc_out_info;
5019 	int err = 0;
5020 	struct ia_css_capture_settings *mycs;
5021 	unsigned int i;
5022 	bool need_extra_yuv_scaler = false;
5023 	struct ia_css_binary_descr prim_descr[MAX_NUM_PRIMARY_STAGES];
5024 
5025 	IA_CSS_ENTER_PRIVATE("");
5026 	assert(pipe);
5027 	assert(pipe->stream);
5028 	assert(pipe->mode == IA_CSS_PIPE_ID_CAPTURE ||
5029 	       pipe->mode == IA_CSS_PIPE_ID_COPY);
5030 
5031 	online = pipe->stream->config.online;
5032 	sensor = (pipe->stream->config.mode == IA_CSS_INPUT_MODE_SENSOR);
5033 	memory = pipe->stream->config.mode == IA_CSS_INPUT_MODE_MEMORY;
5034 	continuous = pipe->stream->config.continuous;
5035 
5036 	mycs = &pipe->pipe_settings.capture;
5037 	pipe_out_info = &pipe->output_info[0];
5038 	pipe_vf_out_info = &pipe->vf_output_info[0];
5039 
5040 	if (mycs->primary_binary[0].info)
5041 		return 0;
5042 
5043 	err = set_num_primary_stages(&mycs->num_primary_stage,
5044 				     pipe->config.isp_pipe_version);
5045 	if (err) {
5046 		IA_CSS_LEAVE_ERR_PRIVATE(err);
5047 		return err;
5048 	}
5049 
5050 	if (pipe->enable_viewfinder[IA_CSS_PIPE_OUTPUT_STAGE_0]) {
5051 		err = ia_css_util_check_vf_out_info(pipe_out_info, pipe_vf_out_info);
5052 		if (err) {
5053 			IA_CSS_LEAVE_ERR_PRIVATE(err);
5054 			return err;
5055 		}
5056 	} else {
5057 		err = ia_css_frame_check_info(pipe_out_info);
5058 		if (err) {
5059 			IA_CSS_LEAVE_ERR_PRIVATE(err);
5060 			return err;
5061 		}
5062 	}
5063 	need_pp = need_capture_pp(pipe);
5064 
5065 	/*
5066 	 * we use the vf output info to get the primary/capture_pp binary
5067 	 * configured for vf_veceven. It will select the closest downscaling
5068 	 * factor.
5069 	 */
5070 	vf_info = *pipe_vf_out_info;
5071 
5072 	/*
5073 	 * WARNING: The #if def flag has been added below as a
5074 	 * temporary solution to solve the problem of enabling the
5075 	 * view finder in a single binary in a capture flow. The
5076 	 * vf-pp stage has been removed for Skycam in the solution
5077 	 * provided. The vf-pp stage should be re-introduced when
5078 	 * required. This should not be considered as a clean solution.
5079 	 * Proper investigation should be done to come up with the clean
5080 	 * solution.
5081 	 */
5082 	ia_css_frame_info_set_format(&vf_info, IA_CSS_FRAME_FORMAT_YUV_LINE);
5083 
5084 	/*
5085 	 * TODO: All this yuv_scaler and capturepp calculation logic
5086 	 * can be shared later. Capture_pp is also a yuv_scale binary
5087 	 * with extra XNR funcionality. Therefore, it can be made as the
5088 	 * first step of the cascade.
5089 	 */
5090 	capt_pp_out_info = pipe->out_yuv_ds_input_info;
5091 	capt_pp_out_info.format = IA_CSS_FRAME_FORMAT_YUV420;
5092 	capt_pp_out_info.res.width  /= MAX_PREFERRED_YUV_DS_PER_STEP;
5093 	capt_pp_out_info.res.height /= MAX_PREFERRED_YUV_DS_PER_STEP;
5094 	ia_css_frame_info_set_width(&capt_pp_out_info, capt_pp_out_info.res.width, 0);
5095 
5096 	need_extra_yuv_scaler = need_downscaling(capt_pp_out_info.res,
5097 						 pipe_out_info->res);
5098 
5099 	if (need_extra_yuv_scaler) {
5100 		struct ia_css_cas_binary_descr cas_scaler_descr = { };
5101 
5102 		err = ia_css_pipe_create_cas_scaler_desc_single_output(
5103 			  &capt_pp_out_info,
5104 			  pipe_out_info,
5105 			  NULL,
5106 			  &cas_scaler_descr);
5107 		if (err) {
5108 			IA_CSS_LEAVE_ERR_PRIVATE(err);
5109 			return err;
5110 		}
5111 		mycs->num_yuv_scaler = cas_scaler_descr.num_stage;
5112 		mycs->yuv_scaler_binary = kzalloc_objs(struct ia_css_binary,
5113 						       cas_scaler_descr.num_stage);
5114 		if (!mycs->yuv_scaler_binary) {
5115 			err = -ENOMEM;
5116 			IA_CSS_LEAVE_ERR_PRIVATE(err);
5117 			return err;
5118 		}
5119 		mycs->is_output_stage = kzalloc_objs(bool,
5120 						     cas_scaler_descr.num_stage);
5121 		if (!mycs->is_output_stage) {
5122 			err = -ENOMEM;
5123 			IA_CSS_LEAVE_ERR_PRIVATE(err);
5124 			return err;
5125 		}
5126 		for (i = 0; i < cas_scaler_descr.num_stage; i++) {
5127 			struct ia_css_binary_descr yuv_scaler_descr;
5128 
5129 			mycs->is_output_stage[i] = cas_scaler_descr.is_output_stage[i];
5130 			ia_css_pipe_get_yuvscaler_binarydesc(pipe,
5131 							     &yuv_scaler_descr, &cas_scaler_descr.in_info[i],
5132 							     &cas_scaler_descr.out_info[i],
5133 							     &cas_scaler_descr.internal_out_info[i],
5134 							     &cas_scaler_descr.vf_info[i]);
5135 			err = ia_css_binary_find(&yuv_scaler_descr,
5136 						 &mycs->yuv_scaler_binary[i]);
5137 			if (err) {
5138 				IA_CSS_LEAVE_ERR_PRIVATE(err);
5139 				return err;
5140 			}
5141 		}
5142 		ia_css_pipe_destroy_cas_scaler_desc(&cas_scaler_descr);
5143 
5144 	} else {
5145 		capt_pp_out_info = pipe->output_info[0];
5146 	}
5147 
5148 	/* TODO Do we disable ldc for skycam */
5149 	need_ldc = need_capt_ldc(pipe);
5150 
5151 	/* we build up the pipeline starting at the end */
5152 	/* Capture post-processing */
5153 	if (need_pp) {
5154 		struct ia_css_binary_descr capture_pp_descr;
5155 
5156 		capt_pp_in_info = need_ldc ? &capt_ldc_out_info : &prim_out_info;
5157 
5158 		ia_css_pipe_get_capturepp_binarydesc(pipe,
5159 						     &capture_pp_descr,
5160 						     capt_pp_in_info,
5161 						     &capt_pp_out_info,
5162 						     &vf_info);
5163 
5164 		err = ia_css_binary_find(&capture_pp_descr,
5165 					 &mycs->capture_pp_binary);
5166 		if (err) {
5167 			IA_CSS_LEAVE_ERR_PRIVATE(err);
5168 			return err;
5169 		}
5170 
5171 		if (need_ldc) {
5172 			struct ia_css_binary_descr capt_ldc_descr;
5173 
5174 			ia_css_pipe_get_ldc_binarydesc(pipe,
5175 						       &capt_ldc_descr,
5176 						       &prim_out_info,
5177 						       &capt_ldc_out_info);
5178 
5179 			err = ia_css_binary_find(&capt_ldc_descr,
5180 						 &mycs->capture_ldc_binary);
5181 			if (err) {
5182 				IA_CSS_LEAVE_ERR_PRIVATE(err);
5183 				return err;
5184 			}
5185 		}
5186 	} else {
5187 		prim_out_info = *pipe_out_info;
5188 	}
5189 
5190 	/* Primary */
5191 	for (i = 0; i < mycs->num_primary_stage; i++) {
5192 		struct ia_css_frame_info *local_vf_info = NULL;
5193 
5194 		if (pipe->enable_viewfinder[IA_CSS_PIPE_OUTPUT_STAGE_0] &&
5195 		    (i == mycs->num_primary_stage - 1))
5196 			local_vf_info = &vf_info;
5197 		ia_css_pipe_get_primary_binarydesc(pipe, &prim_descr[i],
5198 						   &prim_in_info, &prim_out_info,
5199 						   local_vf_info, i);
5200 		err = ia_css_binary_find(&prim_descr[i], &mycs->primary_binary[i]);
5201 		if (err) {
5202 			IA_CSS_LEAVE_ERR_PRIVATE(err);
5203 			return err;
5204 		}
5205 	}
5206 
5207 	/* Viewfinder post-processing */
5208 	if (need_pp)
5209 		vf_pp_in_info = &mycs->capture_pp_binary.vf_frame_info;
5210 	else
5211 		vf_pp_in_info = &mycs->primary_binary[mycs->num_primary_stage - 1].vf_frame_info;
5212 
5213 	/*
5214 	 * WARNING: The #if def flag has been added below as a
5215 	 * temporary solution to solve the problem of enabling the
5216 	 * view finder in a single binary in a capture flow. The
5217 	 * vf-pp stage has been removed for Skycam in the solution
5218 	 * provided. The vf-pp stage should be re-introduced when
5219 	 * required. Thisshould not be considered as a clean solution.
5220 	 * Proper  * investigation should be done to come up with the clean
5221 	 * solution.
5222 	 */
5223 	if (pipe->enable_viewfinder[IA_CSS_PIPE_OUTPUT_STAGE_0]) {
5224 		struct ia_css_binary_descr vf_pp_descr;
5225 
5226 		ia_css_pipe_get_vfpp_binarydesc(pipe,
5227 						&vf_pp_descr, vf_pp_in_info, pipe_vf_out_info);
5228 		err = ia_css_binary_find(&vf_pp_descr, &mycs->vf_pp_binary);
5229 		if (err) {
5230 			IA_CSS_LEAVE_ERR_PRIVATE(err);
5231 			return err;
5232 		}
5233 	}
5234 	err = allocate_delay_frames(pipe);
5235 
5236 	if (err)
5237 		return err;
5238 
5239 	if (IS_ISP2401)
5240 		/*
5241 		 * When the input system is 2401, only the Direct Sensor Mode
5242 		 * Offline Capture uses the ISP copy binary.
5243 		 */
5244 		need_isp_copy_binary = !online && sensor;
5245 	else
5246 		need_isp_copy_binary = !online && !continuous && !memory;
5247 
5248 	/* ISP Copy */
5249 	if (need_isp_copy_binary) {
5250 		err = load_copy_binary(pipe,
5251 				       &mycs->copy_binary,
5252 				       &mycs->primary_binary[0]);
5253 		if (err) {
5254 			IA_CSS_LEAVE_ERR_PRIVATE(err);
5255 			return err;
5256 		}
5257 	}
5258 
5259 	return 0;
5260 }
5261 
5262 static int
allocate_delay_frames(struct ia_css_pipe * pipe)5263 allocate_delay_frames(struct ia_css_pipe *pipe)
5264 {
5265 	unsigned int num_delay_frames = 0, i = 0;
5266 	unsigned int dvs_frame_delay = 0;
5267 	struct ia_css_frame_info ref_info;
5268 	int err = 0;
5269 	enum ia_css_pipe_id mode = IA_CSS_PIPE_ID_VIDEO;
5270 	struct ia_css_frame **delay_frames = NULL;
5271 
5272 	IA_CSS_ENTER_PRIVATE("");
5273 
5274 	if (!pipe) {
5275 		IA_CSS_ERROR("Invalid args - pipe %p", pipe);
5276 		return -EINVAL;
5277 	}
5278 
5279 	mode = pipe->mode;
5280 	dvs_frame_delay = pipe->dvs_frame_delay;
5281 
5282 	if (dvs_frame_delay > 0)
5283 		num_delay_frames = dvs_frame_delay + 1;
5284 
5285 	switch (mode) {
5286 	case IA_CSS_PIPE_ID_CAPTURE: {
5287 		struct ia_css_capture_settings *mycs_capture = &pipe->pipe_settings.capture;
5288 		(void)mycs_capture;
5289 		return err;
5290 	}
5291 	break;
5292 	case IA_CSS_PIPE_ID_VIDEO: {
5293 		struct ia_css_video_settings *mycs_video = &pipe->pipe_settings.video;
5294 
5295 		ref_info = mycs_video->video_binary.internal_frame_info;
5296 
5297 		/*
5298 		 * The ref frame expects
5299 		 * 1. Y plane
5300 		 * 2. UV plane with line interleaving, like below
5301 		 *	UUUUUU(width/2 times) VVVVVVVV..(width/2 times)
5302 		 *
5303 		 * This format is not YUV420(which has Y, U and V planes).
5304 		 * Its closer to NV12, except that the UV plane has UV
5305 		 * interleaving, like UVUVUVUVUVUVUVUVU...
5306 		 *
5307 		 * TODO: make this ref_frame format as a separate frame format
5308 		 */
5309 		ref_info.format        = IA_CSS_FRAME_FORMAT_NV12;
5310 		delay_frames = mycs_video->delay_frames;
5311 	}
5312 	break;
5313 	case IA_CSS_PIPE_ID_PREVIEW: {
5314 		struct ia_css_preview_settings *mycs_preview = &pipe->pipe_settings.preview;
5315 
5316 		ref_info = mycs_preview->preview_binary.internal_frame_info;
5317 
5318 		/*
5319 		 * The ref frame expects
5320 		 * 1. Y plane
5321 		 * 2. UV plane with line interleaving, like below
5322 		 *	UUUUUU(width/2 times) VVVVVVVV..(width/2 times)
5323 		 *
5324 		 * This format is not YUV420(which has Y, U and V planes).
5325 		 * Its closer to NV12, except that the UV plane has UV
5326 		 * interleaving, like UVUVUVUVUVUVUVUVU...
5327 		 *
5328 		 * TODO: make this ref_frame format as a separate frame format
5329 		 */
5330 		ref_info.format        = IA_CSS_FRAME_FORMAT_NV12;
5331 		delay_frames = mycs_preview->delay_frames;
5332 	}
5333 	break;
5334 	default:
5335 		return -EINVAL;
5336 	}
5337 
5338 	ref_info.raw_bit_depth = SH_CSS_REF_BIT_DEPTH;
5339 
5340 	assert(num_delay_frames <= MAX_NUM_VIDEO_DELAY_FRAMES);
5341 	for (i = 0; i < num_delay_frames; i++) {
5342 		err = ia_css_frame_allocate_from_info(&delay_frames[i],	&ref_info);
5343 		if (err)
5344 			return err;
5345 	}
5346 	IA_CSS_LEAVE_PRIVATE("");
5347 	return 0;
5348 }
5349 
load_advanced_binaries(struct ia_css_pipe * pipe)5350 static int load_advanced_binaries(struct ia_css_pipe *pipe)
5351 {
5352 	struct ia_css_frame_info pre_in_info, gdc_in_info,
5353 			post_in_info, post_out_info,
5354 			vf_info, *vf_pp_in_info, *pipe_out_info,
5355 			*pipe_vf_out_info;
5356 	bool need_pp;
5357 	bool need_isp_copy = true;
5358 	int err = 0;
5359 
5360 	IA_CSS_ENTER_PRIVATE("");
5361 
5362 	assert(pipe);
5363 	assert(pipe->mode == IA_CSS_PIPE_ID_CAPTURE ||
5364 	       pipe->mode == IA_CSS_PIPE_ID_COPY);
5365 	if (pipe->pipe_settings.capture.pre_isp_binary.info)
5366 		return 0;
5367 	pipe_out_info = &pipe->output_info[0];
5368 	pipe_vf_out_info = &pipe->vf_output_info[0];
5369 
5370 	vf_info = *pipe_vf_out_info;
5371 	err = ia_css_util_check_vf_out_info(pipe_out_info, &vf_info);
5372 	if (err)
5373 		return err;
5374 	need_pp = need_capture_pp(pipe);
5375 
5376 	ia_css_frame_info_set_format(&vf_info,
5377 				     IA_CSS_FRAME_FORMAT_YUV_LINE);
5378 
5379 	/* we build up the pipeline starting at the end */
5380 	/* Capture post-processing */
5381 	if (need_pp) {
5382 		struct ia_css_binary_descr capture_pp_descr;
5383 
5384 		ia_css_pipe_get_capturepp_binarydesc(pipe, &capture_pp_descr,
5385 						     &post_out_info,
5386 						     pipe_out_info, &vf_info);
5387 		err = ia_css_binary_find(&capture_pp_descr,
5388 					 &pipe->pipe_settings.capture.capture_pp_binary);
5389 		if (err)
5390 			return err;
5391 	} else {
5392 		post_out_info = *pipe_out_info;
5393 	}
5394 
5395 	/* Post-gdc */
5396 	{
5397 		struct ia_css_binary_descr post_gdc_descr;
5398 
5399 		ia_css_pipe_get_post_gdc_binarydesc(pipe, &post_gdc_descr,
5400 						    &post_in_info,
5401 						    &post_out_info, &vf_info);
5402 		err = ia_css_binary_find(&post_gdc_descr,
5403 					 &pipe->pipe_settings.capture.post_isp_binary);
5404 		if (err)
5405 			return err;
5406 	}
5407 
5408 	/* Gdc */
5409 	{
5410 		struct ia_css_binary_descr gdc_descr;
5411 
5412 		ia_css_pipe_get_gdc_binarydesc(pipe, &gdc_descr, &gdc_in_info,
5413 					       &pipe->pipe_settings.capture.post_isp_binary.in_frame_info);
5414 		err = ia_css_binary_find(&gdc_descr,
5415 					 &pipe->pipe_settings.capture.anr_gdc_binary);
5416 		if (err)
5417 			return err;
5418 	}
5419 	pipe->pipe_settings.capture.anr_gdc_binary.left_padding =
5420 	    pipe->pipe_settings.capture.post_isp_binary.left_padding;
5421 
5422 	/* Pre-gdc */
5423 	{
5424 		struct ia_css_binary_descr pre_gdc_descr;
5425 
5426 		ia_css_pipe_get_pre_gdc_binarydesc(pipe, &pre_gdc_descr, &pre_in_info,
5427 						   &pipe->pipe_settings.capture.anr_gdc_binary.in_frame_info);
5428 		err = ia_css_binary_find(&pre_gdc_descr,
5429 					 &pipe->pipe_settings.capture.pre_isp_binary);
5430 		if (err)
5431 			return err;
5432 	}
5433 	pipe->pipe_settings.capture.pre_isp_binary.left_padding =
5434 	    pipe->pipe_settings.capture.anr_gdc_binary.left_padding;
5435 
5436 	/* Viewfinder post-processing */
5437 	if (need_pp) {
5438 		vf_pp_in_info =
5439 		    &pipe->pipe_settings.capture.capture_pp_binary.vf_frame_info;
5440 	} else {
5441 		vf_pp_in_info =
5442 		    &pipe->pipe_settings.capture.post_isp_binary.vf_frame_info;
5443 	}
5444 
5445 	{
5446 		struct ia_css_binary_descr vf_pp_descr;
5447 
5448 		ia_css_pipe_get_vfpp_binarydesc(pipe,
5449 						&vf_pp_descr, vf_pp_in_info, pipe_vf_out_info);
5450 		err = ia_css_binary_find(&vf_pp_descr,
5451 					 &pipe->pipe_settings.capture.vf_pp_binary);
5452 		if (err)
5453 			return err;
5454 	}
5455 
5456 	/* Copy */
5457 	if (IS_ISP2401)
5458 		/* For CSI2+, only the direct sensor mode/online requires ISP copy */
5459 		need_isp_copy = pipe->stream->config.mode == IA_CSS_INPUT_MODE_SENSOR;
5460 
5461 	if (need_isp_copy)
5462 		load_copy_binary(pipe,
5463 				 &pipe->pipe_settings.capture.copy_binary,
5464 				 &pipe->pipe_settings.capture.pre_isp_binary);
5465 
5466 	return err;
5467 }
5468 
load_bayer_isp_binaries(struct ia_css_pipe * pipe)5469 static int load_bayer_isp_binaries(struct ia_css_pipe *pipe)
5470 {
5471 	struct ia_css_frame_info pre_isp_in_info, *pipe_out_info;
5472 	int err = 0;
5473 	struct ia_css_binary_descr pre_de_descr;
5474 
5475 	IA_CSS_ENTER_PRIVATE("");
5476 	assert(pipe);
5477 	assert(pipe->mode == IA_CSS_PIPE_ID_CAPTURE ||
5478 	       pipe->mode == IA_CSS_PIPE_ID_COPY);
5479 	pipe_out_info = &pipe->output_info[0];
5480 
5481 	if (pipe->pipe_settings.capture.pre_isp_binary.info)
5482 		return 0;
5483 
5484 	err = ia_css_frame_check_info(pipe_out_info);
5485 	if (err)
5486 		return err;
5487 
5488 	ia_css_pipe_get_pre_de_binarydesc(pipe, &pre_de_descr,
5489 					  &pre_isp_in_info,
5490 					  pipe_out_info);
5491 
5492 	err = ia_css_binary_find(&pre_de_descr,
5493 				 &pipe->pipe_settings.capture.pre_isp_binary);
5494 
5495 	return err;
5496 }
5497 
load_low_light_binaries(struct ia_css_pipe * pipe)5498 static int load_low_light_binaries(struct ia_css_pipe *pipe)
5499 {
5500 	struct ia_css_frame_info pre_in_info, anr_in_info,
5501 			post_in_info, post_out_info,
5502 			vf_info, *pipe_vf_out_info, *pipe_out_info,
5503 			*vf_pp_in_info;
5504 	bool need_pp;
5505 	bool need_isp_copy = true;
5506 	int err = 0;
5507 
5508 	IA_CSS_ENTER_PRIVATE("");
5509 	assert(pipe);
5510 	assert(pipe->mode == IA_CSS_PIPE_ID_CAPTURE ||
5511 	       pipe->mode == IA_CSS_PIPE_ID_COPY);
5512 
5513 	if (pipe->pipe_settings.capture.pre_isp_binary.info)
5514 		return 0;
5515 	pipe_vf_out_info = &pipe->vf_output_info[0];
5516 	pipe_out_info = &pipe->output_info[0];
5517 
5518 	vf_info = *pipe_vf_out_info;
5519 	err = ia_css_util_check_vf_out_info(pipe_out_info,
5520 					    &vf_info);
5521 	if (err)
5522 		return err;
5523 	need_pp = need_capture_pp(pipe);
5524 
5525 	ia_css_frame_info_set_format(&vf_info,
5526 				     IA_CSS_FRAME_FORMAT_YUV_LINE);
5527 
5528 	/* we build up the pipeline starting at the end */
5529 	/* Capture post-processing */
5530 	if (need_pp) {
5531 		struct ia_css_binary_descr capture_pp_descr;
5532 
5533 		ia_css_pipe_get_capturepp_binarydesc(pipe, &capture_pp_descr,
5534 						     &post_out_info,
5535 						     pipe_out_info, &vf_info);
5536 		err = ia_css_binary_find(&capture_pp_descr,
5537 					 &pipe->pipe_settings.capture.capture_pp_binary);
5538 		if (err)
5539 			return err;
5540 	} else {
5541 		post_out_info = *pipe_out_info;
5542 	}
5543 
5544 	/* Post-anr */
5545 	{
5546 		struct ia_css_binary_descr post_anr_descr;
5547 
5548 		ia_css_pipe_get_post_anr_binarydesc(pipe,
5549 						    &post_anr_descr, &post_in_info, &post_out_info, &vf_info);
5550 		err = ia_css_binary_find(&post_anr_descr,
5551 					 &pipe->pipe_settings.capture.post_isp_binary);
5552 		if (err)
5553 			return err;
5554 	}
5555 
5556 	/* Anr */
5557 	{
5558 		struct ia_css_binary_descr anr_descr;
5559 
5560 		ia_css_pipe_get_anr_binarydesc(pipe, &anr_descr, &anr_in_info,
5561 					       &pipe->pipe_settings.capture.post_isp_binary.in_frame_info);
5562 		err = ia_css_binary_find(&anr_descr,
5563 					 &pipe->pipe_settings.capture.anr_gdc_binary);
5564 		if (err)
5565 			return err;
5566 	}
5567 	pipe->pipe_settings.capture.anr_gdc_binary.left_padding =
5568 	    pipe->pipe_settings.capture.post_isp_binary.left_padding;
5569 
5570 	/* Pre-anr */
5571 	{
5572 		struct ia_css_binary_descr pre_anr_descr;
5573 
5574 		ia_css_pipe_get_pre_anr_binarydesc(pipe, &pre_anr_descr, &pre_in_info,
5575 						   &pipe->pipe_settings.capture.anr_gdc_binary.in_frame_info);
5576 		err = ia_css_binary_find(&pre_anr_descr,
5577 					 &pipe->pipe_settings.capture.pre_isp_binary);
5578 		if (err)
5579 			return err;
5580 	}
5581 	pipe->pipe_settings.capture.pre_isp_binary.left_padding =
5582 	    pipe->pipe_settings.capture.anr_gdc_binary.left_padding;
5583 
5584 	/* Viewfinder post-processing */
5585 	if (need_pp) {
5586 		vf_pp_in_info =
5587 		    &pipe->pipe_settings.capture.capture_pp_binary.vf_frame_info;
5588 	} else {
5589 		vf_pp_in_info =
5590 		    &pipe->pipe_settings.capture.post_isp_binary.vf_frame_info;
5591 	}
5592 
5593 	{
5594 		struct ia_css_binary_descr vf_pp_descr;
5595 
5596 		ia_css_pipe_get_vfpp_binarydesc(pipe, &vf_pp_descr,
5597 						vf_pp_in_info, pipe_vf_out_info);
5598 		err = ia_css_binary_find(&vf_pp_descr,
5599 					 &pipe->pipe_settings.capture.vf_pp_binary);
5600 		if (err)
5601 			return err;
5602 	}
5603 
5604 	/* Copy */
5605 	if (IS_ISP2401)
5606 		/* For CSI2+, only the direct sensor mode/online requires ISP copy */
5607 		need_isp_copy = pipe->stream->config.mode == IA_CSS_INPUT_MODE_SENSOR;
5608 
5609 	if (need_isp_copy)
5610 		err = load_copy_binary(pipe,
5611 				       &pipe->pipe_settings.capture.copy_binary,
5612 				       &pipe->pipe_settings.capture.pre_isp_binary);
5613 
5614 	return err;
5615 }
5616 
copy_on_sp(struct ia_css_pipe * pipe)5617 static bool copy_on_sp(struct ia_css_pipe *pipe)
5618 {
5619 	bool rval;
5620 
5621 	assert(pipe);
5622 	ia_css_debug_dtrace(IA_CSS_DEBUG_TRACE_PRIVATE, "copy_on_sp() enter:\n");
5623 
5624 	rval = true;
5625 
5626 	rval &=	(pipe->mode == IA_CSS_PIPE_ID_CAPTURE);
5627 
5628 	rval &= (pipe->config.default_capture_config.mode == IA_CSS_CAPTURE_MODE_RAW);
5629 
5630 	rval &= ((pipe->stream->config.input_config.format ==
5631 		    ATOMISP_INPUT_FORMAT_BINARY_8) ||
5632 		    (pipe->config.mode == IA_CSS_PIPE_MODE_COPY));
5633 
5634 	return rval;
5635 }
5636 
load_capture_binaries(struct ia_css_pipe * pipe)5637 static int load_capture_binaries(struct ia_css_pipe *pipe)
5638 {
5639 	int err = 0;
5640 	bool must_be_raw;
5641 
5642 	IA_CSS_ENTER_PRIVATE("");
5643 	assert(pipe);
5644 	assert(pipe->mode == IA_CSS_PIPE_ID_CAPTURE ||
5645 	       pipe->mode == IA_CSS_PIPE_ID_COPY);
5646 
5647 	if (pipe->pipe_settings.capture.primary_binary[0].info) {
5648 		IA_CSS_LEAVE_ERR_PRIVATE(0);
5649 		return 0;
5650 	}
5651 
5652 	/* in primary, advanced,low light or bayer,
5653 						the input format must be raw */
5654 	must_be_raw =
5655 	    pipe->config.default_capture_config.mode == IA_CSS_CAPTURE_MODE_ADVANCED ||
5656 	    pipe->config.default_capture_config.mode == IA_CSS_CAPTURE_MODE_BAYER ||
5657 	    pipe->config.default_capture_config.mode == IA_CSS_CAPTURE_MODE_LOW_LIGHT;
5658 	err = ia_css_util_check_input(&pipe->stream->config, must_be_raw, false);
5659 	if (err) {
5660 		IA_CSS_LEAVE_ERR_PRIVATE(err);
5661 		return err;
5662 	}
5663 	if (copy_on_sp(pipe) &&
5664 	    pipe->stream->config.input_config.format == ATOMISP_INPUT_FORMAT_BINARY_8) {
5665 		ia_css_frame_info_init(
5666 		    &pipe->output_info[0],
5667 		    JPEG_BYTES,
5668 		    1,
5669 		    IA_CSS_FRAME_FORMAT_BINARY_8,
5670 		    0);
5671 		IA_CSS_LEAVE_ERR_PRIVATE(0);
5672 		return 0;
5673 	}
5674 
5675 	switch (pipe->config.default_capture_config.mode) {
5676 	case IA_CSS_CAPTURE_MODE_RAW:
5677 		err = load_copy_binaries(pipe);
5678 		if (!err && IS_ISP2401)
5679 			pipe->pipe_settings.capture.copy_binary.online = pipe->stream->config.online;
5680 
5681 		break;
5682 	case IA_CSS_CAPTURE_MODE_BAYER:
5683 		err = load_bayer_isp_binaries(pipe);
5684 		break;
5685 	case IA_CSS_CAPTURE_MODE_PRIMARY:
5686 		err = load_primary_binaries(pipe);
5687 		break;
5688 	case IA_CSS_CAPTURE_MODE_ADVANCED:
5689 		err = load_advanced_binaries(pipe);
5690 		break;
5691 	case IA_CSS_CAPTURE_MODE_LOW_LIGHT:
5692 		err = load_low_light_binaries(pipe);
5693 		break;
5694 	}
5695 	if (err) {
5696 		IA_CSS_LEAVE_ERR_PRIVATE(err);
5697 		return err;
5698 	}
5699 
5700 	IA_CSS_LEAVE_ERR_PRIVATE(err);
5701 	return err;
5702 }
5703 
5704 static int
unload_capture_binaries(struct ia_css_pipe * pipe)5705 unload_capture_binaries(struct ia_css_pipe *pipe)
5706 {
5707 	unsigned int i;
5708 
5709 	IA_CSS_ENTER_PRIVATE("pipe = %p", pipe);
5710 
5711 	if (!pipe || (pipe->mode != IA_CSS_PIPE_ID_CAPTURE &&
5712 		      pipe->mode != IA_CSS_PIPE_ID_COPY)) {
5713 		IA_CSS_LEAVE_ERR_PRIVATE(-EINVAL);
5714 		return -EINVAL;
5715 	}
5716 	ia_css_binary_unload(&pipe->pipe_settings.capture.copy_binary);
5717 	for (i = 0; i < MAX_NUM_PRIMARY_STAGES; i++)
5718 		ia_css_binary_unload(&pipe->pipe_settings.capture.primary_binary[i]);
5719 	ia_css_binary_unload(&pipe->pipe_settings.capture.pre_isp_binary);
5720 	ia_css_binary_unload(&pipe->pipe_settings.capture.anr_gdc_binary);
5721 	ia_css_binary_unload(&pipe->pipe_settings.capture.post_isp_binary);
5722 	ia_css_binary_unload(&pipe->pipe_settings.capture.capture_pp_binary);
5723 	ia_css_binary_unload(&pipe->pipe_settings.capture.capture_ldc_binary);
5724 	ia_css_binary_unload(&pipe->pipe_settings.capture.vf_pp_binary);
5725 
5726 	for (i = 0; i < pipe->pipe_settings.capture.num_yuv_scaler; i++)
5727 		ia_css_binary_unload(&pipe->pipe_settings.capture.yuv_scaler_binary[i]);
5728 
5729 	kfree(pipe->pipe_settings.capture.is_output_stage);
5730 	pipe->pipe_settings.capture.is_output_stage = NULL;
5731 	kfree(pipe->pipe_settings.capture.yuv_scaler_binary);
5732 	pipe->pipe_settings.capture.yuv_scaler_binary = NULL;
5733 
5734 	IA_CSS_LEAVE_ERR_PRIVATE(0);
5735 	return 0;
5736 }
5737 
5738 static bool
need_downscaling(const struct ia_css_resolution in_res,const struct ia_css_resolution out_res)5739 need_downscaling(const struct ia_css_resolution in_res,
5740 		 const struct ia_css_resolution out_res)
5741 {
5742 	if (in_res.width > out_res.width || in_res.height > out_res.height)
5743 		return true;
5744 
5745 	return false;
5746 }
5747 
5748 static bool
need_yuv_scaler_stage(const struct ia_css_pipe * pipe)5749 need_yuv_scaler_stage(const struct ia_css_pipe *pipe)
5750 {
5751 	unsigned int i;
5752 	struct ia_css_resolution in_res, out_res;
5753 
5754 	bool need_format_conversion = false;
5755 
5756 	IA_CSS_ENTER_PRIVATE("");
5757 	assert(pipe);
5758 	assert(pipe->mode == IA_CSS_PIPE_ID_YUVPP);
5759 
5760 	/* TODO: make generic function */
5761 	need_format_conversion =
5762 	    ((pipe->stream->config.input_config.format ==
5763 		ATOMISP_INPUT_FORMAT_YUV420_8_LEGACY) &&
5764 		(pipe->output_info[0].format != IA_CSS_FRAME_FORMAT_CSI_MIPI_LEGACY_YUV420_8));
5765 
5766 	in_res = pipe->config.input_effective_res;
5767 
5768 	if (pipe->config.enable_dz)
5769 		return true;
5770 
5771 	if ((pipe->output_info[0].res.width != 0) && need_format_conversion)
5772 		return true;
5773 
5774 	for (i = 0; i < IA_CSS_PIPE_MAX_OUTPUT_STAGE; i++) {
5775 		out_res = pipe->output_info[i].res;
5776 
5777 		/* A non-zero width means it is a valid output port */
5778 		if ((out_res.width != 0) && need_downscaling(in_res, out_res))
5779 			return true;
5780 	}
5781 
5782 	return false;
5783 }
5784 
5785 /*
5786  * TODO: it is temporarily created from ia_css_pipe_create_cas_scaler_desc
5787  * which has some hard-coded knowledge which prevents reuse of the function.
5788  * Later, merge this with ia_css_pipe_create_cas_scaler_desc
5789  */
ia_css_pipe_create_cas_scaler_desc_single_output(struct ia_css_frame_info * in_info,struct ia_css_frame_info * out_info,struct ia_css_frame_info * vf_info,struct ia_css_cas_binary_descr * descr)5790 static int ia_css_pipe_create_cas_scaler_desc_single_output(
5791 	    struct ia_css_frame_info *in_info,
5792 	    struct ia_css_frame_info *out_info,
5793 	    struct ia_css_frame_info *vf_info,
5794 	    struct ia_css_cas_binary_descr *descr)
5795 {
5796 	unsigned int i;
5797 	unsigned int hor_ds_factor = 0, ver_ds_factor = 0;
5798 	int err = 0;
5799 	struct ia_css_frame_info tmp_in_info;
5800 	unsigned int max_scale_factor_per_stage = MAX_PREFERRED_YUV_DS_PER_STEP;
5801 
5802 	assert(in_info);
5803 	assert(out_info);
5804 
5805 	ia_css_debug_dtrace(IA_CSS_DEBUG_TRACE_PRIVATE,
5806 			    "ia_css_pipe_create_cas_scaler_desc() enter:\n");
5807 
5808 	/* We assume that this function is used only for single output port case. */
5809 	descr->num_output_stage = 1;
5810 
5811 	hor_ds_factor = CEIL_DIV(in_info->res.width, out_info->res.width);
5812 	ver_ds_factor = CEIL_DIV(in_info->res.height, out_info->res.height);
5813 	/* use the same horizontal and vertical downscaling factor for simplicity */
5814 	assert(hor_ds_factor == ver_ds_factor);
5815 
5816 	i = 1;
5817 	while (i < hor_ds_factor) {
5818 		descr->num_stage++;
5819 		i *= max_scale_factor_per_stage;
5820 	}
5821 
5822 	descr->in_info = kmalloc(descr->num_stage *
5823 				 sizeof(struct ia_css_frame_info),
5824 				 GFP_KERNEL);
5825 	if (!descr->in_info) {
5826 		err = -ENOMEM;
5827 		goto ERR;
5828 	}
5829 	descr->internal_out_info = kmalloc(descr->num_stage *
5830 					   sizeof(struct ia_css_frame_info),
5831 					   GFP_KERNEL);
5832 	if (!descr->internal_out_info) {
5833 		err = -ENOMEM;
5834 		goto ERR;
5835 	}
5836 	descr->out_info = kmalloc(descr->num_stage *
5837 				  sizeof(struct ia_css_frame_info),
5838 				  GFP_KERNEL);
5839 	if (!descr->out_info) {
5840 		err = -ENOMEM;
5841 		goto ERR;
5842 	}
5843 	descr->vf_info = kmalloc(descr->num_stage *
5844 				 sizeof(struct ia_css_frame_info),
5845 				 GFP_KERNEL);
5846 	if (!descr->vf_info) {
5847 		err = -ENOMEM;
5848 		goto ERR;
5849 	}
5850 	descr->is_output_stage = kmalloc(descr->num_stage * sizeof(bool),
5851 					 GFP_KERNEL);
5852 	if (!descr->is_output_stage) {
5853 		err = -ENOMEM;
5854 		goto ERR;
5855 	}
5856 
5857 	tmp_in_info = *in_info;
5858 	for (i = 0; i < descr->num_stage; i++) {
5859 		descr->in_info[i] = tmp_in_info;
5860 		if ((tmp_in_info.res.width / max_scale_factor_per_stage) <= out_info->res.width) {
5861 			descr->is_output_stage[i] = true;
5862 			if ((descr->num_output_stage > 1) && (i != (descr->num_stage - 1))) {
5863 				descr->internal_out_info[i].res.width = out_info->res.width;
5864 				descr->internal_out_info[i].res.height = out_info->res.height;
5865 				descr->internal_out_info[i].padded_width = out_info->padded_width;
5866 				descr->internal_out_info[i].format = IA_CSS_FRAME_FORMAT_YUV420;
5867 			} else {
5868 				assert(i == (descr->num_stage - 1));
5869 				descr->internal_out_info[i].res.width = 0;
5870 				descr->internal_out_info[i].res.height = 0;
5871 			}
5872 			descr->out_info[i].res.width = out_info->res.width;
5873 			descr->out_info[i].res.height = out_info->res.height;
5874 			descr->out_info[i].padded_width = out_info->padded_width;
5875 			descr->out_info[i].format = out_info->format;
5876 			if (vf_info) {
5877 				descr->vf_info[i].res.width = vf_info->res.width;
5878 				descr->vf_info[i].res.height = vf_info->res.height;
5879 				descr->vf_info[i].padded_width = vf_info->padded_width;
5880 				ia_css_frame_info_set_format(&descr->vf_info[i], IA_CSS_FRAME_FORMAT_YUV_LINE);
5881 			} else {
5882 				descr->vf_info[i].res.width = 0;
5883 				descr->vf_info[i].res.height = 0;
5884 				descr->vf_info[i].padded_width = 0;
5885 			}
5886 		} else {
5887 			descr->is_output_stage[i] = false;
5888 			descr->internal_out_info[i].res.width = tmp_in_info.res.width /
5889 								max_scale_factor_per_stage;
5890 			descr->internal_out_info[i].res.height = tmp_in_info.res.height /
5891 				max_scale_factor_per_stage;
5892 			descr->internal_out_info[i].format = IA_CSS_FRAME_FORMAT_YUV420;
5893 			ia_css_frame_info_init(&descr->internal_out_info[i],
5894 					       tmp_in_info.res.width / max_scale_factor_per_stage,
5895 					       tmp_in_info.res.height / max_scale_factor_per_stage,
5896 					       IA_CSS_FRAME_FORMAT_YUV420, 0);
5897 			descr->out_info[i].res.width = 0;
5898 			descr->out_info[i].res.height = 0;
5899 			descr->vf_info[i].res.width = 0;
5900 			descr->vf_info[i].res.height = 0;
5901 		}
5902 		tmp_in_info = descr->internal_out_info[i];
5903 	}
5904 ERR:
5905 	ia_css_debug_dtrace(IA_CSS_DEBUG_TRACE_PRIVATE,
5906 			    "ia_css_pipe_create_cas_scaler_desc() leave, err=%d\n",
5907 			    err);
5908 	return err;
5909 }
5910 
5911 /* FIXME: merge most of this and single output version */
5912 static int
ia_css_pipe_create_cas_scaler_desc(struct ia_css_pipe * pipe,struct ia_css_cas_binary_descr * descr)5913 ia_css_pipe_create_cas_scaler_desc(struct ia_css_pipe *pipe,
5914 				   struct ia_css_cas_binary_descr *descr)
5915 {
5916 	struct ia_css_frame_info in_info = IA_CSS_BINARY_DEFAULT_FRAME_INFO;
5917 	struct ia_css_frame_info *out_info[IA_CSS_PIPE_MAX_OUTPUT_STAGE];
5918 	struct ia_css_frame_info *vf_out_info[IA_CSS_PIPE_MAX_OUTPUT_STAGE];
5919 	struct ia_css_frame_info tmp_in_info = IA_CSS_BINARY_DEFAULT_FRAME_INFO;
5920 	unsigned int i, j;
5921 	unsigned int hor_scale_factor[IA_CSS_PIPE_MAX_OUTPUT_STAGE],
5922 		    ver_scale_factor[IA_CSS_PIPE_MAX_OUTPUT_STAGE],
5923 		    scale_factor = 0;
5924 	unsigned int num_stages = 0;
5925 	int err = 0;
5926 
5927 	unsigned int max_scale_factor_per_stage = MAX_PREFERRED_YUV_DS_PER_STEP;
5928 
5929 	ia_css_debug_dtrace(IA_CSS_DEBUG_TRACE_PRIVATE,
5930 			    "ia_css_pipe_create_cas_scaler_desc() enter:\n");
5931 
5932 	for (i = 0; i < IA_CSS_PIPE_MAX_OUTPUT_STAGE; i++) {
5933 		out_info[i] = NULL;
5934 		vf_out_info[i] = NULL;
5935 		hor_scale_factor[i] = 0;
5936 		ver_scale_factor[i] = 0;
5937 	}
5938 
5939 	in_info.res = pipe->config.input_effective_res;
5940 	in_info.padded_width = in_info.res.width;
5941 	descr->num_output_stage = 0;
5942 	/* Find out how much scaling we need for each output */
5943 	for (i = 0; i < IA_CSS_PIPE_MAX_OUTPUT_STAGE; i++) {
5944 		if (pipe->output_info[i].res.width != 0) {
5945 			out_info[i] = &pipe->output_info[i];
5946 			if (pipe->vf_output_info[i].res.width != 0)
5947 				vf_out_info[i] = &pipe->vf_output_info[i];
5948 			descr->num_output_stage += 1;
5949 		}
5950 
5951 		if (out_info[i]) {
5952 			hor_scale_factor[i] = CEIL_DIV(in_info.res.width, out_info[i]->res.width);
5953 			ver_scale_factor[i] = CEIL_DIV(in_info.res.height, out_info[i]->res.height);
5954 			/* use the same horizontal and vertical scaling factor for simplicity */
5955 			assert(hor_scale_factor[i] == ver_scale_factor[i]);
5956 			scale_factor = 1;
5957 			do {
5958 				num_stages++;
5959 				scale_factor *= max_scale_factor_per_stage;
5960 			} while (scale_factor < hor_scale_factor[i]);
5961 
5962 			in_info.res = out_info[i]->res;
5963 		}
5964 	}
5965 
5966 	if (need_yuv_scaler_stage(pipe) && (num_stages == 0))
5967 		num_stages = 1;
5968 
5969 	descr->num_stage = num_stages;
5970 
5971 	descr->in_info = kmalloc_objs(struct ia_css_frame_info,
5972 				      descr->num_stage);
5973 	if (!descr->in_info) {
5974 		err = -ENOMEM;
5975 		goto ERR;
5976 	}
5977 	descr->internal_out_info = kmalloc(descr->num_stage *
5978 					   sizeof(struct ia_css_frame_info),
5979 					   GFP_KERNEL);
5980 	if (!descr->internal_out_info) {
5981 		err = -ENOMEM;
5982 		goto ERR;
5983 	}
5984 	descr->out_info = kmalloc(descr->num_stage *
5985 				  sizeof(struct ia_css_frame_info),
5986 				  GFP_KERNEL);
5987 	if (!descr->out_info) {
5988 		err = -ENOMEM;
5989 		goto ERR;
5990 	}
5991 	descr->vf_info = kmalloc(descr->num_stage *
5992 				 sizeof(struct ia_css_frame_info),
5993 				 GFP_KERNEL);
5994 	if (!descr->vf_info) {
5995 		err = -ENOMEM;
5996 		goto ERR;
5997 	}
5998 	descr->is_output_stage = kmalloc(descr->num_stage * sizeof(bool),
5999 					 GFP_KERNEL);
6000 	if (!descr->is_output_stage) {
6001 		err = -ENOMEM;
6002 		goto ERR;
6003 	}
6004 
6005 	for (i = 0; i < IA_CSS_PIPE_MAX_OUTPUT_STAGE; i++) {
6006 		if (out_info[i]) {
6007 			if (i > 0) {
6008 				assert((out_info[i - 1]->res.width >= out_info[i]->res.width) &&
6009 				       (out_info[i - 1]->res.height >= out_info[i]->res.height));
6010 			}
6011 		}
6012 	}
6013 
6014 	tmp_in_info.res = pipe->config.input_effective_res;
6015 	tmp_in_info.format = IA_CSS_FRAME_FORMAT_YUV420;
6016 	for (i = 0, j = 0; i < descr->num_stage; i++) {
6017 		assert(j < 2);
6018 		assert(out_info[j]);
6019 
6020 		descr->in_info[i] = tmp_in_info;
6021 		if ((tmp_in_info.res.width / max_scale_factor_per_stage) <=
6022 		    out_info[j]->res.width) {
6023 			descr->is_output_stage[i] = true;
6024 			if ((descr->num_output_stage > 1) && (i != (descr->num_stage - 1))) {
6025 				descr->internal_out_info[i].res.width = out_info[j]->res.width;
6026 				descr->internal_out_info[i].res.height = out_info[j]->res.height;
6027 				descr->internal_out_info[i].padded_width = out_info[j]->padded_width;
6028 				descr->internal_out_info[i].format = IA_CSS_FRAME_FORMAT_YUV420;
6029 			} else {
6030 				assert(i == (descr->num_stage - 1));
6031 				descr->internal_out_info[i].res.width = 0;
6032 				descr->internal_out_info[i].res.height = 0;
6033 			}
6034 			descr->out_info[i].res.width = out_info[j]->res.width;
6035 			descr->out_info[i].res.height = out_info[j]->res.height;
6036 			descr->out_info[i].padded_width = out_info[j]->padded_width;
6037 			descr->out_info[i].format = out_info[j]->format;
6038 			if (vf_out_info[j]) {
6039 				descr->vf_info[i].res.width = vf_out_info[j]->res.width;
6040 				descr->vf_info[i].res.height = vf_out_info[j]->res.height;
6041 				descr->vf_info[i].padded_width = vf_out_info[j]->padded_width;
6042 				ia_css_frame_info_set_format(&descr->vf_info[i], IA_CSS_FRAME_FORMAT_YUV_LINE);
6043 			} else {
6044 				descr->vf_info[i].res.width = 0;
6045 				descr->vf_info[i].res.height = 0;
6046 				descr->vf_info[i].padded_width = 0;
6047 			}
6048 			j++;
6049 		} else {
6050 			descr->is_output_stage[i] = false;
6051 			descr->internal_out_info[i].res.width = tmp_in_info.res.width /
6052 								max_scale_factor_per_stage;
6053 			descr->internal_out_info[i].res.height = tmp_in_info.res.height /
6054 				max_scale_factor_per_stage;
6055 			descr->internal_out_info[i].format = IA_CSS_FRAME_FORMAT_YUV420;
6056 			ia_css_frame_info_init(&descr->internal_out_info[i],
6057 					       tmp_in_info.res.width / max_scale_factor_per_stage,
6058 					       tmp_in_info.res.height / max_scale_factor_per_stage,
6059 					       IA_CSS_FRAME_FORMAT_YUV420, 0);
6060 			descr->out_info[i].res.width = 0;
6061 			descr->out_info[i].res.height = 0;
6062 			descr->vf_info[i].res.width = 0;
6063 			descr->vf_info[i].res.height = 0;
6064 		}
6065 		tmp_in_info = descr->internal_out_info[i];
6066 	}
6067 ERR:
6068 	ia_css_debug_dtrace(IA_CSS_DEBUG_TRACE_PRIVATE,
6069 			    "ia_css_pipe_create_cas_scaler_desc() leave, err=%d\n",
6070 			    err);
6071 	return err;
6072 }
6073 
ia_css_pipe_destroy_cas_scaler_desc(struct ia_css_cas_binary_descr * descr)6074 static void ia_css_pipe_destroy_cas_scaler_desc(struct ia_css_cas_binary_descr
6075 	*descr)
6076 {
6077 	ia_css_debug_dtrace(IA_CSS_DEBUG_TRACE_PRIVATE,
6078 			    "ia_css_pipe_destroy_cas_scaler_desc() enter:\n");
6079 	kfree(descr->in_info);
6080 	descr->in_info = NULL;
6081 	kfree(descr->internal_out_info);
6082 	descr->internal_out_info = NULL;
6083 	kfree(descr->out_info);
6084 	descr->out_info = NULL;
6085 	kfree(descr->vf_info);
6086 	descr->vf_info = NULL;
6087 	kfree(descr->is_output_stage);
6088 	descr->is_output_stage = NULL;
6089 	ia_css_debug_dtrace(IA_CSS_DEBUG_TRACE_PRIVATE,
6090 			    "ia_css_pipe_destroy_cas_scaler_desc() leave\n");
6091 }
6092 
6093 static int
load_yuvpp_binaries(struct ia_css_pipe * pipe)6094 load_yuvpp_binaries(struct ia_css_pipe *pipe)
6095 {
6096 	int err = 0;
6097 	bool need_scaler = false;
6098 	struct ia_css_frame_info *vf_pp_in_info[IA_CSS_PIPE_MAX_OUTPUT_STAGE];
6099 	struct ia_css_yuvpp_settings *mycs;
6100 	struct ia_css_binary *next_binary;
6101 	struct ia_css_cas_binary_descr cas_scaler_descr = { };
6102 	unsigned int i, j;
6103 	bool need_isp_copy_binary = false;
6104 
6105 	IA_CSS_ENTER_PRIVATE("");
6106 	assert(pipe);
6107 	assert(pipe->stream);
6108 	assert(pipe->mode == IA_CSS_PIPE_ID_YUVPP);
6109 
6110 	if (pipe->pipe_settings.yuvpp.copy_binary.info)
6111 		goto ERR;
6112 
6113 	/* Set both must_be_raw and must_be_yuv to false then yuvpp can take rgb inputs */
6114 	err = ia_css_util_check_input(&pipe->stream->config, false, false);
6115 	if (err)
6116 		goto ERR;
6117 
6118 	mycs = &pipe->pipe_settings.yuvpp;
6119 
6120 	for (i = 0; i < IA_CSS_PIPE_MAX_OUTPUT_STAGE; i++) {
6121 		if (pipe->vf_output_info[i].res.width != 0) {
6122 			err = ia_css_util_check_vf_out_info(&pipe->output_info[i],
6123 							    &pipe->vf_output_info[i]);
6124 			if (err)
6125 				goto ERR;
6126 		}
6127 		vf_pp_in_info[i] = NULL;
6128 	}
6129 
6130 	need_scaler = need_yuv_scaler_stage(pipe);
6131 
6132 	/* we build up the pipeline starting at the end */
6133 	/* Capture post-processing */
6134 	if (need_scaler) {
6135 		struct ia_css_binary_descr yuv_scaler_descr;
6136 
6137 		err = ia_css_pipe_create_cas_scaler_desc(pipe,
6138 							 &cas_scaler_descr);
6139 		if (err)
6140 			goto ERR;
6141 		mycs->num_output = cas_scaler_descr.num_output_stage;
6142 		mycs->num_yuv_scaler = cas_scaler_descr.num_stage;
6143 		mycs->yuv_scaler_binary = kzalloc_objs(struct ia_css_binary,
6144 						       cas_scaler_descr.num_stage);
6145 		if (!mycs->yuv_scaler_binary) {
6146 			err = -ENOMEM;
6147 			goto ERR;
6148 		}
6149 		mycs->is_output_stage = kzalloc_objs(bool,
6150 						     cas_scaler_descr.num_stage);
6151 		if (!mycs->is_output_stage) {
6152 			err = -ENOMEM;
6153 			goto ERR;
6154 		}
6155 		for (i = 0; i < cas_scaler_descr.num_stage; i++) {
6156 			mycs->is_output_stage[i] = cas_scaler_descr.is_output_stage[i];
6157 			ia_css_pipe_get_yuvscaler_binarydesc(pipe,
6158 							     &yuv_scaler_descr,
6159 							     &cas_scaler_descr.in_info[i],
6160 							     &cas_scaler_descr.out_info[i],
6161 							     &cas_scaler_descr.internal_out_info[i],
6162 							     &cas_scaler_descr.vf_info[i]);
6163 			err = ia_css_binary_find(&yuv_scaler_descr,
6164 						 &mycs->yuv_scaler_binary[i]);
6165 			if (err)
6166 				goto ERR;
6167 		}
6168 		ia_css_pipe_destroy_cas_scaler_desc(&cas_scaler_descr);
6169 	} else {
6170 		mycs->num_output = 1;
6171 	}
6172 
6173 	if (need_scaler)
6174 		next_binary = &mycs->yuv_scaler_binary[0];
6175 	else
6176 		next_binary = NULL;
6177 
6178 	/*
6179 	 * NOTES
6180 	 * - Why does the "yuvpp" pipe needs "isp_copy_binary" (i.e. ISP Copy) when
6181 	 *   its input is "ATOMISP_INPUT_FORMAT_YUV422_8"?
6182 	 *
6183 	 *   In most use cases, the first stage in the "yuvpp" pipe is the "yuv_scale_
6184 	 *   binary". However, the "yuv_scale_binary" does NOT support the input-frame
6185 	 *   format as "IA_CSS_STREAM _FORMAT_YUV422_8".
6186 	 *
6187 	 *   Hence, the "isp_copy_binary" is required to be present in front of the "yuv
6188 	 *   _scale_binary". It would translate the input-frame to the frame formats that
6189 	 *   are supported by the "yuv_scale_binary".
6190 	 *
6191 	 *   Please refer to "FrameWork/css/isp/pipes/capture_pp/capture_pp_1.0/capture_
6192 	 *   pp_defs.h" for the list of input-frame formats that are supported by the
6193 	 *   "yuv_scale_binary".
6194 	 */
6195 	if (IS_ISP2401)
6196 		need_isp_copy_binary =
6197 		    (pipe->stream->config.input_config.format == ATOMISP_INPUT_FORMAT_YUV422_8);
6198 	else
6199 		need_isp_copy_binary = true;
6200 
6201 	if (need_isp_copy_binary) {
6202 		err = load_copy_binary(pipe,
6203 				       &mycs->copy_binary,
6204 				       next_binary);
6205 
6206 		if (err)
6207 			goto ERR;
6208 
6209 		/*
6210 		 * NOTES
6211 		 * - Why is "pipe->pipe_settings.capture.copy_binary.online" specified?
6212 		 *
6213 		 *   In some use cases, the first stage in the "yuvpp" pipe is the
6214 		 *   "isp_copy_binary". The "isp_copy_binary" is designed to process
6215 		 *   the input from either the system DDR or from the IPU internal VMEM.
6216 		 *   So it provides the flag "online" to specify where its input is from,
6217 		 *   i.e.:
6218 		 *
6219 		 *      (1) "online <= true", the input is from the IPU internal VMEM.
6220 		 *      (2) "online <= false", the input is from the system DDR.
6221 		 *
6222 		 *   In other use cases, the first stage in the "yuvpp" pipe is the
6223 		 *   "yuv_scale_binary". "The "yuv_scale_binary" is designed to process the
6224 		 *   input ONLY from the system DDR. So it does not provide the flag "online"
6225 		 *   to specify where its input is from.
6226 		 */
6227 		pipe->pipe_settings.capture.copy_binary.online = pipe->stream->config.online;
6228 	}
6229 
6230 	/* Viewfinder post-processing */
6231 	if (need_scaler) {
6232 		for (i = 0, j = 0; i < mycs->num_yuv_scaler; i++) {
6233 			if (mycs->is_output_stage[i]) {
6234 				assert(j < 2);
6235 				vf_pp_in_info[j] =
6236 				    &mycs->yuv_scaler_binary[i].vf_frame_info;
6237 				j++;
6238 			}
6239 		}
6240 		mycs->num_vf_pp = j;
6241 	} else {
6242 		vf_pp_in_info[0] =
6243 		    &mycs->copy_binary.vf_frame_info;
6244 		for (i = 1; i < IA_CSS_PIPE_MAX_OUTPUT_STAGE; i++)
6245 			vf_pp_in_info[i] = NULL;
6246 
6247 		mycs->num_vf_pp = 1;
6248 	}
6249 	mycs->vf_pp_binary = kzalloc_objs(struct ia_css_binary, mycs->num_vf_pp);
6250 	if (!mycs->vf_pp_binary) {
6251 		err = -ENOMEM;
6252 		goto ERR;
6253 	}
6254 
6255 	{
6256 		struct ia_css_binary_descr vf_pp_descr;
6257 
6258 		for (i = 0; i < mycs->num_vf_pp; i++) {
6259 			if (pipe->vf_output_info[i].res.width != 0) {
6260 				ia_css_pipe_get_vfpp_binarydesc(pipe,
6261 								&vf_pp_descr, vf_pp_in_info[i], &pipe->vf_output_info[i]);
6262 				err = ia_css_binary_find(&vf_pp_descr, &mycs->vf_pp_binary[i]);
6263 				if (err)
6264 					goto ERR;
6265 			}
6266 		}
6267 	}
6268 
6269 ERR:
6270 	if (need_scaler)
6271 		ia_css_pipe_destroy_cas_scaler_desc(&cas_scaler_descr);
6272 
6273 	ia_css_debug_dtrace(IA_CSS_DEBUG_TRACE_PRIVATE, "load_yuvpp_binaries() leave, err=%d\n",
6274 			    err);
6275 	return err;
6276 }
6277 
6278 static int
unload_yuvpp_binaries(struct ia_css_pipe * pipe)6279 unload_yuvpp_binaries(struct ia_css_pipe *pipe)
6280 {
6281 	unsigned int i;
6282 
6283 	IA_CSS_ENTER_PRIVATE("pipe = %p", pipe);
6284 
6285 	if ((!pipe) || (pipe->mode != IA_CSS_PIPE_ID_YUVPP)) {
6286 		IA_CSS_LEAVE_ERR_PRIVATE(-EINVAL);
6287 		return -EINVAL;
6288 	}
6289 	ia_css_binary_unload(&pipe->pipe_settings.yuvpp.copy_binary);
6290 	for (i = 0; i < pipe->pipe_settings.yuvpp.num_yuv_scaler; i++)
6291 		ia_css_binary_unload(&pipe->pipe_settings.yuvpp.yuv_scaler_binary[i]);
6292 
6293 	for (i = 0; i < pipe->pipe_settings.yuvpp.num_vf_pp; i++)
6294 		ia_css_binary_unload(&pipe->pipe_settings.yuvpp.vf_pp_binary[i]);
6295 
6296 	kfree(pipe->pipe_settings.yuvpp.is_output_stage);
6297 	pipe->pipe_settings.yuvpp.is_output_stage = NULL;
6298 	kfree(pipe->pipe_settings.yuvpp.yuv_scaler_binary);
6299 	pipe->pipe_settings.yuvpp.yuv_scaler_binary = NULL;
6300 	kfree(pipe->pipe_settings.yuvpp.vf_pp_binary);
6301 	pipe->pipe_settings.yuvpp.vf_pp_binary = NULL;
6302 
6303 	IA_CSS_LEAVE_ERR_PRIVATE(0);
6304 	return 0;
6305 }
6306 
yuvpp_start(struct ia_css_pipe * pipe)6307 static int yuvpp_start(struct ia_css_pipe *pipe)
6308 {
6309 	int err = 0;
6310 	enum sh_css_pipe_config_override copy_ovrd;
6311 	enum ia_css_input_mode yuvpp_pipe_input_mode;
6312 	unsigned int thread_id;
6313 
6314 	IA_CSS_ENTER_PRIVATE("pipe = %p", pipe);
6315 	if ((!pipe) || (pipe->mode != IA_CSS_PIPE_ID_YUVPP)) {
6316 		IA_CSS_LEAVE_ERR_PRIVATE(-EINVAL);
6317 		return -EINVAL;
6318 	}
6319 
6320 	yuvpp_pipe_input_mode = pipe->stream->config.mode;
6321 
6322 	sh_css_metrics_start_frame();
6323 
6324 	/* multi stream video needs mipi buffers */
6325 
6326 	err = send_mipi_frames(pipe);
6327 	if (err) {
6328 		IA_CSS_LEAVE_ERR_PRIVATE(err);
6329 		return err;
6330 	}
6331 
6332 	ia_css_pipeline_get_sp_thread_id(ia_css_pipe_get_pipe_num(pipe), &thread_id);
6333 	copy_ovrd = 1 << thread_id;
6334 
6335 	start_pipe(pipe, copy_ovrd, yuvpp_pipe_input_mode);
6336 
6337 	IA_CSS_LEAVE_ERR_PRIVATE(err);
6338 	return err;
6339 }
6340 
6341 static int
sh_css_pipe_unload_binaries(struct ia_css_pipe * pipe)6342 sh_css_pipe_unload_binaries(struct ia_css_pipe *pipe)
6343 {
6344 	int err = 0;
6345 
6346 	IA_CSS_ENTER_PRIVATE("pipe = %p", pipe);
6347 
6348 	if (!pipe) {
6349 		IA_CSS_LEAVE_ERR_PRIVATE(-EINVAL);
6350 		return -EINVAL;
6351 	}
6352 	/* PIPE_MODE_COPY has no binaries, but has output frames to outside*/
6353 	if (pipe->config.mode == IA_CSS_PIPE_MODE_COPY) {
6354 		IA_CSS_LEAVE_ERR_PRIVATE(0);
6355 		return 0;
6356 	}
6357 
6358 	switch (pipe->mode) {
6359 	case IA_CSS_PIPE_ID_PREVIEW:
6360 		err = unload_preview_binaries(pipe);
6361 		break;
6362 	case IA_CSS_PIPE_ID_VIDEO:
6363 		err = unload_video_binaries(pipe);
6364 		break;
6365 	case IA_CSS_PIPE_ID_CAPTURE:
6366 		err = unload_capture_binaries(pipe);
6367 		break;
6368 	case IA_CSS_PIPE_ID_YUVPP:
6369 		err = unload_yuvpp_binaries(pipe);
6370 		break;
6371 	default:
6372 		break;
6373 	}
6374 	IA_CSS_LEAVE_ERR_PRIVATE(err);
6375 	return err;
6376 }
6377 
6378 static int
sh_css_pipe_load_binaries(struct ia_css_pipe * pipe)6379 sh_css_pipe_load_binaries(struct ia_css_pipe *pipe)
6380 {
6381 	int err = 0;
6382 
6383 	assert(pipe);
6384 	ia_css_debug_dtrace(IA_CSS_DEBUG_TRACE_PRIVATE, "sh_css_pipe_load_binaries() enter:\n");
6385 
6386 	/* PIPE_MODE_COPY has no binaries, but has output frames to outside*/
6387 	if (pipe->config.mode == IA_CSS_PIPE_MODE_COPY)
6388 		return err;
6389 
6390 	switch (pipe->mode) {
6391 	case IA_CSS_PIPE_ID_PREVIEW:
6392 		err = load_preview_binaries(pipe);
6393 		break;
6394 	case IA_CSS_PIPE_ID_VIDEO:
6395 		err = load_video_binaries(pipe);
6396 		break;
6397 	case IA_CSS_PIPE_ID_CAPTURE:
6398 		err = load_capture_binaries(pipe);
6399 		break;
6400 	case IA_CSS_PIPE_ID_YUVPP:
6401 		err = load_yuvpp_binaries(pipe);
6402 		break;
6403 	default:
6404 		err = -EINVAL;
6405 		break;
6406 	}
6407 	if (err) {
6408 		if (sh_css_pipe_unload_binaries(pipe)) {
6409 			/*
6410 			 * currently css does not support multiple error
6411 			 * returns in a single function, using -EINVAL in
6412 			 * this case
6413 			 */
6414 			err = -EINVAL;
6415 		}
6416 	}
6417 	return err;
6418 }
6419 
6420 static int
create_host_yuvpp_pipeline(struct ia_css_pipe * pipe)6421 create_host_yuvpp_pipeline(struct ia_css_pipe *pipe)
6422 {
6423 	struct ia_css_pipeline *me;
6424 	int err = 0;
6425 	struct ia_css_pipeline_stage *vf_pp_stage = NULL,
6426 		*copy_stage = NULL,
6427 		*yuv_scaler_stage = NULL;
6428 	struct ia_css_binary *copy_binary,
6429 		*vf_pp_binary,
6430 		*yuv_scaler_binary;
6431 	bool need_scaler = false;
6432 	unsigned int num_stage, num_output_stage;
6433 	unsigned int i, j;
6434 
6435 	struct ia_css_frame *in_frame = NULL;
6436 	struct ia_css_frame *out_frame[IA_CSS_PIPE_MAX_OUTPUT_STAGE];
6437 	struct ia_css_frame *bin_out_frame[IA_CSS_BINARY_MAX_OUTPUT_PORTS];
6438 	struct ia_css_frame *vf_frame[IA_CSS_PIPE_MAX_OUTPUT_STAGE];
6439 	struct ia_css_pipeline_stage_desc stage_desc;
6440 	bool need_in_frameinfo_memory = false;
6441 	bool sensor = false;
6442 	bool buffered_sensor = false;
6443 	bool online = false;
6444 	bool continuous = false;
6445 
6446 	IA_CSS_ENTER_PRIVATE("pipe = %p", pipe);
6447 	if ((!pipe) || (!pipe->stream) || (pipe->mode != IA_CSS_PIPE_ID_YUVPP)) {
6448 		IA_CSS_LEAVE_ERR_PRIVATE(-EINVAL);
6449 		return -EINVAL;
6450 	}
6451 	me = &pipe->pipeline;
6452 	ia_css_pipeline_clean(me);
6453 	for (i = 0; i < IA_CSS_PIPE_MAX_OUTPUT_STAGE; i++) {
6454 		out_frame[i] = NULL;
6455 		vf_frame[i] = NULL;
6456 	}
6457 	ia_css_pipe_util_create_output_frames(bin_out_frame);
6458 	num_stage  = pipe->pipe_settings.yuvpp.num_yuv_scaler;
6459 	num_output_stage   = pipe->pipe_settings.yuvpp.num_output;
6460 
6461 	if (IS_ISP2401) {
6462 		/*
6463 		 * When the input system is 2401, always enable 'in_frameinfo_memory'
6464 		 * except for the following:
6465 		 * - Direct Sensor Mode Online Capture
6466 		 * - Direct Sensor Mode Continuous Capture
6467 		 * - Buffered Sensor Mode Continuous Capture
6468 		 */
6469 		sensor = pipe->stream->config.mode == IA_CSS_INPUT_MODE_SENSOR;
6470 		buffered_sensor = pipe->stream->config.mode == IA_CSS_INPUT_MODE_BUFFERED_SENSOR;
6471 		online = pipe->stream->config.online;
6472 		continuous = pipe->stream->config.continuous;
6473 		need_in_frameinfo_memory =
6474 		!((sensor && (online || continuous)) || (buffered_sensor && continuous));
6475 	} else {
6476 		/* Construct in_frame info (only in case we have dynamic input */
6477 		need_in_frameinfo_memory = pipe->stream->config.mode == IA_CSS_INPUT_MODE_MEMORY;
6478 	}
6479 	/*
6480 	 * the input frame can come from:
6481 	 *
6482 	 *  a) memory: connect yuvscaler to me->in_frame
6483 	 *  b) sensor, via copy binary: connect yuvscaler to copy binary later
6484 	 *     on
6485 	 */
6486 	if (need_in_frameinfo_memory) {
6487 		/* TODO: improve for different input formats. */
6488 
6489 		/*
6490 		 * "pipe->stream->config.input_config.format" represents the sensor output
6491 		 * frame format, e.g. YUV422 8-bit.
6492 		 *
6493 		 * "in_frame_format" represents the imaging pipe's input frame format, e.g.
6494 		 * Bayer-Quad RAW.
6495 		 */
6496 		int in_frame_format;
6497 
6498 		if (pipe->stream->config.input_config.format ==
6499 		    ATOMISP_INPUT_FORMAT_YUV420_8_LEGACY) {
6500 			in_frame_format = IA_CSS_FRAME_FORMAT_CSI_MIPI_LEGACY_YUV420_8;
6501 		} else if (pipe->stream->config.input_config.format ==
6502 			    ATOMISP_INPUT_FORMAT_YUV422_8) {
6503 			/*
6504 			 * When the sensor output frame format is "ATOMISP_INPUT_FORMAT_YUV422_8",
6505 			 * the "isp_copy_var" binary is selected as the first stage in the yuvpp
6506 			 * pipe.
6507 			 *
6508 			 * For the "isp_copy_var" binary, it reads the YUV422-8 pixels from
6509 			 * the frame buffer (at DDR) to the frame-line buffer (at VMEM).
6510 			 *
6511 			 * By now, the "isp_copy_var" binary does NOT provide a separated
6512 			 * frame-line buffer to store the YUV422-8 pixels. Instead, it stores
6513 			 * the YUV422-8 pixels in the frame-line buffer which is designed to
6514 			 * store the Bayer-Quad RAW pixels.
6515 			 *
6516 			 * To direct the "isp_copy_var" binary reading from the RAW frame-line
6517 			 * buffer, its input frame format must be specified as "IA_CSS_FRAME_
6518 			 * FORMAT_RAW".
6519 			 */
6520 			in_frame_format = IA_CSS_FRAME_FORMAT_RAW;
6521 		} else {
6522 			in_frame_format = IA_CSS_FRAME_FORMAT_NV12;
6523 		}
6524 
6525 		err = init_in_frameinfo_memory_defaults(pipe,
6526 							&me->in_frame,
6527 							in_frame_format);
6528 
6529 		if (err) {
6530 			IA_CSS_LEAVE_ERR_PRIVATE(err);
6531 			return err;
6532 		}
6533 
6534 		in_frame = &me->in_frame;
6535 	} else {
6536 		in_frame = NULL;
6537 	}
6538 
6539 	for (i = 0; i < num_output_stage; i++) {
6540 		assert(i < IA_CSS_PIPE_MAX_OUTPUT_STAGE);
6541 		if (pipe->output_info[i].res.width != 0) {
6542 			err = init_out_frameinfo_defaults(pipe, &me->out_frame[i], i);
6543 			if (err) {
6544 				IA_CSS_LEAVE_ERR_PRIVATE(err);
6545 				return err;
6546 			}
6547 			out_frame[i] = &me->out_frame[i];
6548 		}
6549 
6550 		/* Construct vf_frame info (only in case we have VF) */
6551 		if (pipe->vf_output_info[i].res.width != 0) {
6552 			err = init_vf_frameinfo_defaults(pipe, &me->vf_frame[i], i);
6553 			if (err) {
6554 				IA_CSS_LEAVE_ERR_PRIVATE(err);
6555 				return err;
6556 			}
6557 			vf_frame[i] = &me->vf_frame[i];
6558 		}
6559 	}
6560 
6561 	copy_binary       = &pipe->pipe_settings.yuvpp.copy_binary;
6562 	vf_pp_binary      = pipe->pipe_settings.yuvpp.vf_pp_binary;
6563 	yuv_scaler_binary = pipe->pipe_settings.yuvpp.yuv_scaler_binary;
6564 	need_scaler = need_yuv_scaler_stage(pipe);
6565 
6566 	if (pipe->pipe_settings.yuvpp.copy_binary.info) {
6567 		struct ia_css_frame *in_frame_local = NULL;
6568 
6569 		if (IS_ISP2401 && !online) {
6570 			/* After isp copy is enabled in_frame needs to be passed. */
6571 			in_frame_local = in_frame;
6572 		}
6573 
6574 		if (need_scaler) {
6575 			ia_css_pipe_util_set_output_frames(bin_out_frame,
6576 							   0, NULL);
6577 			ia_css_pipe_get_generic_stage_desc(&stage_desc,
6578 							   copy_binary,
6579 							   bin_out_frame,
6580 							   in_frame_local,
6581 							   NULL);
6582 		} else {
6583 			ia_css_pipe_util_set_output_frames(bin_out_frame,
6584 							   0, out_frame[0]);
6585 			ia_css_pipe_get_generic_stage_desc(&stage_desc,
6586 							   copy_binary,
6587 							   bin_out_frame,
6588 							   in_frame_local,
6589 							   NULL);
6590 		}
6591 
6592 		err = ia_css_pipeline_create_and_add_stage(me,
6593 							   &stage_desc,
6594 							   &copy_stage);
6595 
6596 		if (err) {
6597 			IA_CSS_LEAVE_ERR_PRIVATE(err);
6598 			return err;
6599 		}
6600 
6601 		if (copy_stage) {
6602 			/* if we use yuv scaler binary, vf output should be from there */
6603 			copy_stage->args.copy_vf = !need_scaler;
6604 			/* for yuvpp pipe, it should always be enabled */
6605 			copy_stage->args.copy_output = true;
6606 			/* connect output of copy binary to input of yuv scaler */
6607 			in_frame = copy_stage->args.out_frame[0];
6608 		}
6609 	}
6610 
6611 	if (need_scaler) {
6612 		struct ia_css_frame *tmp_out_frame = NULL;
6613 		struct ia_css_frame *tmp_vf_frame = NULL;
6614 		struct ia_css_frame *tmp_in_frame = in_frame;
6615 
6616 		for (i = 0, j = 0; i < num_stage; i++) {
6617 			assert(j < num_output_stage);
6618 			if (pipe->pipe_settings.yuvpp.is_output_stage[i]) {
6619 				tmp_out_frame = out_frame[j];
6620 				tmp_vf_frame = vf_frame[j];
6621 			} else {
6622 				tmp_out_frame = NULL;
6623 				tmp_vf_frame = NULL;
6624 			}
6625 
6626 			err = add_yuv_scaler_stage(pipe, me, tmp_in_frame,
6627 						   tmp_out_frame,
6628 						   NULL,
6629 						   &yuv_scaler_binary[i],
6630 						   &yuv_scaler_stage);
6631 
6632 			if (err) {
6633 				IA_CSS_LEAVE_ERR_PRIVATE(err);
6634 				return err;
6635 			}
6636 			/* we use output port 1 as internal output port */
6637 			tmp_in_frame = yuv_scaler_stage->args.out_frame[1];
6638 			if (pipe->pipe_settings.yuvpp.is_output_stage[i]) {
6639 				if (tmp_vf_frame && (tmp_vf_frame->frame_info.res.width != 0)) {
6640 					in_frame = yuv_scaler_stage->args.out_vf_frame;
6641 					err = add_vf_pp_stage(pipe, in_frame,
6642 							      tmp_vf_frame,
6643 							      &vf_pp_binary[j],
6644 							      &vf_pp_stage);
6645 
6646 					if (err) {
6647 						IA_CSS_LEAVE_ERR_PRIVATE(err);
6648 						return err;
6649 					}
6650 				}
6651 				j++;
6652 			}
6653 		}
6654 	} else if (copy_stage) {
6655 		if (vf_frame[0] && vf_frame[0]->frame_info.res.width != 0) {
6656 			in_frame = copy_stage->args.out_vf_frame;
6657 			err = add_vf_pp_stage(pipe, in_frame, vf_frame[0],
6658 					      &vf_pp_binary[0], &vf_pp_stage);
6659 		}
6660 		if (err) {
6661 			IA_CSS_LEAVE_ERR_PRIVATE(err);
6662 			return err;
6663 		}
6664 	}
6665 
6666 	ia_css_pipeline_finalize_stages(&pipe->pipeline,
6667 					pipe->stream->config.continuous);
6668 
6669 	IA_CSS_LEAVE_ERR_PRIVATE(0);
6670 
6671 	return 0;
6672 }
6673 
6674 static int
create_host_copy_pipeline(struct ia_css_pipe * pipe,unsigned int max_input_width,struct ia_css_frame * out_frame)6675 create_host_copy_pipeline(struct ia_css_pipe *pipe,
6676 			  unsigned int max_input_width,
6677 			  struct ia_css_frame *out_frame)
6678 {
6679 	struct ia_css_pipeline *me;
6680 	int err = 0;
6681 	struct ia_css_pipeline_stage_desc stage_desc;
6682 
6683 	ia_css_debug_dtrace(IA_CSS_DEBUG_TRACE_PRIVATE,
6684 			    "create_host_copy_pipeline() enter:\n");
6685 
6686 	/* pipeline already created as part of create_host_pipeline_structure */
6687 	me = &pipe->pipeline;
6688 	ia_css_pipeline_clean(me);
6689 
6690 	/* Construct out_frame info */
6691 	if (copy_on_sp(pipe) &&
6692 	    pipe->stream->config.input_config.format == ATOMISP_INPUT_FORMAT_BINARY_8) {
6693 		ia_css_frame_info_init(&out_frame->frame_info, JPEG_BYTES, 1,
6694 				       IA_CSS_FRAME_FORMAT_BINARY_8, 0);
6695 	} else if (out_frame->frame_info.format == IA_CSS_FRAME_FORMAT_RAW) {
6696 		out_frame->frame_info.raw_bit_depth =
6697 		ia_css_pipe_util_pipe_input_format_bpp(pipe);
6698 	}
6699 
6700 	me->num_stages = 1;
6701 	me->pipe_id = IA_CSS_PIPE_ID_COPY;
6702 	pipe->mode  = IA_CSS_PIPE_ID_COPY;
6703 
6704 	ia_css_pipe_get_sp_func_stage_desc(&stage_desc, out_frame,
6705 					   IA_CSS_PIPELINE_RAW_COPY,
6706 					   max_input_width);
6707 	err = ia_css_pipeline_create_and_add_stage(me, &stage_desc, NULL);
6708 
6709 	ia_css_pipeline_finalize_stages(&pipe->pipeline,
6710 					pipe->stream->config.continuous);
6711 
6712 	ia_css_debug_dtrace(IA_CSS_DEBUG_TRACE_PRIVATE,
6713 			    "create_host_copy_pipeline() leave:\n");
6714 
6715 	return err;
6716 }
6717 
6718 static int
create_host_isyscopy_capture_pipeline(struct ia_css_pipe * pipe)6719 create_host_isyscopy_capture_pipeline(struct ia_css_pipe *pipe)
6720 {
6721 	struct ia_css_pipeline *me = &pipe->pipeline;
6722 	int err = 0;
6723 	struct ia_css_pipeline_stage_desc stage_desc;
6724 	struct ia_css_frame *out_frame = &me->out_frame[0];
6725 	struct ia_css_pipeline_stage *out_stage = NULL;
6726 	unsigned int thread_id;
6727 	enum sh_css_queue_id queue_id;
6728 	unsigned int max_input_width = MAX_VECTORS_PER_INPUT_LINE_CONT * ISP_VEC_NELEMS;
6729 
6730 	ia_css_debug_dtrace(IA_CSS_DEBUG_TRACE_PRIVATE,
6731 			    "create_host_isyscopy_capture_pipeline() enter:\n");
6732 	ia_css_pipeline_clean(me);
6733 
6734 	/* Construct out_frame info */
6735 	err = sh_css_pipe_get_output_frame_info(pipe, &out_frame->frame_info, 0);
6736 	if (err)
6737 		return err;
6738 	ia_css_pipeline_get_sp_thread_id(ia_css_pipe_get_pipe_num(pipe), &thread_id);
6739 	ia_css_query_internal_queue_id(IA_CSS_BUFFER_TYPE_OUTPUT_FRAME, thread_id, &queue_id);
6740 	out_frame->dynamic_queue_id = queue_id;
6741 	out_frame->buf_type = IA_CSS_BUFFER_TYPE_OUTPUT_FRAME;
6742 
6743 	me->num_stages = 1;
6744 	me->pipe_id = IA_CSS_PIPE_ID_CAPTURE;
6745 	pipe->mode  = IA_CSS_PIPE_ID_CAPTURE;
6746 	ia_css_pipe_get_sp_func_stage_desc(&stage_desc, out_frame,
6747 					   IA_CSS_PIPELINE_ISYS_COPY,
6748 					   max_input_width);
6749 	err = ia_css_pipeline_create_and_add_stage(me,
6750 						   &stage_desc, &out_stage);
6751 	if (err)
6752 		return err;
6753 
6754 	ia_css_pipeline_finalize_stages(me, pipe->stream->config.continuous);
6755 
6756 	ia_css_debug_dtrace(IA_CSS_DEBUG_TRACE_PRIVATE,
6757 			    "create_host_isyscopy_capture_pipeline() leave:\n");
6758 
6759 	return err;
6760 }
6761 
6762 static int
create_host_regular_capture_pipeline(struct ia_css_pipe * pipe)6763 create_host_regular_capture_pipeline(struct ia_css_pipe *pipe)
6764 {
6765 	struct ia_css_pipeline *me;
6766 	int err = 0;
6767 	enum ia_css_capture_mode mode;
6768 	struct ia_css_pipeline_stage *current_stage = NULL;
6769 	struct ia_css_pipeline_stage *yuv_scaler_stage = NULL;
6770 	struct ia_css_binary *copy_binary,
6771 		*primary_binary[MAX_NUM_PRIMARY_STAGES],
6772 		*vf_pp_binary,
6773 		*pre_isp_binary,
6774 		*anr_gdc_binary,
6775 		*post_isp_binary,
6776 		*yuv_scaler_binary,
6777 		*capture_pp_binary,
6778 		*capture_ldc_binary;
6779 	bool need_pp = false;
6780 	bool raw;
6781 
6782 	struct ia_css_frame *in_frame;
6783 	struct ia_css_frame *out_frame;
6784 	struct ia_css_frame *out_frames[IA_CSS_BINARY_MAX_OUTPUT_PORTS];
6785 	struct ia_css_frame *vf_frame;
6786 	struct ia_css_pipeline_stage_desc stage_desc;
6787 	bool need_in_frameinfo_memory = false;
6788 	bool sensor = false;
6789 	bool buffered_sensor = false;
6790 	bool online = false;
6791 	bool continuous = false;
6792 	unsigned int i, num_yuv_scaler, num_primary_stage;
6793 	bool need_yuv_pp = false;
6794 	bool *is_output_stage = NULL;
6795 	bool need_ldc = false;
6796 
6797 	IA_CSS_ENTER_PRIVATE("");
6798 	assert(pipe);
6799 	assert(pipe->stream);
6800 	assert(pipe->mode == IA_CSS_PIPE_ID_CAPTURE ||
6801 	       pipe->mode == IA_CSS_PIPE_ID_COPY);
6802 
6803 	me = &pipe->pipeline;
6804 	mode = pipe->config.default_capture_config.mode;
6805 	raw = (mode == IA_CSS_CAPTURE_MODE_RAW);
6806 	ia_css_pipeline_clean(me);
6807 	ia_css_pipe_util_create_output_frames(out_frames);
6808 
6809 	if (IS_ISP2401) {
6810 		/*
6811 		 * When the input system is 2401, always enable 'in_frameinfo_memory'
6812 		 * except for the following:
6813 		 * - Direct Sensor Mode Online Capture
6814 		 * - Direct Sensor Mode Online Capture
6815 		 * - Direct Sensor Mode Continuous Capture
6816 		 * - Buffered Sensor Mode Continuous Capture
6817 		 */
6818 		sensor = (pipe->stream->config.mode == IA_CSS_INPUT_MODE_SENSOR);
6819 		buffered_sensor = (pipe->stream->config.mode == IA_CSS_INPUT_MODE_BUFFERED_SENSOR);
6820 		online = pipe->stream->config.online;
6821 		continuous = pipe->stream->config.continuous;
6822 		need_in_frameinfo_memory =
6823 		!((sensor && (online || continuous)) || (buffered_sensor &&
6824 							(online || continuous)));
6825 	} else {
6826 		/* Construct in_frame info (only in case we have dynamic input */
6827 		need_in_frameinfo_memory = pipe->stream->config.mode == IA_CSS_INPUT_MODE_MEMORY;
6828 	}
6829 
6830 	if (need_in_frameinfo_memory) {
6831 		err = init_in_frameinfo_memory_defaults(pipe, &me->in_frame,
6832 							IA_CSS_FRAME_FORMAT_RAW);
6833 		if (err) {
6834 			IA_CSS_LEAVE_ERR_PRIVATE(err);
6835 			return err;
6836 		}
6837 
6838 		in_frame = &me->in_frame;
6839 	} else {
6840 		in_frame = NULL;
6841 	}
6842 
6843 	err = init_out_frameinfo_defaults(pipe, &me->out_frame[0], 0);
6844 	if (err) {
6845 		IA_CSS_LEAVE_ERR_PRIVATE(err);
6846 		return err;
6847 	}
6848 	out_frame = &me->out_frame[0];
6849 
6850 	/* Construct vf_frame info (only in case we have VF) */
6851 	if (pipe->enable_viewfinder[IA_CSS_PIPE_OUTPUT_STAGE_0]) {
6852 		if (mode == IA_CSS_CAPTURE_MODE_RAW || mode == IA_CSS_CAPTURE_MODE_BAYER) {
6853 			/* These modes don't support viewfinder output */
6854 			vf_frame = NULL;
6855 		} else {
6856 			init_vf_frameinfo_defaults(pipe, &me->vf_frame[0], 0);
6857 			vf_frame = &me->vf_frame[0];
6858 		}
6859 	} else {
6860 		vf_frame = NULL;
6861 	}
6862 
6863 	copy_binary       = &pipe->pipe_settings.capture.copy_binary;
6864 	num_primary_stage = pipe->pipe_settings.capture.num_primary_stage;
6865 	if ((num_primary_stage == 0) && (mode == IA_CSS_CAPTURE_MODE_PRIMARY)) {
6866 		IA_CSS_LEAVE_ERR_PRIVATE(-EINVAL);
6867 		return -EINVAL;
6868 	}
6869 
6870 	for (i = 0; i < num_primary_stage; i++)
6871 		primary_binary[i] = &pipe->pipe_settings.capture.primary_binary[i];
6872 
6873 	vf_pp_binary      = &pipe->pipe_settings.capture.vf_pp_binary;
6874 	pre_isp_binary    = &pipe->pipe_settings.capture.pre_isp_binary;
6875 	anr_gdc_binary    = &pipe->pipe_settings.capture.anr_gdc_binary;
6876 	post_isp_binary   = &pipe->pipe_settings.capture.post_isp_binary;
6877 	capture_pp_binary = &pipe->pipe_settings.capture.capture_pp_binary;
6878 	yuv_scaler_binary = pipe->pipe_settings.capture.yuv_scaler_binary;
6879 	num_yuv_scaler	  = pipe->pipe_settings.capture.num_yuv_scaler;
6880 	is_output_stage   = pipe->pipe_settings.capture.is_output_stage;
6881 	capture_ldc_binary = &pipe->pipe_settings.capture.capture_ldc_binary;
6882 
6883 	need_pp = (need_capture_pp(pipe) || pipe->output_stage) &&
6884 		    mode != IA_CSS_CAPTURE_MODE_RAW &&
6885 		    mode != IA_CSS_CAPTURE_MODE_BAYER;
6886 	need_yuv_pp = (yuv_scaler_binary && yuv_scaler_binary->info);
6887 	need_ldc = (capture_ldc_binary && capture_ldc_binary->info);
6888 
6889 	if (pipe->pipe_settings.capture.copy_binary.info) {
6890 		if (raw) {
6891 			ia_css_pipe_util_set_output_frames(out_frames, 0, out_frame);
6892 			if (IS_ISP2401) {
6893 				if (!continuous) {
6894 					ia_css_pipe_get_generic_stage_desc(&stage_desc,
6895 									   copy_binary,
6896 									   out_frames,
6897 									   in_frame,
6898 									   NULL);
6899 				} else {
6900 					in_frame = pipe->stream->last_pipe->continuous_frames[0];
6901 					ia_css_pipe_get_generic_stage_desc(&stage_desc,
6902 									   copy_binary,
6903 									   out_frames,
6904 									   in_frame,
6905 									   NULL);
6906 				}
6907 			} else {
6908 				ia_css_pipe_get_generic_stage_desc(&stage_desc,
6909 								   copy_binary,
6910 								   out_frames,
6911 								   NULL, NULL);
6912 			}
6913 		} else {
6914 			ia_css_pipe_util_set_output_frames(out_frames, 0,
6915 							   in_frame);
6916 			ia_css_pipe_get_generic_stage_desc(&stage_desc,
6917 							   copy_binary,
6918 							   out_frames,
6919 							   NULL, NULL);
6920 		}
6921 
6922 		err = ia_css_pipeline_create_and_add_stage(me,
6923 							   &stage_desc,
6924 							   &current_stage);
6925 		if (err) {
6926 			IA_CSS_LEAVE_ERR_PRIVATE(err);
6927 			return err;
6928 		}
6929 	} else if (pipe->stream->config.continuous) {
6930 		in_frame = pipe->stream->last_pipe->continuous_frames[0];
6931 	}
6932 
6933 	if (mode == IA_CSS_CAPTURE_MODE_PRIMARY) {
6934 		struct ia_css_frame *local_in_frame = NULL;
6935 		struct ia_css_frame *local_out_frame = NULL;
6936 
6937 		for (i = 0; i < num_primary_stage; i++) {
6938 			if (i == 0)
6939 				local_in_frame = in_frame;
6940 			else
6941 				local_in_frame = NULL;
6942 			if (!need_pp && (i == num_primary_stage - 1) && (!IS_ISP2401 || !need_ldc))
6943 				local_out_frame = out_frame;
6944 			else
6945 				local_out_frame = NULL;
6946 			ia_css_pipe_util_set_output_frames(out_frames, 0, local_out_frame);
6947 			/*
6948 			 * WARNING: The #if def flag has been added below as a
6949 			 * temporary solution to solve the problem of enabling the
6950 			 * view finder in a single binary in a capture flow. The
6951 			 * vf-pp stage has been removed from Skycam in the solution
6952 			 * provided. The vf-pp stage should be re-introduced when
6953 			 * required. This  * should not be considered as a clean solution.
6954 			 * Proper investigation should be done to come up with the clean
6955 			 * solution.
6956 			 */
6957 			ia_css_pipe_get_generic_stage_desc(&stage_desc,
6958 							   primary_binary[i],
6959 							   out_frames,
6960 							   local_in_frame,
6961 							   NULL);
6962 			err = ia_css_pipeline_create_and_add_stage(me,
6963 								   &stage_desc,
6964 								   &current_stage);
6965 			if (err) {
6966 				IA_CSS_LEAVE_ERR_PRIVATE(err);
6967 				return err;
6968 			}
6969 		}
6970 		/* If we use copy iso primary, the input must be yuv iso raw */
6971 		current_stage->args.copy_vf =
6972 		    primary_binary[0]->info->sp.pipeline.mode ==
6973 		    IA_CSS_BINARY_MODE_COPY;
6974 		current_stage->args.copy_output = current_stage->args.copy_vf;
6975 	} else if (mode == IA_CSS_CAPTURE_MODE_ADVANCED ||
6976 		    mode == IA_CSS_CAPTURE_MODE_LOW_LIGHT) {
6977 		ia_css_pipe_util_set_output_frames(out_frames, 0, NULL);
6978 		ia_css_pipe_get_generic_stage_desc(&stage_desc, pre_isp_binary,
6979 						   out_frames, in_frame, NULL);
6980 		err = ia_css_pipeline_create_and_add_stage(me, &stage_desc,
6981 							   NULL);
6982 		if (err) {
6983 			IA_CSS_LEAVE_ERR_PRIVATE(err);
6984 			return err;
6985 		}
6986 		ia_css_pipe_util_set_output_frames(out_frames, 0, NULL);
6987 		ia_css_pipe_get_generic_stage_desc(&stage_desc, anr_gdc_binary,
6988 						   out_frames, NULL, NULL);
6989 		err = ia_css_pipeline_create_and_add_stage(me, &stage_desc,
6990 							   NULL);
6991 		if (err) {
6992 			IA_CSS_LEAVE_ERR_PRIVATE(err);
6993 			return err;
6994 		}
6995 
6996 		if (need_pp) {
6997 			ia_css_pipe_util_set_output_frames(out_frames, 0, NULL);
6998 			ia_css_pipe_get_generic_stage_desc(&stage_desc,
6999 							   post_isp_binary,
7000 							   out_frames,
7001 							   NULL, NULL);
7002 		} else {
7003 			ia_css_pipe_util_set_output_frames(out_frames, 0,
7004 							   out_frame);
7005 			ia_css_pipe_get_generic_stage_desc(&stage_desc,
7006 							   post_isp_binary,
7007 							   out_frames,
7008 							   NULL, NULL);
7009 		}
7010 
7011 		err = ia_css_pipeline_create_and_add_stage(me, &stage_desc,
7012 							   &current_stage);
7013 		if (err) {
7014 			IA_CSS_LEAVE_ERR_PRIVATE(err);
7015 			return err;
7016 		}
7017 	} else if (mode == IA_CSS_CAPTURE_MODE_BAYER) {
7018 		ia_css_pipe_util_set_output_frames(out_frames, 0, out_frame);
7019 		ia_css_pipe_get_generic_stage_desc(&stage_desc, pre_isp_binary,
7020 						   out_frames, in_frame, NULL);
7021 		err = ia_css_pipeline_create_and_add_stage(me, &stage_desc,
7022 							   NULL);
7023 		if (err) {
7024 			IA_CSS_LEAVE_ERR_PRIVATE(err);
7025 			return err;
7026 		}
7027 	}
7028 
7029 	if (need_pp && current_stage) {
7030 		struct ia_css_frame *local_in_frame = NULL;
7031 
7032 		local_in_frame = current_stage->args.out_frame[0];
7033 
7034 		if (need_ldc) {
7035 			ia_css_pipe_util_set_output_frames(out_frames, 0, NULL);
7036 			ia_css_pipe_get_generic_stage_desc(&stage_desc,
7037 							   capture_ldc_binary,
7038 							   out_frames,
7039 							   local_in_frame,
7040 							   NULL);
7041 			err = ia_css_pipeline_create_and_add_stage(me,
7042 								   &stage_desc,
7043 								   &current_stage);
7044 			local_in_frame = current_stage->args.out_frame[0];
7045 		}
7046 		err = add_capture_pp_stage(pipe, me, local_in_frame,
7047 					   need_yuv_pp ? NULL : out_frame,
7048 					   capture_pp_binary,
7049 					   &current_stage);
7050 		if (err) {
7051 			IA_CSS_LEAVE_ERR_PRIVATE(err);
7052 			return err;
7053 		}
7054 	}
7055 
7056 	if (need_yuv_pp && current_stage) {
7057 		struct ia_css_frame *tmp_in_frame = current_stage->args.out_frame[0];
7058 		struct ia_css_frame *tmp_out_frame = NULL;
7059 
7060 		for (i = 0; i < num_yuv_scaler; i++) {
7061 			if (is_output_stage[i])
7062 				tmp_out_frame = out_frame;
7063 			else
7064 				tmp_out_frame = NULL;
7065 
7066 			err = add_yuv_scaler_stage(pipe, me, tmp_in_frame,
7067 						   tmp_out_frame, NULL,
7068 						   &yuv_scaler_binary[i],
7069 						   &yuv_scaler_stage);
7070 			if (err) {
7071 				IA_CSS_LEAVE_ERR_PRIVATE(err);
7072 				return err;
7073 			}
7074 			/* we use output port 1 as internal output port */
7075 			tmp_in_frame = yuv_scaler_stage->args.out_frame[1];
7076 		}
7077 	}
7078 
7079 	/*
7080 	 * WARNING: The #if def flag has been added below as a
7081 	 * temporary solution to solve the problem of enabling the
7082 	 * view finder in a single binary in a capture flow. The vf-pp
7083 	 * stage has been removed from Skycam in the solution provided.
7084 	 * The vf-pp stage should be re-introduced when required. This
7085 	 * should not be considered as a clean solution. Proper
7086 	 * investigation should be done to come up with the clean solution.
7087 	 */
7088 	if (mode != IA_CSS_CAPTURE_MODE_RAW &&
7089 	    mode != IA_CSS_CAPTURE_MODE_BAYER &&
7090 	    current_stage && vf_frame) {
7091 		in_frame = current_stage->args.out_vf_frame;
7092 		err = add_vf_pp_stage(pipe, in_frame, vf_frame, vf_pp_binary,
7093 				      &current_stage);
7094 		if (err) {
7095 			IA_CSS_LEAVE_ERR_PRIVATE(err);
7096 			return err;
7097 		}
7098 	}
7099 	ia_css_pipeline_finalize_stages(&pipe->pipeline, pipe->stream->config.continuous);
7100 
7101 	ia_css_debug_dtrace(IA_CSS_DEBUG_TRACE_PRIVATE,
7102 			    "create_host_regular_capture_pipeline() leave:\n");
7103 
7104 	return 0;
7105 }
7106 
7107 static int
create_host_capture_pipeline(struct ia_css_pipe * pipe)7108 create_host_capture_pipeline(struct ia_css_pipe *pipe)
7109 {
7110 	int err = 0;
7111 
7112 	IA_CSS_ENTER_PRIVATE("pipe = %p", pipe);
7113 
7114 	if (pipe->config.mode == IA_CSS_PIPE_MODE_COPY)
7115 		err = create_host_isyscopy_capture_pipeline(pipe);
7116 	else
7117 		err = create_host_regular_capture_pipeline(pipe);
7118 	if (err) {
7119 		IA_CSS_LEAVE_ERR_PRIVATE(err);
7120 		return err;
7121 	}
7122 
7123 	IA_CSS_LEAVE_ERR_PRIVATE(err);
7124 
7125 	return err;
7126 }
7127 
capture_start(struct ia_css_pipe * pipe)7128 static int capture_start(struct ia_css_pipe *pipe)
7129 {
7130 	struct ia_css_pipeline *me;
7131 	unsigned int thread_id;
7132 
7133 	int err = 0;
7134 	enum sh_css_pipe_config_override copy_ovrd;
7135 
7136 	IA_CSS_ENTER_PRIVATE("pipe = %p", pipe);
7137 	if (!pipe) {
7138 		IA_CSS_LEAVE_ERR_PRIVATE(-EINVAL);
7139 		return -EINVAL;
7140 	}
7141 
7142 	me = &pipe->pipeline;
7143 
7144 	if ((pipe->config.default_capture_config.mode == IA_CSS_CAPTURE_MODE_RAW   ||
7145 	     pipe->config.default_capture_config.mode == IA_CSS_CAPTURE_MODE_BAYER) &&
7146 	    (pipe->config.mode != IA_CSS_PIPE_MODE_COPY)) {
7147 		if (copy_on_sp(pipe)) {
7148 			err = start_copy_on_sp(pipe, &me->out_frame[0]);
7149 			IA_CSS_LEAVE_ERR_PRIVATE(err);
7150 			return err;
7151 		}
7152 	}
7153 	/* old isys: need to send_mipi_frames() in all pipe modes */
7154 	if (!IS_ISP2401 || pipe->config.mode != IA_CSS_PIPE_MODE_COPY) {
7155 		err = send_mipi_frames(pipe);
7156 		if (err) {
7157 			IA_CSS_LEAVE_ERR_PRIVATE(err);
7158 			return err;
7159 		}
7160 	}
7161 
7162 	ia_css_pipeline_get_sp_thread_id(ia_css_pipe_get_pipe_num(pipe), &thread_id);
7163 	copy_ovrd = 1 << thread_id;
7164 
7165 	start_pipe(pipe, copy_ovrd, pipe->stream->config.mode);
7166 
7167 	/*
7168 	 * old isys: for IA_CSS_PIPE_MODE_COPY pipe, isys rx has to be configured,
7169 	 * which is currently done in start_binary(); but COPY pipe contains no binary,
7170 	 * and does not call start_binary(); so we need to configure the rx here.
7171 	 */
7172 	if (!IS_ISP2401 &&
7173 	    pipe->config.mode == IA_CSS_PIPE_MODE_COPY &&
7174 	    pipe->stream->reconfigure_css_rx) {
7175 		ia_css_isys_rx_configure(&pipe->stream->csi_rx_config,
7176 					 pipe->stream->config.mode);
7177 		pipe->stream->reconfigure_css_rx = false;
7178 	}
7179 
7180 	IA_CSS_LEAVE_ERR_PRIVATE(err);
7181 	return err;
7182 }
7183 
7184 static int
sh_css_pipe_get_output_frame_info(struct ia_css_pipe * pipe,struct ia_css_frame_info * info,unsigned int idx)7185 sh_css_pipe_get_output_frame_info(struct ia_css_pipe *pipe,
7186 				  struct ia_css_frame_info *info,
7187 				  unsigned int idx)
7188 {
7189 	assert(pipe);
7190 	assert(info);
7191 
7192 	ia_css_debug_dtrace(IA_CSS_DEBUG_TRACE_PRIVATE,
7193 			    "sh_css_pipe_get_output_frame_info() enter:\n");
7194 
7195 	*info = pipe->output_info[idx];
7196 	if (copy_on_sp(pipe) &&
7197 	    pipe->stream->config.input_config.format == ATOMISP_INPUT_FORMAT_BINARY_8) {
7198 		ia_css_frame_info_init(
7199 		    info,
7200 		    JPEG_BYTES,
7201 		    1,
7202 		    IA_CSS_FRAME_FORMAT_BINARY_8,
7203 		    0);
7204 	} else if (info->format == IA_CSS_FRAME_FORMAT_RAW ||
7205 		   info->format == IA_CSS_FRAME_FORMAT_RAW_PACKED) {
7206 		info->raw_bit_depth =
7207 		ia_css_pipe_util_pipe_input_format_bpp(pipe);
7208 	}
7209 
7210 	ia_css_debug_dtrace(IA_CSS_DEBUG_TRACE_PRIVATE,
7211 			    "sh_css_pipe_get_output_frame_info() leave:\n");
7212 	return 0;
7213 }
7214 
7215 void
ia_css_stream_send_input_frame(const struct ia_css_stream * stream,const unsigned short * data,unsigned int width,unsigned int height)7216 ia_css_stream_send_input_frame(const struct ia_css_stream *stream,
7217 			       const unsigned short *data,
7218 			       unsigned int width,
7219 			       unsigned int height)
7220 {
7221 	assert(stream);
7222 
7223 	ia_css_inputfifo_send_input_frame(
7224 	    data, width, height,
7225 	    stream->config.channel_id,
7226 	    stream->config.input_config.format,
7227 	    stream->config.pixels_per_clock == 2);
7228 }
7229 
7230 void
ia_css_stream_start_input_frame(const struct ia_css_stream * stream)7231 ia_css_stream_start_input_frame(const struct ia_css_stream *stream)
7232 {
7233 	assert(stream);
7234 
7235 	ia_css_inputfifo_start_frame(
7236 	    stream->config.channel_id,
7237 	    stream->config.input_config.format,
7238 	    stream->config.pixels_per_clock == 2);
7239 }
7240 
7241 void
ia_css_stream_send_input_line(const struct ia_css_stream * stream,const unsigned short * data,unsigned int width,const unsigned short * data2,unsigned int width2)7242 ia_css_stream_send_input_line(const struct ia_css_stream *stream,
7243 			      const unsigned short *data,
7244 			      unsigned int width,
7245 			      const unsigned short *data2,
7246 			      unsigned int width2)
7247 {
7248 	assert(stream);
7249 
7250 	ia_css_inputfifo_send_line(stream->config.channel_id,
7251 				   data, width, data2, width2);
7252 }
7253 
7254 void
ia_css_stream_send_input_embedded_line(const struct ia_css_stream * stream,enum atomisp_input_format format,const unsigned short * data,unsigned int width)7255 ia_css_stream_send_input_embedded_line(const struct ia_css_stream *stream,
7256 				       enum atomisp_input_format format,
7257 				       const unsigned short *data,
7258 				       unsigned int width)
7259 {
7260 	assert(stream);
7261 	if (!data || width == 0)
7262 		return;
7263 	ia_css_inputfifo_send_embedded_line(stream->config.channel_id,
7264 					    format, data, width);
7265 }
7266 
7267 void
ia_css_stream_end_input_frame(const struct ia_css_stream * stream)7268 ia_css_stream_end_input_frame(const struct ia_css_stream *stream)
7269 {
7270 	assert(stream);
7271 
7272 	ia_css_inputfifo_end_frame(stream->config.channel_id);
7273 }
7274 
7275 bool
ia_css_pipeline_uses_params(struct ia_css_pipeline * me)7276 ia_css_pipeline_uses_params(struct ia_css_pipeline *me)
7277 {
7278 	struct ia_css_pipeline_stage *stage;
7279 
7280 	assert(me);
7281 
7282 	ia_css_debug_dtrace(IA_CSS_DEBUG_TRACE,
7283 			    "ia_css_pipeline_uses_params() enter: me=%p\n", me);
7284 
7285 	for (stage = me->stages; stage; stage = stage->next)
7286 		if (stage->binary_info && stage->binary_info->enable.params) {
7287 			ia_css_debug_dtrace(IA_CSS_DEBUG_TRACE,
7288 					    "ia_css_pipeline_uses_params() leave: return_bool=true\n");
7289 			return true;
7290 		}
7291 	ia_css_debug_dtrace(IA_CSS_DEBUG_TRACE,
7292 			    "ia_css_pipeline_uses_params() leave: return_bool=false\n");
7293 	return false;
7294 }
7295 
7296 /*
7297  * @brief Tag a specific frame in continuous capture.
7298  * Refer to "sh_css_internal.h" for details.
7299  */
ia_css_stream_capture_frame(struct ia_css_stream * stream,unsigned int exp_id)7300 int ia_css_stream_capture_frame(struct ia_css_stream *stream,
7301 				unsigned int exp_id)
7302 {
7303 	struct sh_css_tag_descr tag_descr;
7304 	u32 encoded_tag_descr;
7305 	int err;
7306 
7307 	assert(stream);
7308 	IA_CSS_ENTER("exp_id=%d", exp_id);
7309 
7310 	/* Only continuous streams have a tagger */
7311 	if (exp_id == 0 || !stream->config.continuous) {
7312 		IA_CSS_LEAVE_ERR(-EINVAL);
7313 		return -EINVAL;
7314 	}
7315 
7316 	if (!sh_css_sp_is_running()) {
7317 		/* SP is not running. The queues are not valid */
7318 		IA_CSS_LEAVE_ERR(-EBUSY);
7319 		return -EBUSY;
7320 	}
7321 
7322 	/* Create the tag descriptor from the parameters */
7323 	sh_css_create_tag_descr(0, 0, 0, exp_id, &tag_descr);
7324 	/* Encode the tag descriptor into a 32-bit value */
7325 	encoded_tag_descr = sh_css_encode_tag_descr(&tag_descr);
7326 	/*
7327 	 * Enqueue the encoded tag to the host2sp queue.
7328 	 * Note: The pipe and stage IDs for tag_cmd queue are hard-coded to 0
7329 	 * on both host and the SP side.
7330 	 * It is mainly because it is enough to have only one tag_cmd queue
7331 	 */
7332 	err = ia_css_bufq_enqueue_tag_cmd(encoded_tag_descr);
7333 
7334 	IA_CSS_LEAVE_ERR(err);
7335 	return err;
7336 }
7337 
7338 /*
7339  * @brief Configure the continuous capture.
7340  * Refer to "sh_css_internal.h" for details.
7341  */
ia_css_stream_capture(struct ia_css_stream * stream,int num_captures,unsigned int skip,int offset)7342 int ia_css_stream_capture(struct ia_css_stream *stream, int num_captures,
7343 			  unsigned int skip, int offset)
7344 {
7345 	struct sh_css_tag_descr tag_descr;
7346 	unsigned int encoded_tag_descr;
7347 	int return_err;
7348 
7349 	if (!stream)
7350 		return -EINVAL;
7351 
7352 	ia_css_debug_dtrace(IA_CSS_DEBUG_TRACE,
7353 			    "ia_css_stream_capture() enter: num_captures=%d, skip=%d, offset=%d\n",
7354 			    num_captures, skip, offset);
7355 
7356 	/* Check if the tag descriptor is valid */
7357 	if (num_captures < SH_CSS_MINIMUM_TAG_ID) {
7358 		ia_css_debug_dtrace(IA_CSS_DEBUG_TRACE,
7359 				    "ia_css_stream_capture() leave: return_err=%d\n",
7360 				    -EINVAL);
7361 		return -EINVAL;
7362 	}
7363 
7364 	/* Create the tag descriptor from the parameters */
7365 	sh_css_create_tag_descr(num_captures, skip, offset, 0, &tag_descr);
7366 
7367 	/* Encode the tag descriptor into a 32-bit value */
7368 	encoded_tag_descr = sh_css_encode_tag_descr(&tag_descr);
7369 
7370 	if (!sh_css_sp_is_running()) {
7371 		/* SP is not running. The queues are not valid */
7372 		ia_css_debug_dtrace(IA_CSS_DEBUG_TRACE,
7373 				    "ia_css_stream_capture() leaving:queues unavailable\n");
7374 		return -EBUSY;
7375 	}
7376 
7377 	/*
7378 	 * Enqueue the encoded tag to the host2sp queue.
7379 	 * Note: The pipe and stage IDs for tag_cmd queue are hard-coded to 0
7380 	 * on both host and the SP side.
7381 	 * It is mainly because it is enough to have only one tag_cmd queue
7382 	 */
7383 	return_err = ia_css_bufq_enqueue_tag_cmd((uint32_t)encoded_tag_descr);
7384 
7385 	ia_css_debug_dtrace(IA_CSS_DEBUG_TRACE,
7386 			    "ia_css_stream_capture() leave: return_err=%d\n",
7387 			    return_err);
7388 
7389 	return return_err;
7390 }
7391 
7392 static void
sh_css_init_host_sp_control_vars(void)7393 sh_css_init_host_sp_control_vars(void)
7394 {
7395 	const struct ia_css_fw_info *fw;
7396 	unsigned int HIVE_ADDR_ia_css_ispctrl_sp_isp_started;
7397 
7398 	unsigned int HIVE_ADDR_host_sp_queues_initialized;
7399 	unsigned int HIVE_ADDR_sp_sleep_mode;
7400 	unsigned int HIVE_ADDR_ia_css_dmaproxy_sp_invalidate_tlb;
7401 	unsigned int HIVE_ADDR_sp_stop_copy_preview;
7402 	unsigned int HIVE_ADDR_host_sp_com;
7403 	unsigned int o = offsetof(struct host_sp_communication, host2sp_command)
7404 			    / sizeof(int);
7405 
7406 	unsigned int i;
7407 
7408 	ia_css_debug_dtrace(IA_CSS_DEBUG_TRACE_PRIVATE,
7409 			    "sh_css_init_host_sp_control_vars() enter: void\n");
7410 
7411 	fw = &sh_css_sp_fw;
7412 	HIVE_ADDR_ia_css_ispctrl_sp_isp_started = fw->info.sp.isp_started;
7413 
7414 	HIVE_ADDR_host_sp_queues_initialized =
7415 	    fw->info.sp.host_sp_queues_initialized;
7416 	HIVE_ADDR_sp_sleep_mode = fw->info.sp.sleep_mode;
7417 	HIVE_ADDR_ia_css_dmaproxy_sp_invalidate_tlb = fw->info.sp.invalidate_tlb;
7418 	HIVE_ADDR_sp_stop_copy_preview = fw->info.sp.stop_copy_preview;
7419 	HIVE_ADDR_host_sp_com = fw->info.sp.host_sp_com;
7420 
7421 	sp_dmem_store_uint32(SP0_ID,
7422 			     (unsigned int)sp_address_of(ia_css_ispctrl_sp_isp_started),
7423 			     (uint32_t)(0));
7424 
7425 	sp_dmem_store_uint32(SP0_ID,
7426 			     (unsigned int)sp_address_of(host_sp_queues_initialized),
7427 			     (uint32_t)(0));
7428 	sp_dmem_store_uint32(SP0_ID,
7429 			     (unsigned int)sp_address_of(sp_sleep_mode),
7430 			     (uint32_t)(0));
7431 	sp_dmem_store_uint32(SP0_ID,
7432 			     (unsigned int)sp_address_of(ia_css_dmaproxy_sp_invalidate_tlb),
7433 			     (uint32_t)(false));
7434 	sp_dmem_store_uint32(SP0_ID,
7435 			     (unsigned int)sp_address_of(sp_stop_copy_preview),
7436 			     my_css.stop_copy_preview ? (uint32_t)(1) : (uint32_t)(0));
7437 	store_sp_array_uint(host_sp_com, o, host2sp_cmd_ready);
7438 
7439 	for (i = 0; i < N_CSI_PORTS; i++) {
7440 		sh_css_update_host2sp_num_mipi_frames
7441 		(my_css.num_mipi_frames[i]);
7442 	}
7443 
7444 	ia_css_debug_dtrace(IA_CSS_DEBUG_TRACE_PRIVATE,
7445 			    "sh_css_init_host_sp_control_vars() leave: return_void\n");
7446 }
7447 
7448 /*
7449  * create the internal structures and fill in the configuration data
7450  */
7451 
7452 static const struct
7453 ia_css_pipe_config ia_css_pipe_default_config = DEFAULT_PIPE_CONFIG;
7454 
ia_css_pipe_config_defaults(struct ia_css_pipe_config * pipe_config)7455 void ia_css_pipe_config_defaults(struct ia_css_pipe_config *pipe_config)
7456 {
7457 	ia_css_debug_dtrace(IA_CSS_DEBUG_TRACE, "ia_css_pipe_config_defaults()\n");
7458 	memcpy(pipe_config, &ia_css_pipe_default_config, sizeof(*pipe_config));
7459 }
7460 
7461 void
ia_css_pipe_extra_config_defaults(struct ia_css_pipe_extra_config * extra_config)7462 ia_css_pipe_extra_config_defaults(struct ia_css_pipe_extra_config *extra_config)
7463 {
7464 	if (!extra_config) {
7465 		IA_CSS_ERROR("NULL input parameter");
7466 		return;
7467 	}
7468 
7469 	extra_config->enable_raw_binning = false;
7470 	extra_config->enable_yuv_ds = false;
7471 	extra_config->enable_high_speed = false;
7472 	extra_config->enable_dvs_6axis = false;
7473 	extra_config->enable_reduced_pipe = false;
7474 	extra_config->disable_vf_pp = false;
7475 	extra_config->enable_fractional_ds = false;
7476 }
7477 
ia_css_stream_config_defaults(struct ia_css_stream_config * stream_config)7478 void ia_css_stream_config_defaults(struct ia_css_stream_config *stream_config)
7479 {
7480 	ia_css_debug_dtrace(IA_CSS_DEBUG_TRACE, "ia_css_stream_config_defaults()\n");
7481 	assert(stream_config);
7482 	memset(stream_config, 0, sizeof(*stream_config));
7483 	stream_config->online = true;
7484 	stream_config->left_padding = -1;
7485 	stream_config->pixels_per_clock = 1;
7486 	/*
7487 	 * temporary default value for backwards compatibility.
7488 	 * This field used to be hardcoded within CSS but this has now
7489 	 * been moved to the stream_config struct.
7490 	 */
7491 	stream_config->source.port.rxcount = 0x04040404;
7492 }
7493 
ia_css_pipe_create(const struct ia_css_pipe_config * config,struct ia_css_pipe ** pipe)7494 int ia_css_pipe_create(const struct ia_css_pipe_config *config,
7495 		       struct ia_css_pipe **pipe)
7496 {
7497 	int err = 0;
7498 
7499 	IA_CSS_ENTER_PRIVATE("config = %p, pipe = %p", config, pipe);
7500 
7501 	if (!config || !pipe) {
7502 		IA_CSS_LEAVE_ERR_PRIVATE(-EINVAL);
7503 		return -EINVAL;
7504 	}
7505 
7506 	err = ia_css_pipe_create_extra(config, NULL, pipe);
7507 
7508 	if (err == 0)
7509 		IA_CSS_LOG("pipe created successfully = %p", *pipe);
7510 
7511 	IA_CSS_LEAVE_ERR_PRIVATE(err);
7512 
7513 	return err;
7514 }
7515 
7516 int
ia_css_pipe_create_extra(const struct ia_css_pipe_config * config,const struct ia_css_pipe_extra_config * extra_config,struct ia_css_pipe ** pipe)7517 ia_css_pipe_create_extra(const struct ia_css_pipe_config *config,
7518 			 const struct ia_css_pipe_extra_config *extra_config,
7519 			 struct ia_css_pipe **pipe)
7520 {
7521 	int err = -EINVAL;
7522 	struct ia_css_pipe *internal_pipe = NULL;
7523 	unsigned int i;
7524 
7525 	IA_CSS_ENTER_PRIVATE("config = %p, extra_config = %p and pipe = %p", config, extra_config, pipe);
7526 
7527 	/* do not allow to create more than the maximum limit */
7528 	if (my_css.pipe_counter >= IA_CSS_PIPELINE_NUM_MAX) {
7529 		IA_CSS_LEAVE_ERR_PRIVATE(-ENOSPC);
7530 		return -EINVAL;
7531 	}
7532 
7533 	if ((!pipe) || (!config)) {
7534 		IA_CSS_LEAVE_ERR_PRIVATE(-EINVAL);
7535 		return -EINVAL;
7536 	}
7537 
7538 	ia_css_debug_dump_pipe_config(config);
7539 	ia_css_debug_dump_pipe_extra_config(extra_config);
7540 
7541 	err = create_pipe(config->mode, &internal_pipe, false);
7542 	if (err) {
7543 		IA_CSS_LEAVE_ERR_PRIVATE(err);
7544 		return err;
7545 	}
7546 
7547 	/* now we have a pipe structure to fill */
7548 	internal_pipe->config = *config;
7549 	if (extra_config)
7550 		internal_pipe->extra_config = *extra_config;
7551 	else
7552 		ia_css_pipe_extra_config_defaults(&internal_pipe->extra_config);
7553 
7554 	/*
7555 	 * Use config value when dvs_frame_delay setting equal to 2,
7556 	 * otherwise always 1 by default
7557 	 */
7558 	if (internal_pipe->config.dvs_frame_delay == IA_CSS_FRAME_DELAY_2)
7559 		internal_pipe->dvs_frame_delay = 2;
7560 	else
7561 		internal_pipe->dvs_frame_delay = 1;
7562 
7563 	/*
7564 	 * we still keep enable_raw_binning for backward compatibility,
7565 	 * for any new fractional bayer downscaling, we should use
7566 	 * bayer_ds_out_res. if both are specified, bayer_ds_out_res will
7567 	 * take precedence.if none is specified, we set bayer_ds_out_res
7568 	 * equal to IF output resolution(IF may do cropping on sensor output)
7569 	 * or use default decimation factor 1.
7570 	 */
7571 
7572 	/* YUV downscaling */
7573 	if ((internal_pipe->config.vf_pp_in_res.width ||
7574 	     internal_pipe->config.capt_pp_in_res.width)) {
7575 		enum ia_css_frame_format format;
7576 
7577 		if (internal_pipe->config.vf_pp_in_res.width) {
7578 			format = IA_CSS_FRAME_FORMAT_YUV_LINE;
7579 			ia_css_frame_info_init(
7580 			    &internal_pipe->vf_yuv_ds_input_info,
7581 			    internal_pipe->config.vf_pp_in_res.width,
7582 			    internal_pipe->config.vf_pp_in_res.height,
7583 			    format, 0);
7584 		}
7585 		if (internal_pipe->config.capt_pp_in_res.width) {
7586 			format = IA_CSS_FRAME_FORMAT_YUV420;
7587 			ia_css_frame_info_init(
7588 			    &internal_pipe->out_yuv_ds_input_info,
7589 			    internal_pipe->config.capt_pp_in_res.width,
7590 			    internal_pipe->config.capt_pp_in_res.height,
7591 			    format, 0);
7592 		}
7593 	}
7594 	if (internal_pipe->config.vf_pp_in_res.width &&
7595 	    internal_pipe->config.mode == IA_CSS_PIPE_MODE_PREVIEW) {
7596 		ia_css_frame_info_init(
7597 		    &internal_pipe->vf_yuv_ds_input_info,
7598 		    internal_pipe->config.vf_pp_in_res.width,
7599 		    internal_pipe->config.vf_pp_in_res.height,
7600 		    IA_CSS_FRAME_FORMAT_YUV_LINE, 0);
7601 	}
7602 	/* handle bayer downscaling output info */
7603 	if (internal_pipe->config.bayer_ds_out_res.width) {
7604 		ia_css_frame_info_init(
7605 		    &internal_pipe->bds_output_info,
7606 		    internal_pipe->config.bayer_ds_out_res.width,
7607 		    internal_pipe->config.bayer_ds_out_res.height,
7608 		    IA_CSS_FRAME_FORMAT_RAW, 0);
7609 	}
7610 
7611 	/* handle output info, assume always needed */
7612 	for (i = 0; i < IA_CSS_PIPE_MAX_OUTPUT_STAGE; i++) {
7613 		if (internal_pipe->config.output_info[i].res.width) {
7614 			err = sh_css_pipe_configure_output(
7615 				    internal_pipe,
7616 				    internal_pipe->config.output_info[i].res.width,
7617 				    internal_pipe->config.output_info[i].res.height,
7618 				    internal_pipe->config.output_info[i].padded_width,
7619 				    internal_pipe->config.output_info[i].format,
7620 				    i);
7621 			if (err) {
7622 				IA_CSS_LEAVE_ERR_PRIVATE(err);
7623 				kvfree(internal_pipe);
7624 				internal_pipe = NULL;
7625 				return err;
7626 			}
7627 		}
7628 
7629 		/* handle vf output info, when configured */
7630 		internal_pipe->enable_viewfinder[i] =
7631 		    (internal_pipe->config.vf_output_info[i].res.width != 0);
7632 		if (internal_pipe->config.vf_output_info[i].res.width) {
7633 			err = sh_css_pipe_configure_viewfinder(
7634 				    internal_pipe,
7635 				    internal_pipe->config.vf_output_info[i].res.width,
7636 				    internal_pipe->config.vf_output_info[i].res.height,
7637 				    internal_pipe->config.vf_output_info[i].padded_width,
7638 				    internal_pipe->config.vf_output_info[i].format,
7639 				    i);
7640 			if (err) {
7641 				IA_CSS_LEAVE_ERR_PRIVATE(err);
7642 				kvfree(internal_pipe);
7643 				internal_pipe = NULL;
7644 				return err;
7645 			}
7646 		}
7647 	}
7648 	/* set all info to zeroes first */
7649 	memset(&internal_pipe->info, 0, sizeof(internal_pipe->info));
7650 
7651 	/* all went well, return the pipe */
7652 	*pipe = internal_pipe;
7653 	IA_CSS_LEAVE_ERR_PRIVATE(0);
7654 	return 0;
7655 }
7656 
7657 int
ia_css_pipe_get_info(const struct ia_css_pipe * pipe,struct ia_css_pipe_info * pipe_info)7658 ia_css_pipe_get_info(const struct ia_css_pipe *pipe,
7659 		     struct ia_css_pipe_info *pipe_info)
7660 {
7661 	ia_css_debug_dtrace(IA_CSS_DEBUG_TRACE,
7662 			    "ia_css_pipe_get_info()\n");
7663 	if (!pipe_info) {
7664 		ia_css_debug_dtrace(IA_CSS_DEBUG_ERROR,
7665 				    "ia_css_pipe_get_info: pipe_info cannot be NULL\n");
7666 		return -EINVAL;
7667 	}
7668 	if (!pipe || !pipe->stream) {
7669 		ia_css_debug_dtrace(IA_CSS_DEBUG_ERROR,
7670 				    "ia_css_pipe_get_info: ia_css_stream_create needs to be called before ia_css_[stream/pipe]_get_info\n");
7671 		return -EINVAL;
7672 	}
7673 	/* we succeeded return the info */
7674 	*pipe_info = pipe->info;
7675 	ia_css_debug_dtrace(IA_CSS_DEBUG_TRACE, "ia_css_pipe_get_info() leave\n");
7676 	return 0;
7677 }
7678 
ia_css_pipe_has_dvs_stats(struct ia_css_pipe_info * pipe_info)7679 bool ia_css_pipe_has_dvs_stats(struct ia_css_pipe_info *pipe_info)
7680 {
7681 	unsigned int i;
7682 
7683 	if (pipe_info) {
7684 		for (i = 0; i < IA_CSS_DVS_STAT_NUM_OF_LEVELS; i++) {
7685 			if (pipe_info->grid_info.dvs_grid.dvs_stat_grid_info.grd_cfg[i].grd_start.enable)
7686 				return true;
7687 		}
7688 	}
7689 
7690 	return false;
7691 }
7692 
7693 int
ia_css_pipe_override_frame_format(struct ia_css_pipe * pipe,int pin_index,enum ia_css_frame_format new_format)7694 ia_css_pipe_override_frame_format(struct ia_css_pipe *pipe,
7695 				  int pin_index,
7696 				  enum ia_css_frame_format new_format)
7697 {
7698 	int err = 0;
7699 
7700 	IA_CSS_ENTER_PRIVATE("pipe = %p, pin_index = %d, new_formats = %d", pipe, pin_index, new_format);
7701 
7702 	if (!pipe) {
7703 		IA_CSS_ERROR("pipe is not set");
7704 		err = -EINVAL;
7705 		IA_CSS_LEAVE_ERR_PRIVATE(err);
7706 		return err;
7707 	}
7708 	if (0 != pin_index && 1 != pin_index) {
7709 		IA_CSS_ERROR("pin index is not valid");
7710 		err = -EINVAL;
7711 		IA_CSS_LEAVE_ERR_PRIVATE(err);
7712 		return err;
7713 	}
7714 	if (new_format != IA_CSS_FRAME_FORMAT_NV12_TILEY) {
7715 		IA_CSS_ERROR("new format is not valid");
7716 		err = -EINVAL;
7717 		IA_CSS_LEAVE_ERR_PRIVATE(err);
7718 		return err;
7719 	} else {
7720 		err = ia_css_pipe_check_format(pipe, new_format);
7721 		if (!err) {
7722 			if (pin_index == 0)
7723 				pipe->output_info[0].format = new_format;
7724 			else
7725 				pipe->vf_output_info[0].format = new_format;
7726 		}
7727 	}
7728 	IA_CSS_LEAVE_ERR_PRIVATE(err);
7729 	return err;
7730 }
7731 
7732 /* Configuration of INPUT_SYSTEM_VERSION_2401 is done on SP */
7733 static int
ia_css_stream_configure_rx(struct ia_css_stream * stream)7734 ia_css_stream_configure_rx(struct ia_css_stream *stream)
7735 {
7736 	struct ia_css_input_port *config;
7737 
7738 	assert(stream);
7739 
7740 	config = &stream->config.source.port;
7741 	/* AM: this code is not reliable, especially for 2400 */
7742 	if (config->num_lanes == 1)
7743 		stream->csi_rx_config.mode = MONO_1L_1L_0L;
7744 	else if (config->num_lanes == 2)
7745 		stream->csi_rx_config.mode = MONO_2L_1L_0L;
7746 	else if (config->num_lanes == 3)
7747 		stream->csi_rx_config.mode = MONO_3L_1L_0L;
7748 	else if (config->num_lanes == 4)
7749 		stream->csi_rx_config.mode = MONO_4L_1L_0L;
7750 	else if (config->num_lanes != 0)
7751 		return -EINVAL;
7752 
7753 	if (config->port > MIPI_PORT2_ID)
7754 		return -EINVAL;
7755 	stream->csi_rx_config.port =
7756 	ia_css_isys_port_to_mipi_port(config->port);
7757 	stream->csi_rx_config.timeout    = config->timeout;
7758 	stream->csi_rx_config.initcount  = 0;
7759 	stream->csi_rx_config.synccount  = 0x28282828;
7760 	stream->csi_rx_config.rxcount    = config->rxcount;
7761 	if (config->compression.type == IA_CSS_CSI2_COMPRESSION_TYPE_NONE)
7762 		stream->csi_rx_config.comp = MIPI_PREDICTOR_NONE;
7763 	else
7764 		/*
7765 		 * not implemented yet, requires extension of the rx_cfg_t
7766 		 * struct
7767 		 */
7768 		return -EINVAL;
7769 
7770 	stream->csi_rx_config.is_two_ppc = (stream->config.pixels_per_clock == 2);
7771 	stream->reconfigure_css_rx = true;
7772 	return 0;
7773 }
7774 
7775 static struct ia_css_pipe *
find_pipe(struct ia_css_pipe * pipes[],unsigned int num_pipes,enum ia_css_pipe_mode mode,bool copy_pipe)7776 find_pipe(struct ia_css_pipe *pipes[], unsigned int num_pipes,
7777 	  enum ia_css_pipe_mode mode, bool copy_pipe)
7778 {
7779 	unsigned int i;
7780 
7781 	assert(pipes);
7782 	for (i = 0; i < num_pipes; i++) {
7783 		assert(pipes[i]);
7784 		if (pipes[i]->config.mode != mode)
7785 			continue;
7786 		if (copy_pipe && pipes[i]->mode != IA_CSS_PIPE_ID_COPY)
7787 			continue;
7788 		return pipes[i];
7789 	}
7790 	return NULL;
7791 }
7792 
7793 static int
metadata_info_init(const struct ia_css_metadata_config * mdc,struct ia_css_metadata_info * md)7794 metadata_info_init(const struct ia_css_metadata_config *mdc,
7795 		   struct ia_css_metadata_info *md)
7796 {
7797 	/* Either both width and height should be set or neither */
7798 	if ((mdc->resolution.height > 0) ^ (mdc->resolution.width > 0))
7799 		return -EINVAL;
7800 
7801 	md->resolution = mdc->resolution;
7802 	/*
7803 	 * We round up the stride to a multiple of the width
7804 	 * of the port going to DDR, this is a HW requirements (DMA).
7805 	 */
7806 	md->stride = CEIL_MUL(mdc->resolution.width, HIVE_ISP_DDR_WORD_BYTES);
7807 	md->size = mdc->resolution.height * md->stride;
7808 	return 0;
7809 }
7810 
7811 int
ia_css_stream_create(const struct ia_css_stream_config * stream_config,int num_pipes,struct ia_css_pipe * pipes[],struct ia_css_stream ** stream)7812 ia_css_stream_create(const struct ia_css_stream_config *stream_config,
7813 		     int num_pipes,
7814 		     struct ia_css_pipe *pipes[],
7815 		     struct ia_css_stream **stream)
7816 {
7817 	struct ia_css_pipe *curr_pipe;
7818 	struct ia_css_stream *curr_stream = NULL;
7819 	bool spcopyonly;
7820 	bool sensor_binning_changed;
7821 	int i, j;
7822 	int err = -EINVAL;
7823 	struct ia_css_metadata_info md_info;
7824 	struct ia_css_resolution effective_res;
7825 
7826 	IA_CSS_ENTER("num_pipes=%d", num_pipes);
7827 	ia_css_debug_dump_stream_config(stream_config, num_pipes);
7828 
7829 	/* some checks */
7830 	if (num_pipes == 0 ||
7831 	    !stream ||
7832 	    !pipes) {
7833 		err = -EINVAL;
7834 		IA_CSS_LEAVE_ERR(err);
7835 		return err;
7836 	}
7837 
7838 	if (!IS_ISP2401) {
7839 		/* We don't support metadata for JPEG stream, since they both use str2mem */
7840 		if (stream_config->input_config.format == ATOMISP_INPUT_FORMAT_BINARY_8 &&
7841 		    stream_config->metadata_config.resolution.height > 0) {
7842 			err = -EINVAL;
7843 			IA_CSS_LEAVE_ERR(err);
7844 			return err;
7845 		}
7846 	} else {
7847 		if (stream_config->online && stream_config->pack_raw_pixels) {
7848 			IA_CSS_LOG("online and pack raw is invalid on input system 2401");
7849 			err = -EINVAL;
7850 			IA_CSS_LEAVE_ERR(err);
7851 			return err;
7852 		}
7853 	}
7854 
7855 	ia_css_debug_pipe_graph_dump_stream_config(stream_config);
7856 
7857 	/* check if mipi size specified */
7858 	if (stream_config->mode == IA_CSS_INPUT_MODE_BUFFERED_SENSOR)
7859 		if (!IS_ISP2401 || !stream_config->online)
7860 		{
7861 			unsigned int port = (unsigned int)stream_config->source.port.port;
7862 
7863 			if (port >= N_MIPI_PORT_ID) {
7864 				err = -EINVAL;
7865 				IA_CSS_LEAVE_ERR(err);
7866 				return err;
7867 			}
7868 
7869 			if (my_css.size_mem_words != 0) {
7870 				my_css.mipi_frame_size[port] = my_css.size_mem_words;
7871 			} else if (stream_config->mipi_buffer_config.size_mem_words != 0) {
7872 				my_css.mipi_frame_size[port] = stream_config->mipi_buffer_config.size_mem_words;
7873 			} else {
7874 				ia_css_debug_dtrace(IA_CSS_DEBUG_TRACE,
7875 						    "ia_css_stream_create() exit: error, need to set mipi frame size.\n");
7876 				assert(stream_config->mipi_buffer_config.size_mem_words != 0);
7877 				err = -EINVAL;
7878 				IA_CSS_LEAVE_ERR(err);
7879 				return err;
7880 			}
7881 
7882 			if (my_css.size_mem_words != 0) {
7883 				my_css.num_mipi_frames[port] =
7884 				    2; /* Temp change: Default for backwards compatibility. */
7885 			} else if (stream_config->mipi_buffer_config.nof_mipi_buffers != 0) {
7886 				my_css.num_mipi_frames[port] =
7887 				    stream_config->mipi_buffer_config.nof_mipi_buffers;
7888 			} else {
7889 				ia_css_debug_dtrace(IA_CSS_DEBUG_TRACE,
7890 						    "ia_css_stream_create() exit: error, need to set number of mipi frames.\n");
7891 				assert(stream_config->mipi_buffer_config.nof_mipi_buffers != 0);
7892 				err = -EINVAL;
7893 				IA_CSS_LEAVE_ERR(err);
7894 				return err;
7895 			}
7896 		}
7897 
7898 	/* Currently we only supported metadata up to a certain size. */
7899 	err = metadata_info_init(&stream_config->metadata_config, &md_info);
7900 	if (err) {
7901 		IA_CSS_LEAVE_ERR(err);
7902 		return err;
7903 	}
7904 
7905 	/* allocate the stream instance */
7906 	curr_stream = kzalloc_obj(struct ia_css_stream);
7907 	if (!curr_stream) {
7908 		err = -ENOMEM;
7909 		IA_CSS_LEAVE_ERR(err);
7910 		return err;
7911 	}
7912 	/* default all to 0 */
7913 	curr_stream->info.metadata_info = md_info;
7914 
7915 	/* allocate pipes */
7916 	curr_stream->num_pipes = num_pipes;
7917 	curr_stream->pipes = kzalloc_objs(struct ia_css_pipe *, num_pipes);
7918 	if (!curr_stream->pipes) {
7919 		curr_stream->num_pipes = 0;
7920 		kfree(curr_stream);
7921 		curr_stream = NULL;
7922 		err = -ENOMEM;
7923 		IA_CSS_LEAVE_ERR(err);
7924 		return err;
7925 	}
7926 	/* store pipes */
7927 	spcopyonly = (num_pipes == 1) && (pipes[0]->config.mode == IA_CSS_PIPE_MODE_COPY);
7928 	for (i = 0; i < num_pipes; i++)
7929 		curr_stream->pipes[i] = pipes[i];
7930 	curr_stream->last_pipe = curr_stream->pipes[0];
7931 	/* take over stream config */
7932 	curr_stream->config = *stream_config;
7933 
7934 	if (IS_ISP2401) {
7935 		if (stream_config->mode == IA_CSS_INPUT_MODE_BUFFERED_SENSOR &&
7936 		    stream_config->online)
7937 			curr_stream->config.online = false;
7938 
7939 		if (curr_stream->config.online) {
7940 			curr_stream->config.source.port.num_lanes =
7941 			    stream_config->source.port.num_lanes;
7942 			curr_stream->config.mode =  IA_CSS_INPUT_MODE_BUFFERED_SENSOR;
7943 		}
7944 	}
7945 	/* in case driver doesn't configure init number of raw buffers, configure it here */
7946 	if (curr_stream->config.target_num_cont_raw_buf == 0)
7947 		curr_stream->config.target_num_cont_raw_buf = NUM_CONTINUOUS_FRAMES;
7948 	if (curr_stream->config.init_num_cont_raw_buf == 0)
7949 		curr_stream->config.init_num_cont_raw_buf = curr_stream->config.target_num_cont_raw_buf;
7950 
7951 	/* Enable locking & unlocking of buffers in RAW buffer pool */
7952 	if (curr_stream->config.ia_css_enable_raw_buffer_locking)
7953 		sh_css_sp_configure_enable_raw_pool_locking(
7954 		    curr_stream->config.lock_all);
7955 
7956 	/* copy mode specific stuff */
7957 	switch (curr_stream->config.mode) {
7958 	case IA_CSS_INPUT_MODE_SENSOR:
7959 	case IA_CSS_INPUT_MODE_BUFFERED_SENSOR:
7960 		if (!IS_ISP2401)
7961 			ia_css_stream_configure_rx(curr_stream);
7962 		break;
7963 	case IA_CSS_INPUT_MODE_PRBS:
7964 		if (!IS_ISP2401) {
7965 			IA_CSS_LOG("mode prbs");
7966 			sh_css_sp_configure_prbs(curr_stream->config.source.prbs.seed);
7967 		}
7968 		break;
7969 	case IA_CSS_INPUT_MODE_MEMORY:
7970 		IA_CSS_LOG("mode memory");
7971 		curr_stream->reconfigure_css_rx = false;
7972 		break;
7973 	default:
7974 		IA_CSS_LOG("mode sensor/default");
7975 	}
7976 
7977 	for (i = 0; i < num_pipes; i++) {
7978 		struct ia_css_resolution effective_res;
7979 
7980 		curr_pipe = pipes[i];
7981 		/* set current stream */
7982 		curr_pipe->stream = curr_stream;
7983 		/* take over effective info */
7984 
7985 		effective_res = curr_pipe->config.input_effective_res;
7986 		if (effective_res.height == 0 || effective_res.width == 0) {
7987 			effective_res = curr_pipe->stream->config.input_config.effective_res;
7988 
7989 			curr_pipe->config.input_effective_res = effective_res;
7990 		}
7991 		IA_CSS_LOG("effective_res=%dx%d",
7992 			   effective_res.width,
7993 			   effective_res.height);
7994 	}
7995 
7996 	err = ia_css_stream_isp_parameters_init(curr_stream);
7997 	if (err)
7998 		goto ERR;
7999 	IA_CSS_LOG("isp_params_configs: %p", curr_stream->isp_params_configs);
8000 
8001 	/* sensor binning */
8002 	if (!spcopyonly) {
8003 		sensor_binning_changed =
8004 		    sh_css_params_set_binning_factor(curr_stream,
8005 						     curr_stream->config.sensor_binning_factor);
8006 	} else {
8007 		sensor_binning_changed = false;
8008 	}
8009 
8010 	IA_CSS_LOG("sensor_binning=%d, changed=%d",
8011 		   curr_stream->config.sensor_binning_factor, sensor_binning_changed);
8012 	/* loop over pipes */
8013 	IA_CSS_LOG("num_pipes=%d", num_pipes);
8014 	curr_stream->cont_capt = false;
8015 	/* Temporary hack: we give the preview pipe a reference to the capture
8016 	    * pipe in continuous capture mode. */
8017 	if (curr_stream->config.continuous) {
8018 		/* Search for the preview pipe and create the copy pipe */
8019 		struct ia_css_pipe *preview_pipe;
8020 		struct ia_css_pipe *video_pipe;
8021 		struct ia_css_pipe *capture_pipe = NULL;
8022 		struct ia_css_pipe *copy_pipe = NULL;
8023 
8024 		if (num_pipes >= 2) {
8025 			curr_stream->cont_capt = true;
8026 			curr_stream->disable_cont_vf = curr_stream->config.disable_cont_viewfinder;
8027 			curr_stream->stop_copy_preview = my_css.stop_copy_preview;
8028 		}
8029 
8030 		/* Create copy pipe here, since it may not be exposed to the driver */
8031 		preview_pipe = find_pipe(pipes, num_pipes,
8032 					 IA_CSS_PIPE_MODE_PREVIEW, false);
8033 		video_pipe = find_pipe(pipes, num_pipes,
8034 				       IA_CSS_PIPE_MODE_VIDEO, false);
8035 
8036 		if (curr_stream->cont_capt) {
8037 			capture_pipe = find_pipe(pipes, num_pipes,
8038 						 IA_CSS_PIPE_MODE_CAPTURE,
8039 						 false);
8040 			if (!capture_pipe) {
8041 				err = -EINVAL;
8042 				goto ERR;
8043 			}
8044 		}
8045 		/* We do not support preview and video pipe at the same time */
8046 		if (preview_pipe && video_pipe) {
8047 			err = -EINVAL;
8048 			goto ERR;
8049 		}
8050 
8051 		if (preview_pipe && !preview_pipe->pipe_settings.preview.copy_pipe) {
8052 			err = create_pipe(IA_CSS_PIPE_MODE_CAPTURE, &copy_pipe, true);
8053 			if (err)
8054 				goto ERR;
8055 			ia_css_pipe_config_defaults(&copy_pipe->config);
8056 			preview_pipe->pipe_settings.preview.copy_pipe = copy_pipe;
8057 			copy_pipe->stream = curr_stream;
8058 		}
8059 		if (preview_pipe && curr_stream->cont_capt)
8060 			preview_pipe->pipe_settings.preview.capture_pipe = capture_pipe;
8061 
8062 		if (video_pipe && !video_pipe->pipe_settings.video.copy_pipe) {
8063 			err = create_pipe(IA_CSS_PIPE_MODE_CAPTURE, &copy_pipe, true);
8064 			if (err)
8065 				goto ERR;
8066 			ia_css_pipe_config_defaults(&copy_pipe->config);
8067 			video_pipe->pipe_settings.video.copy_pipe = copy_pipe;
8068 			copy_pipe->stream = curr_stream;
8069 		}
8070 		if (video_pipe && curr_stream->cont_capt)
8071 			video_pipe->pipe_settings.video.capture_pipe = capture_pipe;
8072 	}
8073 	for (i = 0; i < num_pipes; i++) {
8074 		curr_pipe = pipes[i];
8075 		/* set current stream */
8076 		curr_pipe->stream = curr_stream;
8077 
8078 		/* take over effective info */
8079 
8080 		effective_res = curr_pipe->config.input_effective_res;
8081 		err = ia_css_util_check_res(
8082 			effective_res.width,
8083 			effective_res.height);
8084 		if (err)
8085 			goto ERR;
8086 
8087 		/* sensor binning per pipe */
8088 		if (sensor_binning_changed)
8089 			sh_css_pipe_free_shading_table(curr_pipe);
8090 	}
8091 
8092 	/* now pipes have been configured, info should be available */
8093 	for (i = 0; i < num_pipes; i++) {
8094 		struct ia_css_pipe_info *pipe_info = NULL;
8095 
8096 		curr_pipe = pipes[i];
8097 
8098 		err = sh_css_pipe_load_binaries(curr_pipe);
8099 		if (err)
8100 			goto ERR;
8101 
8102 		/* handle each pipe */
8103 		pipe_info = &curr_pipe->info;
8104 		for (j = 0; j < IA_CSS_PIPE_MAX_OUTPUT_STAGE; j++) {
8105 			err = sh_css_pipe_get_output_frame_info(curr_pipe,
8106 								&pipe_info->output_info[j], j);
8107 			if (err)
8108 				goto ERR;
8109 		}
8110 
8111 		if (!spcopyonly) {
8112 			if (!IS_ISP2401)
8113 				err = sh_css_pipe_get_shading_info(curr_pipe,
8114 								   &pipe_info->shading_info,
8115 								   NULL);
8116 			else
8117 				err = sh_css_pipe_get_shading_info(curr_pipe,
8118 								   &pipe_info->shading_info,
8119 								   &curr_pipe->config);
8120 
8121 			if (err)
8122 				goto ERR;
8123 			err = sh_css_pipe_get_grid_info(curr_pipe,
8124 							&pipe_info->grid_info);
8125 			if (err)
8126 				goto ERR;
8127 			for (j = 0; j < IA_CSS_PIPE_MAX_OUTPUT_STAGE; j++) {
8128 				sh_css_pipe_get_viewfinder_frame_info(curr_pipe,
8129 								      &pipe_info->vf_output_info[j],
8130 								      j);
8131 				if (err)
8132 					goto ERR;
8133 			}
8134 		}
8135 
8136 		my_css.active_pipes[ia_css_pipe_get_pipe_num(curr_pipe)] = curr_pipe;
8137 	}
8138 
8139 	curr_stream->started = false;
8140 
8141 	/* Map SP threads before doing anything. */
8142 	err = map_sp_threads(curr_stream, true);
8143 	if (err) {
8144 		IA_CSS_LOG("map_sp_threads: return_err=%d", err);
8145 		goto ERR;
8146 	}
8147 
8148 	for (i = 0; i < num_pipes; i++) {
8149 		curr_pipe = pipes[i];
8150 		ia_css_pipe_map_queue(curr_pipe, true);
8151 	}
8152 
8153 	/* Create host side pipeline objects without stages */
8154 	err = create_host_pipeline_structure(curr_stream);
8155 	if (err) {
8156 		IA_CSS_LOG("create_host_pipeline_structure: return_err=%d", err);
8157 		goto ERR;
8158 	}
8159 
8160 	/* assign curr_stream */
8161 	*stream = curr_stream;
8162 
8163 ERR:
8164 	if (!err) {
8165 		/* working mode: enter into the seed list */
8166 		if (my_css_save.mode == sh_css_mode_working) {
8167 			for (i = 0; i < MAX_ACTIVE_STREAMS; i++) {
8168 				if (!my_css_save.stream_seeds[i].stream) {
8169 					IA_CSS_LOG("entered stream into loc=%d", i);
8170 					my_css_save.stream_seeds[i].orig_stream = stream;
8171 					my_css_save.stream_seeds[i].stream = curr_stream;
8172 					my_css_save.stream_seeds[i].num_pipes = num_pipes;
8173 					my_css_save.stream_seeds[i].stream_config = *stream_config;
8174 					for (j = 0; j < num_pipes; j++) {
8175 						my_css_save.stream_seeds[i].pipe_config[j] = pipes[j]->config;
8176 						my_css_save.stream_seeds[i].pipes[j] = pipes[j];
8177 						my_css_save.stream_seeds[i].orig_pipes[j] = &pipes[j];
8178 					}
8179 					break;
8180 				}
8181 			}
8182 		} else {
8183 			ia_css_stream_destroy(curr_stream);
8184 		}
8185 	} else {
8186 		ia_css_stream_destroy(curr_stream);
8187 	}
8188 	IA_CSS_LEAVE("return_err=%d mode=%d", err, my_css_save.mode);
8189 	return err;
8190 }
8191 
8192 int
ia_css_stream_destroy(struct ia_css_stream * stream)8193 ia_css_stream_destroy(struct ia_css_stream *stream)
8194 {
8195 	int i;
8196 	int err = 0;
8197 
8198 	IA_CSS_ENTER_PRIVATE("stream = %p", stream);
8199 	if (!stream) {
8200 		err = -EINVAL;
8201 		IA_CSS_LEAVE_ERR_PRIVATE(err);
8202 		return err;
8203 	}
8204 
8205 	ia_css_stream_isp_parameters_uninit(stream);
8206 
8207 	if ((stream->last_pipe) &&
8208 	    ia_css_pipeline_is_mapped(stream->last_pipe->pipe_num)) {
8209 		if (IS_ISP2401) {
8210 			for (i = 0; i < stream->num_pipes; i++) {
8211 				struct ia_css_pipe *entry = stream->pipes[i];
8212 				unsigned int sp_thread_id;
8213 				struct sh_css_sp_pipeline_terminal *sp_pipeline_input_terminal;
8214 
8215 				assert(entry);
8216 				if (entry) {
8217 					/* get the SP thread id */
8218 					if (!ia_css_pipeline_get_sp_thread_id(
8219 							ia_css_pipe_get_pipe_num(entry), &sp_thread_id))
8220 						return -EINVAL;
8221 
8222 					/* get the target input terminal */
8223 					sp_pipeline_input_terminal =
8224 						&sh_css_sp_group.pipe_io[sp_thread_id].input;
8225 
8226 					for (i = 0; i < IA_CSS_STREAM_MAX_ISYS_STREAM_PER_CH; i++) {
8227 						ia_css_isys_stream_h isys_stream =
8228 							&sp_pipeline_input_terminal->context.virtual_input_system_stream[i];
8229 						if (stream->config.isys_config[i].valid && isys_stream->valid)
8230 							ia_css_isys_stream_destroy(isys_stream);
8231 					}
8232 				}
8233 			}
8234 
8235 			if (stream->config.mode == IA_CSS_INPUT_MODE_BUFFERED_SENSOR) {
8236 				for (i = 0; i < stream->num_pipes; i++) {
8237 					struct ia_css_pipe *entry = stream->pipes[i];
8238 					/*
8239 					 * free any mipi frames that are remaining:
8240 					 * some test stream create-destroy cycles do
8241 					 * not generate output frames
8242 					 * and the mipi buffer is not freed in the
8243 					 * deque function
8244 					 */
8245 					if (entry)
8246 						free_mipi_frames(entry);
8247 				}
8248 			}
8249 			stream_unregister_with_csi_rx(stream);
8250 		}
8251 
8252 		for (i = 0; i < stream->num_pipes; i++) {
8253 			struct ia_css_pipe *curr_pipe = stream->pipes[i];
8254 
8255 			assert(curr_pipe);
8256 			ia_css_pipe_map_queue(curr_pipe, false);
8257 		}
8258 
8259 		err = map_sp_threads(stream, false);
8260 		if (err) {
8261 			IA_CSS_LEAVE_ERR_PRIVATE(err);
8262 			return err;
8263 		}
8264 	}
8265 
8266 	/* remove references from pipes to stream */
8267 	for (i = 0; i < stream->num_pipes; i++) {
8268 		struct ia_css_pipe *entry = stream->pipes[i];
8269 
8270 		assert(entry);
8271 		if (entry) {
8272 			/* clear reference to stream */
8273 			entry->stream = NULL;
8274 			/* check internal copy pipe */
8275 			if (entry->mode == IA_CSS_PIPE_ID_PREVIEW &&
8276 			    entry->pipe_settings.preview.copy_pipe) {
8277 				IA_CSS_LOG("clearing stream on internal preview copy pipe");
8278 				entry->pipe_settings.preview.copy_pipe->stream = NULL;
8279 			}
8280 			if (entry->mode == IA_CSS_PIPE_ID_VIDEO &&
8281 			    entry->pipe_settings.video.copy_pipe) {
8282 				IA_CSS_LOG("clearing stream on internal video copy pipe");
8283 				entry->pipe_settings.video.copy_pipe->stream = NULL;
8284 			}
8285 			err = sh_css_pipe_unload_binaries(entry);
8286 		}
8287 	}
8288 	/* free associated memory of stream struct */
8289 	kfree(stream->pipes);
8290 	stream->pipes = NULL;
8291 	stream->num_pipes = 0;
8292 
8293 	/* working mode: take out of the seed list */
8294 	if (my_css_save.mode == sh_css_mode_working) {
8295 		for (i = 0; i < MAX_ACTIVE_STREAMS; i++) {
8296 			if (my_css_save.stream_seeds[i].stream == stream) {
8297 				IA_CSS_LOG("took out stream %d", i);
8298 				my_css_save.stream_seeds[i].stream = NULL;
8299 				break;
8300 			}
8301 		}
8302 	}
8303 
8304 	kfree(stream);
8305 	IA_CSS_LEAVE_ERR(err);
8306 
8307 	return err;
8308 }
8309 
8310 int
ia_css_stream_get_info(const struct ia_css_stream * stream,struct ia_css_stream_info * stream_info)8311 ia_css_stream_get_info(const struct ia_css_stream *stream,
8312 		       struct ia_css_stream_info *stream_info)
8313 {
8314 	ia_css_debug_dtrace(IA_CSS_DEBUG_TRACE, "ia_css_stream_get_info: enter/exit\n");
8315 	assert(stream);
8316 	assert(stream_info);
8317 
8318 	*stream_info = stream->info;
8319 	return 0;
8320 }
8321 
8322 int
ia_css_stream_start(struct ia_css_stream * stream)8323 ia_css_stream_start(struct ia_css_stream *stream)
8324 {
8325 	int err = 0;
8326 
8327 	IA_CSS_ENTER("stream = %p", stream);
8328 	if ((!stream) || (!stream->last_pipe)) {
8329 		IA_CSS_LEAVE_ERR(-EINVAL);
8330 		return -EINVAL;
8331 	}
8332 	IA_CSS_LOG("starting %d", stream->last_pipe->mode);
8333 
8334 	sh_css_sp_set_disable_continuous_viewfinder(stream->disable_cont_vf);
8335 
8336 	/* Create host side pipeline. */
8337 	err = create_host_pipeline(stream);
8338 	if (err) {
8339 		IA_CSS_LEAVE_ERR(err);
8340 		return err;
8341 	}
8342 
8343 	if (IS_ISP2401 &&
8344 	    ((stream->config.mode == IA_CSS_INPUT_MODE_SENSOR) ||
8345 	     (stream->config.mode == IA_CSS_INPUT_MODE_BUFFERED_SENSOR)))
8346 		stream_register_with_csi_rx(stream);
8347 
8348 	/* Initialize mipi size checks */
8349 	if (!IS_ISP2401 && stream->config.mode == IA_CSS_INPUT_MODE_BUFFERED_SENSOR) {
8350 		unsigned int idx;
8351 		unsigned int port = (unsigned int)(stream->config.source.port.port);
8352 
8353 		for (idx = 0; idx < IA_CSS_MIPI_SIZE_CHECK_MAX_NOF_ENTRIES_PER_PORT; idx++) {
8354 			sh_css_sp_group.config.mipi_sizes_for_check[port][idx] =
8355 			sh_css_get_mipi_sizes_for_check(port, idx);
8356 		}
8357 	}
8358 
8359 	if (stream->config.mode != IA_CSS_INPUT_MODE_MEMORY) {
8360 		if (IS_ISP2401)
8361 			err = sh_css_config_input_network_2401(stream);
8362 		else
8363 			err = sh_css_config_input_network_2400(stream);
8364 		if (err)
8365 			return err;
8366 	}
8367 
8368 	err = sh_css_pipe_start(stream);
8369 	IA_CSS_LEAVE_ERR(err);
8370 	return err;
8371 }
8372 
8373 int
ia_css_stream_stop(struct ia_css_stream * stream)8374 ia_css_stream_stop(struct ia_css_stream *stream)
8375 {
8376 	int err = 0;
8377 
8378 	ia_css_debug_dtrace(IA_CSS_DEBUG_TRACE, "ia_css_stream_stop() enter/exit\n");
8379 	assert(stream);
8380 	assert(stream->last_pipe);
8381 	ia_css_debug_dtrace(IA_CSS_DEBUG_TRACE, "ia_css_stream_stop: stopping %d\n",
8382 			    stream->last_pipe->mode);
8383 
8384 	/* De-initialize mipi size checks */
8385 	if (!IS_ISP2401 && stream->config.mode == IA_CSS_INPUT_MODE_BUFFERED_SENSOR) {
8386 		unsigned int idx;
8387 		unsigned int port = (unsigned int)(stream->config.source.port.port);
8388 
8389 		for (idx = 0; idx < IA_CSS_MIPI_SIZE_CHECK_MAX_NOF_ENTRIES_PER_PORT; idx++)
8390 			sh_css_sp_group.config.mipi_sizes_for_check[port][idx] = 0;
8391 	}
8392 
8393 	err = ia_css_pipeline_request_stop(&stream->last_pipe->pipeline);
8394 	if (err)
8395 		return err;
8396 
8397 	/*
8398 	 * Ideally, unmapping should happen after pipeline_stop, but current
8399 	 * semantics do not allow that.
8400 	 */
8401 	/* err = map_sp_threads(stream, false); */
8402 
8403 	return err;
8404 }
8405 
8406 bool
ia_css_stream_has_stopped(struct ia_css_stream * stream)8407 ia_css_stream_has_stopped(struct ia_css_stream *stream)
8408 {
8409 	bool stopped;
8410 
8411 	assert(stream);
8412 
8413 	stopped = ia_css_pipeline_has_stopped(&stream->last_pipe->pipeline);
8414 
8415 	return stopped;
8416 }
8417 
8418 /* ISP2400 */
8419 /*
8420  * Destroy the stream and all the pipes related to it.
8421  * The stream handle is used to identify the correct entry in the css_save struct
8422  */
8423 int
ia_css_stream_unload(struct ia_css_stream * stream)8424 ia_css_stream_unload(struct ia_css_stream *stream)
8425 {
8426 	int i;
8427 
8428 	assert(stream);
8429 	ia_css_debug_dtrace(IA_CSS_DEBUG_TRACE,	"ia_css_stream_unload() enter,\n");
8430 	/* some checks */
8431 	assert(stream);
8432 	for (i = 0; i < MAX_ACTIVE_STREAMS; i++)
8433 		if (my_css_save.stream_seeds[i].stream == stream) {
8434 			int j;
8435 
8436 			ia_css_debug_dtrace(IA_CSS_DEBUG_TRACE,
8437 					    "ia_css_stream_unload(): unloading %d (%p)\n", i,
8438 					    my_css_save.stream_seeds[i].stream);
8439 			ia_css_stream_destroy(stream);
8440 			for (j = 0; j < my_css_save.stream_seeds[i].num_pipes; j++)
8441 				ia_css_pipe_destroy(my_css_save.stream_seeds[i].pipes[j]);
8442 			ia_css_debug_dtrace(IA_CSS_DEBUG_TRACE,
8443 					    "ia_css_stream_unload(): after unloading %d (%p)\n", i,
8444 					    my_css_save.stream_seeds[i].stream);
8445 			break;
8446 		}
8447 	ia_css_debug_dtrace(IA_CSS_DEBUG_TRACE,	"ia_css_stream_unload() exit,\n");
8448 	return 0;
8449 }
8450 
8451 int
ia_css_temp_pipe_to_pipe_id(const struct ia_css_pipe * pipe,enum ia_css_pipe_id * pipe_id)8452 ia_css_temp_pipe_to_pipe_id(const struct ia_css_pipe *pipe,
8453 			    enum ia_css_pipe_id *pipe_id)
8454 {
8455 	ia_css_debug_dtrace(IA_CSS_DEBUG_TRACE, "ia_css_temp_pipe_to_pipe_id() enter/exit\n");
8456 	if (pipe)
8457 		*pipe_id = pipe->mode;
8458 	else
8459 		*pipe_id = IA_CSS_PIPE_ID_COPY;
8460 
8461 	return 0;
8462 }
8463 
8464 enum atomisp_input_format
ia_css_stream_get_format(const struct ia_css_stream * stream)8465 ia_css_stream_get_format(const struct ia_css_stream *stream)
8466 {
8467 	return stream->config.input_config.format;
8468 }
8469 
8470 bool
ia_css_stream_get_two_pixels_per_clock(const struct ia_css_stream * stream)8471 ia_css_stream_get_two_pixels_per_clock(const struct ia_css_stream *stream)
8472 {
8473 	return (stream->config.pixels_per_clock == 2);
8474 }
8475 
8476 struct ia_css_binary *
ia_css_stream_get_shading_correction_binary(const struct ia_css_stream * stream)8477 ia_css_stream_get_shading_correction_binary(const struct ia_css_stream
8478 	*stream)
8479 {
8480 	struct ia_css_pipe *pipe;
8481 
8482 	assert(stream);
8483 
8484 	pipe = stream->pipes[0];
8485 
8486 	if (stream->num_pipes == 2) {
8487 		assert(stream->pipes[1]);
8488 		if (stream->pipes[1]->config.mode == IA_CSS_PIPE_MODE_VIDEO ||
8489 		    stream->pipes[1]->config.mode == IA_CSS_PIPE_MODE_PREVIEW)
8490 			pipe = stream->pipes[1];
8491 	}
8492 
8493 	return ia_css_pipe_get_shading_correction_binary(pipe);
8494 }
8495 
8496 struct ia_css_binary *
ia_css_stream_get_dvs_binary(const struct ia_css_stream * stream)8497 ia_css_stream_get_dvs_binary(const struct ia_css_stream *stream)
8498 {
8499 	int i;
8500 	struct ia_css_pipe *video_pipe = NULL;
8501 
8502 	/* First we find the video pipe */
8503 	for (i = 0; i < stream->num_pipes; i++) {
8504 		struct ia_css_pipe *pipe = stream->pipes[i];
8505 
8506 		if (pipe->config.mode == IA_CSS_PIPE_MODE_VIDEO) {
8507 			video_pipe = pipe;
8508 			break;
8509 		}
8510 	}
8511 	if (video_pipe)
8512 		return &video_pipe->pipe_settings.video.video_binary;
8513 	return NULL;
8514 }
8515 
8516 struct ia_css_binary *
ia_css_stream_get_3a_binary(const struct ia_css_stream * stream)8517 ia_css_stream_get_3a_binary(const struct ia_css_stream *stream)
8518 {
8519 	struct ia_css_pipe *pipe;
8520 	struct ia_css_binary *s3a_binary = NULL;
8521 
8522 	assert(stream);
8523 
8524 	pipe = stream->pipes[0];
8525 
8526 	if (stream->num_pipes == 2) {
8527 		assert(stream->pipes[1]);
8528 		if (stream->pipes[1]->config.mode == IA_CSS_PIPE_MODE_VIDEO ||
8529 		    stream->pipes[1]->config.mode == IA_CSS_PIPE_MODE_PREVIEW)
8530 			pipe = stream->pipes[1];
8531 	}
8532 
8533 	s3a_binary = ia_css_pipe_get_s3a_binary(pipe);
8534 
8535 	return s3a_binary;
8536 }
8537 
8538 int
ia_css_stream_set_output_padded_width(struct ia_css_stream * stream,unsigned int output_padded_width)8539 ia_css_stream_set_output_padded_width(struct ia_css_stream *stream,
8540 				      unsigned int output_padded_width)
8541 {
8542 	struct ia_css_pipe *pipe;
8543 
8544 	assert(stream);
8545 
8546 	pipe = stream->last_pipe;
8547 
8548 	assert(pipe);
8549 
8550 	/* set the config also just in case (redundant info? why do we save config in pipe?) */
8551 	pipe->config.output_info[IA_CSS_PIPE_OUTPUT_STAGE_0].padded_width = output_padded_width;
8552 	pipe->output_info[IA_CSS_PIPE_OUTPUT_STAGE_0].padded_width = output_padded_width;
8553 
8554 	return 0;
8555 }
8556 
8557 static struct ia_css_binary *
ia_css_pipe_get_shading_correction_binary(const struct ia_css_pipe * pipe)8558 ia_css_pipe_get_shading_correction_binary(const struct ia_css_pipe *pipe)
8559 {
8560 	struct ia_css_binary *binary = NULL;
8561 
8562 	assert(pipe);
8563 
8564 	switch (pipe->config.mode) {
8565 	case IA_CSS_PIPE_MODE_PREVIEW:
8566 		binary = (struct ia_css_binary *)&pipe->pipe_settings.preview.preview_binary;
8567 		break;
8568 	case IA_CSS_PIPE_MODE_VIDEO:
8569 		binary = (struct ia_css_binary *)&pipe->pipe_settings.video.video_binary;
8570 		break;
8571 	case IA_CSS_PIPE_MODE_CAPTURE:
8572 		if (pipe->config.default_capture_config.mode == IA_CSS_CAPTURE_MODE_PRIMARY) {
8573 			unsigned int i;
8574 
8575 			for (i = 0; i < pipe->pipe_settings.capture.num_primary_stage; i++) {
8576 				if (pipe->pipe_settings.capture.primary_binary[i].info->sp.enable.sc) {
8577 					binary = (struct ia_css_binary *)&pipe->pipe_settings.capture.primary_binary[i];
8578 					break;
8579 				}
8580 			}
8581 		} else if (pipe->config.default_capture_config.mode ==
8582 			    IA_CSS_CAPTURE_MODE_BAYER)
8583 			binary = (struct ia_css_binary *)&pipe->pipe_settings.capture.pre_isp_binary;
8584 		else if (pipe->config.default_capture_config.mode ==
8585 			    IA_CSS_CAPTURE_MODE_ADVANCED ||
8586 			    pipe->config.default_capture_config.mode == IA_CSS_CAPTURE_MODE_LOW_LIGHT) {
8587 			if (pipe->config.isp_pipe_version == IA_CSS_PIPE_VERSION_1)
8588 				binary = (struct ia_css_binary *)&pipe->pipe_settings.capture.pre_isp_binary;
8589 			else if (pipe->config.isp_pipe_version == IA_CSS_PIPE_VERSION_2_2)
8590 				binary = (struct ia_css_binary *)&pipe->pipe_settings.capture.post_isp_binary;
8591 		}
8592 		break;
8593 	default:
8594 		break;
8595 	}
8596 
8597 	if (binary && binary->info->sp.enable.sc)
8598 		return binary;
8599 
8600 	return NULL;
8601 }
8602 
8603 static struct ia_css_binary *
ia_css_pipe_get_s3a_binary(const struct ia_css_pipe * pipe)8604 ia_css_pipe_get_s3a_binary(const struct ia_css_pipe *pipe)
8605 {
8606 	struct ia_css_binary *binary = NULL;
8607 
8608 	assert(pipe);
8609 
8610 	switch (pipe->config.mode) {
8611 	case IA_CSS_PIPE_MODE_PREVIEW:
8612 		binary = (struct ia_css_binary *)&pipe->pipe_settings.preview.preview_binary;
8613 		break;
8614 	case IA_CSS_PIPE_MODE_VIDEO:
8615 		binary = (struct ia_css_binary *)&pipe->pipe_settings.video.video_binary;
8616 		break;
8617 	case IA_CSS_PIPE_MODE_CAPTURE:
8618 		if (pipe->config.default_capture_config.mode == IA_CSS_CAPTURE_MODE_PRIMARY) {
8619 			unsigned int i;
8620 
8621 			for (i = 0; i < pipe->pipe_settings.capture.num_primary_stage; i++) {
8622 				if (pipe->pipe_settings.capture.primary_binary[i].info->sp.enable.s3a) {
8623 					binary = (struct ia_css_binary *)&pipe->pipe_settings.capture.primary_binary[i];
8624 					break;
8625 				}
8626 			}
8627 		} else if (pipe->config.default_capture_config.mode ==
8628 			    IA_CSS_CAPTURE_MODE_BAYER) {
8629 			binary = (struct ia_css_binary *)&pipe->pipe_settings.capture.pre_isp_binary;
8630 		} else if (pipe->config.default_capture_config.mode ==
8631 			    IA_CSS_CAPTURE_MODE_ADVANCED ||
8632 			    pipe->config.default_capture_config.mode == IA_CSS_CAPTURE_MODE_LOW_LIGHT) {
8633 			if (pipe->config.isp_pipe_version == IA_CSS_PIPE_VERSION_1)
8634 				binary = (struct ia_css_binary *)&pipe->pipe_settings.capture.pre_isp_binary;
8635 			else if (pipe->config.isp_pipe_version == IA_CSS_PIPE_VERSION_2_2)
8636 				binary = (struct ia_css_binary *)&pipe->pipe_settings.capture.post_isp_binary;
8637 			else
8638 				assert(0);
8639 		}
8640 		break;
8641 	default:
8642 		break;
8643 	}
8644 
8645 	if (binary && !binary->info->sp.enable.s3a)
8646 		binary = NULL;
8647 
8648 	return binary;
8649 }
8650 
8651 static struct ia_css_binary *
ia_css_pipe_get_sdis_binary(const struct ia_css_pipe * pipe)8652 ia_css_pipe_get_sdis_binary(const struct ia_css_pipe *pipe)
8653 {
8654 	struct ia_css_binary *binary = NULL;
8655 
8656 	assert(pipe);
8657 
8658 	switch (pipe->config.mode) {
8659 	case IA_CSS_PIPE_MODE_VIDEO:
8660 		binary = (struct ia_css_binary *)&pipe->pipe_settings.video.video_binary;
8661 		break;
8662 	default:
8663 		break;
8664 	}
8665 
8666 	if (binary && !binary->info->sp.enable.dis)
8667 		binary = NULL;
8668 
8669 	return binary;
8670 }
8671 
8672 struct ia_css_pipeline *
ia_css_pipe_get_pipeline(const struct ia_css_pipe * pipe)8673 ia_css_pipe_get_pipeline(const struct ia_css_pipe *pipe)
8674 {
8675 	assert(pipe);
8676 
8677 	return (struct ia_css_pipeline *)&pipe->pipeline;
8678 }
8679 
8680 unsigned int
ia_css_pipe_get_pipe_num(const struct ia_css_pipe * pipe)8681 ia_css_pipe_get_pipe_num(const struct ia_css_pipe *pipe)
8682 {
8683 	assert(pipe);
8684 
8685 	/*
8686 	 * KW was not sure this function was not returning a value
8687 	 * that was out of range; so added an assert, and, for the
8688 	 * case when asserts are not enabled, clip to the largest
8689 	 * value; pipe_num is unsigned so the value cannot be too small
8690 	 */
8691 	assert(pipe->pipe_num < IA_CSS_PIPELINE_NUM_MAX);
8692 
8693 	if (pipe->pipe_num >= IA_CSS_PIPELINE_NUM_MAX)
8694 		return (IA_CSS_PIPELINE_NUM_MAX - 1);
8695 
8696 	return pipe->pipe_num;
8697 }
8698 
8699 unsigned int
ia_css_pipe_get_isp_pipe_version(const struct ia_css_pipe * pipe)8700 ia_css_pipe_get_isp_pipe_version(const struct ia_css_pipe *pipe)
8701 {
8702 	assert(pipe);
8703 
8704 	return (unsigned int)pipe->config.isp_pipe_version;
8705 }
8706 
8707 #define SP_START_TIMEOUT_US 30000000
8708 
8709 int
ia_css_start_sp(void)8710 ia_css_start_sp(void)
8711 {
8712 	unsigned long timeout;
8713 	int err = 0;
8714 
8715 	IA_CSS_ENTER("");
8716 	sh_css_sp_start_isp();
8717 
8718 	/* waiting for the SP is completely started */
8719 	timeout = SP_START_TIMEOUT_US;
8720 	while ((ia_css_spctrl_get_state(SP0_ID) != IA_CSS_SP_SW_INITIALIZED) && timeout) {
8721 		timeout--;
8722 		udelay(1);
8723 	}
8724 	if (timeout == 0) {
8725 		IA_CSS_ERROR("timeout during SP initialization");
8726 		return -EINVAL;
8727 	}
8728 
8729 	/* Workaround, in order to run two streams in parallel. See TASK 4271*/
8730 	/* TODO: Fix this. */
8731 
8732 	sh_css_init_host_sp_control_vars();
8733 
8734 	/* buffers should be initialized only when sp is started */
8735 	/* AM: At the moment it will be done only when there is no stream active. */
8736 
8737 	sh_css_setup_queues();
8738 	ia_css_bufq_dump_queue_info();
8739 
8740 	IA_CSS_LEAVE_ERR(err);
8741 	return err;
8742 }
8743 
8744 /*
8745  * Time to wait SP for termincate. Only condition when this can happen
8746  * is a fatal hw failure, but we must be able to detect this and emit
8747  * a proper error trace.
8748  */
8749 #define SP_SHUTDOWN_TIMEOUT_US 200000
8750 
8751 int
ia_css_stop_sp(void)8752 ia_css_stop_sp(void)
8753 {
8754 	unsigned long timeout;
8755 	int err = 0;
8756 
8757 	IA_CSS_ENTER("void");
8758 
8759 	if (!sh_css_sp_is_running()) {
8760 		err = -EINVAL;
8761 		IA_CSS_LEAVE("SP already stopped : return_err=%d", err);
8762 
8763 		/* Return an error - stop SP should not have been called by driver */
8764 		return err;
8765 	}
8766 
8767 	/* For now, stop whole SP */
8768 	if (!sh_css_write_host2sp_command(host2sp_cmd_terminate)) {
8769 		IA_CSS_ERROR("Call to 'sh-css_write_host2sp_command()' failed");
8770 		ia_css_debug_dump_sp_sw_debug_info();
8771 	}
8772 
8773 	sh_css_sp_set_sp_running(false);
8774 
8775 	timeout = SP_SHUTDOWN_TIMEOUT_US;
8776 	while (!ia_css_spctrl_is_idle(SP0_ID) && timeout) {
8777 		timeout--;
8778 		udelay(1);
8779 	}
8780 	if (ia_css_spctrl_get_state(SP0_ID) != IA_CSS_SP_SW_TERMINATED)
8781 		IA_CSS_WARNING("SP has not terminated (SW)");
8782 
8783 	if (timeout == 0) {
8784 		IA_CSS_WARNING("SP is not idle");
8785 		ia_css_debug_dump_sp_sw_debug_info();
8786 	}
8787 	timeout = SP_SHUTDOWN_TIMEOUT_US;
8788 	while (!isp_ctrl_getbit(ISP0_ID, ISP_SC_REG, ISP_IDLE_BIT) && timeout) {
8789 		timeout--;
8790 		udelay(1);
8791 	}
8792 	if (timeout == 0) {
8793 		IA_CSS_WARNING("ISP is not idle");
8794 		ia_css_debug_dump_sp_sw_debug_info();
8795 	}
8796 
8797 	sh_css_hmm_buffer_record_uninit();
8798 
8799 	/* clear pending param sets from refcount */
8800 	sh_css_param_clear_param_sets();
8801 
8802 	IA_CSS_LEAVE_ERR(err);
8803 	return err;
8804 }
8805 
8806 int
ia_css_update_continuous_frames(struct ia_css_stream * stream)8807 ia_css_update_continuous_frames(struct ia_css_stream *stream)
8808 {
8809 	struct ia_css_pipe *pipe;
8810 	unsigned int i;
8811 
8812 	ia_css_debug_dtrace(
8813 	    IA_CSS_DEBUG_TRACE,
8814 	    "sh_css_update_continuous_frames() enter:\n");
8815 
8816 	if (!stream) {
8817 		ia_css_debug_dtrace(
8818 		    IA_CSS_DEBUG_TRACE,
8819 		    "sh_css_update_continuous_frames() leave: invalid stream, return_void\n");
8820 		return -EINVAL;
8821 	}
8822 
8823 	pipe = stream->continuous_pipe;
8824 
8825 	for (i = stream->config.init_num_cont_raw_buf;
8826 		i < stream->config.target_num_cont_raw_buf; i++)
8827 		sh_css_update_host2sp_offline_frame(i,
8828 						    pipe->continuous_frames[i], pipe->cont_md_buffers[i]);
8829 
8830 	sh_css_update_host2sp_cont_num_raw_frames
8831 	(stream->config.target_num_cont_raw_buf, true);
8832 	ia_css_debug_dtrace(
8833 	    IA_CSS_DEBUG_TRACE,
8834 	    "sh_css_update_continuous_frames() leave: return_void\n");
8835 
8836 	return 0;
8837 }
8838 
ia_css_pipe_map_queue(struct ia_css_pipe * pipe,bool map)8839 void ia_css_pipe_map_queue(struct ia_css_pipe *pipe, bool map)
8840 {
8841 	unsigned int thread_id;
8842 	unsigned int pipe_num;
8843 	bool need_input_queue;
8844 
8845 	IA_CSS_ENTER("");
8846 	assert(pipe);
8847 
8848 	pipe_num = pipe->pipe_num;
8849 
8850 	ia_css_pipeline_get_sp_thread_id(pipe_num, &thread_id);
8851 
8852 	if (IS_ISP2401)
8853 		need_input_queue = true;
8854 	else
8855 		need_input_queue = pipe->stream->config.mode == IA_CSS_INPUT_MODE_MEMORY;
8856 
8857 	/* map required buffer queues to resources */
8858 	/* TODO: to be improved */
8859 	if (pipe->mode == IA_CSS_PIPE_ID_PREVIEW) {
8860 		if (need_input_queue)
8861 			ia_css_queue_map(thread_id, IA_CSS_BUFFER_TYPE_INPUT_FRAME, map);
8862 		ia_css_queue_map(thread_id, IA_CSS_BUFFER_TYPE_OUTPUT_FRAME, map);
8863 		ia_css_queue_map(thread_id, IA_CSS_BUFFER_TYPE_PARAMETER_SET, map);
8864 		ia_css_queue_map(thread_id, IA_CSS_BUFFER_TYPE_PER_FRAME_PARAMETER_SET, map);
8865 		ia_css_queue_map(thread_id, IA_CSS_BUFFER_TYPE_METADATA, map);
8866 		if (pipe->pipe_settings.preview.preview_binary.info &&
8867 		    pipe->pipe_settings.preview.preview_binary.info->sp.enable.s3a)
8868 			ia_css_queue_map(thread_id, IA_CSS_BUFFER_TYPE_3A_STATISTICS, map);
8869 	} else if (pipe->mode == IA_CSS_PIPE_ID_CAPTURE) {
8870 		unsigned int i;
8871 
8872 		if (need_input_queue)
8873 			ia_css_queue_map(thread_id, IA_CSS_BUFFER_TYPE_INPUT_FRAME, map);
8874 		ia_css_queue_map(thread_id, IA_CSS_BUFFER_TYPE_OUTPUT_FRAME, map);
8875 		ia_css_queue_map(thread_id, IA_CSS_BUFFER_TYPE_VF_OUTPUT_FRAME, map);
8876 		ia_css_queue_map(thread_id, IA_CSS_BUFFER_TYPE_PARAMETER_SET, map);
8877 		ia_css_queue_map(thread_id, IA_CSS_BUFFER_TYPE_PER_FRAME_PARAMETER_SET, map);
8878 		ia_css_queue_map(thread_id, IA_CSS_BUFFER_TYPE_METADATA, map);
8879 		if (pipe->config.default_capture_config.mode == IA_CSS_CAPTURE_MODE_PRIMARY) {
8880 			for (i = 0; i < pipe->pipe_settings.capture.num_primary_stage; i++) {
8881 				if (pipe->pipe_settings.capture.primary_binary[i].info &&
8882 				    pipe->pipe_settings.capture.primary_binary[i].info->sp.enable.s3a) {
8883 					ia_css_queue_map(thread_id, IA_CSS_BUFFER_TYPE_3A_STATISTICS, map);
8884 					break;
8885 				}
8886 			}
8887 		} else if (pipe->config.default_capture_config.mode ==
8888 			    IA_CSS_CAPTURE_MODE_ADVANCED ||
8889 			    pipe->config.default_capture_config.mode == IA_CSS_CAPTURE_MODE_LOW_LIGHT ||
8890 			    pipe->config.default_capture_config.mode == IA_CSS_CAPTURE_MODE_BAYER) {
8891 			if (pipe->pipe_settings.capture.pre_isp_binary.info &&
8892 			    pipe->pipe_settings.capture.pre_isp_binary.info->sp.enable.s3a)
8893 				ia_css_queue_map(thread_id, IA_CSS_BUFFER_TYPE_3A_STATISTICS, map);
8894 		}
8895 	} else if (pipe->mode == IA_CSS_PIPE_ID_VIDEO) {
8896 		if (need_input_queue)
8897 			ia_css_queue_map(thread_id, IA_CSS_BUFFER_TYPE_INPUT_FRAME, map);
8898 		ia_css_queue_map(thread_id, IA_CSS_BUFFER_TYPE_OUTPUT_FRAME, map);
8899 		if (pipe->enable_viewfinder[IA_CSS_PIPE_OUTPUT_STAGE_0])
8900 			ia_css_queue_map(thread_id, IA_CSS_BUFFER_TYPE_VF_OUTPUT_FRAME, map);
8901 		ia_css_queue_map(thread_id, IA_CSS_BUFFER_TYPE_PARAMETER_SET, map);
8902 		ia_css_queue_map(thread_id, IA_CSS_BUFFER_TYPE_PER_FRAME_PARAMETER_SET, map);
8903 		ia_css_queue_map(thread_id, IA_CSS_BUFFER_TYPE_METADATA, map);
8904 		if (pipe->pipe_settings.video.video_binary.info &&
8905 		    pipe->pipe_settings.video.video_binary.info->sp.enable.s3a)
8906 			ia_css_queue_map(thread_id, IA_CSS_BUFFER_TYPE_3A_STATISTICS, map);
8907 		if (pipe->pipe_settings.video.video_binary.info &&
8908 		    (pipe->pipe_settings.video.video_binary.info->sp.enable.dis
8909 		    ))
8910 			ia_css_queue_map(thread_id, IA_CSS_BUFFER_TYPE_DIS_STATISTICS, map);
8911 	} else if (pipe->mode == IA_CSS_PIPE_ID_COPY) {
8912 		if (need_input_queue)
8913 			ia_css_queue_map(thread_id, IA_CSS_BUFFER_TYPE_INPUT_FRAME, map);
8914 		if (!pipe->stream->config.continuous)
8915 			ia_css_queue_map(thread_id, IA_CSS_BUFFER_TYPE_OUTPUT_FRAME, map);
8916 		ia_css_queue_map(thread_id, IA_CSS_BUFFER_TYPE_METADATA, map);
8917 	} else if (pipe->mode == IA_CSS_PIPE_ID_YUVPP) {
8918 		unsigned int idx;
8919 
8920 		for (idx = 0; idx < IA_CSS_PIPE_MAX_OUTPUT_STAGE; idx++) {
8921 			ia_css_queue_map(thread_id, IA_CSS_BUFFER_TYPE_OUTPUT_FRAME + idx, map);
8922 			if (pipe->enable_viewfinder[idx])
8923 				ia_css_queue_map(thread_id, IA_CSS_BUFFER_TYPE_VF_OUTPUT_FRAME + idx, map);
8924 		}
8925 		if (need_input_queue)
8926 			ia_css_queue_map(thread_id, IA_CSS_BUFFER_TYPE_INPUT_FRAME, map);
8927 		ia_css_queue_map(thread_id, IA_CSS_BUFFER_TYPE_PARAMETER_SET, map);
8928 		ia_css_queue_map(thread_id, IA_CSS_BUFFER_TYPE_METADATA, map);
8929 	}
8930 	IA_CSS_LEAVE("");
8931 }
8932 
8933 
8934 int
ia_css_unlock_raw_frame(struct ia_css_stream * stream,uint32_t exp_id)8935 ia_css_unlock_raw_frame(struct ia_css_stream *stream, uint32_t exp_id)
8936 {
8937 	int ret;
8938 
8939 	IA_CSS_ENTER("");
8940 
8941 	/*
8942 	 * Only continuous streams have a tagger to which we can send the
8943 	 * unlock message.
8944 	 */
8945 	if (!stream || !stream->config.continuous) {
8946 		IA_CSS_ERROR("invalid stream pointer");
8947 		return -EINVAL;
8948 	}
8949 
8950 	if (exp_id > IA_CSS_ISYS_MAX_EXPOSURE_ID ||
8951 	    exp_id < IA_CSS_ISYS_MIN_EXPOSURE_ID) {
8952 		IA_CSS_ERROR("invalid exposure ID: %d\n", exp_id);
8953 		return -EINVAL;
8954 	}
8955 
8956 	/*
8957 	 * Send the event. Since we verified that the exp_id is valid,
8958 	 * we can safely assign it to an 8-bit argument here.
8959 	 */
8960 	ret = ia_css_bufq_enqueue_psys_event(
8961 	    IA_CSS_PSYS_SW_EVENT_UNLOCK_RAW_BUFFER, exp_id, 0, 0);
8962 
8963 	IA_CSS_LEAVE_ERR(ret);
8964 	return ret;
8965 }
8966 
8967 static void
sh_css_hmm_buffer_record_init(void)8968 sh_css_hmm_buffer_record_init(void)
8969 {
8970 	int i;
8971 
8972 	for (i = 0; i < MAX_HMM_BUFFER_NUM; i++)
8973 		sh_css_hmm_buffer_record_reset(&hmm_buffer_record[i]);
8974 }
8975 
8976 static void
sh_css_hmm_buffer_record_uninit(void)8977 sh_css_hmm_buffer_record_uninit(void)
8978 {
8979 	int i;
8980 	struct sh_css_hmm_buffer_record *buffer_record = NULL;
8981 
8982 	buffer_record = &hmm_buffer_record[0];
8983 	for (i = 0; i < MAX_HMM_BUFFER_NUM; i++) {
8984 		if (buffer_record->in_use) {
8985 			if (buffer_record->h_vbuf)
8986 				ia_css_rmgr_rel_vbuf(hmm_buffer_pool, &buffer_record->h_vbuf);
8987 			sh_css_hmm_buffer_record_reset(buffer_record);
8988 		}
8989 		buffer_record++;
8990 	}
8991 }
8992 
8993 static void
sh_css_hmm_buffer_record_reset(struct sh_css_hmm_buffer_record * buffer_record)8994 sh_css_hmm_buffer_record_reset(struct sh_css_hmm_buffer_record *buffer_record)
8995 {
8996 	assert(buffer_record);
8997 	buffer_record->in_use = false;
8998 	buffer_record->type = IA_CSS_BUFFER_TYPE_INVALID;
8999 	buffer_record->h_vbuf = NULL;
9000 	buffer_record->kernel_ptr = 0;
9001 }
9002 
9003 static struct sh_css_hmm_buffer_record
sh_css_hmm_buffer_record_acquire(struct ia_css_rmgr_vbuf_handle * h_vbuf,enum ia_css_buffer_type type,hrt_address kernel_ptr)9004 *sh_css_hmm_buffer_record_acquire(struct ia_css_rmgr_vbuf_handle *h_vbuf,
9005 				    enum ia_css_buffer_type type,
9006 				    hrt_address kernel_ptr)
9007 {
9008 	int i;
9009 	struct sh_css_hmm_buffer_record *buffer_record = NULL;
9010 	struct sh_css_hmm_buffer_record *out_buffer_record = NULL;
9011 
9012 	assert(h_vbuf);
9013 	assert((type > IA_CSS_BUFFER_TYPE_INVALID) &&
9014 	       (type < IA_CSS_NUM_DYNAMIC_BUFFER_TYPE));
9015 	assert(kernel_ptr != 0);
9016 
9017 	buffer_record = &hmm_buffer_record[0];
9018 	for (i = 0; i < MAX_HMM_BUFFER_NUM; i++) {
9019 		if (!buffer_record->in_use) {
9020 			buffer_record->in_use = true;
9021 			buffer_record->type = type;
9022 			buffer_record->h_vbuf = h_vbuf;
9023 			buffer_record->kernel_ptr = kernel_ptr;
9024 			out_buffer_record = buffer_record;
9025 			break;
9026 		}
9027 		buffer_record++;
9028 	}
9029 
9030 	return out_buffer_record;
9031 }
9032 
9033 static struct sh_css_hmm_buffer_record
sh_css_hmm_buffer_record_validate(ia_css_ptr ddr_buffer_addr,enum ia_css_buffer_type type)9034 *sh_css_hmm_buffer_record_validate(ia_css_ptr ddr_buffer_addr,
9035 				    enum ia_css_buffer_type type)
9036 {
9037 	int i;
9038 	struct sh_css_hmm_buffer_record *buffer_record = NULL;
9039 	bool found_record = false;
9040 
9041 	buffer_record = &hmm_buffer_record[0];
9042 	for (i = 0; i < MAX_HMM_BUFFER_NUM; i++) {
9043 		if ((buffer_record->in_use) &&
9044 		    (buffer_record->type == type) &&
9045 		    (buffer_record->h_vbuf) &&
9046 		    (buffer_record->h_vbuf->vptr == ddr_buffer_addr)) {
9047 			found_record = true;
9048 			break;
9049 		}
9050 		buffer_record++;
9051 	}
9052 
9053 	if (found_record)
9054 		return buffer_record;
9055 	else
9056 		return NULL;
9057 }
9058