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