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