• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "gdc_device.h"		/* gdc_lut_store(), ... */
17 #include "isp.h"			/* ISP_VEC_ELEMBITS */
18 #include "vamem.h"
19 #if !defined(HAS_NO_HMEM)
20 #ifndef __INLINE_HMEM__
21 #define __INLINE_HMEM__
22 #endif
23 #include "hmem.h"
24 #endif /* !defined(HAS_NO_HMEM) */
25 #define IA_CSS_INCLUDE_PARAMETERS
26 #define IA_CSS_INCLUDE_ACC_PARAMETERS
27 
28 #include "hmm.h"
29 #include "sh_css_params.h"
30 #include "ia_css_queue.h"
31 #include "sw_event_global.h"		/* Event IDs */
32 
33 #include "platform_support.h"
34 #include "assert_support.h"
35 #include "misc_support.h"	/* NOT_USED */
36 #include "math_support.h"	/* max(), min()  EVEN_FLOOR()*/
37 
38 #include "ia_css_stream.h"
39 #include "sh_css_params_internal.h"
40 #include "sh_css_param_shading.h"
41 #include "sh_css_param_dvs.h"
42 #include "ia_css_refcount.h"
43 #include "sh_css_internal.h"
44 #include "ia_css_control.h"
45 #include "ia_css_shading.h"
46 #include "sh_css_defs.h"
47 #include "sh_css_sp.h"
48 #include "ia_css_pipeline.h"
49 #include "ia_css_debug.h"
50 
51 #include "ia_css_isp_param.h"
52 #include "ia_css_isp_params.h"
53 #include "ia_css_mipi.h"
54 #include "ia_css_morph.h"
55 #include "ia_css_host_data.h"
56 #include "ia_css_pipe.h"
57 #include "ia_css_pipe_binarydesc.h"
58 
59 /* Include all kernel host interfaces for ISP1 */
60 
61 #include "anr/anr_1.0/ia_css_anr.host.h"
62 #include "cnr/cnr_1.0/ia_css_cnr.host.h"
63 #include "csc/csc_1.0/ia_css_csc.host.h"
64 #include "de/de_1.0/ia_css_de.host.h"
65 #include "dp/dp_1.0/ia_css_dp.host.h"
66 #include "bnr/bnr_1.0/ia_css_bnr.host.h"
67 #include "dvs/dvs_1.0/ia_css_dvs.host.h"
68 #include "fpn/fpn_1.0/ia_css_fpn.host.h"
69 #include "gc/gc_1.0/ia_css_gc.host.h"
70 #include "macc/macc_1.0/ia_css_macc.host.h"
71 #include "ctc/ctc_1.0/ia_css_ctc.host.h"
72 #include "ob/ob_1.0/ia_css_ob.host.h"
73 #include "raw/raw_1.0/ia_css_raw.host.h"
74 #include "fixedbds/fixedbds_1.0/ia_css_fixedbds_param.h"
75 #include "s3a/s3a_1.0/ia_css_s3a.host.h"
76 #include "sc/sc_1.0/ia_css_sc.host.h"
77 #include "sdis/sdis_1.0/ia_css_sdis.host.h"
78 #include "tnr/tnr_1.0/ia_css_tnr.host.h"
79 #include "uds/uds_1.0/ia_css_uds_param.h"
80 #include "wb/wb_1.0/ia_css_wb.host.h"
81 #include "ynr/ynr_1.0/ia_css_ynr.host.h"
82 #include "xnr/xnr_1.0/ia_css_xnr.host.h"
83 
84 /* Include additional kernel host interfaces for ISP2 */
85 
86 #include "aa/aa_2/ia_css_aa2.host.h"
87 #include "anr/anr_2/ia_css_anr2.host.h"
88 #include "bh/bh_2/ia_css_bh.host.h"
89 #include "cnr/cnr_2/ia_css_cnr2.host.h"
90 #include "ctc/ctc1_5/ia_css_ctc1_5.host.h"
91 #include "de/de_2/ia_css_de2.host.h"
92 #include "gc/gc_2/ia_css_gc2.host.h"
93 #include "sdis/sdis_2/ia_css_sdis2.host.h"
94 #include "ynr/ynr_2/ia_css_ynr2.host.h"
95 #include "fc/fc_1.0/ia_css_formats.host.h"
96 
97 #include "xnr/xnr_3.0/ia_css_xnr3.host.h"
98 
99 #if defined(HAS_OUTPUT_SYSTEM)
100 #include <components/output_system/sc_output_system_1.0/host/output_system.host.h>
101 #endif
102 
103 #include "sh_css_frac.h"
104 #include "ia_css_bufq.h"
105 
106 #define FPNTBL_BYTES(binary) \
107 	(sizeof(char) * (binary)->in_frame_info.res.height * \
108 	 (binary)->in_frame_info.padded_width)
109 
110 #define ISP2400_SCTBL_BYTES(binary) \
111 	(sizeof(unsigned short) * (binary)->sctbl_height * \
112 	 (binary)->sctbl_aligned_width_per_color * IA_CSS_SC_NUM_COLORS)
113 
114 #define ISP2401_SCTBL_BYTES(binary) \
115 	(sizeof(unsigned short) * max((binary)->sctbl_height, (binary)->sctbl_legacy_height) * \
116 			/* height should be the larger height between new api and legacy api */ \
117 	 (binary)->sctbl_aligned_width_per_color * IA_CSS_SC_NUM_COLORS)
118 
119 #define MORPH_PLANE_BYTES(binary) \
120 	(SH_CSS_MORPH_TABLE_ELEM_BYTES * (binary)->morph_tbl_aligned_width * \
121 	 (binary)->morph_tbl_height)
122 
123 /* We keep a second copy of the ptr struct for the SP to access.
124    Again, this would not be necessary on the chip. */
125 static ia_css_ptr sp_ddr_ptrs;
126 
127 /* sp group address on DDR */
128 static ia_css_ptr xmem_sp_group_ptrs;
129 
130 static ia_css_ptr xmem_sp_stage_ptrs[IA_CSS_PIPE_ID_NUM]
131 [SH_CSS_MAX_STAGES];
132 static ia_css_ptr xmem_isp_stage_ptrs[IA_CSS_PIPE_ID_NUM]
133 [SH_CSS_MAX_STAGES];
134 
135 static ia_css_ptr default_gdc_lut;
136 static int interleaved_lut_temp[4][HRT_GDC_N];
137 
138 /* END DO NOT MOVE INTO VIMALS_WORLD */
139 
140 /* Digital Zoom lookup table. See documentation for more details about the
141  * contents of this table.
142  */
143 static const int zoom_table[4][HRT_GDC_N] = {
144 	{
145 		0 << 4,   0 << 4,   0 << 4,   0 << 4,   0 << 4,   0 << 4,   0 << 4,   0 << 4,
146 		  0 << 4,   0 << 4,   0 << 4,   0 << 4,   0 << 4,   0 << 4,   0 << 4,   0 << 4,
147 		  0 << 4,   0 << 4,   0 << 4,   0 << 4,   0 << 4,   0 << 4,   0 << 4,   0 << 4,
148 		  0 << 4,   0 << 4,   0 << 4,   0 << 4,   0 << 4,   0 << 4,   0 << 4,   0 << 4,
149 		  0 << 4,   0 << 4,   0 << 4,   0 << 4,   0 << 4,   0 << 4,   0 << 4,   0 << 4,
150 		  0 << 4,   0 << 4,   0 << 4,   0 << 4,   0 << 4,   0 << 4,   0 << 4,   0 << 4,
151 		  0 << 4,   0 << 4,   0 << 4,   0 << 4,   0 << 4,   0 << 4,   0 << 4,   0 << 4,
152 		  0 << 4,   0 << 4,   0 << 4,   0 << 4,   0 << 4,   0 << 4,   0 << 4,   0 << 4,
153 		  0 << 4,   0 << 4,   0 << 4,   0 << 4,   0 << 4,   0 << 4,   0 << 4,   0 << 4,
154 		  0 << 4,   0 << 4,   0 << 4,   0 << 4,   0 << 4,   0 << 4,   0 << 4,   0 << 4,
155 		  0 << 4,   0 << 4,   0 << 4,   0 << 4,   0 << 4,   0 << 4,   0 << 4,   0 << 4,
156 		  0 << 4,   0 << 4,   0 << 4,   0 << 4,   0 << 4,   0 << 4,   0 << 4,   0 << 4,
157 		  -1 << 4,  -1 << 4,  -1 << 4,  -1 << 4,  -1 << 4,  -1 << 4,  -1 << 4,  -1 << 4,
158 		  -1 << 4,  -1 << 4,  -1 << 4,  -1 << 4,  -1 << 4,  -1 << 4,  -1 << 4,  -1 << 4,
159 		  -1 << 4,  -1 << 4,  -1 << 4,  -1 << 4,  -1 << 4,  -1 << 4,  -1 << 4,  -1 << 4,
160 		  -1 << 4,  -1 << 4,  -1 << 4,  -1 << 4,  -1 << 4,  -1 << 4,  -1 << 4,  -1 << 4,
161 		  -1 << 4,  -1 << 4,  -1 << 4,  -1 << 4,  -1 << 4,  -1 << 4,  -1 << 4,  -1 << 4,
162 		  -1 << 4,  -1 << 4,  -1 << 4,  -1 << 4,  -1 << 4,  -1 << 4,  -1 << 4,  -1 << 4,
163 		  -2 << 4,  -2 << 4,  -2 << 4,  -2 << 4,  -2 << 4,  -2 << 4,  -2 << 4,  -2 << 4,
164 		  -2 << 4,  -2 << 4,  -2 << 4,  -2 << 4,  -2 << 4,  -2 << 4,  -2 << 4,  -2 << 4,
165 		  -2 << 4,  -2 << 4,  -2 << 4,  -2 << 4,  -2 << 4,  -2 << 4,  -2 << 4,  -2 << 4,
166 		  -2 << 4,  -2 << 4,  -2 << 4,  -2 << 4,  -2 << 4,  -2 << 4,  -2 << 4,  -2 << 4,
167 		  -3 << 4,  -3 << 4,  -3 << 4,  -3 << 4,  -3 << 4,  -3 << 4,  -3 << 4,  -3 << 4,
168 		  -3 << 4,  -3 << 4,  -3 << 4,  -3 << 4,  -3 << 4,  -3 << 4,  -3 << 4,  -3 << 4,
169 		  -3 << 4,  -3 << 4,  -3 << 4,  -3 << 4,  -3 << 4,  -3 << 4,  -3 << 4,  -3 << 4,
170 		  -3 << 4,  -3 << 4,  -3 << 4,  -3 << 4,  -3 << 4,  -3 << 4,  -3 << 4,  -3 << 4,
171 		  -4 << 4,  -4 << 4,  -4 << 4,  -4 << 4,  -4 << 4,  -4 << 4,  -4 << 4,  -4 << 4,
172 		  -4 << 4,  -4 << 4,  -4 << 4,  -4 << 4,  -4 << 4,  -4 << 4,  -4 << 4,  -4 << 4,
173 		  -4 << 4,  -4 << 4,  -4 << 4,  -4 << 4,  -4 << 4,  -4 << 4,  -4 << 4,  -4 << 4,
174 		  -4 << 4,  -4 << 4,  -4 << 4,  -4 << 4,  -4 << 4,  -4 << 4,  -4 << 4,  -4 << 4,
175 		  -5 << 4,  -5 << 4,  -5 << 4,  -5 << 4,  -5 << 4,  -5 << 4,  -5 << 4,  -5 << 4,
176 		  -5 << 4,  -5 << 4,  -5 << 4,  -5 << 4,  -5 << 4,  -5 << 4,  -5 << 4,  -5 << 4,
177 		  -6 << 4,  -6 << 4,  -6 << 4,  -6 << 4,  -6 << 4,  -6 << 4,  -6 << 4,  -6 << 4,
178 		  -6 << 4,  -6 << 4,  -6 << 4,  -6 << 4,  -6 << 4,  -6 << 4,  -6 << 4,  -6 << 4,
179 		  -6 << 4,  -6 << 4,  -6 << 4,  -6 << 4,  -6 << 4,  -6 << 4,  -6 << 4,  -6 << 4,
180 		  -6 << 4,  -6 << 4,  -6 << 4,  -6 << 4,  -6 << 4,  -6 << 4,  -6 << 4,  -6 << 4,
181 		  -7 << 4,  -7 << 4,  -7 << 4,  -7 << 4,  -7 << 4,  -7 << 4,  -7 << 4,  -7 << 4,
182 		  -7 << 4,  -7 << 4,  -7 << 4,  -7 << 4,  -7 << 4,  -7 << 4,  -7 << 4,  -7 << 4,
183 		  -7 << 4,  -7 << 4,  -7 << 4,  -7 << 4,  -7 << 4,  -7 << 4,  -7 << 4,  -7 << 4,
184 		  -7 << 4,  -7 << 4,  -7 << 4,  -7 << 4,  -7 << 4,  -7 << 4,  -7 << 4,  -7 << 4,
185 		  -8 << 4,  -8 << 4,  -8 << 4,  -8 << 4,  -8 << 4,  -8 << 4,  -8 << 4,  -8 << 4,
186 		  -8 << 4,  -8 << 4,  -8 << 4,  -8 << 4,  -8 << 4,  -8 << 4,  -8 << 4,  -8 << 4,
187 		  -9 << 4,  -9 << 4,  -9 << 4,  -9 << 4,  -9 << 4,  -9 << 4,  -9 << 4,  -9 << 4,
188 		  -9 << 4,  -9 << 4,  -9 << 4,  -9 << 4,  -9 << 4,  -9 << 4,  -9 << 4,  -9 << 4,
189 		  -9 << 4,  -9 << 4,  -9 << 4,  -9 << 4,  -9 << 4,  -9 << 4,  -9 << 4,  -9 << 4,
190 		  -9 << 4,  -9 << 4,  -9 << 4,  -9 << 4,  -9 << 4,  -9 << 4,  -9 << 4,  -9 << 4,
191 		  -10 << 4, -10 << 4, -10 << 4, -10 << 4, -10 << 4, -10 << 4, -10 << 4, -10 << 4,
192 		  -10 << 4, -10 << 4, -10 << 4, -10 << 4, -10 << 4, -10 << 4, -10 << 4, -10 << 4,
193 		  -11 << 4, -11 << 4, -11 << 4, -11 << 4, -11 << 4, -11 << 4, -11 << 4, -11 << 4,
194 		  -11 << 4, -11 << 4, -11 << 4, -11 << 4, -11 << 4, -11 << 4, -11 << 4, -11 << 4,
195 		  -11 << 4, -11 << 4, -11 << 4, -11 << 4, -11 << 4, -11 << 4, -11 << 4, -11 << 4,
196 		  -11 << 4, -11 << 4, -11 << 4, -11 << 4, -11 << 4, -11 << 4, -11 << 4, -11 << 4,
197 		  -12 << 4, -12 << 4, -12 << 4, -12 << 4, -12 << 4, -12 << 4, -12 << 4, -12 << 4,
198 		  -12 << 4, -12 << 4, -12 << 4, -12 << 4, -12 << 4, -12 << 4, -12 << 4, -12 << 4,
199 		  -13 << 4, -13 << 4, -13 << 4, -13 << 4, -13 << 4, -13 << 4, -13 << 4, -13 << 4,
200 		  -13 << 4, -13 << 4, -13 << 4, -13 << 4, -13 << 4, -13 << 4, -13 << 4, -13 << 4,
201 		  -13 << 4, -13 << 4, -13 << 4, -13 << 4, -13 << 4, -13 << 4, -13 << 4, -13 << 4,
202 		  -13 << 4, -13 << 4, -13 << 4, -13 << 4, -13 << 4, -13 << 4, -13 << 4, -13 << 4,
203 		  -14 << 4, -14 << 4, -14 << 4, -14 << 4, -14 << 4, -14 << 4, -14 << 4, -14 << 4,
204 		  -14 << 4, -14 << 4, -14 << 4, -14 << 4, -14 << 4, -14 << 4, -14 << 4, -14 << 4,
205 		  -14 << 4, -14 << 4, -14 << 4, -14 << 4, -14 << 4, -14 << 4, -14 << 4, -14 << 4,
206 		  -14 << 4, -14 << 4, -14 << 4, -14 << 4, -14 << 4, -14 << 4, -14 << 4, -14 << 4,
207 		  -15 << 4, -15 << 4, -15 << 4, -15 << 4, -15 << 4, -15 << 4, -15 << 4, -15 << 4,
208 		  -15 << 4, -15 << 4, -15 << 4, -15 << 4, -15 << 4, -15 << 4, -15 << 4, -15 << 4,
209 		  -16 << 4, -16 << 4, -16 << 4, -16 << 4, -16 << 4, -16 << 4, -16 << 4, -16 << 4,
210 		  -16 << 4, -16 << 4, -16 << 4, -16 << 4, -16 << 4, -16 << 4, -16 << 4, -16 << 4,
211 		  -16 << 4, -16 << 4, -16 << 4, -16 << 4, -16 << 4, -16 << 4, -16 << 4, -16 << 4,
212 		  -16 << 4, -16 << 4, -16 << 4, -16 << 4, -16 << 4, -16 << 4, -16 << 4, -16 << 4,
213 		  -16 << 4, -16 << 4, -16 << 4, -16 << 4, -16 << 4, -16 << 4, -16 << 4, -16 << 4,
214 		  -16 << 4, -16 << 4, -16 << 4, -16 << 4, -16 << 4, -16 << 4, -16 << 4, -16 << 4,
215 		  -17 << 4, -17 << 4, -17 << 4, -17 << 4, -17 << 4, -17 << 4, -17 << 4, -17 << 4,
216 		  -17 << 4, -17 << 4, -17 << 4, -17 << 4, -17 << 4, -17 << 4, -17 << 4, -17 << 4,
217 		  -17 << 4, -17 << 4, -17 << 4, -17 << 4, -17 << 4, -17 << 4, -17 << 4, -17 << 4,
218 		  -17 << 4, -17 << 4, -17 << 4, -17 << 4, -17 << 4, -17 << 4, -17 << 4, -17 << 4,
219 		  -18 << 4, -18 << 4, -18 << 4, -18 << 4, -18 << 4, -18 << 4, -18 << 4, -18 << 4,
220 		  -18 << 4, -18 << 4, -18 << 4, -18 << 4, -18 << 4, -18 << 4, -18 << 4, -18 << 4,
221 		  -18 << 4, -18 << 4, -18 << 4, -18 << 4, -18 << 4, -18 << 4, -18 << 4, -18 << 4,
222 		  -18 << 4, -18 << 4, -18 << 4, -18 << 4, -18 << 4, -18 << 4, -18 << 4, -18 << 4,
223 		  -18 << 4, -18 << 4, -18 << 4, -18 << 4, -18 << 4, -18 << 4, -18 << 4, -18 << 4,
224 		  -18 << 4, -18 << 4, -18 << 4, -18 << 4, -18 << 4, -18 << 4, -18 << 4, -18 << 4,
225 		  -18 << 4, -18 << 4, -18 << 4, -18 << 4, -18 << 4, -18 << 4, -18 << 4, -18 << 4,
226 		  -18 << 4, -18 << 4, -18 << 4, -18 << 4, -18 << 4, -18 << 4, -18 << 4, -18 << 4,
227 		  -18 << 4, -18 << 4, -18 << 4, -18 << 4, -18 << 4, -18 << 4, -18 << 4, -18 << 4,
228 		  -18 << 4, -18 << 4, -18 << 4, -18 << 4, -18 << 4, -18 << 4, -18 << 4, -18 << 4,
229 		  -18 << 4, -18 << 4, -18 << 4, -18 << 4, -18 << 4, -18 << 4, -18 << 4, -18 << 4,
230 		  -18 << 4, -18 << 4, -18 << 4, -18 << 4, -18 << 4, -18 << 4, -18 << 4, -18 << 4,
231 		  -18 << 4, -18 << 4, -18 << 4, -18 << 4, -18 << 4, -18 << 4, -18 << 4, -18 << 4,
232 		  -18 << 4, -18 << 4, -18 << 4, -18 << 4, -18 << 4, -18 << 4, -18 << 4, -18 << 4,
233 		  -18 << 4, -18 << 4, -18 << 4, -18 << 4, -18 << 4, -18 << 4, -18 << 4, -18 << 4,
234 		  -18 << 4, -18 << 4, -18 << 4, -18 << 4, -18 << 4, -18 << 4, -18 << 4, -18 << 4,
235 		  -18 << 4, -18 << 4, -18 << 4, -18 << 4, -18 << 4, -18 << 4, -18 << 4, -18 << 4,
236 		  -18 << 4, -18 << 4, -18 << 4, -18 << 4, -18 << 4, -18 << 4, -18 << 4, -18 << 4,
237 		  -18 << 4, -18 << 4, -18 << 4, -18 << 4, -18 << 4, -18 << 4, -18 << 4, -18 << 4,
238 		  -18 << 4, -18 << 4, -18 << 4, -18 << 4, -18 << 4, -18 << 4, -18 << 4, -18 << 4,
239 		  -18 << 4, -18 << 4, -18 << 4, -18 << 4, -18 << 4, -18 << 4, -18 << 4, -18 << 4,
240 		  -18 << 4, -18 << 4, -18 << 4, -18 << 4, -18 << 4, -18 << 4, -18 << 4, -18 << 4,
241 		  -18 << 4, -18 << 4, -18 << 4, -18 << 4, -18 << 4, -18 << 4, -18 << 4, -18 << 4,
242 		  -18 << 4, -18 << 4, -18 << 4, -18 << 4, -18 << 4, -18 << 4, -18 << 4, -18 << 4,
243 		  -17 << 4, -17 << 4, -17 << 4, -17 << 4, -17 << 4, -17 << 4, -17 << 4, -17 << 4,
244 		  -17 << 4, -17 << 4, -17 << 4, -17 << 4, -17 << 4, -17 << 4, -17 << 4, -17 << 4,
245 		  -17 << 4, -17 << 4, -17 << 4, -17 << 4, -17 << 4, -17 << 4, -17 << 4, -17 << 4,
246 		  -17 << 4, -17 << 4, -17 << 4, -17 << 4, -17 << 4, -17 << 4, -17 << 4, -17 << 4,
247 		  -16 << 4, -16 << 4, -16 << 4, -16 << 4, -16 << 4, -16 << 4, -16 << 4, -16 << 4,
248 		  -16 << 4, -16 << 4, -16 << 4, -16 << 4, -16 << 4, -16 << 4, -16 << 4, -16 << 4,
249 		  -15 << 4, -15 << 4, -15 << 4, -15 << 4, -15 << 4, -15 << 4, -15 << 4, -15 << 4,
250 		  -15 << 4, -15 << 4, -15 << 4, -15 << 4, -15 << 4, -15 << 4, -15 << 4, -15 << 4,
251 		  -15 << 4, -15 << 4, -15 << 4, -15 << 4, -15 << 4, -15 << 4, -15 << 4, -15 << 4,
252 		  -15 << 4, -15 << 4, -15 << 4, -15 << 4, -15 << 4, -15 << 4, -15 << 4, -15 << 4,
253 		  -14 << 4, -14 << 4, -14 << 4, -14 << 4, -14 << 4, -14 << 4, -14 << 4, -14 << 4,
254 		  -14 << 4, -14 << 4, -14 << 4, -14 << 4, -14 << 4, -14 << 4, -14 << 4, -14 << 4,
255 		  -13 << 4, -13 << 4, -13 << 4, -13 << 4, -13 << 4, -13 << 4, -13 << 4, -13 << 4,
256 		  -13 << 4, -13 << 4, -13 << 4, -13 << 4, -13 << 4, -13 << 4, -13 << 4, -13 << 4,
257 		  -12 << 4, -12 << 4, -12 << 4, -12 << 4, -12 << 4, -12 << 4, -12 << 4, -12 << 4,
258 		  -12 << 4, -12 << 4, -12 << 4, -12 << 4, -12 << 4, -12 << 4, -12 << 4, -12 << 4,
259 		  -11 << 4, -11 << 4, -11 << 4, -11 << 4, -11 << 4, -11 << 4, -11 << 4, -11 << 4,
260 		  -11 << 4, -11 << 4, -11 << 4, -11 << 4, -11 << 4, -11 << 4, -11 << 4, -11 << 4,
261 		  -9 << 4,  -9 << 4,  -9 << 4,  -9 << 4,  -9 << 4,  -9 << 4,  -9 << 4,  -9 << 4,
262 		  -9 << 4,  -9 << 4,  -9 << 4,  -9 << 4,  -9 << 4,  -9 << 4,  -9 << 4,  -9 << 4,
263 		  -8 << 4,  -8 << 4,  -8 << 4,  -8 << 4,  -8 << 4,  -8 << 4,  -8 << 4,  -8 << 4,
264 		  -8 << 4,  -8 << 4,  -8 << 4,  -8 << 4,  -8 << 4,  -8 << 4,  -8 << 4,  -8 << 4,
265 		  -7 << 4,  -7 << 4,  -7 << 4,  -7 << 4,  -7 << 4,  -7 << 4,  -7 << 4,  -7 << 4,
266 		  -7 << 4,  -7 << 4,  -7 << 4,  -7 << 4,  -7 << 4,  -7 << 4,  -7 << 4,  -7 << 4,
267 		  -5 << 4,  -5 << 4,  -5 << 4,  -5 << 4,  -5 << 4,  -5 << 4,  -5 << 4,  -5 << 4,
268 		  -5 << 4,  -5 << 4,  -5 << 4,  -5 << 4,  -5 << 4,  -5 << 4,  -5 << 4,  -5 << 4,
269 		  -3 << 4,  -3 << 4,  -3 << 4,  -3 << 4,  -3 << 4,  -3 << 4,  -3 << 4,  -3 << 4,
270 		  -3 << 4,  -3 << 4,  -3 << 4,  -3 << 4,  -3 << 4,  -3 << 4,  -3 << 4,  -3 << 4,
271 		  -1 << 4,  -1 << 4,  -1 << 4,  -1 << 4,  -1 << 4,  -1 << 4,  -1 << 4,  -1 << 4,
272 		  -1 << 4,  -1 << 4,  -1 << 4,  -1 << 4,  -1 << 4,  -1 << 4,  -1 << 4,  -1 << 4
273 	},
274 	{
275 		0 << 4,   0 << 4,   0 << 4,   0 << 4,   0 << 4,   0 << 4,   0 << 4,   0 << 4,
276 		  0 << 4,   0 << 4,   0 << 4,   0 << 4,   0 << 4,   0 << 4,   0 << 4,   0 << 4,
277 		  2 << 4,   2 << 4,   2 << 4,   2 << 4,   2 << 4,   2 << 4,   2 << 4,   2 << 4,
278 		  2 << 4,   2 << 4,   2 << 4,   2 << 4,   2 << 4,   2 << 4,   2 << 4,   2 << 4,
279 		  4 << 4,   4 << 4,   4 << 4,   4 << 4,   4 << 4,   4 << 4,   4 << 4,   4 << 4,
280 		  4 << 4,   4 << 4,   4 << 4,   4 << 4,   4 << 4,   4 << 4,   4 << 4,   4 << 4,
281 		  7 << 4,   7 << 4,   7 << 4,   7 << 4,   7 << 4,   7 << 4,   7 << 4,   7 << 4,
282 		  7 << 4,   7 << 4,   7 << 4,   7 << 4,   7 << 4,   7 << 4,   7 << 4,   7 << 4,
283 		  9 << 4,   9 << 4,   9 << 4,   9 << 4,   9 << 4,   9 << 4,   9 << 4,   9 << 4,
284 		  9 << 4,   9 << 4,   9 << 4,   9 << 4,   9 << 4,   9 << 4,   9 << 4,   9 << 4,
285 		  12 << 4,  12 << 4,  12 << 4,  12 << 4,  12 << 4,  12 << 4,  12 << 4,  12 << 4,
286 		  12 << 4,  12 << 4,  12 << 4,  12 << 4,  12 << 4,  12 << 4,  12 << 4,  12 << 4,
287 		  16 << 4,  16 << 4,  16 << 4,  16 << 4,  16 << 4,  16 << 4,  16 << 4,  16 << 4,
288 		  16 << 4,  16 << 4,  16 << 4,  16 << 4,  16 << 4,  16 << 4,  16 << 4,  16 << 4,
289 		  19 << 4,  19 << 4,  19 << 4,  19 << 4,  19 << 4,  19 << 4,  19 << 4,  19 << 4,
290 		  19 << 4,  19 << 4,  19 << 4,  19 << 4,  19 << 4,  19 << 4,  19 << 4,  19 << 4,
291 		  23 << 4,  23 << 4,  23 << 4,  23 << 4,  23 << 4,  23 << 4,  23 << 4,  23 << 4,
292 		  23 << 4,  23 << 4,  23 << 4,  23 << 4,  23 << 4,  23 << 4,  23 << 4,  23 << 4,
293 		  27 << 4,  27 << 4,  27 << 4,  27 << 4,  27 << 4,  27 << 4,  27 << 4,  27 << 4,
294 		  27 << 4,  27 << 4,  27 << 4,  27 << 4,  27 << 4,  27 << 4,  27 << 4,  27 << 4,
295 		  31 << 4,  31 << 4,  31 << 4,  31 << 4,  31 << 4,  31 << 4,  31 << 4,  31 << 4,
296 		  31 << 4,  31 << 4,  31 << 4,  31 << 4,  31 << 4,  31 << 4,  31 << 4,  31 << 4,
297 		  35 << 4,  35 << 4,  35 << 4,  35 << 4,  35 << 4,  35 << 4,  35 << 4,  35 << 4,
298 		  35 << 4,  35 << 4,  35 << 4,  35 << 4,  35 << 4,  35 << 4,  35 << 4,  35 << 4,
299 		  39 << 4,  39 << 4,  39 << 4,  39 << 4,  39 << 4,  39 << 4,  39 << 4,  39 << 4,
300 		  39 << 4,  39 << 4,  39 << 4,  39 << 4,  39 << 4,  39 << 4,  39 << 4,  39 << 4,
301 		  43 << 4,  43 << 4,  43 << 4,  43 << 4,  43 << 4,  43 << 4,  43 << 4,  43 << 4,
302 		  43 << 4,  43 << 4,  43 << 4,  43 << 4,  43 << 4,  43 << 4,  43 << 4,  43 << 4,
303 		  48 << 4,  48 << 4,  48 << 4,  48 << 4,  48 << 4,  48 << 4,  48 << 4,  48 << 4,
304 		  48 << 4,  48 << 4,  48 << 4,  48 << 4,  48 << 4,  48 << 4,  48 << 4,  48 << 4,
305 		  53 << 4,  53 << 4,  53 << 4,  53 << 4,  53 << 4,  53 << 4,  53 << 4,  53 << 4,
306 		  53 << 4,  53 << 4,  53 << 4,  53 << 4,  53 << 4,  53 << 4,  53 << 4,  53 << 4,
307 		  58 << 4,  58 << 4,  58 << 4,  58 << 4,  58 << 4,  58 << 4,  58 << 4,  58 << 4,
308 		  58 << 4,  58 << 4,  58 << 4,  58 << 4,  58 << 4,  58 << 4,  58 << 4,  58 << 4,
309 		  62 << 4,  62 << 4,  62 << 4,  62 << 4,  62 << 4,  62 << 4,  62 << 4,  62 << 4,
310 		  62 << 4,  62 << 4,  62 << 4,  62 << 4,  62 << 4,  62 << 4,  62 << 4,  62 << 4,
311 		  67 << 4,  67 << 4,  67 << 4,  67 << 4,  67 << 4,  67 << 4,  67 << 4,  67 << 4,
312 		  67 << 4,  67 << 4,  67 << 4,  67 << 4,  67 << 4,  67 << 4,  67 << 4,  67 << 4,
313 		  73 << 4,  73 << 4,  73 << 4,  73 << 4,  73 << 4,  73 << 4,  73 << 4,  73 << 4,
314 		  73 << 4,  73 << 4,  73 << 4,  73 << 4,  73 << 4,  73 << 4,  73 << 4,  73 << 4,
315 		  78 << 4,  78 << 4,  78 << 4,  78 << 4,  78 << 4,  78 << 4,  78 << 4,  78 << 4,
316 		  78 << 4,  78 << 4,  78 << 4,  78 << 4,  78 << 4,  78 << 4,  78 << 4,  78 << 4,
317 		  83 << 4,  83 << 4,  83 << 4,  83 << 4,  83 << 4,  83 << 4,  83 << 4,  83 << 4,
318 		  83 << 4,  83 << 4,  83 << 4,  83 << 4,  83 << 4,  83 << 4,  83 << 4,  83 << 4,
319 		  88 << 4,  88 << 4,  88 << 4,  88 << 4,  88 << 4,  88 << 4,  88 << 4,  88 << 4,
320 		  88 << 4,  88 << 4,  88 << 4,  88 << 4,  88 << 4,  88 << 4,  88 << 4,  88 << 4,
321 		  94 << 4,  94 << 4,  94 << 4,  94 << 4,  94 << 4,  94 << 4,  94 << 4,  94 << 4,
322 		  94 << 4,  94 << 4,  94 << 4,  94 << 4,  94 << 4,  94 << 4,  94 << 4,  94 << 4,
323 		  99 << 4,  99 << 4,  99 << 4,  99 << 4,  99 << 4,  99 << 4,  99 << 4,  99 << 4,
324 		  99 << 4,  99 << 4,  99 << 4,  99 << 4,  99 << 4,  99 << 4,  99 << 4,  99 << 4,
325 		  105 << 4, 105 << 4, 105 << 4, 105 << 4, 105 << 4, 105 << 4, 105 << 4, 105 << 4,
326 		  105 << 4, 105 << 4, 105 << 4, 105 << 4, 105 << 4, 105 << 4, 105 << 4, 105 << 4,
327 		  110 << 4, 110 << 4, 110 << 4, 110 << 4, 110 << 4, 110 << 4, 110 << 4, 110 << 4,
328 		  110 << 4, 110 << 4, 110 << 4, 110 << 4, 110 << 4, 110 << 4, 110 << 4, 110 << 4,
329 		  116 << 4, 116 << 4, 116 << 4, 116 << 4, 116 << 4, 116 << 4, 116 << 4, 116 << 4,
330 		  116 << 4, 116 << 4, 116 << 4, 116 << 4, 116 << 4, 116 << 4, 116 << 4, 116 << 4,
331 		  121 << 4, 121 << 4, 121 << 4, 121 << 4, 121 << 4, 121 << 4, 121 << 4, 121 << 4,
332 		  121 << 4, 121 << 4, 121 << 4, 121 << 4, 121 << 4, 121 << 4, 121 << 4, 121 << 4,
333 		  127 << 4, 127 << 4, 127 << 4, 127 << 4, 127 << 4, 127 << 4, 127 << 4, 127 << 4,
334 		  127 << 4, 127 << 4, 127 << 4, 127 << 4, 127 << 4, 127 << 4, 127 << 4, 127 << 4,
335 		  132 << 4, 132 << 4, 132 << 4, 132 << 4, 132 << 4, 132 << 4, 132 << 4, 132 << 4,
336 		  132 << 4, 132 << 4, 132 << 4, 132 << 4, 132 << 4, 132 << 4, 132 << 4, 132 << 4,
337 		  138 << 4, 138 << 4, 138 << 4, 138 << 4, 138 << 4, 138 << 4, 138 << 4, 138 << 4,
338 		  138 << 4, 138 << 4, 138 << 4, 138 << 4, 138 << 4, 138 << 4, 138 << 4, 138 << 4,
339 		  144 << 4, 144 << 4, 144 << 4, 144 << 4, 144 << 4, 144 << 4, 144 << 4, 144 << 4,
340 		  144 << 4, 144 << 4, 144 << 4, 144 << 4, 144 << 4, 144 << 4, 144 << 4, 144 << 4,
341 		  149 << 4, 149 << 4, 149 << 4, 149 << 4, 149 << 4, 149 << 4, 149 << 4, 149 << 4,
342 		  149 << 4, 149 << 4, 149 << 4, 149 << 4, 149 << 4, 149 << 4, 149 << 4, 149 << 4,
343 		  154 << 4, 154 << 4, 154 << 4, 154 << 4, 154 << 4, 154 << 4, 154 << 4, 154 << 4,
344 		  154 << 4, 154 << 4, 154 << 4, 154 << 4, 154 << 4, 154 << 4, 154 << 4, 154 << 4,
345 		  160 << 4, 160 << 4, 160 << 4, 160 << 4, 160 << 4, 160 << 4, 160 << 4, 160 << 4,
346 		  160 << 4, 160 << 4, 160 << 4, 160 << 4, 160 << 4, 160 << 4, 160 << 4, 160 << 4,
347 		  165 << 4, 165 << 4, 165 << 4, 165 << 4, 165 << 4, 165 << 4, 165 << 4, 165 << 4,
348 		  165 << 4, 165 << 4, 165 << 4, 165 << 4, 165 << 4, 165 << 4, 165 << 4, 165 << 4,
349 		  170 << 4, 170 << 4, 170 << 4, 170 << 4, 170 << 4, 170 << 4, 170 << 4, 170 << 4,
350 		  170 << 4, 170 << 4, 170 << 4, 170 << 4, 170 << 4, 170 << 4, 170 << 4, 170 << 4,
351 		  176 << 4, 176 << 4, 176 << 4, 176 << 4, 176 << 4, 176 << 4, 176 << 4, 176 << 4,
352 		  176 << 4, 176 << 4, 176 << 4, 176 << 4, 176 << 4, 176 << 4, 176 << 4, 176 << 4,
353 		  181 << 4, 181 << 4, 181 << 4, 181 << 4, 181 << 4, 181 << 4, 181 << 4, 181 << 4,
354 		  181 << 4, 181 << 4, 181 << 4, 181 << 4, 181 << 4, 181 << 4, 181 << 4, 181 << 4,
355 		  186 << 4, 186 << 4, 186 << 4, 186 << 4, 186 << 4, 186 << 4, 186 << 4, 186 << 4,
356 		  186 << 4, 186 << 4, 186 << 4, 186 << 4, 186 << 4, 186 << 4, 186 << 4, 186 << 4,
357 		  191 << 4, 191 << 4, 191 << 4, 191 << 4, 191 << 4, 191 << 4, 191 << 4, 191 << 4,
358 		  191 << 4, 191 << 4, 191 << 4, 191 << 4, 191 << 4, 191 << 4, 191 << 4, 191 << 4,
359 		  195 << 4, 195 << 4, 195 << 4, 195 << 4, 195 << 4, 195 << 4, 195 << 4, 195 << 4,
360 		  195 << 4, 195 << 4, 195 << 4, 195 << 4, 195 << 4, 195 << 4, 195 << 4, 195 << 4,
361 		  200 << 4, 200 << 4, 200 << 4, 200 << 4, 200 << 4, 200 << 4, 200 << 4, 200 << 4,
362 		  200 << 4, 200 << 4, 200 << 4, 200 << 4, 200 << 4, 200 << 4, 200 << 4, 200 << 4,
363 		  205 << 4, 205 << 4, 205 << 4, 205 << 4, 205 << 4, 205 << 4, 205 << 4, 205 << 4,
364 		  205 << 4, 205 << 4, 205 << 4, 205 << 4, 205 << 4, 205 << 4, 205 << 4, 205 << 4,
365 		  209 << 4, 209 << 4, 209 << 4, 209 << 4, 209 << 4, 209 << 4, 209 << 4, 209 << 4,
366 		  209 << 4, 209 << 4, 209 << 4, 209 << 4, 209 << 4, 209 << 4, 209 << 4, 209 << 4,
367 		  213 << 4, 213 << 4, 213 << 4, 213 << 4, 213 << 4, 213 << 4, 213 << 4, 213 << 4,
368 		  213 << 4, 213 << 4, 213 << 4, 213 << 4, 213 << 4, 213 << 4, 213 << 4, 213 << 4,
369 		  218 << 4, 218 << 4, 218 << 4, 218 << 4, 218 << 4, 218 << 4, 218 << 4, 218 << 4,
370 		  218 << 4, 218 << 4, 218 << 4, 218 << 4, 218 << 4, 218 << 4, 218 << 4, 218 << 4,
371 		  222 << 4, 222 << 4, 222 << 4, 222 << 4, 222 << 4, 222 << 4, 222 << 4, 222 << 4,
372 		  222 << 4, 222 << 4, 222 << 4, 222 << 4, 222 << 4, 222 << 4, 222 << 4, 222 << 4,
373 		  225 << 4, 225 << 4, 225 << 4, 225 << 4, 225 << 4, 225 << 4, 225 << 4, 225 << 4,
374 		  225 << 4, 225 << 4, 225 << 4, 225 << 4, 225 << 4, 225 << 4, 225 << 4, 225 << 4,
375 		  229 << 4, 229 << 4, 229 << 4, 229 << 4, 229 << 4, 229 << 4, 229 << 4, 229 << 4,
376 		  229 << 4, 229 << 4, 229 << 4, 229 << 4, 229 << 4, 229 << 4, 229 << 4, 229 << 4,
377 		  232 << 4, 232 << 4, 232 << 4, 232 << 4, 232 << 4, 232 << 4, 232 << 4, 232 << 4,
378 		  232 << 4, 232 << 4, 232 << 4, 232 << 4, 232 << 4, 232 << 4, 232 << 4, 232 << 4,
379 		  236 << 4, 236 << 4, 236 << 4, 236 << 4, 236 << 4, 236 << 4, 236 << 4, 236 << 4,
380 		  236 << 4, 236 << 4, 236 << 4, 236 << 4, 236 << 4, 236 << 4, 236 << 4, 236 << 4,
381 		  239 << 4, 239 << 4, 239 << 4, 239 << 4, 239 << 4, 239 << 4, 239 << 4, 239 << 4,
382 		  239 << 4, 239 << 4, 239 << 4, 239 << 4, 239 << 4, 239 << 4, 239 << 4, 239 << 4,
383 		  241 << 4, 241 << 4, 241 << 4, 241 << 4, 241 << 4, 241 << 4, 241 << 4, 241 << 4,
384 		  241 << 4, 241 << 4, 241 << 4, 241 << 4, 241 << 4, 241 << 4, 241 << 4, 241 << 4,
385 		  244 << 4, 244 << 4, 244 << 4, 244 << 4, 244 << 4, 244 << 4, 244 << 4, 244 << 4,
386 		  244 << 4, 244 << 4, 244 << 4, 244 << 4, 244 << 4, 244 << 4, 244 << 4, 244 << 4,
387 		  246 << 4, 246 << 4, 246 << 4, 246 << 4, 246 << 4, 246 << 4, 246 << 4, 246 << 4,
388 		  246 << 4, 246 << 4, 246 << 4, 246 << 4, 246 << 4, 246 << 4, 246 << 4, 246 << 4,
389 		  248 << 4, 248 << 4, 248 << 4, 248 << 4, 248 << 4, 248 << 4, 248 << 4, 248 << 4,
390 		  248 << 4, 248 << 4, 248 << 4, 248 << 4, 248 << 4, 248 << 4, 248 << 4, 248 << 4,
391 		  250 << 4, 250 << 4, 250 << 4, 250 << 4, 250 << 4, 250 << 4, 250 << 4, 250 << 4,
392 		  250 << 4, 250 << 4, 250 << 4, 250 << 4, 250 << 4, 250 << 4, 250 << 4, 250 << 4,
393 		  252 << 4, 252 << 4, 252 << 4, 252 << 4, 252 << 4, 252 << 4, 252 << 4, 252 << 4,
394 		  252 << 4, 252 << 4, 252 << 4, 252 << 4, 252 << 4, 252 << 4, 252 << 4, 252 << 4,
395 		  253 << 4, 253 << 4, 253 << 4, 253 << 4, 253 << 4, 253 << 4, 253 << 4, 253 << 4,
396 		  253 << 4, 253 << 4, 253 << 4, 253 << 4, 253 << 4, 253 << 4, 253 << 4, 253 << 4,
397 		  254 << 4, 254 << 4, 254 << 4, 254 << 4, 254 << 4, 254 << 4, 254 << 4, 254 << 4,
398 		  254 << 4, 254 << 4, 254 << 4, 254 << 4, 254 << 4, 254 << 4, 254 << 4, 254 << 4,
399 		  255 << 4, 255 << 4, 255 << 4, 255 << 4, 255 << 4, 255 << 4, 255 << 4, 255 << 4,
400 		  255 << 4, 255 << 4, 255 << 4, 255 << 4, 255 << 4, 255 << 4, 255 << 4, 255 << 4,
401 		  255 << 4, 255 << 4, 255 << 4, 255 << 4, 255 << 4, 255 << 4, 255 << 4, 255 << 4,
402 		  255 << 4, 255 << 4, 255 << 4, 255 << 4, 255 << 4, 255 << 4, 255 << 4, 255 << 4
403 	},
404 	{
405 		256 << 4, 256 << 4, 256 << 4, 256 << 4, 256 << 4, 256 << 4, 256 << 4, 256 << 4,
406 		    256 << 4, 256 << 4, 256 << 4, 256 << 4, 256 << 4, 256 << 4, 256 << 4, 256 << 4,
407 		    255 << 4, 255 << 4, 255 << 4, 255 << 4, 255 << 4, 255 << 4, 255 << 4, 255 << 4,
408 		    255 << 4, 255 << 4, 255 << 4, 255 << 4, 255 << 4, 255 << 4, 255 << 4, 255 << 4,
409 		    255 << 4, 255 << 4, 255 << 4, 255 << 4, 255 << 4, 255 << 4, 255 << 4, 255 << 4,
410 		    255 << 4, 255 << 4, 255 << 4, 255 << 4, 255 << 4, 255 << 4, 255 << 4, 255 << 4,
411 		    254 << 4, 254 << 4, 254 << 4, 254 << 4, 254 << 4, 254 << 4, 254 << 4, 254 << 4,
412 		    254 << 4, 254 << 4, 254 << 4, 254 << 4, 254 << 4, 254 << 4, 254 << 4, 254 << 4,
413 		    253 << 4, 253 << 4, 253 << 4, 253 << 4, 253 << 4, 253 << 4, 253 << 4, 253 << 4,
414 		    253 << 4, 253 << 4, 253 << 4, 253 << 4, 253 << 4, 253 << 4, 253 << 4, 253 << 4,
415 		    252 << 4, 252 << 4, 252 << 4, 252 << 4, 252 << 4, 252 << 4, 252 << 4, 252 << 4,
416 		    252 << 4, 252 << 4, 252 << 4, 252 << 4, 252 << 4, 252 << 4, 252 << 4, 252 << 4,
417 		    250 << 4, 250 << 4, 250 << 4, 250 << 4, 250 << 4, 250 << 4, 250 << 4, 250 << 4,
418 		    250 << 4, 250 << 4, 250 << 4, 250 << 4, 250 << 4, 250 << 4, 250 << 4, 250 << 4,
419 		    248 << 4, 248 << 4, 248 << 4, 248 << 4, 248 << 4, 248 << 4, 248 << 4, 248 << 4,
420 		    248 << 4, 248 << 4, 248 << 4, 248 << 4, 248 << 4, 248 << 4, 248 << 4, 248 << 4,
421 		    246 << 4, 246 << 4, 246 << 4, 246 << 4, 246 << 4, 246 << 4, 246 << 4, 246 << 4,
422 		    246 << 4, 246 << 4, 246 << 4, 246 << 4, 246 << 4, 246 << 4, 246 << 4, 246 << 4,
423 		    244 << 4, 244 << 4, 244 << 4, 244 << 4, 244 << 4, 244 << 4, 244 << 4, 244 << 4,
424 		    244 << 4, 244 << 4, 244 << 4, 244 << 4, 244 << 4, 244 << 4, 244 << 4, 244 << 4,
425 		    241 << 4, 241 << 4, 241 << 4, 241 << 4, 241 << 4, 241 << 4, 241 << 4, 241 << 4,
426 		    241 << 4, 241 << 4, 241 << 4, 241 << 4, 241 << 4, 241 << 4, 241 << 4, 241 << 4,
427 		    239 << 4, 239 << 4, 239 << 4, 239 << 4, 239 << 4, 239 << 4, 239 << 4, 239 << 4,
428 		    239 << 4, 239 << 4, 239 << 4, 239 << 4, 239 << 4, 239 << 4, 239 << 4, 239 << 4,
429 		    236 << 4, 236 << 4, 236 << 4, 236 << 4, 236 << 4, 236 << 4, 236 << 4, 236 << 4,
430 		    236 << 4, 236 << 4, 236 << 4, 236 << 4, 236 << 4, 236 << 4, 236 << 4, 236 << 4,
431 		    232 << 4, 232 << 4, 232 << 4, 232 << 4, 232 << 4, 232 << 4, 232 << 4, 232 << 4,
432 		    232 << 4, 232 << 4, 232 << 4, 232 << 4, 232 << 4, 232 << 4, 232 << 4, 232 << 4,
433 		    229 << 4, 229 << 4, 229 << 4, 229 << 4, 229 << 4, 229 << 4, 229 << 4, 229 << 4,
434 		    229 << 4, 229 << 4, 229 << 4, 229 << 4, 229 << 4, 229 << 4, 229 << 4, 229 << 4,
435 		    225 << 4, 225 << 4, 225 << 4, 225 << 4, 225 << 4, 225 << 4, 225 << 4, 225 << 4,
436 		    225 << 4, 225 << 4, 225 << 4, 225 << 4, 225 << 4, 225 << 4, 225 << 4, 225 << 4,
437 		    222 << 4, 222 << 4, 222 << 4, 222 << 4, 222 << 4, 222 << 4, 222 << 4, 222 << 4,
438 		    222 << 4, 222 << 4, 222 << 4, 222 << 4, 222 << 4, 222 << 4, 222 << 4, 222 << 4,
439 		    218 << 4, 218 << 4, 218 << 4, 218 << 4, 218 << 4, 218 << 4, 218 << 4, 218 << 4,
440 		    218 << 4, 218 << 4, 218 << 4, 218 << 4, 218 << 4, 218 << 4, 218 << 4, 218 << 4,
441 		    213 << 4, 213 << 4, 213 << 4, 213 << 4, 213 << 4, 213 << 4, 213 << 4, 213 << 4,
442 		    213 << 4, 213 << 4, 213 << 4, 213 << 4, 213 << 4, 213 << 4, 213 << 4, 213 << 4,
443 		    209 << 4, 209 << 4, 209 << 4, 209 << 4, 209 << 4, 209 << 4, 209 << 4, 209 << 4,
444 		    209 << 4, 209 << 4, 209 << 4, 209 << 4, 209 << 4, 209 << 4, 209 << 4, 209 << 4,
445 		    205 << 4, 205 << 4, 205 << 4, 205 << 4, 205 << 4, 205 << 4, 205 << 4, 205 << 4,
446 		    205 << 4, 205 << 4, 205 << 4, 205 << 4, 205 << 4, 205 << 4, 205 << 4, 205 << 4,
447 		    200 << 4, 200 << 4, 200 << 4, 200 << 4, 200 << 4, 200 << 4, 200 << 4, 200 << 4,
448 		    200 << 4, 200 << 4, 200 << 4, 200 << 4, 200 << 4, 200 << 4, 200 << 4, 200 << 4,
449 		    195 << 4, 195 << 4, 195 << 4, 195 << 4, 195 << 4, 195 << 4, 195 << 4, 195 << 4,
450 		    195 << 4, 195 << 4, 195 << 4, 195 << 4, 195 << 4, 195 << 4, 195 << 4, 195 << 4,
451 		    191 << 4, 191 << 4, 191 << 4, 191 << 4, 191 << 4, 191 << 4, 191 << 4, 191 << 4,
452 		    191 << 4, 191 << 4, 191 << 4, 191 << 4, 191 << 4, 191 << 4, 191 << 4, 191 << 4,
453 		    186 << 4, 186 << 4, 186 << 4, 186 << 4, 186 << 4, 186 << 4, 186 << 4, 186 << 4,
454 		    186 << 4, 186 << 4, 186 << 4, 186 << 4, 186 << 4, 186 << 4, 186 << 4, 186 << 4,
455 		    181 << 4, 181 << 4, 181 << 4, 181 << 4, 181 << 4, 181 << 4, 181 << 4, 181 << 4,
456 		    181 << 4, 181 << 4, 181 << 4, 181 << 4, 181 << 4, 181 << 4, 181 << 4, 181 << 4,
457 		    176 << 4, 176 << 4, 176 << 4, 176 << 4, 176 << 4, 176 << 4, 176 << 4, 176 << 4,
458 		    176 << 4, 176 << 4, 176 << 4, 176 << 4, 176 << 4, 176 << 4, 176 << 4, 176 << 4,
459 		    170 << 4, 170 << 4, 170 << 4, 170 << 4, 170 << 4, 170 << 4, 170 << 4, 170 << 4,
460 		    170 << 4, 170 << 4, 170 << 4, 170 << 4, 170 << 4, 170 << 4, 170 << 4, 170 << 4,
461 		    165 << 4, 165 << 4, 165 << 4, 165 << 4, 165 << 4, 165 << 4, 165 << 4, 165 << 4,
462 		    165 << 4, 165 << 4, 165 << 4, 165 << 4, 165 << 4, 165 << 4, 165 << 4, 165 << 4,
463 		    160 << 4, 160 << 4, 160 << 4, 160 << 4, 160 << 4, 160 << 4, 160 << 4, 160 << 4,
464 		    160 << 4, 160 << 4, 160 << 4, 160 << 4, 160 << 4, 160 << 4, 160 << 4, 160 << 4,
465 		    154 << 4, 154 << 4, 154 << 4, 154 << 4, 154 << 4, 154 << 4, 154 << 4, 154 << 4,
466 		    154 << 4, 154 << 4, 154 << 4, 154 << 4, 154 << 4, 154 << 4, 154 << 4, 154 << 4,
467 		    149 << 4, 149 << 4, 149 << 4, 149 << 4, 149 << 4, 149 << 4, 149 << 4, 149 << 4,
468 		    149 << 4, 149 << 4, 149 << 4, 149 << 4, 149 << 4, 149 << 4, 149 << 4, 149 << 4,
469 		    144 << 4, 144 << 4, 144 << 4, 144 << 4, 144 << 4, 144 << 4, 144 << 4, 144 << 4,
470 		    144 << 4, 144 << 4, 144 << 4, 144 << 4, 144 << 4, 144 << 4, 144 << 4, 144 << 4,
471 		    138 << 4, 138 << 4, 138 << 4, 138 << 4, 138 << 4, 138 << 4, 138 << 4, 138 << 4,
472 		    138 << 4, 138 << 4, 138 << 4, 138 << 4, 138 << 4, 138 << 4, 138 << 4, 138 << 4,
473 		    132 << 4, 132 << 4, 132 << 4, 132 << 4, 132 << 4, 132 << 4, 132 << 4, 132 << 4,
474 		    132 << 4, 132 << 4, 132 << 4, 132 << 4, 132 << 4, 132 << 4, 132 << 4, 132 << 4,
475 		    127 << 4, 127 << 4, 127 << 4, 127 << 4, 127 << 4, 127 << 4, 127 << 4, 127 << 4,
476 		    127 << 4, 127 << 4, 127 << 4, 127 << 4, 127 << 4, 127 << 4, 127 << 4, 127 << 4,
477 		    121 << 4, 121 << 4, 121 << 4, 121 << 4, 121 << 4, 121 << 4, 121 << 4, 121 << 4,
478 		    121 << 4, 121 << 4, 121 << 4, 121 << 4, 121 << 4, 121 << 4, 121 << 4, 121 << 4,
479 		    116 << 4, 116 << 4, 116 << 4, 116 << 4, 116 << 4, 116 << 4, 116 << 4, 116 << 4,
480 		    116 << 4, 116 << 4, 116 << 4, 116 << 4, 116 << 4, 116 << 4, 116 << 4, 116 << 4,
481 		    110 << 4, 110 << 4, 110 << 4, 110 << 4, 110 << 4, 110 << 4, 110 << 4, 110 << 4,
482 		    110 << 4, 110 << 4, 110 << 4, 110 << 4, 110 << 4, 110 << 4, 110 << 4, 110 << 4,
483 		    105 << 4, 105 << 4, 105 << 4, 105 << 4, 105 << 4, 105 << 4, 105 << 4, 105 << 4,
484 		    105 << 4, 105 << 4, 105 << 4, 105 << 4, 105 << 4, 105 << 4, 105 << 4, 105 << 4,
485 		    99 << 4,  99 << 4,  99 << 4,  99 << 4,  99 << 4,  99 << 4,  99 << 4,  99 << 4,
486 		    99 << 4,  99 << 4,  99 << 4,  99 << 4,  99 << 4,  99 << 4,  99 << 4,  99 << 4,
487 		    94 << 4,  94 << 4,  94 << 4,  94 << 4,  94 << 4,  94 << 4,  94 << 4,  94 << 4,
488 		    94 << 4,  94 << 4,  94 << 4,  94 << 4,  94 << 4,  94 << 4,  94 << 4,  94 << 4,
489 		    88 << 4,  88 << 4,  88 << 4,  88 << 4,  88 << 4,  88 << 4,  88 << 4,  88 << 4,
490 		    88 << 4,  88 << 4,  88 << 4,  88 << 4,  88 << 4,  88 << 4,  88 << 4,  88 << 4,
491 		    83 << 4,  83 << 4,  83 << 4,  83 << 4,  83 << 4,  83 << 4,  83 << 4,  83 << 4,
492 		    83 << 4,  83 << 4,  83 << 4,  83 << 4,  83 << 4,  83 << 4,  83 << 4,  83 << 4,
493 		    78 << 4,  78 << 4,  78 << 4,  78 << 4,  78 << 4,  78 << 4,  78 << 4,  78 << 4,
494 		    78 << 4,  78 << 4,  78 << 4,  78 << 4,  78 << 4,  78 << 4,  78 << 4,  78 << 4,
495 		    73 << 4,  73 << 4,  73 << 4,  73 << 4,  73 << 4,  73 << 4,  73 << 4,  73 << 4,
496 		    73 << 4,  73 << 4,  73 << 4,  73 << 4,  73 << 4,  73 << 4,  73 << 4,  73 << 4,
497 		    67 << 4,  67 << 4,  67 << 4,  67 << 4,  67 << 4,  67 << 4,  67 << 4,  67 << 4,
498 		    67 << 4,  67 << 4,  67 << 4,  67 << 4,  67 << 4,  67 << 4,  67 << 4,  67 << 4,
499 		    62 << 4,  62 << 4,  62 << 4,  62 << 4,  62 << 4,  62 << 4,  62 << 4,  62 << 4,
500 		    62 << 4,  62 << 4,  62 << 4,  62 << 4,  62 << 4,  62 << 4,  62 << 4,  62 << 4,
501 		    58 << 4,  58 << 4,  58 << 4,  58 << 4,  58 << 4,  58 << 4,  58 << 4,  58 << 4,
502 		    58 << 4,  58 << 4,  58 << 4,  58 << 4,  58 << 4,  58 << 4,  58 << 4,  58 << 4,
503 		    53 << 4,  53 << 4,  53 << 4,  53 << 4,  53 << 4,  53 << 4,  53 << 4,  53 << 4,
504 		    53 << 4,  53 << 4,  53 << 4,  53 << 4,  53 << 4,  53 << 4,  53 << 4,  53 << 4,
505 		    48 << 4,  48 << 4,  48 << 4,  48 << 4,  48 << 4,  48 << 4,  48 << 4,  48 << 4,
506 		    48 << 4,  48 << 4,  48 << 4,  48 << 4,  48 << 4,  48 << 4,  48 << 4,  48 << 4,
507 		    43 << 4,  43 << 4,  43 << 4,  43 << 4,  43 << 4,  43 << 4,  43 << 4,  43 << 4,
508 		    43 << 4,  43 << 4,  43 << 4,  43 << 4,  43 << 4,  43 << 4,  43 << 4,  43 << 4,
509 		    39 << 4,  39 << 4,  39 << 4,  39 << 4,  39 << 4,  39 << 4,  39 << 4,  39 << 4,
510 		    39 << 4,  39 << 4,  39 << 4,  39 << 4,  39 << 4,  39 << 4,  39 << 4,  39 << 4,
511 		    35 << 4,  35 << 4,  35 << 4,  35 << 4,  35 << 4,  35 << 4,  35 << 4,  35 << 4,
512 		    35 << 4,  35 << 4,  35 << 4,  35 << 4,  35 << 4,  35 << 4,  35 << 4,  35 << 4,
513 		    31 << 4,  31 << 4,  31 << 4,  31 << 4,  31 << 4,  31 << 4,  31 << 4,  31 << 4,
514 		    31 << 4,  31 << 4,  31 << 4,  31 << 4,  31 << 4,  31 << 4,  31 << 4,  31 << 4,
515 		    27 << 4,  27 << 4,  27 << 4,  27 << 4,  27 << 4,  27 << 4,  27 << 4,  27 << 4,
516 		    27 << 4,  27 << 4,  27 << 4,  27 << 4,  27 << 4,  27 << 4,  27 << 4,  27 << 4,
517 		    23 << 4,  23 << 4,  23 << 4,  23 << 4,  23 << 4,  23 << 4,  23 << 4,  23 << 4,
518 		    23 << 4,  23 << 4,  23 << 4,  23 << 4,  23 << 4,  23 << 4,  23 << 4,  23 << 4,
519 		    19 << 4,  19 << 4,  19 << 4,  19 << 4,  19 << 4,  19 << 4,  19 << 4,  19 << 4,
520 		    19 << 4,  19 << 4,  19 << 4,  19 << 4,  19 << 4,  19 << 4,  19 << 4,  19 << 4,
521 		    16 << 4,  16 << 4,  16 << 4,  16 << 4,  16 << 4,  16 << 4,  16 << 4,  16 << 4,
522 		    16 << 4,  16 << 4,  16 << 4,  16 << 4,  16 << 4,  16 << 4,  16 << 4,  16 << 4,
523 		    12 << 4,  12 << 4,  12 << 4,  12 << 4,  12 << 4,  12 << 4,  12 << 4,  12 << 4,
524 		    12 << 4,  12 << 4,  12 << 4,  12 << 4,  12 << 4,  12 << 4,  12 << 4,  12 << 4,
525 		    9 << 4,   9 << 4,   9 << 4,   9 << 4,   9 << 4,   9 << 4,   9 << 4,   9 << 4,
526 		    9 << 4,   9 << 4,   9 << 4,   9 << 4,   9 << 4,   9 << 4,   9 << 4,   9 << 4,
527 		    7 << 4,   7 << 4,   7 << 4,   7 << 4,   7 << 4,   7 << 4,   7 << 4,   7 << 4,
528 		    7 << 4,   7 << 4,   7 << 4,   7 << 4,   7 << 4,   7 << 4,   7 << 4,   7 << 4,
529 		    4 << 4,   4 << 4,   4 << 4,   4 << 4,   4 << 4,   4 << 4,   4 << 4,   4 << 4,
530 		    4 << 4,   4 << 4,   4 << 4,   4 << 4,   4 << 4,   4 << 4,   4 << 4,   4 << 4,
531 		    2 << 4,   2 << 4,   2 << 4,   2 << 4,   2 << 4,   2 << 4,   2 << 4,   2 << 4,
532 		    2 << 4,   2 << 4,   2 << 4,   2 << 4,   2 << 4,   2 << 4,   2 << 4,   2 << 4
533 	},
534 	{
535 		0 << 4,   0 << 4,   0 << 4,   0 << 4,   0 << 4,   0 << 4,   0 << 4,   0 << 4,
536 		  0 << 4,   0 << 4,   0 << 4,   0 << 4,   0 << 4,   0 << 4,   0 << 4,   0 << 4,
537 		  -1 << 4,  -1 << 4,  -1 << 4,  -1 << 4,  -1 << 4,  -1 << 4,  -1 << 4,  -1 << 4,
538 		  -1 << 4,  -1 << 4,  -1 << 4,  -1 << 4,  -1 << 4,  -1 << 4,  -1 << 4,  -1 << 4,
539 		  -3 << 4,  -3 << 4,  -3 << 4,  -3 << 4,  -3 << 4,  -3 << 4,  -3 << 4,  -3 << 4,
540 		  -3 << 4,  -3 << 4,  -3 << 4,  -3 << 4,  -3 << 4,  -3 << 4,  -3 << 4,  -3 << 4,
541 		  -5 << 4,  -5 << 4,  -5 << 4,  -5 << 4,  -5 << 4,  -5 << 4,  -5 << 4,  -5 << 4,
542 		  -5 << 4,  -5 << 4,  -5 << 4,  -5 << 4,  -5 << 4,  -5 << 4,  -5 << 4,  -5 << 4,
543 		  -6 << 4,  -6 << 4,  -6 << 4,  -6 << 4,  -6 << 4,  -6 << 4,  -6 << 4,  -6 << 4,
544 		  -6 << 4,  -6 << 4,  -6 << 4,  -6 << 4,  -6 << 4,  -6 << 4,  -6 << 4,  -6 << 4,
545 		  -8 << 4,  -8 << 4,  -8 << 4,  -8 << 4,  -8 << 4,  -8 << 4,  -8 << 4,  -8 << 4,
546 		  -8 << 4,  -8 << 4,  -8 << 4,  -8 << 4,  -8 << 4,  -8 << 4,  -8 << 4,  -8 << 4,
547 		  -9 << 4,  -9 << 4,  -9 << 4,  -9 << 4,  -9 << 4,  -9 << 4,  -9 << 4,  -9 << 4,
548 		  -9 << 4,  -9 << 4,  -9 << 4,  -9 << 4,  -9 << 4,  -9 << 4,  -9 << 4,  -9 << 4,
549 		  -10 << 4, -10 << 4, -10 << 4, -10 << 4, -10 << 4, -10 << 4, -10 << 4, -10 << 4,
550 		  -10 << 4, -10 << 4, -10 << 4, -10 << 4, -10 << 4, -10 << 4, -10 << 4, -10 << 4,
551 		  -12 << 4, -12 << 4, -12 << 4, -12 << 4, -12 << 4, -12 << 4, -12 << 4, -12 << 4,
552 		  -12 << 4, -12 << 4, -12 << 4, -12 << 4, -12 << 4, -12 << 4, -12 << 4, -12 << 4,
553 		  -13 << 4, -13 << 4, -13 << 4, -13 << 4, -13 << 4, -13 << 4, -13 << 4, -13 << 4,
554 		  -13 << 4, -13 << 4, -13 << 4, -13 << 4, -13 << 4, -13 << 4, -13 << 4, -13 << 4,
555 		  -14 << 4, -14 << 4, -14 << 4, -14 << 4, -14 << 4, -14 << 4, -14 << 4, -14 << 4,
556 		  -14 << 4, -14 << 4, -14 << 4, -14 << 4, -14 << 4, -14 << 4, -14 << 4, -14 << 4,
557 		  -15 << 4, -15 << 4, -15 << 4, -15 << 4, -15 << 4, -15 << 4, -15 << 4, -15 << 4,
558 		  -15 << 4, -15 << 4, -15 << 4, -15 << 4, -15 << 4, -15 << 4, -15 << 4, -15 << 4,
559 		  -16 << 4, -16 << 4, -16 << 4, -16 << 4, -16 << 4, -16 << 4, -16 << 4, -16 << 4,
560 		  -16 << 4, -16 << 4, -16 << 4, -16 << 4, -16 << 4, -16 << 4, -16 << 4, -16 << 4,
561 		  -15 << 4, -15 << 4, -15 << 4, -15 << 4, -15 << 4, -15 << 4, -15 << 4, -15 << 4,
562 		  -15 << 4, -15 << 4, -15 << 4, -15 << 4, -15 << 4, -15 << 4, -15 << 4, -15 << 4,
563 		  -17 << 4, -17 << 4, -17 << 4, -17 << 4, -17 << 4, -17 << 4, -17 << 4, -17 << 4,
564 		  -17 << 4, -17 << 4, -17 << 4, -17 << 4, -17 << 4, -17 << 4, -17 << 4, -17 << 4,
565 		  -17 << 4, -17 << 4, -17 << 4, -17 << 4, -17 << 4, -17 << 4, -17 << 4, -17 << 4,
566 		  -17 << 4, -17 << 4, -17 << 4, -17 << 4, -17 << 4, -17 << 4, -17 << 4, -17 << 4,
567 		  -18 << 4, -18 << 4, -18 << 4, -18 << 4, -18 << 4, -18 << 4, -18 << 4, -18 << 4,
568 		  -18 << 4, -18 << 4, -18 << 4, -18 << 4, -18 << 4, -18 << 4, -18 << 4, -18 << 4,
569 		  -18 << 4, -18 << 4, -18 << 4, -18 << 4, -18 << 4, -18 << 4, -18 << 4, -18 << 4,
570 		  -18 << 4, -18 << 4, -18 << 4, -18 << 4, -18 << 4, -18 << 4, -18 << 4, -18 << 4,
571 		  -17 << 4, -17 << 4, -17 << 4, -17 << 4, -17 << 4, -17 << 4, -17 << 4, -17 << 4,
572 		  -17 << 4, -17 << 4, -17 << 4, -17 << 4, -17 << 4, -17 << 4, -17 << 4, -17 << 4,
573 		  -19 << 4, -19 << 4, -19 << 4, -19 << 4, -19 << 4, -19 << 4, -19 << 4, -19 << 4,
574 		  -19 << 4, -19 << 4, -19 << 4, -19 << 4, -19 << 4, -19 << 4, -19 << 4, -19 << 4,
575 		  -19 << 4, -19 << 4, -19 << 4, -19 << 4, -19 << 4, -19 << 4, -19 << 4, -19 << 4,
576 		  -19 << 4, -19 << 4, -19 << 4, -19 << 4, -19 << 4, -19 << 4, -19 << 4, -19 << 4,
577 		  -18 << 4, -18 << 4, -18 << 4, -18 << 4, -18 << 4, -18 << 4, -18 << 4, -18 << 4,
578 		  -18 << 4, -18 << 4, -18 << 4, -18 << 4, -18 << 4, -18 << 4, -18 << 4, -18 << 4,
579 		  -18 << 4, -18 << 4, -18 << 4, -18 << 4, -18 << 4, -18 << 4, -18 << 4, -18 << 4,
580 		  -18 << 4, -18 << 4, -18 << 4, -18 << 4, -18 << 4, -18 << 4, -18 << 4, -18 << 4,
581 		  -19 << 4, -19 << 4, -19 << 4, -19 << 4, -19 << 4, -19 << 4, -19 << 4, -19 << 4,
582 		  -19 << 4, -19 << 4, -19 << 4, -19 << 4, -19 << 4, -19 << 4, -19 << 4, -19 << 4,
583 		  -18 << 4, -18 << 4, -18 << 4, -18 << 4, -18 << 4, -18 << 4, -18 << 4, -18 << 4,
584 		  -18 << 4, -18 << 4, -18 << 4, -18 << 4, -18 << 4, -18 << 4, -18 << 4, -18 << 4,
585 		  -19 << 4, -19 << 4, -19 << 4, -19 << 4, -19 << 4, -19 << 4, -19 << 4, -19 << 4,
586 		  -19 << 4, -19 << 4, -19 << 4, -19 << 4, -19 << 4, -19 << 4, -19 << 4, -19 << 4,
587 		  -18 << 4, -18 << 4, -18 << 4, -18 << 4, -18 << 4, -18 << 4, -18 << 4, -18 << 4,
588 		  -18 << 4, -18 << 4, -18 << 4, -18 << 4, -18 << 4, -18 << 4, -18 << 4, -18 << 4,
589 		  -17 << 4, -17 << 4, -17 << 4, -17 << 4, -17 << 4, -17 << 4, -17 << 4, -17 << 4,
590 		  -17 << 4, -17 << 4, -17 << 4, -17 << 4, -17 << 4, -17 << 4, -17 << 4, -17 << 4,
591 		  -17 << 4, -17 << 4, -17 << 4, -17 << 4, -17 << 4, -17 << 4, -17 << 4, -17 << 4,
592 		  -17 << 4, -17 << 4, -17 << 4, -17 << 4, -17 << 4, -17 << 4, -17 << 4, -17 << 4,
593 		  -17 << 4, -17 << 4, -17 << 4, -17 << 4, -17 << 4, -17 << 4, -17 << 4, -17 << 4,
594 		  -17 << 4, -17 << 4, -17 << 4, -17 << 4, -17 << 4, -17 << 4, -17 << 4, -17 << 4,
595 		  -16 << 4, -16 << 4, -16 << 4, -16 << 4, -16 << 4, -16 << 4, -16 << 4, -16 << 4,
596 		  -16 << 4, -16 << 4, -16 << 4, -16 << 4, -16 << 4, -16 << 4, -16 << 4, -16 << 4,
597 		  -16 << 4, -16 << 4, -16 << 4, -16 << 4, -16 << 4, -16 << 4, -16 << 4, -16 << 4,
598 		  -16 << 4, -16 << 4, -16 << 4, -16 << 4, -16 << 4, -16 << 4, -16 << 4, -16 << 4,
599 		  -16 << 4, -16 << 4, -16 << 4, -16 << 4, -16 << 4, -16 << 4, -16 << 4, -16 << 4,
600 		  -16 << 4, -16 << 4, -16 << 4, -16 << 4, -16 << 4, -16 << 4, -16 << 4, -16 << 4,
601 		  -15 << 4, -15 << 4, -15 << 4, -15 << 4, -15 << 4, -15 << 4, -15 << 4, -15 << 4,
602 		  -15 << 4, -15 << 4, -15 << 4, -15 << 4, -15 << 4, -15 << 4, -15 << 4, -15 << 4,
603 		  -14 << 4, -14 << 4, -14 << 4, -14 << 4, -14 << 4, -14 << 4, -14 << 4, -14 << 4,
604 		  -14 << 4, -14 << 4, -14 << 4, -14 << 4, -14 << 4, -14 << 4, -14 << 4, -14 << 4,
605 		  -14 << 4, -14 << 4, -14 << 4, -14 << 4, -14 << 4, -14 << 4, -14 << 4, -14 << 4,
606 		  -14 << 4, -14 << 4, -14 << 4, -14 << 4, -14 << 4, -14 << 4, -14 << 4, -14 << 4,
607 		  -13 << 4, -13 << 4, -13 << 4, -13 << 4, -13 << 4, -13 << 4, -13 << 4, -13 << 4,
608 		  -13 << 4, -13 << 4, -13 << 4, -13 << 4, -13 << 4, -13 << 4, -13 << 4, -13 << 4,
609 		  -12 << 4, -12 << 4, -12 << 4, -12 << 4, -12 << 4, -12 << 4, -12 << 4, -12 << 4,
610 		  -12 << 4, -12 << 4, -12 << 4, -12 << 4, -12 << 4, -12 << 4, -12 << 4, -12 << 4,
611 		  -12 << 4, -12 << 4, -12 << 4, -12 << 4, -12 << 4, -12 << 4, -12 << 4, -12 << 4,
612 		  -12 << 4, -12 << 4, -12 << 4, -12 << 4, -12 << 4, -12 << 4, -12 << 4, -12 << 4,
613 		  -12 << 4, -12 << 4, -12 << 4, -12 << 4, -12 << 4, -12 << 4, -12 << 4, -12 << 4,
614 		  -12 << 4, -12 << 4, -12 << 4, -12 << 4, -12 << 4, -12 << 4, -12 << 4, -12 << 4,
615 		  -11 << 4, -11 << 4, -11 << 4, -11 << 4, -11 << 4, -11 << 4, -11 << 4, -11 << 4,
616 		  -11 << 4, -11 << 4, -11 << 4, -11 << 4, -11 << 4, -11 << 4, -11 << 4, -11 << 4,
617 		  -11 << 4, -11 << 4, -11 << 4, -11 << 4, -11 << 4, -11 << 4, -11 << 4, -11 << 4,
618 		  -11 << 4, -11 << 4, -11 << 4, -11 << 4, -11 << 4, -11 << 4, -11 << 4, -11 << 4,
619 		  -9 << 4,  -9 << 4,  -9 << 4,  -9 << 4,  -9 << 4,  -9 << 4,  -9 << 4,  -9 << 4,
620 		  -9 << 4,  -9 << 4,  -9 << 4,  -9 << 4,  -9 << 4,  -9 << 4,  -9 << 4,  -9 << 4,
621 		  -9 << 4,  -9 << 4,  -9 << 4,  -9 << 4,  -9 << 4,  -9 << 4,  -9 << 4,  -9 << 4,
622 		  -9 << 4,  -9 << 4,  -9 << 4,  -9 << 4,  -9 << 4,  -9 << 4,  -9 << 4,  -9 << 4,
623 		  -9 << 4,  -9 << 4,  -9 << 4,  -9 << 4,  -9 << 4,  -9 << 4,  -9 << 4,  -9 << 4,
624 		  -9 << 4,  -9 << 4,  -9 << 4,  -9 << 4,  -9 << 4,  -9 << 4,  -9 << 4,  -9 << 4,
625 		  -8 << 4,  -8 << 4,  -8 << 4,  -8 << 4,  -8 << 4,  -8 << 4,  -8 << 4,  -8 << 4,
626 		  -8 << 4,  -8 << 4,  -8 << 4,  -8 << 4,  -8 << 4,  -8 << 4,  -8 << 4,  -8 << 4,
627 		  -6 << 4,  -6 << 4,  -6 << 4,  -6 << 4,  -6 << 4,  -6 << 4,  -6 << 4,  -6 << 4,
628 		  -6 << 4,  -6 << 4,  -6 << 4,  -6 << 4,  -6 << 4,  -6 << 4,  -6 << 4,  -6 << 4,
629 		  -6 << 4,  -6 << 4,  -6 << 4,  -6 << 4,  -6 << 4,  -6 << 4,  -6 << 4,  -6 << 4,
630 		  -6 << 4,  -6 << 4,  -6 << 4,  -6 << 4,  -6 << 4,  -6 << 4,  -6 << 4,  -6 << 4,
631 		  -6 << 4,  -6 << 4,  -6 << 4,  -6 << 4,  -6 << 4,  -6 << 4,  -6 << 4,  -6 << 4,
632 		  -6 << 4,  -6 << 4,  -6 << 4,  -6 << 4,  -6 << 4,  -6 << 4,  -6 << 4,  -6 << 4,
633 		  -5 << 4,  -5 << 4,  -5 << 4,  -5 << 4,  -5 << 4,  -5 << 4,  -5 << 4,  -5 << 4,
634 		  -5 << 4,  -5 << 4,  -5 << 4,  -5 << 4,  -5 << 4,  -5 << 4,  -5 << 4,  -5 << 4,
635 		  -4 << 4,  -4 << 4,  -4 << 4,  -4 << 4,  -4 << 4,  -4 << 4,  -4 << 4,  -4 << 4,
636 		  -4 << 4,  -4 << 4,  -4 << 4,  -4 << 4,  -4 << 4,  -4 << 4,  -4 << 4,  -4 << 4,
637 		  -3 << 4,  -3 << 4,  -3 << 4,  -3 << 4,  -3 << 4,  -3 << 4,  -3 << 4,  -3 << 4,
638 		  -3 << 4,  -3 << 4,  -3 << 4,  -3 << 4,  -3 << 4,  -3 << 4,  -3 << 4,  -3 << 4,
639 		  -4 << 4,  -4 << 4,  -4 << 4,  -4 << 4,  -4 << 4,  -4 << 4,  -4 << 4,  -4 << 4,
640 		  -4 << 4,  -4 << 4,  -4 << 4,  -4 << 4,  -4 << 4,  -4 << 4,  -4 << 4,  -4 << 4,
641 		  -3 << 4,  -3 << 4,  -3 << 4,  -3 << 4,  -3 << 4,  -3 << 4,  -3 << 4,  -3 << 4,
642 		  -3 << 4,  -3 << 4,  -3 << 4,  -3 << 4,  -3 << 4,  -3 << 4,  -3 << 4,  -3 << 4,
643 		  -2 << 4,  -2 << 4,  -2 << 4,  -2 << 4,  -2 << 4,  -2 << 4,  -2 << 4,  -2 << 4,
644 		  -2 << 4,  -2 << 4,  -2 << 4,  -2 << 4,  -2 << 4,  -2 << 4,  -2 << 4,  -2 << 4,
645 		  -2 << 4,  -2 << 4,  -2 << 4,  -2 << 4,  -2 << 4,  -2 << 4,  -2 << 4,  -2 << 4,
646 		  -2 << 4,  -2 << 4,  -2 << 4,  -2 << 4,  -2 << 4,  -2 << 4,  -2 << 4,  -2 << 4,
647 		  -1 << 4,  -1 << 4,  -1 << 4,  -1 << 4,  -1 << 4,  -1 << 4,  -1 << 4,  -1 << 4,
648 		  -1 << 4,  -1 << 4,  -1 << 4,  -1 << 4,  -1 << 4,  -1 << 4,  -1 << 4,  -1 << 4,
649 		  0 << 4,   0 << 4,   0 << 4,   0 << 4,   0 << 4,   0 << 4,   0 << 4,   0 << 4,
650 		  0 << 4,   0 << 4,   0 << 4,   0 << 4,   0 << 4,   0 << 4,   0 << 4,   0 << 4,
651 		  -1 << 4,  -1 << 4,  -1 << 4,  -1 << 4,  -1 << 4,  -1 << 4,  -1 << 4,  -1 << 4,
652 		  -1 << 4,  -1 << 4,  -1 << 4,  -1 << 4,  -1 << 4,  -1 << 4,  -1 << 4,  -1 << 4,
653 		  0 << 4,   0 << 4,   0 << 4,   0 << 4,   0 << 4,   0 << 4,   0 << 4,   0 << 4,
654 		  0 << 4,   0 << 4,   0 << 4,   0 << 4,   0 << 4,   0 << 4,   0 << 4,   0 << 4,
655 		  1 << 4,   1 << 4,   1 << 4,   1 << 4,   1 << 4,   1 << 4,   1 << 4,   1 << 4,
656 		  1 << 4,   1 << 4,   1 << 4,   1 << 4,   1 << 4,   1 << 4,   1 << 4,   1 << 4,
657 		  0 << 4,   0 << 4,   0 << 4,   0 << 4,   0 << 4,   0 << 4,   0 << 4,   0 << 4,
658 		  0 << 4,   0 << 4,   0 << 4,   0 << 4,   0 << 4,   0 << 4,   0 << 4,   0 << 4,
659 		  0 << 4,   0 << 4,   0 << 4,   0 << 4,   0 << 4,   0 << 4,   0 << 4,   0 << 4,
660 		  0 << 4,   0 << 4,   0 << 4,   0 << 4,   0 << 4,   0 << 4,   0 << 4,   0 << 4,
661 		  0 << 4,   0 << 4,   0 << 4,   0 << 4,   0 << 4,   0 << 4,   0 << 4,   0 << 4,
662 		  0 << 4,   0 << 4,   0 << 4,   0 << 4,   0 << 4,   0 << 4,   0 << 4,   0 << 4
663 	}
664 };
665 
666 static const struct ia_css_dz_config default_dz_config = {
667 	HRT_GDC_N,
668 	HRT_GDC_N,
669 	{
670 		\
671 		{0, 0}, \
672 		{0, 0}, \
673 	}
674 };
675 
676 static const struct ia_css_vector default_motion_config = {
677 	0,
678 	0
679 };
680 
681 /* ------ deprecated(bz675) : from ------ */
682 static const struct ia_css_shading_settings default_shading_settings = {
683 	1	/* enable shading table conversion in the css
684 		(This matches the legacy way.) */
685 };
686 
687 /* ------ deprecated(bz675) : to ------ */
688 
689 struct ia_css_isp_skc_dvs_statistics {
690 	ia_css_ptr p_data;
691 };
692 
693 static int
694 ref_sh_css_ddr_address_map(
695     struct sh_css_ddr_address_map *map,
696     struct sh_css_ddr_address_map *out);
697 
698 static int
699 write_ia_css_isp_parameter_set_info_to_ddr(
700     struct ia_css_isp_parameter_set_info *me,
701     ia_css_ptr *out);
702 
703 static int
704 free_ia_css_isp_parameter_set_info(ia_css_ptr ptr);
705 
706 static int
707 sh_css_params_write_to_ddr_internal(
708     struct ia_css_pipe *pipe,
709     unsigned int pipe_id,
710     struct ia_css_isp_parameters *params,
711     const struct ia_css_pipeline_stage *stage,
712     struct sh_css_ddr_address_map *ddr_map,
713     struct sh_css_ddr_address_map_size *ddr_map_size);
714 
715 static int
716 sh_css_create_isp_params(struct ia_css_stream *stream,
717 			 struct ia_css_isp_parameters **isp_params_out);
718 
719 static bool
720 sh_css_init_isp_params_from_global(struct ia_css_stream *stream,
721 				   struct ia_css_isp_parameters *params,
722 				   bool use_default_config,
723 				   struct ia_css_pipe *pipe_in);
724 
725 static int
726 sh_css_init_isp_params_from_config(struct ia_css_pipe *pipe,
727 				   struct ia_css_isp_parameters *params,
728 				   const struct ia_css_isp_config *config,
729 				   struct ia_css_pipe *pipe_in);
730 
731 static int
732 sh_css_set_global_isp_config_on_pipe(
733     struct ia_css_pipe *curr_pipe,
734     const struct ia_css_isp_config *config,
735     struct ia_css_pipe *pipe);
736 
737 #if defined(SH_CSS_ENABLE_PER_FRAME_PARAMS)
738 static int
739 sh_css_set_per_frame_isp_config_on_pipe(
740     struct ia_css_stream *stream,
741     const struct ia_css_isp_config *config,
742     struct ia_css_pipe *pipe);
743 #endif
744 
745 static int
746 sh_css_update_uds_and_crop_info_based_on_zoom_region(
747     const struct ia_css_binary_info *info,
748     const struct ia_css_frame_info *in_frame_info,
749     const struct ia_css_frame_info *out_frame_info,
750     const struct ia_css_resolution *dvs_env,
751     const struct ia_css_dz_config *zoom,
752     const struct ia_css_vector *motion_vector,
753     struct sh_css_uds_info *uds,		/* out */
754     struct sh_css_crop_pos *sp_out_crop_pos,	/* out */
755     struct ia_css_resolution pipe_in_res,
756     bool enable_zoom);
757 
758 ia_css_ptr
sh_css_params_ddr_address_map(void)759 sh_css_params_ddr_address_map(void)
760 {
761 	return sp_ddr_ptrs;
762 }
763 
764 /* ****************************************************
765  * Each coefficient is stored as 7bits to fit 2 of them into one
766  * ISP vector element, so we will store 4 coefficents on every
767  * memory word (32bits)
768  *
769  * 0: Coefficient 0 used bits
770  * 1: Coefficient 1 used bits
771  * 2: Coefficient 2 used bits
772  * 3: Coefficient 3 used bits
773  * x: not used
774  *
775  * xx33333332222222 | xx11111110000000
776  *
777  * ***************************************************
778  */
779 static struct ia_css_host_data *
convert_allocate_fpntbl(struct ia_css_isp_parameters * params)780 convert_allocate_fpntbl(struct ia_css_isp_parameters *params)
781 {
782 	unsigned int i, j;
783 	short *data_ptr;
784 	struct ia_css_host_data *me;
785 	unsigned int isp_format_data_size;
786 	u32 *isp_format_data_ptr;
787 
788 	assert(params);
789 
790 	data_ptr = params->fpn_config.data;
791 	isp_format_data_size = params->fpn_config.height * params->fpn_config.width *
792 			       sizeof(uint32_t);
793 
794 	me = ia_css_host_data_allocate(isp_format_data_size);
795 
796 	if (!me)
797 		return NULL;
798 
799 	isp_format_data_ptr = (uint32_t *)me->address;
800 
801 	for (i = 0; i < params->fpn_config.height; i++) {
802 		for (j = 0;
803 		     j < params->fpn_config.width;
804 		     j += 4, data_ptr += 4, isp_format_data_ptr++) {
805 			int data = data_ptr[0] << 0 |
806 				   data_ptr[1] << 7 |
807 				   data_ptr[2] << 16 |
808 				   data_ptr[3] << 23;
809 			*isp_format_data_ptr = data;
810 		}
811 	}
812 	return me;
813 }
814 
815 static int
store_fpntbl(struct ia_css_isp_parameters * params,ia_css_ptr ptr)816 store_fpntbl(struct ia_css_isp_parameters *params, ia_css_ptr ptr) {
817 	struct ia_css_host_data *isp_data;
818 
819 	assert(params);
820 	assert(ptr != mmgr_NULL);
821 
822 	isp_data = convert_allocate_fpntbl(params);
823 	if (!isp_data)
824 	{
825 		IA_CSS_LEAVE_ERR_PRIVATE(-ENOMEM);
826 		return -ENOMEM;
827 	}
828 	ia_css_params_store_ia_css_host_data(ptr, isp_data);
829 
830 	ia_css_host_data_free(isp_data);
831 	return 0;
832 }
833 
834 static void
convert_raw_to_fpn(struct ia_css_isp_parameters * params)835 convert_raw_to_fpn(struct ia_css_isp_parameters *params)
836 {
837 	int maxval = 0;
838 	unsigned int i;
839 
840 	assert(params);
841 
842 	/* Find the maximum value in the table */
843 	for (i = 0; i < params->fpn_config.height * params->fpn_config.width; i++) {
844 		int val = params->fpn_config.data[i];
845 		/* Make sure FPN value can be represented in 13-bit unsigned
846 		 * number (ISP precision - 1), but note that actual input range
847 		 * depends on precision of input frame data.
848 		 */
849 		if (val < 0) {
850 			/* Checkpatch patch */
851 			val = 0;
852 		} else if (val >= (1 << 13)) {
853 			/* Checkpatch patch */
854 			/* MW: BUG, is "13" a system or application property */
855 			val = (1 << 13) - 1;
856 		}
857 		maxval = max(maxval, val);
858 	}
859 	/* Find the lowest shift value to remap the values in the range
860 	 * 0..maxval to 0..2^shiftval*63.
861 	 */
862 	params->fpn_config.shift = 0;
863 	while (maxval > 63) {
864 		/* MW: BUG, is "63" a system or application property */
865 		maxval >>= 1;
866 		params->fpn_config.shift++;
867 	}
868 	/* Adjust the values in the table for the shift value */
869 	for (i = 0; i < params->fpn_config.height * params->fpn_config.width; i++)
870 		((unsigned short *)params->fpn_config.data)[i] >>= params->fpn_config.shift;
871 }
872 
873 static void
ia_css_process_kernel(struct ia_css_stream * stream,struct ia_css_isp_parameters * params,void (* process)(unsigned int pipe_id,const struct ia_css_pipeline_stage * stage,struct ia_css_isp_parameters * params))874 ia_css_process_kernel(struct ia_css_stream *stream,
875 		      struct ia_css_isp_parameters *params,
876 		      void (*process)(unsigned int pipe_id,
877 				      const struct ia_css_pipeline_stage *stage,
878 				      struct ia_css_isp_parameters *params))
879 {
880 	int i;
881 
882 	for (i = 0; i < stream->num_pipes; i++) {
883 		struct ia_css_pipe *pipe = stream->pipes[i];
884 		struct ia_css_pipeline *pipeline = ia_css_pipe_get_pipeline(pipe);
885 		struct ia_css_pipeline_stage *stage;
886 
887 		/* update the other buffers to the pipe specific copies */
888 		for (stage = pipeline->stages; stage; stage = stage->next) {
889 			if (!stage || !stage->binary) continue;
890 			process(pipeline->pipe_id, stage, params);
891 		}
892 	}
893 }
894 
895 static int
sh_css_select_dp_10bpp_config(const struct ia_css_pipe * pipe,bool * is_dp_10bpp)896 sh_css_select_dp_10bpp_config(const struct ia_css_pipe *pipe,
897 			      bool *is_dp_10bpp) {
898 	int err = 0;
899 	/* Currently we check if 10bpp DPC configuration is required based
900 	 * on the use case,i.e. if BDS and DPC is both enabled. The more cleaner
901 	 * design choice would be to expose the type of DPC (either 10bpp or 13bpp)
902 	 * using the binary info, but the current control flow does not allow this
903 	 * implementation. (This is because the configuration is set before a
904 	 * binary is selected, and the binary info is not available)
905 	 */
906 	if ((!pipe) || (!is_dp_10bpp))
907 	{
908 		IA_CSS_LEAVE_ERR_PRIVATE(-EINVAL);
909 		err = -EINVAL;
910 	} else
911 	{
912 		*is_dp_10bpp = false;
913 
914 		/* check if DPC is enabled from the host */
915 		if (pipe->config.enable_dpc) {
916 			/*check if BDS is enabled*/
917 			unsigned int required_bds_factor = SH_CSS_BDS_FACTOR_1_00;
918 
919 			if ((pipe->config.bayer_ds_out_res.width != 0) &&
920 			    (pipe->config.bayer_ds_out_res.height != 0)) {
921 				if (0 == binarydesc_calculate_bds_factor(
922 					pipe->config.input_effective_res,
923 					pipe->config.bayer_ds_out_res,
924 					&required_bds_factor)) {
925 					if (required_bds_factor != SH_CSS_BDS_FACTOR_1_00) {
926 						/*we use 10bpp BDS configuration*/
927 						*is_dp_10bpp = true;
928 					}
929 				}
930 			}
931 		}
932 	}
933 
934 	return err;
935 }
936 
937 int
sh_css_set_black_frame(struct ia_css_stream * stream,const struct ia_css_frame * raw_black_frame)938 sh_css_set_black_frame(struct ia_css_stream *stream,
939 		       const struct ia_css_frame *raw_black_frame) {
940 	struct ia_css_isp_parameters *params;
941 	/* this function desperately needs to be moved to the ISP or SP such
942 	 * that it can use the DMA.
943 	 */
944 	unsigned int height, width, y, x, k, data;
945 	ia_css_ptr ptr;
946 
947 	assert(stream);
948 	assert(raw_black_frame);
949 
950 	params = stream->isp_params_configs;
951 	height = raw_black_frame->info.res.height;
952 	width = raw_black_frame->info.padded_width,
953 
954 	ptr = raw_black_frame->data
955 	+ raw_black_frame->planes.raw.offset;
956 
957 	IA_CSS_ENTER_PRIVATE("black_frame=%p", raw_black_frame);
958 
959 	if (params->fpn_config.data &&
960 	    (params->fpn_config.width != width || params->fpn_config.height != height))
961 	{
962 		kvfree(params->fpn_config.data);
963 		params->fpn_config.data = NULL;
964 	}
965 	if (!params->fpn_config.data)
966 	{
967 		params->fpn_config.data = kvmalloc(height * width *
968 						   sizeof(short), GFP_KERNEL);
969 		if (!params->fpn_config.data) {
970 			IA_CSS_ERROR("out of memory");
971 			IA_CSS_LEAVE_ERR_PRIVATE(-ENOMEM);
972 			return -ENOMEM;
973 		}
974 		params->fpn_config.width = width;
975 		params->fpn_config.height = height;
976 		params->fpn_config.shift = 0;
977 	}
978 
979 	/* store raw to fpntbl */
980 	for (y = 0; y < height; y++)
981 	{
982 		for (x = 0; x < width; x += (ISP_VEC_NELEMS * 2)) {
983 			int ofs = y * width + x;
984 
985 			for (k = 0; k < ISP_VEC_NELEMS; k += 2) {
986 				hmm_load(ptr, (void *)(&data), sizeof(int));
987 				params->fpn_config.data[ofs + 2 * k] =
988 				    (short)(data & 0xFFFF);
989 				params->fpn_config.data[ofs + 2 * k + 2] =
990 				    (short)((data >> 16) & 0xFFFF);
991 				ptr += sizeof(int);	/* byte system address */
992 			}
993 			for (k = 0; k < ISP_VEC_NELEMS; k += 2) {
994 				hmm_load(ptr, (void *)(&data), sizeof(int));
995 				params->fpn_config.data[ofs + 2 * k + 1] =
996 				    (short)(data & 0xFFFF);
997 				params->fpn_config.data[ofs + 2 * k + 3] =
998 				    (short)((data >> 16) & 0xFFFF);
999 				ptr += sizeof(int);	/* byte system address */
1000 			}
1001 		}
1002 	}
1003 
1004 	/* raw -> fpn */
1005 	convert_raw_to_fpn(params);
1006 
1007 	/* overwrite isp parameter */
1008 	ia_css_process_kernel(stream, params, ia_css_kernel_process_param[IA_CSS_FPN_ID]);
1009 
1010 	IA_CSS_LEAVE_ERR_PRIVATE(0);
1011 
1012 	return 0;
1013 }
1014 
1015 bool
sh_css_params_set_binning_factor(struct ia_css_stream * stream,unsigned int binning_fact)1016 sh_css_params_set_binning_factor(struct ia_css_stream *stream,
1017 				 unsigned int binning_fact)
1018 {
1019 	struct ia_css_isp_parameters *params;
1020 
1021 	IA_CSS_ENTER_PRIVATE("void");
1022 	assert(stream);
1023 
1024 	params = stream->isp_params_configs;
1025 
1026 	if (params->sensor_binning != binning_fact) {
1027 		params->sensor_binning = binning_fact;
1028 		params->sc_table_changed = true;
1029 	}
1030 
1031 	IA_CSS_LEAVE_PRIVATE("void");
1032 
1033 	return params->sc_table_changed;
1034 }
1035 
1036 static void
sh_css_update_shading_table_status(struct ia_css_pipe * pipe,struct ia_css_isp_parameters * params)1037 sh_css_update_shading_table_status(struct ia_css_pipe *pipe,
1038 				   struct ia_css_isp_parameters *params)
1039 {
1040 	if (params && pipe && (pipe->pipe_num != params->sc_table_last_pipe_num)) {
1041 		params->sc_table_dirty = true;
1042 		params->sc_table_last_pipe_num = pipe->pipe_num;
1043 	}
1044 }
1045 
1046 static void
sh_css_set_shading_table(struct ia_css_stream * stream,struct ia_css_isp_parameters * params,const struct ia_css_shading_table * table)1047 sh_css_set_shading_table(struct ia_css_stream *stream,
1048 			 struct ia_css_isp_parameters *params,
1049 			 const struct ia_css_shading_table *table)
1050 {
1051 	IA_CSS_ENTER_PRIVATE("");
1052 	if (!table)
1053 		return;
1054 	assert(stream);
1055 
1056 	if (!table->enable)
1057 		table = NULL;
1058 
1059 	if ((table != params->sc_table) || params->sc_table_dirty) {
1060 		params->sc_table = table;
1061 		params->sc_table_changed = true;
1062 		params->sc_table_dirty = false;
1063 		/* Not very clean, this goes to sh_css.c to invalidate the
1064 		 * shading table for all pipes. Should replaced by a loop
1065 		 * and a pipe-specific call.
1066 		 */
1067 		if (!params->output_frame)
1068 			sh_css_invalidate_shading_tables(stream);
1069 	}
1070 
1071 	IA_CSS_LEAVE_PRIVATE("void");
1072 }
1073 
1074 void
ia_css_params_store_ia_css_host_data(ia_css_ptr ddr_addr,struct ia_css_host_data * data)1075 ia_css_params_store_ia_css_host_data(
1076     ia_css_ptr ddr_addr,
1077     struct ia_css_host_data *data)
1078 {
1079 	assert(data);
1080 	assert(data->address);
1081 	assert(ddr_addr != mmgr_NULL);
1082 
1083 	IA_CSS_ENTER_PRIVATE("");
1084 
1085 	hmm_store(ddr_addr,
1086 		   (void *)(data->address),
1087 		   (size_t)data->size);
1088 
1089 	IA_CSS_LEAVE_PRIVATE("void");
1090 }
1091 
1092 struct ia_css_host_data *
ia_css_params_alloc_convert_sctbl(const struct ia_css_pipeline_stage * stage,const struct ia_css_shading_table * shading_table)1093 ia_css_params_alloc_convert_sctbl(
1094     const struct ia_css_pipeline_stage *stage,
1095     const struct ia_css_shading_table *shading_table)
1096 {
1097 	const struct ia_css_binary *binary = stage->binary;
1098 	struct ia_css_host_data    *sctbl;
1099 	unsigned int i, j, aligned_width;
1100 	unsigned int sctbl_size;
1101 	short int    *ptr;
1102 
1103 	assert(binary);
1104 	assert(shading_table);
1105 
1106 	IA_CSS_ENTER_PRIVATE("");
1107 
1108 	if (!shading_table) {
1109 		IA_CSS_LEAVE_PRIVATE("void");
1110 		return NULL;
1111 	}
1112 
1113 	aligned_width = binary->sctbl_aligned_width_per_color;
1114 	sctbl_size = shading_table->height * IA_CSS_SC_NUM_COLORS * aligned_width *
1115 		     sizeof(short);
1116 
1117 	sctbl = ia_css_host_data_allocate((size_t)sctbl_size);
1118 
1119 	if (!sctbl)
1120 		return NULL;
1121 	ptr = (short int *)sctbl->address;
1122 	memset(ptr,
1123 	       0,
1124 	       sctbl_size);
1125 
1126 	for (i = 0; i < shading_table->height; i++) {
1127 		for (j = 0; j < IA_CSS_SC_NUM_COLORS; j++) {
1128 			memcpy(ptr,
1129 			       &shading_table->data[j]
1130 			       [i * shading_table->width],
1131 			       shading_table->width * sizeof(short));
1132 			ptr += aligned_width;
1133 		}
1134 	}
1135 
1136 	IA_CSS_LEAVE_PRIVATE("void");
1137 	return sctbl;
1138 }
1139 
ia_css_params_store_sctbl(const struct ia_css_pipeline_stage * stage,ia_css_ptr sc_tbl,const struct ia_css_shading_table * sc_config)1140 int ia_css_params_store_sctbl(
1141     const struct ia_css_pipeline_stage *stage,
1142     ia_css_ptr sc_tbl,
1143     const struct ia_css_shading_table  *sc_config)
1144 {
1145 	struct ia_css_host_data *isp_sc_tbl;
1146 
1147 	IA_CSS_ENTER_PRIVATE("");
1148 
1149 	if (!sc_config) {
1150 		IA_CSS_LEAVE_PRIVATE("void");
1151 		return 0;
1152 	}
1153 
1154 	isp_sc_tbl = ia_css_params_alloc_convert_sctbl(stage, sc_config);
1155 	if (!isp_sc_tbl) {
1156 		IA_CSS_LEAVE_ERR_PRIVATE(-ENOMEM);
1157 		return -ENOMEM;
1158 	}
1159 	/* store the shading table to ddr */
1160 	ia_css_params_store_ia_css_host_data(sc_tbl, isp_sc_tbl);
1161 	ia_css_host_data_free(isp_sc_tbl);
1162 
1163 	IA_CSS_LEAVE_PRIVATE("void");
1164 
1165 	return 0;
1166 }
1167 
1168 static void
sh_css_enable_pipeline(const struct ia_css_binary * binary)1169 sh_css_enable_pipeline(const struct ia_css_binary *binary)
1170 {
1171 	if (!binary)
1172 		return;
1173 
1174 	IA_CSS_ENTER_PRIVATE("");
1175 
1176 	ia_css_isp_param_enable_pipeline(&binary->mem_params);
1177 
1178 	IA_CSS_LEAVE_PRIVATE("void");
1179 }
1180 
1181 static int
ia_css_process_zoom_and_motion(struct ia_css_isp_parameters * params,const struct ia_css_pipeline_stage * first_stage)1182 ia_css_process_zoom_and_motion(
1183     struct ia_css_isp_parameters *params,
1184     const struct ia_css_pipeline_stage *first_stage) {
1185 	/* first_stage can be  NULL */
1186 	const struct ia_css_pipeline_stage *stage;
1187 	int err = 0;
1188 	struct ia_css_resolution pipe_in_res;
1189 
1190 	pipe_in_res.width = 0;
1191 	pipe_in_res.height = 0;
1192 
1193 	assert(params);
1194 
1195 	IA_CSS_ENTER_PRIVATE("");
1196 
1197 	/* Go through all stages to udate uds and cropping */
1198 	for (stage = first_stage; stage; stage = stage->next)
1199 	{
1200 		struct ia_css_binary *binary;
1201 		/* note: the var below is made static as it is quite large;
1202 		   if it is not static it ends up on the stack which could
1203 		   cause issues for drivers
1204 		*/
1205 		static struct ia_css_binary tmp_binary;
1206 
1207 		const struct ia_css_binary_xinfo *info = NULL;
1208 
1209 		binary = stage->binary;
1210 		if (binary) {
1211 			info = binary->info;
1212 		} else {
1213 			const struct sh_css_binary_args *args = &stage->args;
1214 			const struct ia_css_frame_info *out_infos[IA_CSS_BINARY_MAX_OUTPUT_PORTS] = {NULL};
1215 
1216 			if (args->out_frame[0])
1217 				out_infos[0] = &args->out_frame[0]->info;
1218 			info = &stage->firmware->info.isp;
1219 			ia_css_binary_fill_info(info, false, false,
1220 						ATOMISP_INPUT_FORMAT_RAW_10,
1221 						args->in_frame  ? &args->in_frame->info  : NULL,
1222 						NULL,
1223 						out_infos,
1224 						args->out_vf_frame ? &args->out_vf_frame->info
1225 						: NULL,
1226 						&tmp_binary,
1227 						NULL,
1228 						-1, true);
1229 			binary = &tmp_binary;
1230 			binary->info = info;
1231 		}
1232 
1233 		if (stage == first_stage) {
1234 			/* we will use pipe_in_res to scale the zoom crop region if needed */
1235 			pipe_in_res = binary->effective_in_frame_res;
1236 		}
1237 
1238 		assert(stage->stage_num < SH_CSS_MAX_STAGES);
1239 		if (params->dz_config.zoom_region.resolution.width == 0 &&
1240 		    params->dz_config.zoom_region.resolution.height == 0) {
1241 			sh_css_update_uds_and_crop_info(
1242 			    &info->sp,
1243 			    &binary->in_frame_info,
1244 			    &binary->out_frame_info[0],
1245 			    &binary->dvs_envelope,
1246 			    &params->dz_config,
1247 			    &params->motion_config,
1248 			    &params->uds[stage->stage_num].uds,
1249 			    &params->uds[stage->stage_num].crop_pos,
1250 			    stage->enable_zoom);
1251 		} else {
1252 			err = sh_css_update_uds_and_crop_info_based_on_zoom_region(
1253 				  &info->sp,
1254 				  &binary->in_frame_info,
1255 				  &binary->out_frame_info[0],
1256 				  &binary->dvs_envelope,
1257 				  &params->dz_config,
1258 				  &params->motion_config,
1259 				  &params->uds[stage->stage_num].uds,
1260 				  &params->uds[stage->stage_num].crop_pos,
1261 				  pipe_in_res,
1262 				  stage->enable_zoom);
1263 			if (err)
1264 				return err;
1265 		}
1266 	}
1267 	params->isp_params_changed = true;
1268 
1269 	IA_CSS_LEAVE_PRIVATE("void");
1270 	return err;
1271 }
1272 
1273 static void
sh_css_set_gamma_table(struct ia_css_isp_parameters * params,const struct ia_css_gamma_table * table)1274 sh_css_set_gamma_table(struct ia_css_isp_parameters *params,
1275 		       const struct ia_css_gamma_table *table)
1276 {
1277 	if (!table)
1278 		return;
1279 	IA_CSS_ENTER_PRIVATE("table=%p", table);
1280 
1281 	assert(params);
1282 	params->gc_table = *table;
1283 	params->config_changed[IA_CSS_GC_ID] = true;
1284 
1285 	IA_CSS_LEAVE_PRIVATE("void");
1286 }
1287 
1288 static void
sh_css_get_gamma_table(const struct ia_css_isp_parameters * params,struct ia_css_gamma_table * table)1289 sh_css_get_gamma_table(const struct ia_css_isp_parameters *params,
1290 		       struct ia_css_gamma_table *table)
1291 {
1292 	if (!table)
1293 		return;
1294 	IA_CSS_ENTER_PRIVATE("table=%p", table);
1295 
1296 	assert(params);
1297 	*table = params->gc_table;
1298 
1299 	IA_CSS_LEAVE_PRIVATE("void");
1300 }
1301 
1302 static void
sh_css_set_ctc_table(struct ia_css_isp_parameters * params,const struct ia_css_ctc_table * table)1303 sh_css_set_ctc_table(struct ia_css_isp_parameters *params,
1304 		     const struct ia_css_ctc_table *table)
1305 {
1306 	if (!table)
1307 		return;
1308 
1309 	IA_CSS_ENTER_PRIVATE("table=%p", table);
1310 
1311 	assert(params);
1312 	params->ctc_table = *table;
1313 	params->config_changed[IA_CSS_CTC_ID] = true;
1314 
1315 	IA_CSS_LEAVE_PRIVATE("void");
1316 }
1317 
1318 static void
sh_css_get_ctc_table(const struct ia_css_isp_parameters * params,struct ia_css_ctc_table * table)1319 sh_css_get_ctc_table(const struct ia_css_isp_parameters *params,
1320 		     struct ia_css_ctc_table *table)
1321 {
1322 	if (!table)
1323 		return;
1324 
1325 	IA_CSS_ENTER_PRIVATE("table=%p", table);
1326 
1327 	assert(params);
1328 	*table = params->ctc_table;
1329 
1330 	IA_CSS_LEAVE_PRIVATE("void");
1331 }
1332 
1333 static void
sh_css_set_macc_table(struct ia_css_isp_parameters * params,const struct ia_css_macc_table * table)1334 sh_css_set_macc_table(struct ia_css_isp_parameters *params,
1335 		      const struct ia_css_macc_table *table)
1336 {
1337 	if (!table)
1338 		return;
1339 
1340 	IA_CSS_ENTER_PRIVATE("table=%p", table);
1341 
1342 	assert(params);
1343 	params->macc_table = *table;
1344 	params->config_changed[IA_CSS_MACC_ID] = true;
1345 
1346 	IA_CSS_LEAVE_PRIVATE("void");
1347 }
1348 
1349 static void
sh_css_get_macc_table(const struct ia_css_isp_parameters * params,struct ia_css_macc_table * table)1350 sh_css_get_macc_table(const struct ia_css_isp_parameters *params,
1351 		      struct ia_css_macc_table *table)
1352 {
1353 	if (!table)
1354 		return;
1355 
1356 	IA_CSS_ENTER_PRIVATE("table=%p", table);
1357 
1358 	assert(params);
1359 	*table = params->macc_table;
1360 
1361 	IA_CSS_LEAVE_PRIVATE("void");
1362 }
1363 
ia_css_morph_table_free(struct ia_css_morph_table * me)1364 void ia_css_morph_table_free(
1365     struct ia_css_morph_table *me)
1366 {
1367 	unsigned int i;
1368 
1369 	if (!me)
1370 		return;
1371 
1372 	IA_CSS_ENTER("");
1373 
1374 	for (i = 0; i < IA_CSS_MORPH_TABLE_NUM_PLANES; i++) {
1375 		if (me->coordinates_x[i]) {
1376 			kvfree(me->coordinates_x[i]);
1377 			me->coordinates_x[i] = NULL;
1378 		}
1379 		if (me->coordinates_y[i]) {
1380 			kvfree(me->coordinates_y[i]);
1381 			me->coordinates_y[i] = NULL;
1382 		}
1383 	}
1384 
1385 	kvfree(me);
1386 	IA_CSS_LEAVE("void");
1387 }
1388 
ia_css_morph_table_allocate(unsigned int width,unsigned int height)1389 struct ia_css_morph_table *ia_css_morph_table_allocate(
1390     unsigned int width,
1391     unsigned int height)
1392 {
1393 	unsigned int i;
1394 	struct ia_css_morph_table *me;
1395 
1396 	IA_CSS_ENTER("");
1397 
1398 	me = kvmalloc(sizeof(*me), GFP_KERNEL);
1399 	if (!me) {
1400 		IA_CSS_ERROR("out of memory");
1401 		return me;
1402 	}
1403 
1404 	for (i = 0; i < IA_CSS_MORPH_TABLE_NUM_PLANES; i++) {
1405 		me->coordinates_x[i] = NULL;
1406 		me->coordinates_y[i] = NULL;
1407 	}
1408 
1409 	for (i = 0; i < IA_CSS_MORPH_TABLE_NUM_PLANES; i++) {
1410 		me->coordinates_x[i] = kvmalloc(height * width *
1411 						sizeof(*me->coordinates_x[i]),
1412 						GFP_KERNEL);
1413 		me->coordinates_y[i] = kvmalloc(height * width *
1414 						sizeof(*me->coordinates_y[i]),
1415 						GFP_KERNEL);
1416 
1417 		if ((!me->coordinates_x[i]) ||
1418 		    (!me->coordinates_y[i])) {
1419 			ia_css_morph_table_free(me);
1420 			me = NULL;
1421 			return me;
1422 		}
1423 	}
1424 	me->width = width;
1425 	me->height = height;
1426 	IA_CSS_LEAVE("");
1427 	return me;
1428 }
1429 
sh_css_params_default_morph_table(struct ia_css_morph_table ** table,const struct ia_css_binary * binary)1430 static int sh_css_params_default_morph_table(
1431     struct ia_css_morph_table **table,
1432     const struct ia_css_binary *binary)
1433 {
1434 	/* MW 2400 advanced requires different scaling */
1435 	unsigned int i, j, k, step, width, height;
1436 	short start_x[IA_CSS_MORPH_TABLE_NUM_PLANES] = { -8, 0, -8, 0, 0, -8 },
1437 		start_y[IA_CSS_MORPH_TABLE_NUM_PLANES] = { 0, 0, -8, -8, -8, 0 };
1438 	struct ia_css_morph_table *tab;
1439 
1440 	assert(table);
1441 	assert(binary);
1442 
1443 	IA_CSS_ENTER_PRIVATE("");
1444 
1445 	step = (ISP_VEC_NELEMS / 16) * 128,
1446 	width = binary->morph_tbl_width,
1447 	height = binary->morph_tbl_height;
1448 
1449 	tab = ia_css_morph_table_allocate(width, height);
1450 	if (!tab)
1451 		return -ENOMEM;
1452 
1453 	for (i = 0; i < IA_CSS_MORPH_TABLE_NUM_PLANES; i++) {
1454 		short val_y = start_y[i];
1455 
1456 		for (j = 0; j < height; j++) {
1457 			short val_x = start_x[i];
1458 			unsigned short *x_ptr, *y_ptr;
1459 
1460 			x_ptr = &tab->coordinates_x[i][j * width];
1461 			y_ptr = &tab->coordinates_y[i][j * width];
1462 			for (k = 0; k < width;
1463 			     k++, x_ptr++, y_ptr++, val_x += (short)step) {
1464 				if (k == 0)
1465 					*x_ptr = 0;
1466 				else if (k == width - 1)
1467 					*x_ptr = val_x + 2 * start_x[i];
1468 				else
1469 					*x_ptr = val_x;
1470 				if (j == 0)
1471 					*y_ptr = 0;
1472 				else
1473 					*y_ptr = val_y;
1474 			}
1475 			val_y += (short)step;
1476 		}
1477 	}
1478 	*table = tab;
1479 
1480 	IA_CSS_LEAVE_ERR_PRIVATE(0);
1481 
1482 	return 0;
1483 }
1484 
1485 static void
sh_css_set_morph_table(struct ia_css_isp_parameters * params,const struct ia_css_morph_table * table)1486 sh_css_set_morph_table(struct ia_css_isp_parameters *params,
1487 		       const struct ia_css_morph_table *table)
1488 {
1489 	if (!table)
1490 		return;
1491 
1492 	IA_CSS_ENTER_PRIVATE("table=%p", table);
1493 
1494 	assert(params);
1495 	if (table->enable == false)
1496 		table = NULL;
1497 	params->morph_table = table;
1498 	params->morph_table_changed = true;
1499 	IA_CSS_LEAVE_PRIVATE("void");
1500 }
1501 
1502 void
ia_css_translate_3a_statistics(struct ia_css_3a_statistics * host_stats,const struct ia_css_isp_3a_statistics_map * isp_stats)1503 ia_css_translate_3a_statistics(
1504     struct ia_css_3a_statistics               *host_stats,
1505     const struct ia_css_isp_3a_statistics_map *isp_stats)
1506 {
1507 	IA_CSS_ENTER("");
1508 	if (host_stats->grid.use_dmem) {
1509 		IA_CSS_LOG("3A: DMEM");
1510 		ia_css_s3a_dmem_decode(host_stats, isp_stats->dmem_stats);
1511 	} else {
1512 		IA_CSS_LOG("3A: VMEM");
1513 		ia_css_s3a_vmem_decode(host_stats, isp_stats->vmem_stats_hi,
1514 				       isp_stats->vmem_stats_lo);
1515 	}
1516 #if !defined(HAS_NO_HMEM)
1517 	IA_CSS_LOG("3A: HMEM");
1518 	ia_css_s3a_hmem_decode(host_stats, isp_stats->hmem_stats);
1519 #endif
1520 
1521 	IA_CSS_LEAVE("void");
1522 }
1523 
1524 void
ia_css_isp_3a_statistics_map_free(struct ia_css_isp_3a_statistics_map * me)1525 ia_css_isp_3a_statistics_map_free(struct ia_css_isp_3a_statistics_map *me)
1526 {
1527 	if (me) {
1528 		if (me->data_allocated) {
1529 			kvfree(me->data_ptr);
1530 			me->data_ptr = NULL;
1531 			me->data_allocated = false;
1532 		}
1533 		kvfree(me);
1534 	}
1535 }
1536 
1537 struct ia_css_isp_3a_statistics_map *
ia_css_isp_3a_statistics_map_allocate(const struct ia_css_isp_3a_statistics * isp_stats,void * data_ptr)1538 ia_css_isp_3a_statistics_map_allocate(
1539     const struct ia_css_isp_3a_statistics *isp_stats,
1540     void *data_ptr)
1541 {
1542 	struct ia_css_isp_3a_statistics_map *me;
1543 	/* Windows compiler does not like adding sizes to a void *
1544 	 * so we use a local char * instead. */
1545 	char *base_ptr;
1546 
1547 	me = kvmalloc(sizeof(*me), GFP_KERNEL);
1548 	if (!me) {
1549 		IA_CSS_LEAVE("cannot allocate memory");
1550 		goto err;
1551 	}
1552 
1553 	me->data_ptr = data_ptr;
1554 	me->data_allocated = !data_ptr;
1555 	if (!data_ptr) {
1556 		me->data_ptr = kvmalloc(isp_stats->size, GFP_KERNEL);
1557 		if (!me->data_ptr) {
1558 			IA_CSS_LEAVE("cannot allocate memory");
1559 			goto err;
1560 		}
1561 	}
1562 	base_ptr = me->data_ptr;
1563 
1564 	me->size = isp_stats->size;
1565 	/* GCC complains when we assign a char * to a void *, so these
1566 	 * casts are necessary unfortunately. */
1567 	me->dmem_stats    = (void *)base_ptr;
1568 	me->vmem_stats_hi = (void *)(base_ptr + isp_stats->dmem_size);
1569 	me->vmem_stats_lo = (void *)(base_ptr + isp_stats->dmem_size +
1570 				     isp_stats->vmem_size);
1571 	me->hmem_stats    = (void *)(base_ptr + isp_stats->dmem_size +
1572 				     2 * isp_stats->vmem_size);
1573 
1574 	IA_CSS_LEAVE("map=%p", me);
1575 	return me;
1576 
1577 err:
1578 	if (me)
1579 		kvfree(me);
1580 	return NULL;
1581 }
1582 
1583 int
ia_css_get_3a_statistics(struct ia_css_3a_statistics * host_stats,const struct ia_css_isp_3a_statistics * isp_stats)1584 ia_css_get_3a_statistics(struct ia_css_3a_statistics           *host_stats,
1585 			 const struct ia_css_isp_3a_statistics *isp_stats) {
1586 	struct ia_css_isp_3a_statistics_map *map;
1587 	int ret = 0;
1588 
1589 	IA_CSS_ENTER("host_stats=%p, isp_stats=%p", host_stats, isp_stats);
1590 
1591 	assert(host_stats);
1592 	assert(isp_stats);
1593 
1594 	map = ia_css_isp_3a_statistics_map_allocate(isp_stats, NULL);
1595 	if (map)
1596 	{
1597 		hmm_load(isp_stats->data_ptr, map->data_ptr, isp_stats->size);
1598 		ia_css_translate_3a_statistics(host_stats, map);
1599 		ia_css_isp_3a_statistics_map_free(map);
1600 	} else
1601 	{
1602 		IA_CSS_ERROR("out of memory");
1603 		ret = -ENOMEM;
1604 	}
1605 
1606 	IA_CSS_LEAVE_ERR(ret);
1607 	return ret;
1608 }
1609 
1610 /* Parameter encoding is not yet orthogonal.
1611    This function hnadles some of the exceptions.
1612 */
1613 static void
ia_css_set_param_exceptions(const struct ia_css_pipe * pipe,struct ia_css_isp_parameters * params)1614 ia_css_set_param_exceptions(const struct ia_css_pipe *pipe,
1615 			    struct ia_css_isp_parameters *params)
1616 {
1617 	assert(params);
1618 
1619 	/* Copy also to DP. Should be done by the driver. */
1620 	params->dp_config.gr = params->wb_config.gr;
1621 	params->dp_config.r  = params->wb_config.r;
1622 	params->dp_config.b  = params->wb_config.b;
1623 	params->dp_config.gb = params->wb_config.gb;
1624 
1625 	if (IS_ISP2401) {
1626 		assert(pipe);
1627 		assert(pipe->mode < IA_CSS_PIPE_ID_NUM);
1628 
1629 		if (pipe->mode < IA_CSS_PIPE_ID_NUM) {
1630 			params->pipe_dp_config[pipe->mode].gr = params->wb_config.gr;
1631 			params->pipe_dp_config[pipe->mode].r  = params->wb_config.r;
1632 			params->pipe_dp_config[pipe->mode].b  = params->wb_config.b;
1633 			params->pipe_dp_config[pipe->mode].gb = params->wb_config.gb;
1634 		}
1635 	}
1636 }
1637 
1638 /* ISP2401 */
1639 static void
sh_css_set_dp_config(const struct ia_css_pipe * pipe,struct ia_css_isp_parameters * params,const struct ia_css_dp_config * config)1640 sh_css_set_dp_config(const struct ia_css_pipe *pipe,
1641 		     struct ia_css_isp_parameters *params,
1642 		     const struct ia_css_dp_config *config)
1643 {
1644 	if (!config)
1645 		return;
1646 
1647 	assert(params);
1648 	assert(pipe);
1649 	assert(pipe->mode < IA_CSS_PIPE_ID_NUM);
1650 
1651 	IA_CSS_ENTER_PRIVATE("config=%p", config);
1652 	ia_css_dp_debug_dtrace(config, IA_CSS_DEBUG_TRACE_PRIVATE);
1653 	if (pipe->mode < IA_CSS_PIPE_ID_NUM) {
1654 		params->pipe_dp_config[pipe->mode] = *config;
1655 		params->pipe_dpc_config_changed[pipe->mode] = true;
1656 	}
1657 	IA_CSS_LEAVE_PRIVATE("void");
1658 }
1659 
1660 static void
sh_css_get_dp_config(const struct ia_css_pipe * pipe,const struct ia_css_isp_parameters * params,struct ia_css_dp_config * config)1661 sh_css_get_dp_config(const struct ia_css_pipe *pipe,
1662 		     const struct ia_css_isp_parameters *params,
1663 		     struct ia_css_dp_config *config)
1664 {
1665 	if (!config)
1666 		return;
1667 
1668 	assert(params);
1669 	assert(pipe);
1670 	IA_CSS_ENTER_PRIVATE("config=%p", config);
1671 
1672 	*config = params->pipe_dp_config[pipe->mode];
1673 
1674 	IA_CSS_LEAVE_PRIVATE("void");
1675 }
1676 
1677 static void
sh_css_set_nr_config(struct ia_css_isp_parameters * params,const struct ia_css_nr_config * config)1678 sh_css_set_nr_config(struct ia_css_isp_parameters *params,
1679 		     const struct ia_css_nr_config *config)
1680 {
1681 	if (!config)
1682 		return;
1683 	assert(params);
1684 
1685 	IA_CSS_ENTER_PRIVATE("config=%p", config);
1686 
1687 	ia_css_nr_debug_dtrace(config, IA_CSS_DEBUG_TRACE_PRIVATE);
1688 	params->nr_config = *config;
1689 	params->yee_config.nr = *config;
1690 	params->config_changed[IA_CSS_NR_ID]  = true;
1691 	params->config_changed[IA_CSS_YEE_ID] = true;
1692 	params->config_changed[IA_CSS_BNR_ID] = true;
1693 
1694 	IA_CSS_LEAVE_PRIVATE("void");
1695 }
1696 
1697 static void
sh_css_set_ee_config(struct ia_css_isp_parameters * params,const struct ia_css_ee_config * config)1698 sh_css_set_ee_config(struct ia_css_isp_parameters *params,
1699 		     const struct ia_css_ee_config *config)
1700 {
1701 	if (!config)
1702 		return;
1703 	assert(params);
1704 
1705 	IA_CSS_ENTER_PRIVATE("config=%p", config);
1706 	ia_css_ee_debug_dtrace(config, IA_CSS_DEBUG_TRACE_PRIVATE);
1707 
1708 	params->ee_config = *config;
1709 	params->yee_config.ee = *config;
1710 	params->config_changed[IA_CSS_YEE_ID] = true;
1711 
1712 	IA_CSS_LEAVE_PRIVATE("void");
1713 }
1714 
1715 static void
sh_css_get_ee_config(const struct ia_css_isp_parameters * params,struct ia_css_ee_config * config)1716 sh_css_get_ee_config(const struct ia_css_isp_parameters *params,
1717 		     struct ia_css_ee_config *config)
1718 {
1719 	if (!config)
1720 		return;
1721 
1722 	IA_CSS_ENTER_PRIVATE("config=%p", config);
1723 
1724 	assert(params);
1725 	*config = params->ee_config;
1726 
1727 	ia_css_ee_debug_dtrace(config, IA_CSS_DEBUG_TRACE_PRIVATE);
1728 	IA_CSS_LEAVE_PRIVATE("void");
1729 }
1730 
1731 static void
sh_css_set_pipe_dvs_6axis_config(const struct ia_css_pipe * pipe,struct ia_css_isp_parameters * params,const struct ia_css_dvs_6axis_config * dvs_config)1732 sh_css_set_pipe_dvs_6axis_config(const struct ia_css_pipe *pipe,
1733 				 struct ia_css_isp_parameters *params,
1734 				 const struct ia_css_dvs_6axis_config  *dvs_config)
1735 {
1736 	if (!dvs_config)
1737 		return;
1738 	assert(params);
1739 	assert(pipe);
1740 	assert(dvs_config->height_y == dvs_config->height_uv);
1741 	assert((dvs_config->width_y - 1) == 2 * (dvs_config->width_uv - 1));
1742 	assert(pipe->mode < IA_CSS_PIPE_ID_NUM);
1743 
1744 	IA_CSS_ENTER_PRIVATE("dvs_config=%p", dvs_config);
1745 
1746 	copy_dvs_6axis_table(params->pipe_dvs_6axis_config[pipe->mode], dvs_config);
1747 
1748 #if !defined(HAS_NO_DVS_6AXIS_CONFIG_UPDATE)
1749 	params->pipe_dvs_6axis_config_changed[pipe->mode] = true;
1750 #endif
1751 
1752 	IA_CSS_LEAVE_PRIVATE("void");
1753 }
1754 
1755 static void
sh_css_get_pipe_dvs_6axis_config(const struct ia_css_pipe * pipe,const struct ia_css_isp_parameters * params,struct ia_css_dvs_6axis_config * dvs_config)1756 sh_css_get_pipe_dvs_6axis_config(const struct ia_css_pipe *pipe,
1757 				 const struct ia_css_isp_parameters *params,
1758 				 struct ia_css_dvs_6axis_config *dvs_config)
1759 {
1760 	if (!dvs_config)
1761 		return;
1762 	assert(params);
1763 	assert(pipe);
1764 	assert(dvs_config->height_y == dvs_config->height_uv);
1765 	assert((dvs_config->width_y - 1) == 2 * dvs_config->width_uv - 1);
1766 
1767 	IA_CSS_ENTER_PRIVATE("dvs_config=%p", dvs_config);
1768 
1769 	if ((pipe->mode < IA_CSS_PIPE_ID_NUM) &&
1770 	    (dvs_config->width_y == params->pipe_dvs_6axis_config[pipe->mode]->width_y) &&
1771 	    (dvs_config->height_y == params->pipe_dvs_6axis_config[pipe->mode]->height_y) &&
1772 	    (dvs_config->width_uv == params->pipe_dvs_6axis_config[pipe->mode]->width_uv) &&
1773 	    (dvs_config->height_uv == params->pipe_dvs_6axis_config[pipe->mode]->height_uv)
1774 	    &&
1775 	    dvs_config->xcoords_y &&
1776 	    dvs_config->ycoords_y &&
1777 	    dvs_config->xcoords_uv &&
1778 	    dvs_config->ycoords_uv) {
1779 		copy_dvs_6axis_table(dvs_config, params->pipe_dvs_6axis_config[pipe->mode]);
1780 	}
1781 
1782 	IA_CSS_LEAVE_PRIVATE("void");
1783 }
1784 
1785 static void
sh_css_set_baa_config(struct ia_css_isp_parameters * params,const struct ia_css_aa_config * config)1786 sh_css_set_baa_config(struct ia_css_isp_parameters *params,
1787 		      const struct ia_css_aa_config *config)
1788 {
1789 	if (!config)
1790 		return;
1791 	assert(params);
1792 
1793 	IA_CSS_ENTER_PRIVATE("config=%p", config);
1794 
1795 	params->bds_config = *config;
1796 	params->config_changed[IA_CSS_BDS_ID] = true;
1797 
1798 	IA_CSS_LEAVE_PRIVATE("void");
1799 }
1800 
1801 static void
sh_css_get_baa_config(const struct ia_css_isp_parameters * params,struct ia_css_aa_config * config)1802 sh_css_get_baa_config(const struct ia_css_isp_parameters *params,
1803 		      struct ia_css_aa_config *config)
1804 {
1805 	if (!config)
1806 		return;
1807 	assert(params);
1808 
1809 	IA_CSS_ENTER_PRIVATE("config=%p", config);
1810 
1811 	*config = params->bds_config;
1812 
1813 	IA_CSS_LEAVE_PRIVATE("void");
1814 }
1815 
1816 static void
sh_css_set_dz_config(struct ia_css_isp_parameters * params,const struct ia_css_dz_config * config)1817 sh_css_set_dz_config(struct ia_css_isp_parameters *params,
1818 		     const struct ia_css_dz_config *config)
1819 {
1820 	if (!config)
1821 		return;
1822 	assert(params);
1823 
1824 	IA_CSS_ENTER_PRIVATE("dx=%d, dy=%d", config->dx, config->dy);
1825 
1826 	assert(config->dx <= HRT_GDC_N);
1827 	assert(config->dy <= HRT_GDC_N);
1828 
1829 	params->dz_config = *config;
1830 	params->dz_config_changed = true;
1831 	/* JK: Why isp params changed?? */
1832 	params->isp_params_changed = true;
1833 
1834 	IA_CSS_LEAVE_PRIVATE("void");
1835 }
1836 
1837 static void
sh_css_get_dz_config(const struct ia_css_isp_parameters * params,struct ia_css_dz_config * config)1838 sh_css_get_dz_config(const struct ia_css_isp_parameters *params,
1839 		     struct ia_css_dz_config *config)
1840 {
1841 	if (!config)
1842 		return;
1843 	assert(params);
1844 
1845 	IA_CSS_ENTER_PRIVATE("config=%p", config);
1846 
1847 	*config = params->dz_config;
1848 
1849 	IA_CSS_LEAVE_PRIVATE("dx=%d, dy=%d", config->dx, config->dy);
1850 }
1851 
1852 static void
sh_css_set_motion_vector(struct ia_css_isp_parameters * params,const struct ia_css_vector * motion)1853 sh_css_set_motion_vector(struct ia_css_isp_parameters *params,
1854 			 const struct ia_css_vector *motion)
1855 {
1856 	if (!motion)
1857 		return;
1858 	assert(params);
1859 
1860 	IA_CSS_ENTER_PRIVATE("x=%d, y=%d", motion->x, motion->y);
1861 
1862 	params->motion_config = *motion;
1863 	/* JK: Why do isp params change? */
1864 	params->motion_config_changed = true;
1865 	params->isp_params_changed = true;
1866 
1867 	IA_CSS_LEAVE_PRIVATE("void");
1868 }
1869 
1870 static void
sh_css_get_motion_vector(const struct ia_css_isp_parameters * params,struct ia_css_vector * motion)1871 sh_css_get_motion_vector(const struct ia_css_isp_parameters *params,
1872 			 struct ia_css_vector *motion)
1873 {
1874 	if (!motion)
1875 		return;
1876 	assert(params);
1877 
1878 	IA_CSS_ENTER_PRIVATE("motion=%p", motion);
1879 
1880 	*motion = params->motion_config;
1881 
1882 	IA_CSS_LEAVE_PRIVATE("x=%d, y=%d", motion->x, motion->y);
1883 }
1884 
1885 struct ia_css_isp_config *
sh_css_pipe_isp_config_get(struct ia_css_pipe * pipe)1886 sh_css_pipe_isp_config_get(struct ia_css_pipe *pipe)
1887 {
1888 	if (!pipe) {
1889 		IA_CSS_ERROR("pipe=%p", NULL);
1890 		return NULL;
1891 	}
1892 	return pipe->config.p_isp_config;
1893 }
1894 
1895 int
ia_css_stream_set_isp_config(struct ia_css_stream * stream,const struct ia_css_isp_config * config)1896 ia_css_stream_set_isp_config(
1897     struct ia_css_stream *stream,
1898     const struct ia_css_isp_config *config) {
1899 	return ia_css_stream_set_isp_config_on_pipe(stream, config, NULL);
1900 }
1901 
1902 int
ia_css_stream_set_isp_config_on_pipe(struct ia_css_stream * stream,const struct ia_css_isp_config * config,struct ia_css_pipe * pipe)1903 ia_css_stream_set_isp_config_on_pipe(
1904     struct ia_css_stream *stream,
1905     const struct ia_css_isp_config *config,
1906     struct ia_css_pipe *pipe) {
1907 	int err = 0;
1908 
1909 	if ((!stream) || (!config))
1910 		return -EINVAL;
1911 
1912 	IA_CSS_ENTER("stream=%p, config=%p, pipe=%p", stream, config, pipe);
1913 
1914 #if defined(SH_CSS_ENABLE_PER_FRAME_PARAMS)
1915 	if (config->output_frame)
1916 		err = sh_css_set_per_frame_isp_config_on_pipe(stream, config, pipe);
1917 	else
1918 #endif
1919 		err = sh_css_set_global_isp_config_on_pipe(stream->pipes[0], config, pipe);
1920 
1921 	IA_CSS_LEAVE_ERR(err);
1922 	return err;
1923 }
1924 
1925 int
ia_css_pipe_set_isp_config(struct ia_css_pipe * pipe,struct ia_css_isp_config * config)1926 ia_css_pipe_set_isp_config(struct ia_css_pipe *pipe,
1927 			   struct ia_css_isp_config *config) {
1928 	struct ia_css_pipe *pipe_in = pipe;
1929 	int err = 0;
1930 
1931 	IA_CSS_ENTER("pipe=%p", pipe);
1932 
1933 	if ((!pipe) || (!pipe->stream))
1934 		return -EINVAL;
1935 
1936 	ia_css_debug_dtrace(IA_CSS_DEBUG_TRACE, "config=%p\n", config);
1937 
1938 #if defined(SH_CSS_ENABLE_PER_FRAME_PARAMS)
1939 	if (config->output_frame)
1940 		err = sh_css_set_per_frame_isp_config_on_pipe(pipe->stream, config, pipe);
1941 	else
1942 #endif
1943 		err = sh_css_set_global_isp_config_on_pipe(pipe, config, pipe_in);
1944 	IA_CSS_LEAVE_ERR(err);
1945 	return err;
1946 }
1947 
1948 static int
sh_css_set_global_isp_config_on_pipe(struct ia_css_pipe * curr_pipe,const struct ia_css_isp_config * config,struct ia_css_pipe * pipe)1949 sh_css_set_global_isp_config_on_pipe(
1950     struct ia_css_pipe *curr_pipe,
1951     const struct ia_css_isp_config *config,
1952     struct ia_css_pipe *pipe) {
1953 	int err = 0;
1954 	int err1 = 0;
1955 	int err2 = 0;
1956 
1957 	IA_CSS_ENTER_PRIVATE("stream=%p, config=%p, pipe=%p", curr_pipe, config, pipe);
1958 
1959 	err1 = sh_css_init_isp_params_from_config(curr_pipe, curr_pipe->stream->isp_params_configs, config, pipe);
1960 
1961 	/* Now commit all changes to the SP */
1962 	err2 = sh_css_param_update_isp_params(curr_pipe, curr_pipe->stream->isp_params_configs, sh_css_sp_is_running(), pipe);
1963 
1964 	/* The following code is intentional. The sh_css_init_isp_params_from_config interface
1965 	 * throws an error when both DPC and BDS is enabled. The CSS API must pass this error
1966 	 * information to the caller, ie. the host. We do not return this error immediately,
1967 	 * but instead continue with updating the ISP params to enable testing of features
1968 	 * which are currently in TR phase. */
1969 
1970 	err = (err1 != 0) ? err1 : ((err2 != 0) ? err2 : err);
1971 
1972 	IA_CSS_LEAVE_ERR_PRIVATE(err);
1973 	return err;
1974 }
1975 
1976 #if defined(SH_CSS_ENABLE_PER_FRAME_PARAMS)
1977 static int
sh_css_set_per_frame_isp_config_on_pipe(struct ia_css_stream * stream,const struct ia_css_isp_config * config,struct ia_css_pipe * pipe)1978 sh_css_set_per_frame_isp_config_on_pipe(
1979     struct ia_css_stream *stream,
1980     const struct ia_css_isp_config *config,
1981     struct ia_css_pipe *pipe) {
1982 	unsigned int i;
1983 	bool per_frame_config_created = false;
1984 	int err = 0;
1985 	int err1 = 0;
1986 	int err2 = 0;
1987 	int err3 = 0;
1988 
1989 	struct sh_css_ddr_address_map *ddr_ptrs;
1990 	struct sh_css_ddr_address_map_size *ddr_ptrs_size;
1991 	struct ia_css_isp_parameters *params;
1992 
1993 	IA_CSS_ENTER_PRIVATE("stream=%p, config=%p, pipe=%p", stream, config, pipe);
1994 
1995 	if (!pipe)
1996 	{
1997 		err = -EINVAL;
1998 		goto exit;
1999 	}
2000 
2001 	/* create per-frame ISP params object with default values
2002 	 * from stream->isp_params_configs if one doesn't already exist
2003 	*/
2004 	if (!stream->per_frame_isp_params_configs)
2005 	{
2006 		err = sh_css_create_isp_params(stream,
2007 					       &stream->per_frame_isp_params_configs);
2008 		if (err)
2009 			goto exit;
2010 		per_frame_config_created = true;
2011 	}
2012 
2013 	params = stream->per_frame_isp_params_configs;
2014 
2015 	/* update new ISP params object with the new config */
2016 	if (!sh_css_init_isp_params_from_global(stream, params, false, pipe))
2017 	{
2018 		err1 = -EINVAL;
2019 	}
2020 
2021 	err2 = sh_css_init_isp_params_from_config(stream->pipes[0], params, config, pipe);
2022 
2023 	if (per_frame_config_created)
2024 	{
2025 		ddr_ptrs = &params->ddr_ptrs;
2026 		ddr_ptrs_size = &params->ddr_ptrs_size;
2027 		/* create per pipe reference to general ddr_ptrs */
2028 		for (i = 0; i < IA_CSS_PIPE_ID_NUM; i++) {
2029 			ref_sh_css_ddr_address_map(ddr_ptrs, &params->pipe_ddr_ptrs[i]);
2030 			params->pipe_ddr_ptrs_size[i] = *ddr_ptrs_size;
2031 		}
2032 	}
2033 
2034 	/* now commit to ddr */
2035 	err3 = sh_css_param_update_isp_params(stream->pipes[0], params, sh_css_sp_is_running(), pipe);
2036 
2037 	/* The following code is intentional. The sh_css_init_sp_params_from_config and
2038 	 * sh_css_init_isp_params_from_config throws an error when both DPC and BDS is enabled.
2039 	 * The CSS API must pass this error information to the caller, ie. the host.
2040 	 * We do not return this error immediately, but instead continue with updating the ISP params
2041 	 *  to enable testing of features which are currently in TR phase. */
2042 	err = (err1 != 0) ? err1 :
2043 	      (err2 != 0) ? err2 :
2044 	      (err3 != 0) ? err3 : err;
2045 exit:
2046 	IA_CSS_LEAVE_ERR_PRIVATE(err);
2047 	return err;
2048 }
2049 #endif
2050 
2051 static int
sh_css_init_isp_params_from_config(struct ia_css_pipe * pipe,struct ia_css_isp_parameters * params,const struct ia_css_isp_config * config,struct ia_css_pipe * pipe_in)2052 sh_css_init_isp_params_from_config(struct ia_css_pipe *pipe,
2053 				   struct ia_css_isp_parameters *params,
2054 				   const struct ia_css_isp_config *config,
2055 				   struct ia_css_pipe *pipe_in) {
2056 	int err = 0;
2057 	bool is_dp_10bpp = true;
2058 
2059 	assert(pipe);
2060 
2061 	IA_CSS_ENTER_PRIVATE("pipe=%p, config=%p, params=%p", pipe, config, params);
2062 
2063 	ia_css_set_configs(params, config);
2064 
2065 	sh_css_set_nr_config(params, config->nr_config);
2066 	sh_css_set_ee_config(params, config->ee_config);
2067 	sh_css_set_baa_config(params, config->baa_config);
2068 	if ((pipe->mode < IA_CSS_PIPE_ID_NUM) &&
2069 	    (params->pipe_dvs_6axis_config[pipe->mode]))
2070 		sh_css_set_pipe_dvs_6axis_config(pipe, params, config->dvs_6axis_config);
2071 	sh_css_set_dz_config(params, config->dz_config);
2072 	sh_css_set_motion_vector(params, config->motion_vector);
2073 	sh_css_update_shading_table_status(pipe_in, params);
2074 	sh_css_set_shading_table(pipe->stream, params, config->shading_table);
2075 	sh_css_set_morph_table(params, config->morph_table);
2076 	sh_css_set_macc_table(params, config->macc_table);
2077 	sh_css_set_gamma_table(params, config->gamma_table);
2078 	sh_css_set_ctc_table(params, config->ctc_table);
2079 	/* ------ deprecated(bz675) : from ------ */
2080 	sh_css_set_shading_settings(params, config->shading_settings);
2081 	/* ------ deprecated(bz675) : to ------ */
2082 
2083 	params->dis_coef_table_changed = (config->dvs_coefs);
2084 	params->dvs2_coef_table_changed = (config->dvs2_coefs);
2085 
2086 	params->output_frame = config->output_frame;
2087 	params->isp_parameters_id = config->isp_config_id;
2088 
2089 	/* Currently we do not offer CSS interface to set different
2090 	 * configurations for DPC, i.e. depending on DPC being enabled
2091 	 * before (NORM+OBC) or after. The folllowing code to set the
2092 	 * DPC configuration should be updated when this interface is made
2093 	 * available */
2094 	if (IS_ISP2401) {
2095 		sh_css_set_dp_config(pipe, params, config->dp_config);
2096 		ia_css_set_param_exceptions(pipe, params);
2097 	}
2098 
2099 	if (0 ==
2100 	    sh_css_select_dp_10bpp_config(pipe, &is_dp_10bpp))
2101 	{
2102 		/* return an error when both DPC and BDS is enabled by the
2103 		 * user. */
2104 		/* we do not exit from this point immediately to allow internal
2105 		 * firmware feature testing. */
2106 		if (is_dp_10bpp) {
2107 			err = -EINVAL;
2108 		}
2109 	} else
2110 	{
2111 		err = -EINVAL;
2112 		goto exit;
2113 	}
2114 
2115 	if (!IS_ISP2401)
2116 		ia_css_set_param_exceptions(pipe, params);
2117 
2118 exit:
2119 	IA_CSS_LEAVE_ERR_PRIVATE(err);
2120 	return err;
2121 }
2122 
2123 void
ia_css_stream_get_isp_config(const struct ia_css_stream * stream,struct ia_css_isp_config * config)2124 ia_css_stream_get_isp_config(
2125     const struct ia_css_stream *stream,
2126     struct ia_css_isp_config *config)
2127 {
2128 	IA_CSS_ENTER("void");
2129 	ia_css_pipe_get_isp_config(stream->pipes[0], config);
2130 	IA_CSS_LEAVE("void");
2131 }
2132 
2133 void
ia_css_pipe_get_isp_config(struct ia_css_pipe * pipe,struct ia_css_isp_config * config)2134 ia_css_pipe_get_isp_config(struct ia_css_pipe *pipe,
2135 			   struct ia_css_isp_config *config)
2136 {
2137 	struct ia_css_isp_parameters *params = NULL;
2138 
2139 	assert(config);
2140 
2141 	IA_CSS_ENTER("config=%p", config);
2142 
2143 	params = pipe->stream->isp_params_configs;
2144 	assert(params);
2145 
2146 	ia_css_get_configs(params, config);
2147 
2148 	sh_css_get_ee_config(params, config->ee_config);
2149 	sh_css_get_baa_config(params, config->baa_config);
2150 	sh_css_get_pipe_dvs_6axis_config(pipe, params, config->dvs_6axis_config);
2151 	sh_css_get_dp_config(pipe, params, config->dp_config);
2152 	sh_css_get_macc_table(params, config->macc_table);
2153 	sh_css_get_gamma_table(params, config->gamma_table);
2154 	sh_css_get_ctc_table(params, config->ctc_table);
2155 	sh_css_get_dz_config(params, config->dz_config);
2156 	sh_css_get_motion_vector(params, config->motion_vector);
2157 	/* ------ deprecated(bz675) : from ------ */
2158 	sh_css_get_shading_settings(params, config->shading_settings);
2159 	/* ------ deprecated(bz675) : to ------ */
2160 
2161 	config->output_frame = params->output_frame;
2162 	config->isp_config_id = params->isp_parameters_id;
2163 
2164 	IA_CSS_LEAVE("void");
2165 }
2166 
2167 /*
2168  * coding style says the return of "mmgr_NULL" is the error signal
2169  *
2170  * Deprecated: Implement mmgr_realloc()
2171  */
realloc_isp_css_mm_buf(ia_css_ptr * curr_buf,size_t * curr_size,size_t needed_size,bool force,int * err,uint16_t mmgr_attribute)2172 static bool realloc_isp_css_mm_buf(
2173     ia_css_ptr *curr_buf,
2174     size_t *curr_size,
2175     size_t needed_size,
2176     bool force,
2177     int *err,
2178     uint16_t mmgr_attribute)
2179 {
2180 	s32 id;
2181 
2182 	*err = 0;
2183 	/* Possible optimization: add a function sh_css_isp_css_mm_realloc()
2184 	 * and implement on top of hmm. */
2185 
2186 	IA_CSS_ENTER_PRIVATE("void");
2187 
2188 	if (!force && *curr_size >= needed_size) {
2189 		IA_CSS_LEAVE_PRIVATE("false");
2190 		return false;
2191 	}
2192 	/* don't reallocate if single ref to buffer and same size */
2193 	if (*curr_size == needed_size && ia_css_refcount_is_single(*curr_buf)) {
2194 		IA_CSS_LEAVE_PRIVATE("false");
2195 		return false;
2196 	}
2197 
2198 	id = IA_CSS_REFCOUNT_PARAM_BUFFER;
2199 	ia_css_refcount_decrement(id, *curr_buf);
2200 	*curr_buf = ia_css_refcount_increment(id, hmm_alloc(needed_size,
2201 							    HMM_BO_PRIVATE, 0,
2202 							    NULL,
2203 							    mmgr_attribute));
2204 
2205 	if (!*curr_buf) {
2206 		*err = -ENOMEM;
2207 		*curr_size = 0;
2208 	} else {
2209 		*curr_size = needed_size;
2210 	}
2211 	IA_CSS_LEAVE_PRIVATE("true");
2212 	return true;
2213 }
2214 
reallocate_buffer(ia_css_ptr * curr_buf,size_t * curr_size,size_t needed_size,bool force,int * err)2215 static bool reallocate_buffer(
2216     ia_css_ptr *curr_buf,
2217     size_t *curr_size,
2218     size_t needed_size,
2219     bool force,
2220     int *err)
2221 {
2222 	bool ret;
2223 
2224 	IA_CSS_ENTER_PRIVATE("void");
2225 
2226 	ret = realloc_isp_css_mm_buf(curr_buf,
2227 				     curr_size, needed_size, force, err, 0);
2228 
2229 	IA_CSS_LEAVE_PRIVATE("ret=%d", ret);
2230 	return ret;
2231 }
2232 
2233 struct ia_css_isp_3a_statistics *
ia_css_isp_3a_statistics_allocate(const struct ia_css_3a_grid_info * grid)2234 ia_css_isp_3a_statistics_allocate(const struct ia_css_3a_grid_info *grid)
2235 {
2236 	struct ia_css_isp_3a_statistics *me;
2237 
2238 	IA_CSS_ENTER("grid=%p", grid);
2239 
2240 	assert(grid);
2241 
2242 	/* MW: Does "grid->enable" also control the histogram output ?? */
2243 	if (!grid->enable)
2244 		return NULL;
2245 
2246 	me = kvcalloc(1, sizeof(*me), GFP_KERNEL);
2247 	if (!me)
2248 		goto err;
2249 
2250 	if (grid->use_dmem) {
2251 		me->dmem_size = sizeof(struct ia_css_3a_output) *
2252 				grid->aligned_width *
2253 				grid->aligned_height;
2254 	} else {
2255 		me->vmem_size = ISP_S3ATBL_HI_LO_STRIDE_BYTES *
2256 				grid->aligned_height;
2257 	}
2258 #if !defined(HAS_NO_HMEM)
2259 	me->hmem_size = sizeof_hmem(HMEM0_ID);
2260 #endif
2261 
2262 	/* All subsections need to be aligned to the system bus width */
2263 	me->dmem_size = CEIL_MUL(me->dmem_size, HIVE_ISP_DDR_WORD_BYTES);
2264 	me->vmem_size = CEIL_MUL(me->vmem_size, HIVE_ISP_DDR_WORD_BYTES);
2265 	me->hmem_size = CEIL_MUL(me->hmem_size, HIVE_ISP_DDR_WORD_BYTES);
2266 
2267 	me->size = me->dmem_size + me->vmem_size * 2 + me->hmem_size;
2268 	me->data_ptr = hmm_alloc(me->size, HMM_BO_PRIVATE, 0, NULL, 0);
2269 	if (me->data_ptr == mmgr_NULL) {
2270 		kvfree(me);
2271 		me = NULL;
2272 		goto err;
2273 	}
2274 	if (me->dmem_size)
2275 		me->data.dmem.s3a_tbl = me->data_ptr;
2276 	if (me->vmem_size) {
2277 		me->data.vmem.s3a_tbl_hi = me->data_ptr + me->dmem_size;
2278 		me->data.vmem.s3a_tbl_lo = me->data_ptr + me->dmem_size + me->vmem_size;
2279 	}
2280 	if (me->hmem_size)
2281 		me->data_hmem.rgby_tbl = me->data_ptr + me->dmem_size + 2 * me->vmem_size;
2282 
2283 err:
2284 	IA_CSS_LEAVE("return=%p", me);
2285 	return me;
2286 }
2287 
2288 void
ia_css_isp_3a_statistics_free(struct ia_css_isp_3a_statistics * me)2289 ia_css_isp_3a_statistics_free(struct ia_css_isp_3a_statistics *me)
2290 {
2291 	if (me) {
2292 		hmm_free(me->data_ptr);
2293 		kvfree(me);
2294 	}
2295 }
2296 
ia_css_skc_dvs_statistics_allocate(void)2297 struct ia_css_isp_skc_dvs_statistics *ia_css_skc_dvs_statistics_allocate(void)
2298 {
2299 	return NULL;
2300 }
2301 
2302 struct ia_css_metadata *
ia_css_metadata_allocate(const struct ia_css_metadata_info * metadata_info)2303 ia_css_metadata_allocate(const struct ia_css_metadata_info *metadata_info)
2304 {
2305 	struct ia_css_metadata *md = NULL;
2306 
2307 	IA_CSS_ENTER("");
2308 
2309 	if (metadata_info->size == 0)
2310 		return NULL;
2311 
2312 	md = kvmalloc(sizeof(*md), GFP_KERNEL);
2313 	if (!md)
2314 		goto error;
2315 
2316 	md->info = *metadata_info;
2317 	md->exp_id = 0;
2318 	md->address = hmm_alloc(metadata_info->size, HMM_BO_PRIVATE, 0, NULL, 0);
2319 	if (md->address == mmgr_NULL)
2320 		goto error;
2321 
2322 	IA_CSS_LEAVE("return=%p", md);
2323 	return md;
2324 
2325 error:
2326 	ia_css_metadata_free(md);
2327 	IA_CSS_LEAVE("return=%p", NULL);
2328 	return NULL;
2329 }
2330 
2331 void
ia_css_metadata_free(struct ia_css_metadata * me)2332 ia_css_metadata_free(struct ia_css_metadata *me)
2333 {
2334 	if (me) {
2335 		/* The enter and leave macros are placed inside
2336 		 * the condition to avoid false logging of metadata
2337 		 * free events when metadata is disabled.
2338 		 * We found this to be confusing during development
2339 		 * and debugging. */
2340 		IA_CSS_ENTER("me=%p", me);
2341 		hmm_free(me->address);
2342 		kvfree(me);
2343 		IA_CSS_LEAVE("void");
2344 	}
2345 }
2346 
2347 void
ia_css_metadata_free_multiple(unsigned int num_bufs,struct ia_css_metadata ** bufs)2348 ia_css_metadata_free_multiple(unsigned int num_bufs,
2349 			      struct ia_css_metadata **bufs)
2350 {
2351 	unsigned int i;
2352 
2353 	if (bufs) {
2354 		for (i = 0; i < num_bufs; i++)
2355 			ia_css_metadata_free(bufs[i]);
2356 	}
2357 }
2358 
2359 static unsigned int g_param_buffer_dequeue_count;
2360 static unsigned int g_param_buffer_enqueue_count;
2361 
2362 int
ia_css_stream_isp_parameters_init(struct ia_css_stream * stream)2363 ia_css_stream_isp_parameters_init(struct ia_css_stream *stream) {
2364 	int err = 0;
2365 	unsigned int i;
2366 	struct sh_css_ddr_address_map *ddr_ptrs;
2367 	struct sh_css_ddr_address_map_size *ddr_ptrs_size;
2368 	struct ia_css_isp_parameters *params;
2369 
2370 	assert(stream);
2371 	IA_CSS_ENTER_PRIVATE("void");
2372 
2373 	if (!stream)
2374 	{
2375 		IA_CSS_LEAVE_ERR_PRIVATE(-EINVAL);
2376 		return -EINVAL;
2377 	}
2378 	/* TMP: tracking of paramsets */
2379 	g_param_buffer_dequeue_count = 0;
2380 	g_param_buffer_enqueue_count = 0;
2381 
2382 	stream->per_frame_isp_params_configs = NULL;
2383 	err = sh_css_create_isp_params(stream,
2384 				       &stream->isp_params_configs);
2385 	if (err)
2386 		goto ERR;
2387 
2388 	params = stream->isp_params_configs;
2389 	if (!sh_css_init_isp_params_from_global(stream, params, true, NULL))
2390 	{
2391 		/* we do not return the error immediately to enable internal
2392 		 * firmware feature testing */
2393 		err = -EINVAL;
2394 	}
2395 
2396 	ddr_ptrs = &params->ddr_ptrs;
2397 	ddr_ptrs_size = &params->ddr_ptrs_size;
2398 
2399 	/* create per pipe reference to general ddr_ptrs */
2400 	for (i = 0; i < IA_CSS_PIPE_ID_NUM; i++)
2401 	{
2402 		ref_sh_css_ddr_address_map(ddr_ptrs, &params->pipe_ddr_ptrs[i]);
2403 		params->pipe_ddr_ptrs_size[i] = *ddr_ptrs_size;
2404 	}
2405 
2406 ERR:
2407 	IA_CSS_LEAVE_ERR_PRIVATE(err);
2408 	return err;
2409 }
2410 
2411 static void
ia_css_set_sdis_config(struct ia_css_isp_parameters * params,const struct ia_css_dvs_coefficients * dvs_coefs)2412 ia_css_set_sdis_config(
2413     struct ia_css_isp_parameters *params,
2414     const struct ia_css_dvs_coefficients *dvs_coefs)
2415 {
2416 	ia_css_set_sdis_horicoef_config(params, dvs_coefs);
2417 	ia_css_set_sdis_vertcoef_config(params, dvs_coefs);
2418 	ia_css_set_sdis_horiproj_config(params, dvs_coefs);
2419 	ia_css_set_sdis_vertproj_config(params, dvs_coefs);
2420 }
2421 
2422 static void
ia_css_set_sdis2_config(struct ia_css_isp_parameters * params,const struct ia_css_dvs2_coefficients * dvs2_coefs)2423 ia_css_set_sdis2_config(
2424     struct ia_css_isp_parameters *params,
2425     const struct ia_css_dvs2_coefficients *dvs2_coefs)
2426 {
2427 	ia_css_set_sdis2_horicoef_config(params, dvs2_coefs);
2428 	ia_css_set_sdis2_vertcoef_config(params, dvs2_coefs);
2429 	ia_css_set_sdis2_horiproj_config(params, dvs2_coefs);
2430 	ia_css_set_sdis2_vertproj_config(params, dvs2_coefs);
2431 }
2432 
2433 static int
sh_css_create_isp_params(struct ia_css_stream * stream,struct ia_css_isp_parameters ** isp_params_out)2434 sh_css_create_isp_params(struct ia_css_stream *stream,
2435 			 struct ia_css_isp_parameters **isp_params_out) {
2436 	bool succ = true;
2437 	unsigned int i;
2438 	struct sh_css_ddr_address_map *ddr_ptrs;
2439 	struct sh_css_ddr_address_map_size *ddr_ptrs_size;
2440 	int err;
2441 	size_t params_size;
2442 	struct ia_css_isp_parameters *params =
2443 	kvmalloc(sizeof(struct ia_css_isp_parameters), GFP_KERNEL);
2444 
2445 	if (!params)
2446 	{
2447 		*isp_params_out = NULL;
2448 		err = -ENOMEM;
2449 		IA_CSS_ERROR("%s:%d error: cannot allocate memory", __FILE__, __LINE__);
2450 		IA_CSS_LEAVE_ERR_PRIVATE(err);
2451 		return err;
2452 	} else
2453 	{
2454 		memset(params, 0, sizeof(struct ia_css_isp_parameters));
2455 	}
2456 
2457 	ddr_ptrs = &params->ddr_ptrs;
2458 	ddr_ptrs_size = &params->ddr_ptrs_size;
2459 
2460 	for (i = 0; i < IA_CSS_PIPE_ID_NUM; i++)
2461 	{
2462 		memset(&params->pipe_ddr_ptrs[i], 0,
2463 		       sizeof(params->pipe_ddr_ptrs[i]));
2464 		memset(&params->pipe_ddr_ptrs_size[i], 0,
2465 		       sizeof(params->pipe_ddr_ptrs_size[i]));
2466 	}
2467 
2468 	memset(ddr_ptrs, 0, sizeof(*ddr_ptrs));
2469 	memset(ddr_ptrs_size, 0, sizeof(*ddr_ptrs_size));
2470 
2471 	params_size = sizeof(params->uds);
2472 	ddr_ptrs_size->isp_param = params_size;
2473 	ddr_ptrs->isp_param =
2474 	ia_css_refcount_increment(IA_CSS_REFCOUNT_PARAM_BUFFER,
2475 				  hmm_alloc(params_size, HMM_BO_PRIVATE, 0, NULL, 0));
2476 	succ &= (ddr_ptrs->isp_param != mmgr_NULL);
2477 
2478 	ddr_ptrs_size->macc_tbl = sizeof(struct ia_css_macc_table);
2479 	ddr_ptrs->macc_tbl =
2480 	ia_css_refcount_increment(IA_CSS_REFCOUNT_PARAM_BUFFER,
2481 				  hmm_alloc(sizeof(struct ia_css_macc_table), HMM_BO_PRIVATE, 0, NULL, 0));
2482 	succ &= (ddr_ptrs->macc_tbl != mmgr_NULL);
2483 
2484 	*isp_params_out = params;
2485 
2486 	if (!succ)
2487 		return -ENOMEM;
2488 
2489 	return 0;
2490 }
2491 
2492 static bool
sh_css_init_isp_params_from_global(struct ia_css_stream * stream,struct ia_css_isp_parameters * params,bool use_default_config,struct ia_css_pipe * pipe_in)2493 sh_css_init_isp_params_from_global(struct ia_css_stream *stream,
2494 				   struct ia_css_isp_parameters *params,
2495 				   bool use_default_config,
2496 				   struct ia_css_pipe *pipe_in)
2497 {
2498 	bool retval = true;
2499 	int i = 0;
2500 	bool is_dp_10bpp = true;
2501 	unsigned int isp_pipe_version = ia_css_pipe_get_isp_pipe_version(
2502 					    stream->pipes[0]);
2503 	struct ia_css_isp_parameters *stream_params = stream->isp_params_configs;
2504 
2505 	if (!use_default_config && !stream_params) {
2506 		retval = false;
2507 		goto exit;
2508 	}
2509 
2510 	params->output_frame = NULL;
2511 	params->isp_parameters_id = 0;
2512 
2513 	if (use_default_config) {
2514 		ia_css_set_xnr3_config(params, &default_xnr3_config);
2515 
2516 		sh_css_set_nr_config(params, &default_nr_config);
2517 		sh_css_set_ee_config(params, &default_ee_config);
2518 		if (isp_pipe_version == SH_CSS_ISP_PIPE_VERSION_1)
2519 			sh_css_set_macc_table(params, &default_macc_table);
2520 		else if (isp_pipe_version == SH_CSS_ISP_PIPE_VERSION_2_2)
2521 			sh_css_set_macc_table(params, &default_macc2_table);
2522 		sh_css_set_gamma_table(params, &default_gamma_table);
2523 		sh_css_set_ctc_table(params, &default_ctc_table);
2524 		sh_css_set_baa_config(params, &default_baa_config);
2525 		sh_css_set_dz_config(params, &default_dz_config);
2526 		/* ------ deprecated(bz675) : from ------ */
2527 		sh_css_set_shading_settings(params, &default_shading_settings);
2528 		/* ------ deprecated(bz675) : to ------ */
2529 
2530 		ia_css_set_s3a_config(params, &default_3a_config);
2531 		ia_css_set_wb_config(params, &default_wb_config);
2532 		ia_css_set_csc_config(params, &default_cc_config);
2533 		ia_css_set_tnr_config(params, &default_tnr_config);
2534 		ia_css_set_ob_config(params, &default_ob_config);
2535 		ia_css_set_dp_config(params, &default_dp_config);
2536 
2537 		if (!IS_ISP2401) {
2538 			ia_css_set_param_exceptions(pipe_in, params);
2539 		} else {
2540 			for (i = 0; i < stream->num_pipes; i++) {
2541 				if (sh_css_select_dp_10bpp_config(stream->pipes[i],
2542 								&is_dp_10bpp) == 0) {
2543 					/* set the return value as false if both DPC and
2544 					* BDS is enabled by the user. But we do not return
2545 					* the value immediately to enable internal firmware
2546 					* feature testing. */
2547 					if (is_dp_10bpp) {
2548 						sh_css_set_dp_config(stream->pipes[i], params, &default_dp_10bpp_config);
2549 					} else {
2550 						sh_css_set_dp_config(stream->pipes[i], params, &default_dp_config);
2551 					}
2552 				} else {
2553 					retval = false;
2554 					goto exit;
2555 				}
2556 
2557 				ia_css_set_param_exceptions(stream->pipes[i], params);
2558 			}
2559 		}
2560 
2561 		ia_css_set_de_config(params, &default_de_config);
2562 		ia_css_set_gc_config(params, &default_gc_config);
2563 		ia_css_set_anr_config(params, &default_anr_config);
2564 		ia_css_set_anr2_config(params, &default_anr_thres);
2565 		ia_css_set_ce_config(params, &default_ce_config);
2566 		ia_css_set_xnr_table_config(params, &default_xnr_table);
2567 		ia_css_set_ecd_config(params, &default_ecd_config);
2568 		ia_css_set_ynr_config(params, &default_ynr_config);
2569 		ia_css_set_fc_config(params, &default_fc_config);
2570 		ia_css_set_cnr_config(params, &default_cnr_config);
2571 		ia_css_set_macc_config(params, &default_macc_config);
2572 		ia_css_set_ctc_config(params, &default_ctc_config);
2573 		ia_css_set_aa_config(params, &default_aa_config);
2574 		ia_css_set_r_gamma_config(params, &default_r_gamma_table);
2575 		ia_css_set_g_gamma_config(params, &default_g_gamma_table);
2576 		ia_css_set_b_gamma_config(params, &default_b_gamma_table);
2577 		ia_css_set_yuv2rgb_config(params, &default_yuv2rgb_cc_config);
2578 		ia_css_set_rgb2yuv_config(params, &default_rgb2yuv_cc_config);
2579 		ia_css_set_xnr_config(params, &default_xnr_config);
2580 		ia_css_set_sdis_config(params, &default_sdis_config);
2581 		ia_css_set_sdis2_config(params, &default_sdis2_config);
2582 		ia_css_set_formats_config(params, &default_formats_config);
2583 
2584 		params->fpn_config.data = NULL;
2585 		params->config_changed[IA_CSS_FPN_ID] = true;
2586 		params->fpn_config.enabled = 0;
2587 
2588 		params->motion_config = default_motion_config;
2589 		params->motion_config_changed = true;
2590 
2591 		params->morph_table = NULL;
2592 		params->morph_table_changed = true;
2593 
2594 		params->sc_table = NULL;
2595 		params->sc_table_changed = true;
2596 		params->sc_table_dirty = false;
2597 		params->sc_table_last_pipe_num = 0;
2598 
2599 		ia_css_sdis2_clear_coefficients(&params->dvs2_coefs);
2600 		params->dvs2_coef_table_changed = true;
2601 
2602 		ia_css_sdis_clear_coefficients(&params->dvs_coefs);
2603 		params->dis_coef_table_changed = true;
2604 	} else {
2605 		ia_css_set_xnr3_config(params, &stream_params->xnr3_config);
2606 
2607 		sh_css_set_nr_config(params, &stream_params->nr_config);
2608 		sh_css_set_ee_config(params, &stream_params->ee_config);
2609 		if (isp_pipe_version == SH_CSS_ISP_PIPE_VERSION_1)
2610 			sh_css_set_macc_table(params, &stream_params->macc_table);
2611 		else if (isp_pipe_version == SH_CSS_ISP_PIPE_VERSION_2_2)
2612 			sh_css_set_macc_table(params, &stream_params->macc_table);
2613 		sh_css_set_gamma_table(params, &stream_params->gc_table);
2614 		sh_css_set_ctc_table(params, &stream_params->ctc_table);
2615 		sh_css_set_baa_config(params, &stream_params->bds_config);
2616 		sh_css_set_dz_config(params, &stream_params->dz_config);
2617 		/* ------ deprecated(bz675) : from ------ */
2618 		sh_css_set_shading_settings(params, &stream_params->shading_settings);
2619 		/* ------ deprecated(bz675) : to ------ */
2620 
2621 		ia_css_set_s3a_config(params, &stream_params->s3a_config);
2622 		ia_css_set_wb_config(params, &stream_params->wb_config);
2623 		ia_css_set_csc_config(params, &stream_params->cc_config);
2624 		ia_css_set_tnr_config(params, &stream_params->tnr_config);
2625 		ia_css_set_ob_config(params, &stream_params->ob_config);
2626 		ia_css_set_dp_config(params, &stream_params->dp_config);
2627 		ia_css_set_de_config(params, &stream_params->de_config);
2628 		ia_css_set_gc_config(params, &stream_params->gc_config);
2629 		ia_css_set_anr_config(params, &stream_params->anr_config);
2630 		ia_css_set_anr2_config(params, &stream_params->anr_thres);
2631 		ia_css_set_ce_config(params, &stream_params->ce_config);
2632 		ia_css_set_xnr_table_config(params, &stream_params->xnr_table);
2633 		ia_css_set_ecd_config(params, &stream_params->ecd_config);
2634 		ia_css_set_ynr_config(params, &stream_params->ynr_config);
2635 		ia_css_set_fc_config(params, &stream_params->fc_config);
2636 		ia_css_set_cnr_config(params, &stream_params->cnr_config);
2637 		ia_css_set_macc_config(params, &stream_params->macc_config);
2638 		ia_css_set_ctc_config(params, &stream_params->ctc_config);
2639 		ia_css_set_aa_config(params, &stream_params->aa_config);
2640 		ia_css_set_r_gamma_config(params, &stream_params->r_gamma_table);
2641 		ia_css_set_g_gamma_config(params, &stream_params->g_gamma_table);
2642 		ia_css_set_b_gamma_config(params, &stream_params->b_gamma_table);
2643 		ia_css_set_yuv2rgb_config(params, &stream_params->yuv2rgb_cc_config);
2644 		ia_css_set_rgb2yuv_config(params, &stream_params->rgb2yuv_cc_config);
2645 		ia_css_set_xnr_config(params, &stream_params->xnr_config);
2646 		ia_css_set_formats_config(params, &stream_params->formats_config);
2647 
2648 		for (i = 0; i < stream->num_pipes; i++) {
2649 			if (0 ==
2650 			    sh_css_select_dp_10bpp_config(stream->pipes[i], &is_dp_10bpp)) {
2651 				/* set the return value as false if both DPC and
2652 				 * BDS is enabled by the user. But we do not return
2653 				 * the value immediately to enable internal firmware
2654 				 * feature testing. */
2655 
2656 				if (is_dp_10bpp) {
2657 					retval = false;
2658 					/* FIXME: should it ignore this error? */
2659 				}
2660 			} else {
2661 				retval = false;
2662 				goto exit;
2663 			}
2664 			if (IS_ISP2401) {
2665 				if (stream->pipes[i]->mode < IA_CSS_PIPE_ID_NUM) {
2666 					sh_css_set_dp_config(stream->pipes[i], params,
2667 							    &stream_params->pipe_dp_config[stream->pipes[i]->mode]);
2668 					ia_css_set_param_exceptions(stream->pipes[i], params);
2669 				} else {
2670 					retval = false;
2671 					goto exit;
2672 				}
2673 			}
2674 		}
2675 
2676 		if (!IS_ISP2401)
2677 			ia_css_set_param_exceptions(pipe_in, params);
2678 
2679 		params->fpn_config.data = stream_params->fpn_config.data;
2680 		params->config_changed[IA_CSS_FPN_ID] =
2681 		    stream_params->config_changed[IA_CSS_FPN_ID];
2682 		params->fpn_config.enabled = stream_params->fpn_config.enabled;
2683 
2684 		sh_css_set_motion_vector(params, &stream_params->motion_config);
2685 		sh_css_set_morph_table(params, stream_params->morph_table);
2686 
2687 		if (stream_params->sc_table) {
2688 			sh_css_update_shading_table_status(pipe_in, params);
2689 			sh_css_set_shading_table(stream, params, stream_params->sc_table);
2690 		} else {
2691 			params->sc_table = NULL;
2692 			params->sc_table_changed = true;
2693 			params->sc_table_dirty = false;
2694 			params->sc_table_last_pipe_num = 0;
2695 		}
2696 
2697 		/* Only IA_CSS_PIPE_ID_VIDEO & IA_CSS_PIPE_ID_CAPTURE will support dvs_6axis_config*/
2698 		for (i = 0; i < IA_CSS_PIPE_ID_NUM; i++) {
2699 			if (stream_params->pipe_dvs_6axis_config[i]) {
2700 				if (params->pipe_dvs_6axis_config[i]) {
2701 					copy_dvs_6axis_table(params->pipe_dvs_6axis_config[i],
2702 							     stream_params->pipe_dvs_6axis_config[i]);
2703 				} else {
2704 					params->pipe_dvs_6axis_config[i] =
2705 					    generate_dvs_6axis_table_from_config(stream_params->pipe_dvs_6axis_config[i]);
2706 				}
2707 			}
2708 		}
2709 		ia_css_set_sdis_config(params, &stream_params->dvs_coefs);
2710 		params->dis_coef_table_changed = stream_params->dis_coef_table_changed;
2711 
2712 		ia_css_set_sdis2_config(params, &stream_params->dvs2_coefs);
2713 		params->dvs2_coef_table_changed = stream_params->dvs2_coef_table_changed;
2714 		params->sensor_binning = stream_params->sensor_binning;
2715 	}
2716 
2717 exit:
2718 	return retval;
2719 }
2720 
2721 int
sh_css_params_init(void)2722 sh_css_params_init(void) {
2723 	int i, p;
2724 
2725 	IA_CSS_ENTER_PRIVATE("void");
2726 
2727 	/* TMP: tracking of paramsets */
2728 	g_param_buffer_dequeue_count = 0;
2729 	g_param_buffer_enqueue_count = 0;
2730 
2731 	for (p = 0; p < IA_CSS_PIPE_ID_NUM; p++)
2732 	{
2733 		for (i = 0; i < SH_CSS_MAX_STAGES; i++) {
2734 			xmem_sp_stage_ptrs[p][i] =
2735 			ia_css_refcount_increment(-1,
2736 						  hmm_alloc(sizeof(struct sh_css_sp_stage),
2737 							    HMM_BO_PRIVATE, 0, NULL,
2738 							    ATOMISP_MAP_FLAG_CLEARED));
2739 			xmem_isp_stage_ptrs[p][i] =
2740 			ia_css_refcount_increment(-1,
2741 						  hmm_alloc(sizeof(struct sh_css_sp_stage),
2742 							    HMM_BO_PRIVATE, 0, NULL,
2743 							    ATOMISP_MAP_FLAG_CLEARED));
2744 
2745 			if ((xmem_sp_stage_ptrs[p][i] == mmgr_NULL) ||
2746 			    (xmem_isp_stage_ptrs[p][i] == mmgr_NULL)) {
2747 				sh_css_params_uninit();
2748 				IA_CSS_LEAVE_ERR_PRIVATE(-ENOMEM);
2749 				return -ENOMEM;
2750 			}
2751 		}
2752 	}
2753 
2754 	ia_css_config_gamma_table();
2755 	ia_css_config_ctc_table();
2756 	ia_css_config_rgb_gamma_tables();
2757 	ia_css_config_xnr_table();
2758 
2759 	sp_ddr_ptrs = ia_css_refcount_increment(-1,
2760 						hmm_alloc(CEIL_MUL(sizeof(struct sh_css_ddr_address_map),
2761 								   HIVE_ISP_DDR_WORD_BYTES),
2762 							  HMM_BO_PRIVATE, 0, NULL,
2763 							  ATOMISP_MAP_FLAG_CLEARED));
2764 	xmem_sp_group_ptrs = ia_css_refcount_increment(-1,
2765 						       hmm_alloc(sizeof(struct sh_css_sp_group),
2766 								 HMM_BO_PRIVATE, 0, NULL,
2767 								 ATOMISP_MAP_FLAG_CLEARED));
2768 
2769 	if ((sp_ddr_ptrs == mmgr_NULL) ||
2770 	    (xmem_sp_group_ptrs == mmgr_NULL))
2771 	{
2772 		ia_css_uninit();
2773 		IA_CSS_LEAVE_ERR_PRIVATE(-ENOMEM);
2774 		return -ENOMEM;
2775 	}
2776 	IA_CSS_LEAVE_ERR_PRIVATE(0);
2777 	return 0;
2778 }
2779 
host_lut_store(const void * lut)2780 static void host_lut_store(const void *lut)
2781 {
2782 	unsigned int i;
2783 
2784 	for (i = 0; i < N_GDC_ID; i++)
2785 		gdc_lut_store((gdc_ID_t)i, (const int (*)[HRT_GDC_N]) lut);
2786 }
2787 
2788 /* Note that allocation is in ipu address space. */
sh_css_params_alloc_gdc_lut(void)2789 inline ia_css_ptr sh_css_params_alloc_gdc_lut(void)
2790 {
2791 	return hmm_alloc(sizeof(zoom_table), HMM_BO_PRIVATE, 0, NULL, 0);
2792 }
2793 
sh_css_params_free_gdc_lut(ia_css_ptr addr)2794 inline void sh_css_params_free_gdc_lut(ia_css_ptr addr)
2795 {
2796 	if (addr != mmgr_NULL)
2797 		hmm_free(addr);
2798 }
2799 
ia_css_pipe_set_bci_scaler_lut(struct ia_css_pipe * pipe,const void * lut)2800 int ia_css_pipe_set_bci_scaler_lut(struct ia_css_pipe *pipe,
2801 	const void *lut)
2802 {
2803 	int err = 0;
2804 	bool stream_started = false;
2805 
2806 	IA_CSS_ENTER("pipe=%p lut=%p", pipe, lut);
2807 
2808 	if (!lut || !pipe) {
2809 		err = -EINVAL;
2810 		IA_CSS_LEAVE("err=%d", err);
2811 		return err;
2812 	}
2813 
2814 	/* If the pipe belongs to a stream and the stream has started, it is not
2815 	 * safe to store lut to gdc HW. If pipe->stream is NULL, then no stream is
2816 	 * created with this pipe, so it is safe to do this operation as long as
2817 	 * ia_css_init() has been called. */
2818 	if (pipe->stream && pipe->stream->started) {
2819 		ia_css_debug_dtrace(IA_CSS_DEBUG_ERROR,
2820 				    "unable to set scaler lut since stream has started\n");
2821 		stream_started = true;
2822 		err = -ENOTSUPP;
2823 	}
2824 
2825 	/* Free any existing tables. */
2826 	sh_css_params_free_gdc_lut(pipe->scaler_pp_lut);
2827 	pipe->scaler_pp_lut = mmgr_NULL;
2828 
2829 	if (!stream_started) {
2830 		if (!IS_ISP2401)
2831 			pipe->scaler_pp_lut = hmm_alloc(sizeof(zoom_table), HMM_BO_PRIVATE, 0, NULL, 0);
2832 		else
2833 			pipe->scaler_pp_lut = sh_css_params_alloc_gdc_lut();
2834 
2835 		if (pipe->scaler_pp_lut == mmgr_NULL) {
2836 			ia_css_debug_dtrace(IA_CSS_DEBUG_ERROR,
2837 					    "unable to allocate scaler_pp_lut\n");
2838 			err = -ENOMEM;
2839 		} else {
2840 			gdc_lut_convert_to_isp_format((const int(*)[HRT_GDC_N])lut,
2841 						      interleaved_lut_temp);
2842 			hmm_store(pipe->scaler_pp_lut,
2843 				   (int *)interleaved_lut_temp,
2844 				   sizeof(zoom_table));
2845 		}
2846 	}
2847 
2848 	IA_CSS_LEAVE("lut(%u) err=%d", pipe->scaler_pp_lut, err);
2849 	return err;
2850 }
2851 
2852 /* if pipe is NULL, returns default lut addr. */
sh_css_pipe_get_pp_gdc_lut(const struct ia_css_pipe * pipe)2853 ia_css_ptr sh_css_pipe_get_pp_gdc_lut(const struct ia_css_pipe *pipe)
2854 {
2855 	assert(pipe);
2856 
2857 	if (pipe->scaler_pp_lut != mmgr_NULL)
2858 		return pipe->scaler_pp_lut;
2859 	else
2860 		return sh_css_params_get_default_gdc_lut();
2861 }
2862 
sh_css_params_map_and_store_default_gdc_lut(void)2863 int sh_css_params_map_and_store_default_gdc_lut(void)
2864 {
2865 	int err = 0;
2866 
2867 	IA_CSS_ENTER_PRIVATE("void");
2868 
2869 	/* Is table already mapped? Nothing to do if it is mapped. */
2870 	if (default_gdc_lut != mmgr_NULL)
2871 		return err;
2872 
2873 	host_lut_store((void *)zoom_table);
2874 
2875 	if (!IS_ISP2401)
2876 		default_gdc_lut = hmm_alloc(sizeof(zoom_table), HMM_BO_PRIVATE, 0, NULL, 0);
2877 	else
2878 		default_gdc_lut = sh_css_params_alloc_gdc_lut();
2879 
2880 	if (default_gdc_lut == mmgr_NULL)
2881 		return -ENOMEM;
2882 
2883 	gdc_lut_convert_to_isp_format((const int(*)[HRT_GDC_N])zoom_table,
2884 				      interleaved_lut_temp);
2885 	hmm_store(default_gdc_lut, (int *)interleaved_lut_temp,
2886 		   sizeof(zoom_table));
2887 
2888 	IA_CSS_LEAVE_PRIVATE("lut(%u) err=%d", default_gdc_lut, err);
2889 	return err;
2890 }
2891 
sh_css_params_free_default_gdc_lut(void)2892 void sh_css_params_free_default_gdc_lut(void)
2893 {
2894 	IA_CSS_ENTER_PRIVATE("void");
2895 
2896 	sh_css_params_free_gdc_lut(default_gdc_lut);
2897 	default_gdc_lut = mmgr_NULL;
2898 
2899 	IA_CSS_LEAVE_PRIVATE("void");
2900 }
2901 
sh_css_params_get_default_gdc_lut(void)2902 ia_css_ptr sh_css_params_get_default_gdc_lut(void)
2903 {
2904 	return default_gdc_lut;
2905 }
2906 
free_param_set_callback(ia_css_ptr ptr)2907 static void free_param_set_callback(
2908     ia_css_ptr ptr)
2909 {
2910 	IA_CSS_ENTER_PRIVATE("void");
2911 
2912 	free_ia_css_isp_parameter_set_info(ptr);
2913 
2914 	IA_CSS_LEAVE_PRIVATE("void");
2915 }
2916 
free_buffer_callback(ia_css_ptr ptr)2917 static void free_buffer_callback(
2918     ia_css_ptr ptr)
2919 {
2920 	IA_CSS_ENTER_PRIVATE("void");
2921 
2922 	hmm_free(ptr);
2923 
2924 	IA_CSS_LEAVE_PRIVATE("void");
2925 }
2926 
2927 void
sh_css_param_clear_param_sets(void)2928 sh_css_param_clear_param_sets(void)
2929 {
2930 	IA_CSS_ENTER_PRIVATE("void");
2931 
2932 	ia_css_refcount_clear(IA_CSS_REFCOUNT_PARAM_SET_POOL, &free_param_set_callback);
2933 
2934 	IA_CSS_LEAVE_PRIVATE("void");
2935 }
2936 
2937 /*
2938  * MW: we can define hmm_free() to return a NULL
2939  * then you can write ptr = hmm_free(ptr);
2940  */
2941 #define safe_free(id, x)      \
2942 	do {                  \
2943 		ia_css_refcount_decrement(id, x);     \
2944 		(x) = mmgr_NULL;  \
2945 	} while (0)
2946 
free_map(struct sh_css_ddr_address_map * map)2947 static void free_map(struct sh_css_ddr_address_map *map)
2948 {
2949 	unsigned int i;
2950 
2951 	ia_css_ptr *addrs = (ia_css_ptr *)map;
2952 
2953 	IA_CSS_ENTER_PRIVATE("void");
2954 
2955 	/* free buffers */
2956 	for (i = 0; i < (sizeof(struct sh_css_ddr_address_map_size) /
2957 			 sizeof(size_t)); i++) {
2958 		if (addrs[i] == mmgr_NULL)
2959 			continue;
2960 		safe_free(IA_CSS_REFCOUNT_PARAM_BUFFER, addrs[i]);
2961 	}
2962 
2963 	IA_CSS_LEAVE_PRIVATE("void");
2964 }
2965 
2966 void
ia_css_stream_isp_parameters_uninit(struct ia_css_stream * stream)2967 ia_css_stream_isp_parameters_uninit(struct ia_css_stream *stream)
2968 {
2969 	int i;
2970 	struct ia_css_isp_parameters *params = stream->isp_params_configs;
2971 	struct ia_css_isp_parameters *per_frame_params =
2972 		    stream->per_frame_isp_params_configs;
2973 
2974 	IA_CSS_ENTER_PRIVATE("void");
2975 	if (!params) {
2976 		IA_CSS_LEAVE_PRIVATE("isp_param_configs is NULL");
2977 		return;
2978 	}
2979 
2980 	/* free existing ddr_ptr maps */
2981 	for (i = 0; i < IA_CSS_PIPE_ID_NUM; i++) {
2982 		free_map(&params->pipe_ddr_ptrs[i]);
2983 		if (per_frame_params)
2984 			free_map(&per_frame_params->pipe_ddr_ptrs[i]);
2985 		/* Free up theDVS table memory blocks before recomputing new table */
2986 		if (params->pipe_dvs_6axis_config[i])
2987 			free_dvs_6axis_table(&params->pipe_dvs_6axis_config[i]);
2988 		if (per_frame_params && per_frame_params->pipe_dvs_6axis_config[i])
2989 			free_dvs_6axis_table(&per_frame_params->pipe_dvs_6axis_config[i]);
2990 	}
2991 	free_map(&params->ddr_ptrs);
2992 	if (per_frame_params)
2993 		free_map(&per_frame_params->ddr_ptrs);
2994 
2995 	if (params->fpn_config.data) {
2996 		kvfree(params->fpn_config.data);
2997 		params->fpn_config.data = NULL;
2998 	}
2999 
3000 	/* Free up sc_config (temporal shading table) if it is allocated. */
3001 	if (params->sc_config) {
3002 		ia_css_shading_table_free(params->sc_config);
3003 		params->sc_config = NULL;
3004 	}
3005 	if (per_frame_params) {
3006 		if (per_frame_params->sc_config) {
3007 			ia_css_shading_table_free(per_frame_params->sc_config);
3008 			per_frame_params->sc_config = NULL;
3009 		}
3010 	}
3011 
3012 	kvfree(params);
3013 	if (per_frame_params)
3014 		kvfree(per_frame_params);
3015 	stream->isp_params_configs = NULL;
3016 	stream->per_frame_isp_params_configs = NULL;
3017 
3018 	IA_CSS_LEAVE_PRIVATE("void");
3019 }
3020 
3021 void
sh_css_params_uninit(void)3022 sh_css_params_uninit(void)
3023 {
3024 	unsigned int p, i;
3025 
3026 	IA_CSS_ENTER_PRIVATE("void");
3027 
3028 	ia_css_refcount_decrement(-1, sp_ddr_ptrs);
3029 	sp_ddr_ptrs = mmgr_NULL;
3030 	ia_css_refcount_decrement(-1, xmem_sp_group_ptrs);
3031 	xmem_sp_group_ptrs = mmgr_NULL;
3032 
3033 	for (p = 0; p < IA_CSS_PIPE_ID_NUM; p++)
3034 		for (i = 0; i < SH_CSS_MAX_STAGES; i++) {
3035 			ia_css_refcount_decrement(-1, xmem_sp_stage_ptrs[p][i]);
3036 			xmem_sp_stage_ptrs[p][i] = mmgr_NULL;
3037 			ia_css_refcount_decrement(-1, xmem_isp_stage_ptrs[p][i]);
3038 			xmem_isp_stage_ptrs[p][i] = mmgr_NULL;
3039 		}
3040 
3041 	/* go through the pools to clear references */
3042 	ia_css_refcount_clear(IA_CSS_REFCOUNT_PARAM_SET_POOL, &free_param_set_callback);
3043 	ia_css_refcount_clear(IA_CSS_REFCOUNT_PARAM_BUFFER, &free_buffer_callback);
3044 	ia_css_refcount_clear(-1, &free_buffer_callback);
3045 
3046 	IA_CSS_LEAVE_PRIVATE("void");
3047 }
3048 
3049 static struct ia_css_host_data *
convert_allocate_morph_plane(unsigned short * data,unsigned int width,unsigned int height,unsigned int aligned_width)3050 convert_allocate_morph_plane(
3051     unsigned short *data,
3052     unsigned int width,
3053     unsigned int height,
3054     unsigned int aligned_width)
3055 {
3056 	unsigned int i, j, padding, w;
3057 	struct ia_css_host_data *me;
3058 	unsigned int isp_data_size;
3059 	u16 *isp_data_ptr;
3060 
3061 	IA_CSS_ENTER_PRIVATE("void");
3062 
3063 	/* currently we don't have morph table interpolation yet,
3064 	 * so we allow a wider table to be used. This will be removed
3065 	 * in the future. */
3066 	if (width > aligned_width) {
3067 		padding = 0;
3068 		w = aligned_width;
3069 	} else {
3070 		padding = aligned_width - width;
3071 		w = width;
3072 	}
3073 	isp_data_size = height * (w + padding) * sizeof(uint16_t);
3074 
3075 	me = ia_css_host_data_allocate((size_t)isp_data_size);
3076 
3077 	if (!me) {
3078 		IA_CSS_LEAVE_ERR_PRIVATE(-ENOMEM);
3079 		return NULL;
3080 	}
3081 
3082 	isp_data_ptr = (uint16_t *)me->address;
3083 
3084 	memset(isp_data_ptr, 0, (size_t)isp_data_size);
3085 
3086 	for (i = 0; i < height; i++) {
3087 		for (j = 0; j < w; j++)
3088 			*isp_data_ptr++ = (uint16_t)data[j];
3089 		isp_data_ptr += padding;
3090 		data += width;
3091 	}
3092 
3093 	IA_CSS_LEAVE_PRIVATE("void");
3094 	return me;
3095 }
3096 
3097 static int
store_morph_plane(unsigned short * data,unsigned int width,unsigned int height,ia_css_ptr dest,unsigned int aligned_width)3098 store_morph_plane(
3099     unsigned short *data,
3100     unsigned int width,
3101     unsigned int height,
3102     ia_css_ptr dest,
3103     unsigned int aligned_width) {
3104 	struct ia_css_host_data *isp_data;
3105 
3106 	assert(dest != mmgr_NULL);
3107 
3108 	isp_data = convert_allocate_morph_plane(data, width, height, aligned_width);
3109 	if (!isp_data)
3110 	{
3111 		IA_CSS_LEAVE_ERR_PRIVATE(-ENOMEM);
3112 		return -ENOMEM;
3113 	}
3114 	ia_css_params_store_ia_css_host_data(dest, isp_data);
3115 
3116 	ia_css_host_data_free(isp_data);
3117 	return 0;
3118 }
3119 
sh_css_update_isp_params_to_ddr(struct ia_css_isp_parameters * params,ia_css_ptr ddr_ptr)3120 static void sh_css_update_isp_params_to_ddr(
3121     struct ia_css_isp_parameters *params,
3122     ia_css_ptr ddr_ptr)
3123 {
3124 	size_t size = sizeof(params->uds);
3125 
3126 	IA_CSS_ENTER_PRIVATE("void");
3127 
3128 	assert(params);
3129 
3130 	hmm_store(ddr_ptr, &params->uds, size);
3131 	IA_CSS_LEAVE_PRIVATE("void");
3132 }
3133 
sh_css_update_isp_mem_params_to_ddr(const struct ia_css_binary * binary,ia_css_ptr ddr_mem_ptr,size_t size,enum ia_css_isp_memories mem)3134 static void sh_css_update_isp_mem_params_to_ddr(
3135     const struct ia_css_binary *binary,
3136     ia_css_ptr ddr_mem_ptr,
3137     size_t size,
3138     enum ia_css_isp_memories mem)
3139 {
3140 	const struct ia_css_host_data *params;
3141 
3142 	IA_CSS_ENTER_PRIVATE("void");
3143 
3144 	params = ia_css_isp_param_get_mem_init(&binary->mem_params,
3145 					       IA_CSS_PARAM_CLASS_PARAM, mem);
3146 	hmm_store(ddr_mem_ptr, params->address, size);
3147 
3148 	IA_CSS_LEAVE_PRIVATE("void");
3149 }
3150 
ia_css_dequeue_param_buffers(void)3151 void ia_css_dequeue_param_buffers(/*unsigned int pipe_num*/ void)
3152 {
3153 	unsigned int i;
3154 	ia_css_ptr cpy;
3155 	enum sh_css_queue_id param_queue_ids[3] = {	IA_CSS_PARAMETER_SET_QUEUE_ID,
3156 						    IA_CSS_PER_FRAME_PARAMETER_SET_QUEUE_ID,
3157 						    SH_CSS_INVALID_QUEUE_ID
3158 						  };
3159 
3160 	IA_CSS_ENTER_PRIVATE("void");
3161 
3162 	if (!sh_css_sp_is_running()) {
3163 		IA_CSS_LEAVE_PRIVATE("sp is not running");
3164 		/* SP is not running. The queues are not valid */
3165 		return;
3166 	}
3167 
3168 	for (i = 0; SH_CSS_INVALID_QUEUE_ID != param_queue_ids[i]; i++) {
3169 		cpy = (ia_css_ptr)0;
3170 		/* clean-up old copy */
3171 		while (ia_css_bufq_dequeue_buffer(param_queue_ids[i],
3172 						  (uint32_t *)&cpy) == 0) {
3173 			/* TMP: keep track of dequeued param set count
3174 			 */
3175 			g_param_buffer_dequeue_count++;
3176 			ia_css_bufq_enqueue_psys_event(
3177 			    IA_CSS_PSYS_SW_EVENT_BUFFER_DEQUEUED,
3178 			    0,
3179 			    param_queue_ids[i],
3180 			    0);
3181 
3182 			IA_CSS_LOG("dequeued param set %x from %d, release ref", cpy, 0);
3183 			free_ia_css_isp_parameter_set_info(cpy);
3184 			cpy = (ia_css_ptr)0;
3185 		}
3186 	}
3187 
3188 	IA_CSS_LEAVE_PRIVATE("void");
3189 }
3190 
3191 static void
process_kernel_parameters(unsigned int pipe_id,struct ia_css_pipeline_stage * stage,struct ia_css_isp_parameters * params,unsigned int isp_pipe_version,unsigned int raw_bit_depth)3192 process_kernel_parameters(unsigned int pipe_id,
3193 			  struct ia_css_pipeline_stage *stage,
3194 			  struct ia_css_isp_parameters *params,
3195 			  unsigned int isp_pipe_version,
3196 			  unsigned int raw_bit_depth)
3197 {
3198 	unsigned int param_id;
3199 
3200 	(void)isp_pipe_version;
3201 	(void)raw_bit_depth;
3202 
3203 	sh_css_enable_pipeline(stage->binary);
3204 
3205 	if (params->config_changed[IA_CSS_OB_ID]) {
3206 		ia_css_ob_configure(&params->stream_configs.ob,
3207 				    isp_pipe_version, raw_bit_depth);
3208 	}
3209 	if (params->config_changed[IA_CSS_S3A_ID]) {
3210 		ia_css_s3a_configure(raw_bit_depth);
3211 	}
3212 	/* Copy stage uds parameters to config, since they can differ per stage.
3213 	 */
3214 	params->crop_config.crop_pos = params->uds[stage->stage_num].crop_pos;
3215 	params->uds_config.crop_pos  = params->uds[stage->stage_num].crop_pos;
3216 	params->uds_config.uds       = params->uds[stage->stage_num].uds;
3217 	/* Call parameter process functions for all kernels */
3218 	/* Skip SC, since that is called on a temp sc table */
3219 	for (param_id = 0; param_id < IA_CSS_NUM_PARAMETER_IDS; param_id++) {
3220 		if (param_id == IA_CSS_SC_ID) continue;
3221 		if (params->config_changed[param_id])
3222 			ia_css_kernel_process_param[param_id](pipe_id, stage, params);
3223 	}
3224 }
3225 
3226 int
sh_css_param_update_isp_params(struct ia_css_pipe * curr_pipe,struct ia_css_isp_parameters * params,bool commit,struct ia_css_pipe * pipe_in)3227 sh_css_param_update_isp_params(struct ia_css_pipe *curr_pipe,
3228 			       struct ia_css_isp_parameters *params,
3229 			       bool commit,
3230 			       struct ia_css_pipe *pipe_in) {
3231 	int err = 0;
3232 	ia_css_ptr cpy;
3233 	int i;
3234 	unsigned int raw_bit_depth = 10;
3235 	unsigned int isp_pipe_version = SH_CSS_ISP_PIPE_VERSION_1;
3236 	bool acc_cluster_params_changed = false;
3237 	unsigned int thread_id, pipe_num;
3238 
3239 	(void)acc_cluster_params_changed;
3240 
3241 	assert(curr_pipe);
3242 
3243 	IA_CSS_ENTER_PRIVATE("pipe=%p, isp_parameters_id=%d", pipe_in, params->isp_parameters_id);
3244 	raw_bit_depth = ia_css_stream_input_format_bits_per_pixel(curr_pipe->stream);
3245 
3246 	/* now make the map available to the sp */
3247 	if (!commit)
3248 	{
3249 		IA_CSS_LEAVE_ERR_PRIVATE(err);
3250 		return err;
3251 	}
3252 	/* enqueue a copies of the mem_map to
3253 	   the designated pipelines */
3254 	for (i = 0; i < curr_pipe->stream->num_pipes; i++)
3255 	{
3256 		struct ia_css_pipe *pipe;
3257 		struct sh_css_ddr_address_map *cur_map;
3258 		struct sh_css_ddr_address_map_size *cur_map_size;
3259 		struct ia_css_isp_parameter_set_info isp_params_info;
3260 		struct ia_css_pipeline *pipeline;
3261 		struct ia_css_pipeline_stage *stage;
3262 
3263 		enum sh_css_queue_id queue_id;
3264 
3265 		pipe = curr_pipe->stream->pipes[i];
3266 		pipeline = ia_css_pipe_get_pipeline(pipe);
3267 		pipe_num = ia_css_pipe_get_pipe_num(pipe);
3268 		isp_pipe_version = ia_css_pipe_get_isp_pipe_version(pipe);
3269 		ia_css_pipeline_get_sp_thread_id(pipe_num, &thread_id);
3270 
3271 #if defined(SH_CSS_ENABLE_PER_FRAME_PARAMS)
3272 		ia_css_query_internal_queue_id(params->output_frame
3273 					       ? IA_CSS_BUFFER_TYPE_PER_FRAME_PARAMETER_SET
3274 					       : IA_CSS_BUFFER_TYPE_PARAMETER_SET,
3275 					       thread_id, &queue_id);
3276 #else
3277 		ia_css_query_internal_queue_id(IA_CSS_BUFFER_TYPE_PARAMETER_SET, thread_id,
3278 					       &queue_id);
3279 #endif
3280 		if (!sh_css_sp_is_running()) {
3281 			/* SP is not running. The queues are not valid */
3282 			err = -EBUSY;
3283 			break;
3284 		}
3285 		cur_map = &params->pipe_ddr_ptrs[pipeline->pipe_id];
3286 		cur_map_size = &params->pipe_ddr_ptrs_size[pipeline->pipe_id];
3287 
3288 		/* TODO: Normally, zoom and motion parameters shouldn't
3289 		 * be part of "isp_params" as it is resolution/pipe dependent
3290 		 * Therefore, move the zoom config elsewhere (e.g. shading
3291 		 * table can be taken as an example! @GC
3292 		 * */
3293 		{
3294 			/* we have to do this per pipeline because */
3295 			/* the processing is a.o. resolution dependent */
3296 			err = ia_css_process_zoom_and_motion(params,
3297 							     pipeline->stages);
3298 			if (err)
3299 				return err;
3300 		}
3301 		/* check if to actually update the parameters for this pipe */
3302 		/* When API change is implemented making good distinction between
3303 		* stream config and pipe config this skipping code can be moved out of the #ifdef */
3304 		if (pipe_in && (pipe != pipe_in)) {
3305 			IA_CSS_LOG("skipping pipe %p", pipe);
3306 			continue;
3307 		}
3308 
3309 		/* BZ 125915, should be moved till after "update other buff" */
3310 		/* update the other buffers to the pipe specific copies */
3311 		for (stage = pipeline->stages; stage; stage = stage->next) {
3312 			unsigned int mem;
3313 
3314 			if (!stage || !stage->binary)
3315 				continue;
3316 
3317 			process_kernel_parameters(pipeline->pipe_id,
3318 						  stage, params,
3319 						  isp_pipe_version, raw_bit_depth);
3320 
3321 			err = sh_css_params_write_to_ddr_internal(
3322 				  pipe,
3323 				  pipeline->pipe_id,
3324 				  params,
3325 				  stage,
3326 				  cur_map,
3327 				  cur_map_size);
3328 
3329 			if (err)
3330 				break;
3331 			for (mem = 0; mem < IA_CSS_NUM_MEMORIES; mem++) {
3332 				params->isp_mem_params_changed
3333 				[pipeline->pipe_id][stage->stage_num][mem] = false;
3334 			}
3335 		} /* for */
3336 		if (err)
3337 			break;
3338 		/* update isp_params to pipe specific copies */
3339 		if (params->isp_params_changed) {
3340 			reallocate_buffer(&cur_map->isp_param,
3341 					  &cur_map_size->isp_param,
3342 					  cur_map_size->isp_param,
3343 					  true,
3344 					  &err);
3345 			if (err)
3346 				break;
3347 			sh_css_update_isp_params_to_ddr(params, cur_map->isp_param);
3348 		}
3349 
3350 		/* last make referenced copy */
3351 		err = ref_sh_css_ddr_address_map(
3352 			  cur_map,
3353 			  &isp_params_info.mem_map);
3354 		if (err)
3355 			break;
3356 
3357 		/* Update Parameters ID */
3358 		isp_params_info.isp_parameters_id = params->isp_parameters_id;
3359 
3360 		/* Update output frame pointer */
3361 		isp_params_info.output_frame_ptr =
3362 		    (params->output_frame) ? params->output_frame->data : mmgr_NULL;
3363 
3364 		/* now write the copy to ddr */
3365 		err = write_ia_css_isp_parameter_set_info_to_ddr(&isp_params_info, &cpy);
3366 		if (err)
3367 			break;
3368 
3369 		/* enqueue the set to sp */
3370 		IA_CSS_LOG("queue param set %x to %d", cpy, thread_id);
3371 
3372 		err = ia_css_bufq_enqueue_buffer(thread_id, queue_id, (uint32_t)cpy);
3373 		if (err) {
3374 			free_ia_css_isp_parameter_set_info(cpy);
3375 #if defined(SH_CSS_ENABLE_PER_FRAME_PARAMS)
3376 			IA_CSS_LOG("pfp: FAILED to add config id %d for OF %d to q %d on thread %d",
3377 				   isp_params_info.isp_parameters_id,
3378 				   isp_params_info.output_frame_ptr,
3379 				   queue_id, thread_id);
3380 #endif
3381 			break;
3382 		} else {
3383 			/* TMP: check discrepancy between nr of enqueued
3384 			 * parameter sets and dequeued sets
3385 			 */
3386 			g_param_buffer_enqueue_count++;
3387 			assert(g_param_buffer_enqueue_count < g_param_buffer_dequeue_count + 50);
3388 			/*
3389 			 * Tell the SP which queues are not empty,
3390 			 * by sending the software event.
3391 			 */
3392 			if (!sh_css_sp_is_running()) {
3393 				/* SP is not running. The queues are not valid */
3394 				IA_CSS_LEAVE_ERR_PRIVATE(-EBUSY);
3395 				return -EBUSY;
3396 			}
3397 			ia_css_bufq_enqueue_psys_event(
3398 			    IA_CSS_PSYS_SW_EVENT_BUFFER_ENQUEUED,
3399 			    (uint8_t)thread_id,
3400 			    (uint8_t)queue_id,
3401 			    0);
3402 #if defined(SH_CSS_ENABLE_PER_FRAME_PARAMS)
3403 			IA_CSS_LOG("pfp: added config id %d for OF %d to q %d on thread %d",
3404 				   isp_params_info.isp_parameters_id,
3405 				   isp_params_info.output_frame_ptr,
3406 				   queue_id, thread_id);
3407 #endif
3408 		}
3409 		/* clean-up old copy */
3410 		ia_css_dequeue_param_buffers(/*pipe_num*/);
3411 		params->pipe_dvs_6axis_config_changed[pipeline->pipe_id] = false;
3412 	} /* end for each 'active' pipeline */
3413 	/* clear the changed flags after all params
3414 	for all pipelines have been updated */
3415 	params->isp_params_changed = false;
3416 	params->sc_table_changed = false;
3417 	params->dis_coef_table_changed = false;
3418 	params->dvs2_coef_table_changed = false;
3419 	params->morph_table_changed = false;
3420 	params->dz_config_changed = false;
3421 	params->motion_config_changed = false;
3422 	/* ------ deprecated(bz675) : from ------ */
3423 	params->shading_settings_changed = false;
3424 	/* ------ deprecated(bz675) : to ------ */
3425 
3426 	memset(&params->config_changed[0], 0, sizeof(params->config_changed));
3427 
3428 	IA_CSS_LEAVE_ERR_PRIVATE(err);
3429 	return err;
3430 }
3431 
3432 static int
sh_css_params_write_to_ddr_internal(struct ia_css_pipe * pipe,unsigned int pipe_id,struct ia_css_isp_parameters * params,const struct ia_css_pipeline_stage * stage,struct sh_css_ddr_address_map * ddr_map,struct sh_css_ddr_address_map_size * ddr_map_size)3433 sh_css_params_write_to_ddr_internal(
3434     struct ia_css_pipe *pipe,
3435     unsigned int pipe_id,
3436     struct ia_css_isp_parameters *params,
3437     const struct ia_css_pipeline_stage *stage,
3438     struct sh_css_ddr_address_map *ddr_map,
3439     struct sh_css_ddr_address_map_size *ddr_map_size) {
3440 	int err;
3441 	const struct ia_css_binary *binary;
3442 
3443 	unsigned int stage_num;
3444 	unsigned int mem;
3445 	bool buff_realloced;
3446 
3447 	/* struct is > 128 bytes so it should not be on stack (see checkpatch) */
3448 	static struct ia_css_macc_table converted_macc_table;
3449 
3450 	IA_CSS_ENTER_PRIVATE("void");
3451 	assert(params);
3452 	assert(ddr_map);
3453 	assert(ddr_map_size);
3454 	assert(stage);
3455 
3456 	binary = stage->binary;
3457 	assert(binary);
3458 
3459 	stage_num = stage->stage_num;
3460 
3461 	if (binary->info->sp.enable.fpnr)
3462 	{
3463 		buff_realloced = reallocate_buffer(&ddr_map->fpn_tbl,
3464 						   &ddr_map_size->fpn_tbl,
3465 						   (size_t)(FPNTBL_BYTES(binary)),
3466 						   params->config_changed[IA_CSS_FPN_ID],
3467 						   &err);
3468 		if (err) {
3469 			IA_CSS_LEAVE_ERR_PRIVATE(err);
3470 			return err;
3471 		}
3472 		if (params->config_changed[IA_CSS_FPN_ID] || buff_realloced) {
3473 			if (params->fpn_config.enabled) {
3474 				err = store_fpntbl(params, ddr_map->fpn_tbl);
3475 				if (err) {
3476 					IA_CSS_LEAVE_ERR_PRIVATE(err);
3477 					return err;
3478 				}
3479 			}
3480 		}
3481 	}
3482 
3483 	if (binary->info->sp.enable.sc)
3484 	{
3485 		u32 enable_conv;
3486 		size_t bytes;
3487 
3488 		if (!IS_ISP2401)
3489 			bytes = ISP2400_SCTBL_BYTES(binary);
3490 		else
3491 			bytes = ISP2401_SCTBL_BYTES(binary);
3492 
3493 		enable_conv = params->shading_settings.enable_shading_table_conversion;
3494 
3495 		buff_realloced = reallocate_buffer(&ddr_map->sc_tbl,
3496 						   &ddr_map_size->sc_tbl,
3497 						   bytes,
3498 						   params->sc_table_changed,
3499 						   &err);
3500 		if (err) {
3501 			IA_CSS_LEAVE_ERR_PRIVATE(err);
3502 			return err;
3503 		}
3504 
3505 		if (params->shading_settings_changed ||
3506 		    params->sc_table_changed || buff_realloced) {
3507 			if (enable_conv == 0) {
3508 				if (params->sc_table) {
3509 					/* store the shading table to ddr */
3510 					err = ia_css_params_store_sctbl(stage, ddr_map->sc_tbl, params->sc_table);
3511 					if (err) {
3512 						IA_CSS_LEAVE_ERR_PRIVATE(err);
3513 						return err;
3514 					}
3515 					/* set sc_config to isp */
3516 					params->sc_config = (struct ia_css_shading_table *)params->sc_table;
3517 					ia_css_kernel_process_param[IA_CSS_SC_ID](pipe_id, stage, params);
3518 					params->sc_config = NULL;
3519 				} else {
3520 					/* generate the identical shading table */
3521 					if (params->sc_config) {
3522 						ia_css_shading_table_free(params->sc_config);
3523 						params->sc_config = NULL;
3524 					}
3525 					sh_css_params_shading_id_table_generate(&params->sc_config,
3526 										binary->sctbl_width_per_color,
3527 										binary->sctbl_height);
3528 					if (!params->sc_config) {
3529 						IA_CSS_LEAVE_ERR_PRIVATE(-ENOMEM);
3530 						return -ENOMEM;
3531 					}
3532 
3533 					/* store the shading table to ddr */
3534 					err = ia_css_params_store_sctbl(stage, ddr_map->sc_tbl, params->sc_config);
3535 					if (err) {
3536 						IA_CSS_LEAVE_ERR_PRIVATE(err);
3537 						return err;
3538 					}
3539 
3540 					/* set sc_config to isp */
3541 					ia_css_kernel_process_param[IA_CSS_SC_ID](pipe_id, stage, params);
3542 
3543 					/* free the shading table */
3544 					ia_css_shading_table_free(params->sc_config);
3545 					params->sc_config = NULL;
3546 				}
3547 			} else { /* legacy */
3548 				/* ------ deprecated(bz675) : from ------ */
3549 				/* shading table is full resolution, reduce */
3550 				if (params->sc_config) {
3551 					ia_css_shading_table_free(params->sc_config);
3552 					params->sc_config = NULL;
3553 				}
3554 				prepare_shading_table(
3555 				    (const struct ia_css_shading_table *)params->sc_table,
3556 				    params->sensor_binning,
3557 				    &params->sc_config,
3558 				    binary, pipe->required_bds_factor);
3559 				if (!params->sc_config) {
3560 					IA_CSS_LEAVE_ERR_PRIVATE(-ENOMEM);
3561 					return -ENOMEM;
3562 				}
3563 
3564 				/* store the shading table to ddr */
3565 				err = ia_css_params_store_sctbl(stage, ddr_map->sc_tbl, params->sc_config);
3566 				if (err) {
3567 					IA_CSS_LEAVE_ERR_PRIVATE(err);
3568 					return err;
3569 				}
3570 
3571 				/* set sc_config to isp */
3572 				ia_css_kernel_process_param[IA_CSS_SC_ID](pipe_id, stage, params);
3573 
3574 				/* free the shading table */
3575 				ia_css_shading_table_free(params->sc_config);
3576 				params->sc_config = NULL;
3577 				/* ------ deprecated(bz675) : to ------ */
3578 			}
3579 		}
3580 	}
3581 
3582 	/* DPC configuration is made pipe specific to allow flexibility in positioning of the
3583 	 * DPC kernel. The code below sets the pipe specific configuration to
3584 	 * individual binaries. */
3585 	if (IS_ISP2401 &&
3586 	    params->pipe_dpc_config_changed[pipe_id] && binary->info->sp.enable.dpc)
3587 	{
3588 		unsigned int size   =
3589 		    stage->binary->info->mem_offsets.offsets.param->dmem.dp.size;
3590 
3591 		unsigned int offset =
3592 		    stage->binary->info->mem_offsets.offsets.param->dmem.dp.offset;
3593 
3594 		if (size) {
3595 			ia_css_dp_encode((struct sh_css_isp_dp_params *)
3596 					 &binary->mem_params.params[IA_CSS_PARAM_CLASS_PARAM][IA_CSS_ISP_DMEM].address[offset],
3597 					 &params->pipe_dp_config[pipe_id], size);
3598 
3599 			params->isp_params_changed = true;
3600 			params->isp_mem_params_changed[pipe_id][stage->stage_num][IA_CSS_ISP_DMEM] =
3601 			    true;
3602 		}
3603 	}
3604 
3605 	if (params->config_changed[IA_CSS_MACC_ID] && binary->info->sp.enable.macc)
3606 	{
3607 		unsigned int i, j, idx;
3608 		unsigned int idx_map[] = {
3609 			0, 1, 3, 2, 6, 7, 5, 4, 12, 13, 15, 14, 10, 11, 9, 8
3610 		};
3611 
3612 		for (i = 0; i < IA_CSS_MACC_NUM_AXES; i++) {
3613 			idx = 4 * idx_map[i];
3614 			j   = 4 * i;
3615 
3616 			if (binary->info->sp.pipeline.isp_pipe_version == SH_CSS_ISP_PIPE_VERSION_1) {
3617 				converted_macc_table.data[idx] =
3618 				    (int16_t)sDIGIT_FITTING(params->macc_table.data[j],
3619 							    13, SH_CSS_MACC_COEF_SHIFT);
3620 				converted_macc_table.data[idx + 1] =
3621 				    (int16_t)sDIGIT_FITTING(params->macc_table.data[j + 1],
3622 							    13, SH_CSS_MACC_COEF_SHIFT);
3623 				converted_macc_table.data[idx + 2] =
3624 				    (int16_t)sDIGIT_FITTING(params->macc_table.data[j + 2],
3625 							    13, SH_CSS_MACC_COEF_SHIFT);
3626 				converted_macc_table.data[idx + 3] =
3627 				    (int16_t)sDIGIT_FITTING(params->macc_table.data[j + 3],
3628 							    13, SH_CSS_MACC_COEF_SHIFT);
3629 			} else if (binary->info->sp.pipeline.isp_pipe_version ==
3630 				   SH_CSS_ISP_PIPE_VERSION_2_2) {
3631 				converted_macc_table.data[idx] =
3632 				    params->macc_table.data[j];
3633 				converted_macc_table.data[idx + 1] =
3634 				    params->macc_table.data[j + 1];
3635 				converted_macc_table.data[idx + 2] =
3636 				    params->macc_table.data[j + 2];
3637 				converted_macc_table.data[idx + 3] =
3638 				    params->macc_table.data[j + 3];
3639 			}
3640 		}
3641 		reallocate_buffer(&ddr_map->macc_tbl,
3642 				  &ddr_map_size->macc_tbl,
3643 				  ddr_map_size->macc_tbl,
3644 				  true,
3645 				  &err);
3646 		if (err) {
3647 			IA_CSS_LEAVE_ERR_PRIVATE(err);
3648 			return err;
3649 		}
3650 		hmm_store(ddr_map->macc_tbl,
3651 			   converted_macc_table.data,
3652 			   sizeof(converted_macc_table.data));
3653 	}
3654 
3655 	if (binary->info->sp.enable.dvs_6axis)
3656 	{
3657 		/* because UV is packed into the Y plane, calc total
3658 		 * YYU size = /2 gives size of UV-only,
3659 		 * total YYU size = UV-only * 3.
3660 		 */
3661 		buff_realloced = reallocate_buffer(
3662 				     &ddr_map->dvs_6axis_params_y,
3663 				     &ddr_map_size->dvs_6axis_params_y,
3664 				     (size_t)((DVS_6AXIS_BYTES(binary) / 2) * 3),
3665 				     params->pipe_dvs_6axis_config_changed[pipe_id],
3666 				     &err);
3667 		if (err) {
3668 			IA_CSS_LEAVE_ERR_PRIVATE(err);
3669 			return err;
3670 		}
3671 
3672 		if (params->pipe_dvs_6axis_config_changed[pipe_id] || buff_realloced) {
3673 			const struct ia_css_frame_info *dvs_in_frame_info;
3674 
3675 			if (stage->args.delay_frames[0]) {
3676 				/*When delay frames are present(as in case of video),
3677 				they are used for dvs. Configure DVS using those params*/
3678 				dvs_in_frame_info = &stage->args.delay_frames[0]->info;
3679 			} else {
3680 				/*Otherwise, use input frame to configure DVS*/
3681 				dvs_in_frame_info = &stage->args.in_frame->info;
3682 			}
3683 
3684 			/* Generate default DVS unity table on start up*/
3685 			if (!params->pipe_dvs_6axis_config[pipe_id]) {
3686 				struct ia_css_resolution dvs_offset = {0};
3687 
3688 				if (!IS_ISP2401) {
3689 					dvs_offset.width = (PIX_SHIFT_FILTER_RUN_IN_X + binary->dvs_envelope.width) / 2;
3690 				} else {
3691 					if (binary->dvs_envelope.width || binary->dvs_envelope.height) {
3692 						dvs_offset.width  = (PIX_SHIFT_FILTER_RUN_IN_X + binary->dvs_envelope.width) / 2;
3693 					}
3694 				}
3695 				dvs_offset.height = (PIX_SHIFT_FILTER_RUN_IN_Y + binary->dvs_envelope.height) / 2;
3696 
3697 				params->pipe_dvs_6axis_config[pipe_id] =
3698 				    generate_dvs_6axis_table(&binary->out_frame_info[0].res, &dvs_offset);
3699 				if (!params->pipe_dvs_6axis_config[pipe_id]) {
3700 					IA_CSS_LEAVE_ERR_PRIVATE(-ENOMEM);
3701 					return -ENOMEM;
3702 				}
3703 				params->pipe_dvs_6axis_config_changed[pipe_id] = true;
3704 
3705 				store_dvs_6axis_config(params->pipe_dvs_6axis_config[pipe_id],
3706 						    binary,
3707 						    dvs_in_frame_info,
3708 						    ddr_map->dvs_6axis_params_y);
3709 				params->isp_params_changed = true;
3710 			}
3711 		}
3712 	}
3713 
3714 	if (binary->info->sp.enable.ca_gdc)
3715 	{
3716 		unsigned int i;
3717 		ia_css_ptr *virt_addr_tetra_x[
3718 
3719 		IA_CSS_MORPH_TABLE_NUM_PLANES];
3720 		size_t *virt_size_tetra_x[
3721 
3722 		IA_CSS_MORPH_TABLE_NUM_PLANES];
3723 		ia_css_ptr *virt_addr_tetra_y[
3724 
3725 		IA_CSS_MORPH_TABLE_NUM_PLANES];
3726 		size_t *virt_size_tetra_y[
3727 
3728 		IA_CSS_MORPH_TABLE_NUM_PLANES];
3729 
3730 		virt_addr_tetra_x[0] = &ddr_map->tetra_r_x;
3731 		virt_addr_tetra_x[1] = &ddr_map->tetra_gr_x;
3732 		virt_addr_tetra_x[2] = &ddr_map->tetra_gb_x;
3733 		virt_addr_tetra_x[3] = &ddr_map->tetra_b_x;
3734 		virt_addr_tetra_x[4] = &ddr_map->tetra_ratb_x;
3735 		virt_addr_tetra_x[5] = &ddr_map->tetra_batr_x;
3736 
3737 		virt_size_tetra_x[0] = &ddr_map_size->tetra_r_x;
3738 		virt_size_tetra_x[1] = &ddr_map_size->tetra_gr_x;
3739 		virt_size_tetra_x[2] = &ddr_map_size->tetra_gb_x;
3740 		virt_size_tetra_x[3] = &ddr_map_size->tetra_b_x;
3741 		virt_size_tetra_x[4] = &ddr_map_size->tetra_ratb_x;
3742 		virt_size_tetra_x[5] = &ddr_map_size->tetra_batr_x;
3743 
3744 		virt_addr_tetra_y[0] = &ddr_map->tetra_r_y;
3745 		virt_addr_tetra_y[1] = &ddr_map->tetra_gr_y;
3746 		virt_addr_tetra_y[2] = &ddr_map->tetra_gb_y;
3747 		virt_addr_tetra_y[3] = &ddr_map->tetra_b_y;
3748 		virt_addr_tetra_y[4] = &ddr_map->tetra_ratb_y;
3749 		virt_addr_tetra_y[5] = &ddr_map->tetra_batr_y;
3750 
3751 		virt_size_tetra_y[0] = &ddr_map_size->tetra_r_y;
3752 		virt_size_tetra_y[1] = &ddr_map_size->tetra_gr_y;
3753 		virt_size_tetra_y[2] = &ddr_map_size->tetra_gb_y;
3754 		virt_size_tetra_y[3] = &ddr_map_size->tetra_b_y;
3755 		virt_size_tetra_y[4] = &ddr_map_size->tetra_ratb_y;
3756 		virt_size_tetra_y[5] = &ddr_map_size->tetra_batr_y;
3757 
3758 		buff_realloced = false;
3759 		for (i = 0; i < IA_CSS_MORPH_TABLE_NUM_PLANES; i++) {
3760 			buff_realloced |=
3761 			    reallocate_buffer(virt_addr_tetra_x[i],
3762 					    virt_size_tetra_x[i],
3763 					    (size_t)
3764 					    (MORPH_PLANE_BYTES(binary)),
3765 					    params->morph_table_changed,
3766 					    &err);
3767 			if (err) {
3768 				IA_CSS_LEAVE_ERR_PRIVATE(err);
3769 				return err;
3770 			}
3771 			buff_realloced |=
3772 			    reallocate_buffer(virt_addr_tetra_y[i],
3773 					    virt_size_tetra_y[i],
3774 					    (size_t)
3775 					    (MORPH_PLANE_BYTES(binary)),
3776 					    params->morph_table_changed,
3777 					    &err);
3778 			if (err) {
3779 				IA_CSS_LEAVE_ERR_PRIVATE(err);
3780 				return err;
3781 			}
3782 		}
3783 		if (params->morph_table_changed || buff_realloced) {
3784 			const struct ia_css_morph_table *table = params->morph_table;
3785 			struct ia_css_morph_table *id_table = NULL;
3786 
3787 			if ((table) &&
3788 			    (table->width < binary->morph_tbl_width ||
3789 			    table->height < binary->morph_tbl_height)) {
3790 				table = NULL;
3791 			}
3792 			if (!table) {
3793 				err = sh_css_params_default_morph_table(&id_table,
3794 									binary);
3795 				if (err) {
3796 					IA_CSS_LEAVE_ERR_PRIVATE(err);
3797 					return err;
3798 				}
3799 				table = id_table;
3800 			}
3801 
3802 			for (i = 0; i < IA_CSS_MORPH_TABLE_NUM_PLANES; i++) {
3803 				store_morph_plane(table->coordinates_x[i],
3804 						table->width,
3805 						table->height,
3806 						*virt_addr_tetra_x[i],
3807 						binary->morph_tbl_aligned_width);
3808 				store_morph_plane(table->coordinates_y[i],
3809 						table->width,
3810 						table->height,
3811 						*virt_addr_tetra_y[i],
3812 						binary->morph_tbl_aligned_width);
3813 			}
3814 			if (id_table)
3815 				ia_css_morph_table_free(id_table);
3816 		}
3817 	}
3818 
3819 	/* After special cases like SC, FPN since they may change parameters */
3820 	for (mem = 0; mem < N_IA_CSS_MEMORIES; mem++)
3821 	{
3822 		const struct ia_css_isp_data *isp_data =
3823 		    ia_css_isp_param_get_isp_mem_init(&binary->info->sp.mem_initializers,
3824 						    IA_CSS_PARAM_CLASS_PARAM, mem);
3825 		size_t size = isp_data->size;
3826 
3827 		if (!size) continue;
3828 		buff_realloced = reallocate_buffer(&ddr_map->isp_mem_param[stage_num][mem],
3829 						&ddr_map_size->isp_mem_param[stage_num][mem],
3830 						size,
3831 						params->isp_mem_params_changed[pipe_id][stage_num][mem],
3832 						&err);
3833 		if (err) {
3834 			IA_CSS_LEAVE_ERR_PRIVATE(err);
3835 			return err;
3836 		}
3837 		if (params->isp_mem_params_changed[pipe_id][stage_num][mem] || buff_realloced) {
3838 			sh_css_update_isp_mem_params_to_ddr(binary,
3839 							    ddr_map->isp_mem_param[stage_num][mem],
3840 							    ddr_map_size->isp_mem_param[stage_num][mem], mem);
3841 		}
3842 	}
3843 
3844 	IA_CSS_LEAVE_ERR_PRIVATE(0);
3845 	return 0;
3846 }
3847 
ia_css_get_fpn_table(struct ia_css_stream * stream)3848 const struct ia_css_fpn_table *ia_css_get_fpn_table(struct ia_css_stream
3849 	*stream)
3850 {
3851 	struct ia_css_isp_parameters *params;
3852 
3853 	IA_CSS_ENTER_LEAVE("void");
3854 	assert(stream);
3855 
3856 	params = stream->isp_params_configs;
3857 
3858 	return &params->fpn_config;
3859 }
3860 
ia_css_get_shading_table(struct ia_css_stream * stream)3861 struct ia_css_shading_table *ia_css_get_shading_table(struct ia_css_stream
3862 	*stream)
3863 {
3864 	struct ia_css_shading_table *table = NULL;
3865 	struct ia_css_isp_parameters *params;
3866 
3867 	IA_CSS_ENTER("void");
3868 
3869 	assert(stream);
3870 
3871 	params = stream->isp_params_configs;
3872 	if (!params)
3873 		return NULL;
3874 
3875 	if (params->shading_settings.enable_shading_table_conversion == 0) {
3876 		if (params->sc_table) {
3877 			table = (struct ia_css_shading_table *)params->sc_table;
3878 		} else {
3879 			const struct ia_css_binary *binary
3880 			    = ia_css_stream_get_shading_correction_binary(stream);
3881 			if (binary) {
3882 				/* generate the identical shading table */
3883 				if (params->sc_config) {
3884 					ia_css_shading_table_free(params->sc_config);
3885 					params->sc_config = NULL;
3886 				}
3887 				sh_css_params_shading_id_table_generate(&params->sc_config,
3888 									binary->sctbl_width_per_color,
3889 									binary->sctbl_height);
3890 				table = params->sc_config;
3891 				/* The sc_config will be freed in the
3892 				 * ia_css_stream_isp_parameters_uninit function. */
3893 			}
3894 		}
3895 	} else {
3896 		/* ------ deprecated(bz675) : from ------ */
3897 		const struct ia_css_binary *binary
3898 		    = ia_css_stream_get_shading_correction_binary(stream);
3899 		struct ia_css_pipe *pipe;
3900 
3901 		/**********************************************************************/
3902 		/* following code is copied from function ia_css_stream_get_shading_correction_binary()
3903 		 * to match with the binary */
3904 		pipe = stream->pipes[0];
3905 
3906 		if (stream->num_pipes == 2) {
3907 			assert(stream->pipes[1]);
3908 			if (stream->pipes[1]->config.mode == IA_CSS_PIPE_MODE_VIDEO ||
3909 			    stream->pipes[1]->config.mode == IA_CSS_PIPE_MODE_PREVIEW)
3910 				pipe = stream->pipes[1];
3911 		}
3912 		/**********************************************************************/
3913 		if (binary) {
3914 			if (params->sc_config) {
3915 				ia_css_shading_table_free(params->sc_config);
3916 				params->sc_config = NULL;
3917 			}
3918 			prepare_shading_table(
3919 			    (const struct ia_css_shading_table *)params->sc_table,
3920 			    params->sensor_binning,
3921 			    &params->sc_config,
3922 			    binary, pipe->required_bds_factor);
3923 
3924 			table = params->sc_config;
3925 			/* The sc_config will be freed in the
3926 			 * ia_css_stream_isp_parameters_uninit function. */
3927 		}
3928 		/* ------ deprecated(bz675) : to ------ */
3929 	}
3930 
3931 	IA_CSS_LEAVE("table=%p", table);
3932 
3933 	return table;
3934 }
3935 
sh_css_store_sp_group_to_ddr(void)3936 ia_css_ptr sh_css_store_sp_group_to_ddr(void)
3937 {
3938 	IA_CSS_ENTER_LEAVE_PRIVATE("void");
3939 	hmm_store(xmem_sp_group_ptrs,
3940 		   &sh_css_sp_group,
3941 		   sizeof(struct sh_css_sp_group));
3942 	return xmem_sp_group_ptrs;
3943 }
3944 
sh_css_store_sp_stage_to_ddr(unsigned int pipe,unsigned int stage)3945 ia_css_ptr sh_css_store_sp_stage_to_ddr(
3946     unsigned int pipe,
3947     unsigned int stage)
3948 {
3949 	IA_CSS_ENTER_LEAVE_PRIVATE("void");
3950 	hmm_store(xmem_sp_stage_ptrs[pipe][stage],
3951 		   &sh_css_sp_stage,
3952 		   sizeof(struct sh_css_sp_stage));
3953 	return xmem_sp_stage_ptrs[pipe][stage];
3954 }
3955 
sh_css_store_isp_stage_to_ddr(unsigned int pipe,unsigned int stage)3956 ia_css_ptr sh_css_store_isp_stage_to_ddr(
3957     unsigned int pipe,
3958     unsigned int stage)
3959 {
3960 	IA_CSS_ENTER_LEAVE_PRIVATE("void");
3961 	hmm_store(xmem_isp_stage_ptrs[pipe][stage],
3962 		   &sh_css_isp_stage,
3963 		   sizeof(struct sh_css_isp_stage));
3964 	return xmem_isp_stage_ptrs[pipe][stage];
3965 }
3966 
ref_sh_css_ddr_address_map(struct sh_css_ddr_address_map * map,struct sh_css_ddr_address_map * out)3967 static int ref_sh_css_ddr_address_map(
3968     struct sh_css_ddr_address_map *map,
3969     struct sh_css_ddr_address_map *out)
3970 {
3971 	int err = 0;
3972 	unsigned int i;
3973 
3974 	/* we will use a union to copy things; overlaying an array
3975 	   with the struct; that way adding fields in the struct
3976 	   will keep things working, and we will not get type errors.
3977 	*/
3978 	union {
3979 		struct sh_css_ddr_address_map *map;
3980 		ia_css_ptr *addrs;
3981 	} in_addrs, to_addrs;
3982 
3983 	IA_CSS_ENTER_PRIVATE("void");
3984 	assert(map);
3985 	assert(out);
3986 
3987 	in_addrs.map = map;
3988 	to_addrs.map = out;
3989 
3990 	assert(sizeof(struct sh_css_ddr_address_map_size) / sizeof(size_t) ==
3991 	       sizeof(struct sh_css_ddr_address_map) / sizeof(ia_css_ptr));
3992 
3993 	/* copy map using size info */
3994 	for (i = 0; i < (sizeof(struct sh_css_ddr_address_map_size) /
3995 			 sizeof(size_t)); i++) {
3996 		if (in_addrs.addrs[i] == mmgr_NULL)
3997 			to_addrs.addrs[i] = mmgr_NULL;
3998 		else
3999 			to_addrs.addrs[i] = ia_css_refcount_increment(IA_CSS_REFCOUNT_PARAM_BUFFER,
4000 					    in_addrs.addrs[i]);
4001 	}
4002 
4003 	IA_CSS_LEAVE_ERR_PRIVATE(err);
4004 	return err;
4005 }
4006 
write_ia_css_isp_parameter_set_info_to_ddr(struct ia_css_isp_parameter_set_info * me,ia_css_ptr * out)4007 static int write_ia_css_isp_parameter_set_info_to_ddr(
4008     struct ia_css_isp_parameter_set_info *me,
4009     ia_css_ptr *out)
4010 {
4011 	int err = 0;
4012 	bool succ;
4013 
4014 	IA_CSS_ENTER_PRIVATE("void");
4015 
4016 	assert(me);
4017 	assert(out);
4018 
4019 	*out = ia_css_refcount_increment(IA_CSS_REFCOUNT_PARAM_SET_POOL,
4020 					 hmm_alloc(sizeof(struct ia_css_isp_parameter_set_info), HMM_BO_PRIVATE, 0, NULL, 0));
4021 	succ = (*out != mmgr_NULL);
4022 	if (succ)
4023 		hmm_store(*out,
4024 			   me, sizeof(struct ia_css_isp_parameter_set_info));
4025 	else
4026 		err = -ENOMEM;
4027 
4028 	IA_CSS_LEAVE_ERR_PRIVATE(err);
4029 	return err;
4030 }
4031 
4032 static int
free_ia_css_isp_parameter_set_info(ia_css_ptr ptr)4033 free_ia_css_isp_parameter_set_info(
4034     ia_css_ptr ptr) {
4035 	int err = 0;
4036 	struct ia_css_isp_parameter_set_info isp_params_info;
4037 	unsigned int i;
4038 	ia_css_ptr *addrs = (ia_css_ptr *)&isp_params_info.mem_map;
4039 
4040 	IA_CSS_ENTER_PRIVATE("ptr = %u", ptr);
4041 
4042 	/* sanity check - ptr must be valid */
4043 	if (!ia_css_refcount_is_valid(ptr))
4044 	{
4045 		IA_CSS_ERROR("%s: IA_CSS_REFCOUNT_PARAM_SET_POOL(0x%x) invalid arg", __func__,
4046 			     ptr);
4047 		err = -EINVAL;
4048 		IA_CSS_LEAVE_ERR_PRIVATE(err);
4049 		return err;
4050 	}
4051 
4052 	hmm_load(ptr, &isp_params_info.mem_map, sizeof(struct sh_css_ddr_address_map));
4053 	/* copy map using size info */
4054 	for (i = 0; i < (sizeof(struct sh_css_ddr_address_map_size) /
4055 			 sizeof(size_t)); i++)
4056 	{
4057 		if (addrs[i] == mmgr_NULL)
4058 			continue;
4059 
4060 		/* sanity check - ptr must be valid */
4061 		if (!ia_css_refcount_is_valid(addrs[i])) {
4062 			IA_CSS_ERROR("%s: IA_CSS_REFCOUNT_PARAM_BUFFER(0x%x) invalid arg", __func__,
4063 				     ptr);
4064 			err = -EINVAL;
4065 			continue;
4066 		}
4067 
4068 		ia_css_refcount_decrement(IA_CSS_REFCOUNT_PARAM_BUFFER, addrs[i]);
4069 	}
4070 	ia_css_refcount_decrement(IA_CSS_REFCOUNT_PARAM_SET_POOL, ptr);
4071 
4072 	IA_CSS_LEAVE_ERR_PRIVATE(err);
4073 	return err;
4074 }
4075 
4076 /* Mark all parameters as changed to force recomputing the derived ISP parameters */
4077 void
sh_css_invalidate_params(struct ia_css_stream * stream)4078 sh_css_invalidate_params(struct ia_css_stream *stream)
4079 {
4080 	struct	ia_css_isp_parameters *params;
4081 	unsigned int i, j, mem;
4082 
4083 	IA_CSS_ENTER_PRIVATE("void");
4084 	assert(stream);
4085 
4086 	params = stream->isp_params_configs;
4087 	params->isp_params_changed = true;
4088 	for (i = 0; i < IA_CSS_PIPE_ID_NUM; i++) {
4089 		for (j = 0; j < SH_CSS_MAX_STAGES; j++) {
4090 			for (mem = 0; mem < N_IA_CSS_MEMORIES; mem++) {
4091 				params->isp_mem_params_changed[i][j][mem] = true;
4092 			}
4093 		}
4094 	}
4095 
4096 	memset(&params->config_changed[0], 1, sizeof(params->config_changed));
4097 	params->dis_coef_table_changed = true;
4098 	params->dvs2_coef_table_changed = true;
4099 	params->morph_table_changed = true;
4100 	params->sc_table_changed = true;
4101 	params->dz_config_changed = true;
4102 	params->motion_config_changed = true;
4103 
4104 	/*Free up theDVS table memory blocks before recomputing new table  */
4105 	for (i = 0; i < IA_CSS_PIPE_ID_NUM; i++) {
4106 		if (params->pipe_dvs_6axis_config[i]) {
4107 			free_dvs_6axis_table(&params->pipe_dvs_6axis_config[i]);
4108 			params->pipe_dvs_6axis_config_changed[i] = true;
4109 		}
4110 	}
4111 
4112 	IA_CSS_LEAVE_PRIVATE("void");
4113 }
4114 
4115 void
sh_css_update_uds_and_crop_info(const struct ia_css_binary_info * info,const struct ia_css_frame_info * in_frame_info,const struct ia_css_frame_info * out_frame_info,const struct ia_css_resolution * dvs_env,const struct ia_css_dz_config * zoom,const struct ia_css_vector * motion_vector,struct sh_css_uds_info * uds,struct sh_css_crop_pos * sp_out_crop_pos,bool enable_zoom)4116 sh_css_update_uds_and_crop_info(
4117     const struct ia_css_binary_info *info,
4118     const struct ia_css_frame_info *in_frame_info,
4119     const struct ia_css_frame_info *out_frame_info,
4120     const struct ia_css_resolution *dvs_env,
4121     const struct ia_css_dz_config *zoom,
4122     const struct ia_css_vector *motion_vector,
4123     struct sh_css_uds_info *uds,		/* out */
4124     struct sh_css_crop_pos *sp_out_crop_pos,	/* out */
4125 
4126     bool enable_zoom)
4127 {
4128 	IA_CSS_ENTER_PRIVATE("void");
4129 
4130 	assert(info);
4131 	assert(in_frame_info);
4132 	assert(out_frame_info);
4133 	assert(dvs_env);
4134 	assert(zoom);
4135 	assert(motion_vector);
4136 	assert(uds);
4137 	assert(sp_out_crop_pos);
4138 
4139 	uds->curr_dx   = enable_zoom ? (uint16_t)zoom->dx : HRT_GDC_N;
4140 	uds->curr_dy   = enable_zoom ? (uint16_t)zoom->dy : HRT_GDC_N;
4141 
4142 	if (info->enable.dvs_envelope) {
4143 		unsigned int crop_x = 0,
4144 			     crop_y = 0,
4145 			     uds_xc = 0,
4146 			     uds_yc = 0,
4147 			     env_width, env_height;
4148 		int half_env_x, half_env_y;
4149 		int motion_x = motion_vector->x;
4150 		int motion_y = motion_vector->y;
4151 		bool upscale_x = in_frame_info->res.width < out_frame_info->res.width;
4152 		bool upscale_y = in_frame_info->res.height < out_frame_info->res.height;
4153 
4154 		if (info->enable.uds && !info->enable.ds) {
4155 			/**
4156 			 * we calculate with the envelope that we can actually
4157 			 * use, the min dvs envelope is for the filter
4158 			 * initialization.
4159 			 */
4160 			env_width  = dvs_env->width -
4161 				     SH_CSS_MIN_DVS_ENVELOPE;
4162 			env_height = dvs_env->height -
4163 				     SH_CSS_MIN_DVS_ENVELOPE;
4164 			half_env_x = env_width / 2;
4165 			half_env_y = env_height / 2;
4166 			/**
4167 			 * for digital zoom, we use the dvs envelope and make
4168 			 * sure that we don't include the 8 leftmost pixels or
4169 			 * 8 topmost rows.
4170 			 */
4171 			if (upscale_x) {
4172 				uds_xc = (in_frame_info->res.width
4173 					  + env_width
4174 					  + SH_CSS_MIN_DVS_ENVELOPE) / 2;
4175 			} else {
4176 				uds_xc = (out_frame_info->res.width
4177 					  + env_width) / 2
4178 					 + SH_CSS_MIN_DVS_ENVELOPE;
4179 			}
4180 			if (upscale_y) {
4181 				uds_yc = (in_frame_info->res.height
4182 					  + env_height
4183 					  + SH_CSS_MIN_DVS_ENVELOPE) / 2;
4184 			} else {
4185 				uds_yc = (out_frame_info->res.height
4186 					  + env_height) / 2
4187 					 + SH_CSS_MIN_DVS_ENVELOPE;
4188 			}
4189 			/* clip the motion vector to +/- half the envelope */
4190 			motion_x = clamp(motion_x, -half_env_x, half_env_x);
4191 			motion_y = clamp(motion_y, -half_env_y, half_env_y);
4192 			uds_xc += motion_x;
4193 			uds_yc += motion_y;
4194 			/* uds can be pipelined, remove top lines */
4195 			crop_y = 2;
4196 		} else if (info->enable.ds) {
4197 			env_width  = dvs_env->width;
4198 			env_height = dvs_env->height;
4199 			half_env_x = env_width / 2;
4200 			half_env_y = env_height / 2;
4201 			/* clip the motion vector to +/- half the envelope */
4202 			motion_x = clamp(motion_x, -half_env_x, half_env_x);
4203 			motion_y = clamp(motion_y, -half_env_y, half_env_y);
4204 			/* for video with downscaling, the envelope is included
4205 			    in the input resolution. */
4206 			uds_xc = in_frame_info->res.width / 2 + motion_x;
4207 			uds_yc = in_frame_info->res.height / 2 + motion_y;
4208 			crop_x = info->pipeline.left_cropping;
4209 			/* ds == 2 (yuv_ds) can be pipelined, remove top
4210 			   lines */
4211 			if (info->enable.ds & 1)
4212 				crop_y = info->pipeline.top_cropping;
4213 			else
4214 				crop_y = 2;
4215 		} else {
4216 			/* video nodz: here we can only crop. We make sure we
4217 			   crop at least the first 8x8 pixels away. */
4218 			env_width  = dvs_env->width -
4219 				     SH_CSS_MIN_DVS_ENVELOPE;
4220 			env_height = dvs_env->height -
4221 				     SH_CSS_MIN_DVS_ENVELOPE;
4222 			half_env_x = env_width / 2;
4223 			half_env_y = env_height / 2;
4224 			motion_x = clamp(motion_x, -half_env_x, half_env_x);
4225 			motion_y = clamp(motion_y, -half_env_y, half_env_y);
4226 			crop_x = SH_CSS_MIN_DVS_ENVELOPE
4227 				 + half_env_x + motion_x;
4228 			crop_y = SH_CSS_MIN_DVS_ENVELOPE
4229 				 + half_env_y + motion_y;
4230 		}
4231 
4232 		/* Must enforce that the crop position is even */
4233 		crop_x = EVEN_FLOOR(crop_x);
4234 		crop_y = EVEN_FLOOR(crop_y);
4235 		uds_xc = EVEN_FLOOR(uds_xc);
4236 		uds_yc = EVEN_FLOOR(uds_yc);
4237 
4238 		uds->xc = (uint16_t)uds_xc;
4239 		uds->yc = (uint16_t)uds_yc;
4240 		sp_out_crop_pos->x = (uint16_t)crop_x;
4241 		sp_out_crop_pos->y = (uint16_t)crop_y;
4242 	} else {
4243 		/* for down scaling, we always use the center of the image */
4244 		uds->xc = (uint16_t)in_frame_info->res.width / 2;
4245 		uds->yc = (uint16_t)in_frame_info->res.height / 2;
4246 		sp_out_crop_pos->x = (uint16_t)info->pipeline.left_cropping;
4247 		sp_out_crop_pos->y = (uint16_t)info->pipeline.top_cropping;
4248 	}
4249 	IA_CSS_LEAVE_PRIVATE("void");
4250 }
4251 
4252 static int
sh_css_update_uds_and_crop_info_based_on_zoom_region(const struct ia_css_binary_info * info,const struct ia_css_frame_info * in_frame_info,const struct ia_css_frame_info * out_frame_info,const struct ia_css_resolution * dvs_env,const struct ia_css_dz_config * zoom,const struct ia_css_vector * motion_vector,struct sh_css_uds_info * uds,struct sh_css_crop_pos * sp_out_crop_pos,struct ia_css_resolution pipe_in_res,bool enable_zoom)4253 sh_css_update_uds_and_crop_info_based_on_zoom_region(
4254     const struct ia_css_binary_info *info,
4255     const struct ia_css_frame_info *in_frame_info,
4256     const struct ia_css_frame_info *out_frame_info,
4257     const struct ia_css_resolution *dvs_env,
4258     const struct ia_css_dz_config *zoom,
4259     const struct ia_css_vector *motion_vector,
4260     struct sh_css_uds_info *uds,		/* out */
4261     struct sh_css_crop_pos *sp_out_crop_pos,	/* out */
4262     struct ia_css_resolution pipe_in_res,
4263     bool enable_zoom) {
4264 	unsigned int x0 = 0, y0 = 0, x1 = 0, y1 = 0;
4265 	int err = 0;
4266 	/* Note:
4267 	* Filter_Envelope = 0 for NND/LUT
4268 	* Filter_Envelope = 1 for BCI
4269 	* Filter_Envelope = 3 for BLI
4270 	* Currently, not considering this filter envelope because, In uds.sp.c is recalculating
4271 	* the dx/dy based on filter envelope and other information (ia_css_uds_sp_scale_params)
4272 	* Ideally, That should be done on host side not on sp side.
4273 	*/
4274 	unsigned int filter_envelope = 0;
4275 
4276 	IA_CSS_ENTER_PRIVATE("void");
4277 
4278 	assert(info);
4279 	assert(in_frame_info);
4280 	assert(out_frame_info);
4281 	assert(dvs_env);
4282 	assert(zoom);
4283 	assert(motion_vector);
4284 	assert(uds);
4285 	assert(sp_out_crop_pos);
4286 	x0 = zoom->zoom_region.origin.x;
4287 	y0 = zoom->zoom_region.origin.y;
4288 	x1 = zoom->zoom_region.resolution.width + x0;
4289 	y1 = zoom->zoom_region.resolution.height + y0;
4290 
4291 	if ((x0 > x1) || (y0 > y1) || (x1 > pipe_in_res.width) || (y1 > pipe_in_res.height))
4292 		return -EINVAL;
4293 
4294 	if (!enable_zoom)
4295 	{
4296 		uds->curr_dx = HRT_GDC_N;
4297 		uds->curr_dy = HRT_GDC_N;
4298 	}
4299 
4300 	if (info->enable.dvs_envelope)
4301 	{
4302 		/* Zoom region is only supported by the UDS module on ISP
4303 		 * 2 and higher. It is not supported in video mode on ISP 1 */
4304 		return -EINVAL;
4305 	} else
4306 	{
4307 		if (enable_zoom) {
4308 			/* A. Calculate dx/dy based on crop region using in_frame_info
4309 			* Scale the crop region if in_frame_info to the stage is not same as
4310 			* actual effective input of the pipeline
4311 			*/
4312 			if (in_frame_info->res.width != pipe_in_res.width ||
4313 			    in_frame_info->res.height != pipe_in_res.height) {
4314 				x0 = (x0 * in_frame_info->res.width) / (pipe_in_res.width);
4315 				y0 = (y0 * in_frame_info->res.height) / (pipe_in_res.height);
4316 				x1 = (x1 * in_frame_info->res.width) / (pipe_in_res.width);
4317 				y1 = (y1 * in_frame_info->res.height) / (pipe_in_res.height);
4318 			}
4319 			uds->curr_dx =
4320 			    ((x1 - x0 - filter_envelope) * HRT_GDC_N) / in_frame_info->res.width;
4321 			uds->curr_dy =
4322 			    ((y1 - y0 - filter_envelope) * HRT_GDC_N) / in_frame_info->res.height;
4323 
4324 			/* B. Calculate xc/yc based on crop region */
4325 			uds->xc = (uint16_t)x0 + (((x1) - (x0)) / 2);
4326 			uds->yc = (uint16_t)y0 + (((y1) - (y0)) / 2);
4327 		} else {
4328 			uds->xc = (uint16_t)in_frame_info->res.width / 2;
4329 			uds->yc = (uint16_t)in_frame_info->res.height / 2;
4330 		}
4331 
4332 		ia_css_debug_dtrace(IA_CSS_DEBUG_TRACE,
4333 				    "uds->curr_dx=%d, uds->xc=%d, uds->yc=%d\n",
4334 				    uds->curr_dx, uds->xc, uds->yc);
4335 		ia_css_debug_dtrace(IA_CSS_DEBUG_TRACE, "x0=%d, y0=%d, x1=%d, y1=%d\n",
4336 				    x0, y0, x1, y1);
4337 		sp_out_crop_pos->x = (uint16_t)info->pipeline.left_cropping;
4338 		sp_out_crop_pos->y = (uint16_t)info->pipeline.top_cropping;
4339 	}
4340 	IA_CSS_LEAVE_PRIVATE("void");
4341 	return err;
4342 }
4343 
4344 struct ia_css_3a_statistics *
ia_css_3a_statistics_allocate(const struct ia_css_3a_grid_info * grid)4345 ia_css_3a_statistics_allocate(const struct ia_css_3a_grid_info *grid)
4346 {
4347 	struct ia_css_3a_statistics *me;
4348 	int grid_size;
4349 
4350 	IA_CSS_ENTER("grid=%p", grid);
4351 
4352 	assert(grid);
4353 
4354 	me = kvcalloc(1, sizeof(*me), GFP_KERNEL);
4355 	if (!me)
4356 		goto err;
4357 
4358 	me->grid = *grid;
4359 	grid_size = grid->width * grid->height;
4360 	me->data = kvmalloc(grid_size * sizeof(*me->data), GFP_KERNEL);
4361 	if (!me->data)
4362 		goto err;
4363 #if !defined(HAS_NO_HMEM)
4364 	/* No weighted histogram, no structure, treat the histogram data as a byte dump in a byte array */
4365 	me->rgby_data = kvmalloc(sizeof_hmem(HMEM0_ID), GFP_KERNEL);
4366 #else
4367 	me->rgby_data = NULL;
4368 #endif
4369 
4370 	IA_CSS_LEAVE("return=%p", me);
4371 	return me;
4372 err:
4373 	ia_css_3a_statistics_free(me);
4374 
4375 	IA_CSS_LEAVE("return=%p", NULL);
4376 	return NULL;
4377 }
4378 
4379 void
ia_css_3a_statistics_free(struct ia_css_3a_statistics * me)4380 ia_css_3a_statistics_free(struct ia_css_3a_statistics *me)
4381 {
4382 	if (me) {
4383 		kvfree(me->rgby_data);
4384 		kvfree(me->data);
4385 		kvfree(me);
4386 	}
4387 }
4388 
4389 struct ia_css_dvs_statistics *
ia_css_dvs_statistics_allocate(const struct ia_css_dvs_grid_info * grid)4390 ia_css_dvs_statistics_allocate(const struct ia_css_dvs_grid_info *grid)
4391 {
4392 	struct ia_css_dvs_statistics *me;
4393 
4394 	assert(grid);
4395 
4396 	me = kvcalloc(1, sizeof(*me), GFP_KERNEL);
4397 	if (!me)
4398 		goto err;
4399 
4400 	me->grid = *grid;
4401 	me->hor_proj = kvmalloc(grid->height * IA_CSS_DVS_NUM_COEF_TYPES *
4402 				sizeof(*me->hor_proj), GFP_KERNEL);
4403 	if (!me->hor_proj)
4404 		goto err;
4405 
4406 	me->ver_proj = kvmalloc(grid->width * IA_CSS_DVS_NUM_COEF_TYPES *
4407 				sizeof(*me->ver_proj), GFP_KERNEL);
4408 	if (!me->ver_proj)
4409 		goto err;
4410 
4411 	return me;
4412 err:
4413 	ia_css_dvs_statistics_free(me);
4414 	return NULL;
4415 }
4416 
4417 void
ia_css_dvs_statistics_free(struct ia_css_dvs_statistics * me)4418 ia_css_dvs_statistics_free(struct ia_css_dvs_statistics *me)
4419 {
4420 	if (me) {
4421 		kvfree(me->hor_proj);
4422 		kvfree(me->ver_proj);
4423 		kvfree(me);
4424 	}
4425 }
4426 
4427 struct ia_css_dvs_coefficients *
ia_css_dvs_coefficients_allocate(const struct ia_css_dvs_grid_info * grid)4428 ia_css_dvs_coefficients_allocate(const struct ia_css_dvs_grid_info *grid)
4429 {
4430 	struct ia_css_dvs_coefficients *me;
4431 
4432 	assert(grid);
4433 
4434 	me = kvcalloc(1, sizeof(*me), GFP_KERNEL);
4435 	if (!me)
4436 		goto err;
4437 
4438 	me->grid = *grid;
4439 
4440 	me->hor_coefs = kvmalloc(grid->num_hor_coefs *
4441 				 IA_CSS_DVS_NUM_COEF_TYPES *
4442 				 sizeof(*me->hor_coefs), GFP_KERNEL);
4443 	if (!me->hor_coefs)
4444 		goto err;
4445 
4446 	me->ver_coefs = kvmalloc(grid->num_ver_coefs *
4447 				 IA_CSS_DVS_NUM_COEF_TYPES *
4448 				 sizeof(*me->ver_coefs), GFP_KERNEL);
4449 	if (!me->ver_coefs)
4450 		goto err;
4451 
4452 	return me;
4453 err:
4454 	ia_css_dvs_coefficients_free(me);
4455 	return NULL;
4456 }
4457 
4458 void
ia_css_dvs_coefficients_free(struct ia_css_dvs_coefficients * me)4459 ia_css_dvs_coefficients_free(struct ia_css_dvs_coefficients *me)
4460 {
4461 	if (me) {
4462 		kvfree(me->hor_coefs);
4463 		kvfree(me->ver_coefs);
4464 		kvfree(me);
4465 	}
4466 }
4467 
4468 struct ia_css_dvs2_statistics *
ia_css_dvs2_statistics_allocate(const struct ia_css_dvs_grid_info * grid)4469 ia_css_dvs2_statistics_allocate(const struct ia_css_dvs_grid_info *grid)
4470 {
4471 	struct ia_css_dvs2_statistics *me;
4472 
4473 	assert(grid);
4474 
4475 	me = kvcalloc(1, sizeof(*me), GFP_KERNEL);
4476 	if (!me)
4477 		goto err;
4478 
4479 	me->grid = *grid;
4480 
4481 	me->hor_prod.odd_real = kvmalloc(grid->aligned_width *
4482 					 grid->aligned_height *
4483 					 sizeof(*me->hor_prod.odd_real),
4484 					 GFP_KERNEL);
4485 	if (!me->hor_prod.odd_real)
4486 		goto err;
4487 
4488 	me->hor_prod.odd_imag = kvmalloc(grid->aligned_width *
4489 					 grid->aligned_height *
4490 					 sizeof(*me->hor_prod.odd_imag),
4491 					 GFP_KERNEL);
4492 	if (!me->hor_prod.odd_imag)
4493 		goto err;
4494 
4495 	me->hor_prod.even_real = kvmalloc(grid->aligned_width *
4496 					  grid->aligned_height *
4497 					  sizeof(*me->hor_prod.even_real),
4498 					  GFP_KERNEL);
4499 	if (!me->hor_prod.even_real)
4500 		goto err;
4501 
4502 	me->hor_prod.even_imag = kvmalloc(grid->aligned_width *
4503 					  grid->aligned_height *
4504 					  sizeof(*me->hor_prod.even_imag),
4505 					  GFP_KERNEL);
4506 	if (!me->hor_prod.even_imag)
4507 		goto err;
4508 
4509 	me->ver_prod.odd_real = kvmalloc(grid->aligned_width *
4510 					 grid->aligned_height *
4511 					 sizeof(*me->ver_prod.odd_real),
4512 					 GFP_KERNEL);
4513 	if (!me->ver_prod.odd_real)
4514 		goto err;
4515 
4516 	me->ver_prod.odd_imag = kvmalloc(grid->aligned_width *
4517 					 grid->aligned_height *
4518 					 sizeof(*me->ver_prod.odd_imag),
4519 					 GFP_KERNEL);
4520 	if (!me->ver_prod.odd_imag)
4521 		goto err;
4522 
4523 	me->ver_prod.even_real = kvmalloc(grid->aligned_width *
4524 					  grid->aligned_height *
4525 					  sizeof(*me->ver_prod.even_real),
4526 					  GFP_KERNEL);
4527 	if (!me->ver_prod.even_real)
4528 		goto err;
4529 
4530 	me->ver_prod.even_imag = kvmalloc(grid->aligned_width *
4531 					  grid->aligned_height *
4532 					  sizeof(*me->ver_prod.even_imag),
4533 					  GFP_KERNEL);
4534 	if (!me->ver_prod.even_imag)
4535 		goto err;
4536 
4537 	return me;
4538 err:
4539 	ia_css_dvs2_statistics_free(me);
4540 	return NULL;
4541 }
4542 
4543 void
ia_css_dvs2_statistics_free(struct ia_css_dvs2_statistics * me)4544 ia_css_dvs2_statistics_free(struct ia_css_dvs2_statistics *me)
4545 {
4546 	if (me) {
4547 		kvfree(me->hor_prod.odd_real);
4548 		kvfree(me->hor_prod.odd_imag);
4549 		kvfree(me->hor_prod.even_real);
4550 		kvfree(me->hor_prod.even_imag);
4551 		kvfree(me->ver_prod.odd_real);
4552 		kvfree(me->ver_prod.odd_imag);
4553 		kvfree(me->ver_prod.even_real);
4554 		kvfree(me->ver_prod.even_imag);
4555 		kvfree(me);
4556 	}
4557 }
4558 
4559 struct ia_css_dvs2_coefficients *
ia_css_dvs2_coefficients_allocate(const struct ia_css_dvs_grid_info * grid)4560 ia_css_dvs2_coefficients_allocate(const struct ia_css_dvs_grid_info *grid)
4561 {
4562 	struct ia_css_dvs2_coefficients *me;
4563 
4564 	assert(grid);
4565 
4566 	me = kvcalloc(1, sizeof(*me), GFP_KERNEL);
4567 	if (!me)
4568 		goto err;
4569 
4570 	me->grid = *grid;
4571 
4572 	me->hor_coefs.odd_real = kvmalloc(grid->num_hor_coefs *
4573 					  sizeof(*me->hor_coefs.odd_real),
4574 					  GFP_KERNEL);
4575 	if (!me->hor_coefs.odd_real)
4576 		goto err;
4577 
4578 	me->hor_coefs.odd_imag = kvmalloc(grid->num_hor_coefs *
4579 					  sizeof(*me->hor_coefs.odd_imag),
4580 					  GFP_KERNEL);
4581 	if (!me->hor_coefs.odd_imag)
4582 		goto err;
4583 
4584 	me->hor_coefs.even_real = kvmalloc(grid->num_hor_coefs *
4585 					   sizeof(*me->hor_coefs.even_real),
4586 					   GFP_KERNEL);
4587 	if (!me->hor_coefs.even_real)
4588 		goto err;
4589 
4590 	me->hor_coefs.even_imag = kvmalloc(grid->num_hor_coefs *
4591 					   sizeof(*me->hor_coefs.even_imag),
4592 					   GFP_KERNEL);
4593 	if (!me->hor_coefs.even_imag)
4594 		goto err;
4595 
4596 	me->ver_coefs.odd_real = kvmalloc(grid->num_ver_coefs *
4597 					  sizeof(*me->ver_coefs.odd_real),
4598 					  GFP_KERNEL);
4599 	if (!me->ver_coefs.odd_real)
4600 		goto err;
4601 
4602 	me->ver_coefs.odd_imag = kvmalloc(grid->num_ver_coefs *
4603 					  sizeof(*me->ver_coefs.odd_imag),
4604 					  GFP_KERNEL);
4605 	if (!me->ver_coefs.odd_imag)
4606 		goto err;
4607 
4608 	me->ver_coefs.even_real = kvmalloc(grid->num_ver_coefs *
4609 					   sizeof(*me->ver_coefs.even_real),
4610 					   GFP_KERNEL);
4611 	if (!me->ver_coefs.even_real)
4612 		goto err;
4613 
4614 	me->ver_coefs.even_imag = kvmalloc(grid->num_ver_coefs *
4615 					   sizeof(*me->ver_coefs.even_imag),
4616 					   GFP_KERNEL);
4617 	if (!me->ver_coefs.even_imag)
4618 		goto err;
4619 
4620 	return me;
4621 err:
4622 	ia_css_dvs2_coefficients_free(me);
4623 	return NULL;
4624 }
4625 
4626 void
ia_css_dvs2_coefficients_free(struct ia_css_dvs2_coefficients * me)4627 ia_css_dvs2_coefficients_free(struct ia_css_dvs2_coefficients *me)
4628 {
4629 	if (me) {
4630 		kvfree(me->hor_coefs.odd_real);
4631 		kvfree(me->hor_coefs.odd_imag);
4632 		kvfree(me->hor_coefs.even_real);
4633 		kvfree(me->hor_coefs.even_imag);
4634 		kvfree(me->ver_coefs.odd_real);
4635 		kvfree(me->ver_coefs.odd_imag);
4636 		kvfree(me->ver_coefs.even_real);
4637 		kvfree(me->ver_coefs.even_imag);
4638 		kvfree(me);
4639 	}
4640 }
4641 
4642 struct ia_css_dvs_6axis_config *
ia_css_dvs2_6axis_config_allocate(const struct ia_css_stream * stream)4643 ia_css_dvs2_6axis_config_allocate(const struct ia_css_stream *stream)
4644 {
4645 	struct ia_css_dvs_6axis_config *dvs_config = NULL;
4646 	struct ia_css_isp_parameters *params = NULL;
4647 	unsigned int width_y;
4648 	unsigned int height_y;
4649 	unsigned int width_uv;
4650 	unsigned int height_uv;
4651 
4652 	assert(stream);
4653 	params = stream->isp_params_configs;
4654 
4655 	/* Backward compatibility by default consider pipe as Video*/
4656 	if (!params || (params &&
4657 			!params->pipe_dvs_6axis_config[IA_CSS_PIPE_ID_VIDEO])) {
4658 		goto err;
4659 	}
4660 
4661 	dvs_config = kvcalloc(1, sizeof(struct ia_css_dvs_6axis_config),
4662 			      GFP_KERNEL);
4663 	if (!dvs_config)
4664 		goto err;
4665 
4666 	dvs_config->width_y = width_y =
4667 				  params->pipe_dvs_6axis_config[IA_CSS_PIPE_ID_VIDEO]->width_y;
4668 	dvs_config->height_y = height_y =
4669 				   params->pipe_dvs_6axis_config[IA_CSS_PIPE_ID_VIDEO]->height_y;
4670 	dvs_config->width_uv = width_uv =
4671 				   params->pipe_dvs_6axis_config[IA_CSS_PIPE_ID_VIDEO]->width_uv;
4672 	dvs_config->height_uv = height_uv =
4673 				    params->pipe_dvs_6axis_config[IA_CSS_PIPE_ID_VIDEO]->height_uv;
4674 	IA_CSS_LOG("table Y: W %d H %d", width_y, height_y);
4675 	IA_CSS_LOG("table UV: W %d H %d", width_uv, height_uv);
4676 	dvs_config->xcoords_y = kvmalloc(width_y * height_y * sizeof(uint32_t),
4677 					 GFP_KERNEL);
4678 	if (!dvs_config->xcoords_y)
4679 		goto err;
4680 
4681 	dvs_config->ycoords_y = kvmalloc(width_y * height_y * sizeof(uint32_t),
4682 					 GFP_KERNEL);
4683 	if (!dvs_config->ycoords_y)
4684 		goto err;
4685 
4686 	dvs_config->xcoords_uv = kvmalloc(width_uv * height_uv *
4687 					  sizeof(uint32_t),
4688 					  GFP_KERNEL);
4689 	if (!dvs_config->xcoords_uv)
4690 		goto err;
4691 
4692 	dvs_config->ycoords_uv = kvmalloc(width_uv * height_uv *
4693 					  sizeof(uint32_t),
4694 					  GFP_KERNEL);
4695 	if (!dvs_config->ycoords_uv)
4696 		goto err;
4697 
4698 	return dvs_config;
4699 err:
4700 	ia_css_dvs2_6axis_config_free(dvs_config);
4701 	return NULL;
4702 }
4703 
4704 void
ia_css_dvs2_6axis_config_free(struct ia_css_dvs_6axis_config * dvs_6axis_config)4705 ia_css_dvs2_6axis_config_free(struct ia_css_dvs_6axis_config *dvs_6axis_config)
4706 {
4707 	if (dvs_6axis_config) {
4708 		kvfree(dvs_6axis_config->xcoords_y);
4709 		kvfree(dvs_6axis_config->ycoords_y);
4710 		kvfree(dvs_6axis_config->xcoords_uv);
4711 		kvfree(dvs_6axis_config->ycoords_uv);
4712 		kvfree(dvs_6axis_config);
4713 	}
4714 }
4715 
4716 void
ia_css_en_dz_capt_pipe(struct ia_css_stream * stream,bool enable)4717 ia_css_en_dz_capt_pipe(struct ia_css_stream *stream, bool enable)
4718 {
4719 	struct ia_css_pipe *pipe;
4720 	struct ia_css_pipeline *pipeline;
4721 	struct ia_css_pipeline_stage *stage;
4722 	enum ia_css_pipe_id pipe_id;
4723 	int err;
4724 	int i;
4725 
4726 	if (!stream)
4727 		return;
4728 
4729 	for (i = 0; i < stream->num_pipes; i++) {
4730 		pipe = stream->pipes[i];
4731 		pipeline = ia_css_pipe_get_pipeline(pipe);
4732 		pipe_id = pipeline->pipe_id;
4733 
4734 		if (pipe_id == IA_CSS_PIPE_ID_CAPTURE) {
4735 			err = ia_css_pipeline_get_stage(pipeline, IA_CSS_BINARY_MODE_CAPTURE_PP,
4736 							&stage);
4737 			if (!err)
4738 				stage->enable_zoom = enable;
4739 			break;
4740 		}
4741 	}
4742 }
4743