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