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