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