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