• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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(array3_size(height, width, sizeof(short)),
966 						   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;
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 
2477 	if (!succ)
2478 		return -ENOMEM;
2479 
2480 	return 0;
2481 }
2482 
2483 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)2484 sh_css_init_isp_params_from_global(struct ia_css_stream *stream,
2485 				   struct ia_css_isp_parameters *params,
2486 				   bool use_default_config,
2487 				   struct ia_css_pipe *pipe_in)
2488 {
2489 	bool retval = true;
2490 	int i = 0;
2491 	bool is_dp_10bpp = true;
2492 	unsigned int isp_pipe_version = ia_css_pipe_get_isp_pipe_version(
2493 					    stream->pipes[0]);
2494 	struct ia_css_isp_parameters *stream_params = stream->isp_params_configs;
2495 
2496 	if (!use_default_config && !stream_params) {
2497 		retval = false;
2498 		goto exit;
2499 	}
2500 
2501 	params->output_frame = NULL;
2502 	params->isp_parameters_id = 0;
2503 
2504 	if (use_default_config) {
2505 		ia_css_set_xnr3_config(params, &default_xnr3_config);
2506 
2507 		sh_css_set_nr_config(params, &default_nr_config);
2508 		sh_css_set_ee_config(params, &default_ee_config);
2509 		if (isp_pipe_version == SH_CSS_ISP_PIPE_VERSION_1)
2510 			sh_css_set_macc_table(params, &default_macc_table);
2511 		else if (isp_pipe_version == SH_CSS_ISP_PIPE_VERSION_2_2)
2512 			sh_css_set_macc_table(params, &default_macc2_table);
2513 		sh_css_set_gamma_table(params, &default_gamma_table);
2514 		sh_css_set_ctc_table(params, &default_ctc_table);
2515 		sh_css_set_baa_config(params, &default_baa_config);
2516 		sh_css_set_dz_config(params, &default_dz_config);
2517 		/* ------ deprecated(bz675) : from ------ */
2518 		sh_css_set_shading_settings(params, &default_shading_settings);
2519 		/* ------ deprecated(bz675) : to ------ */
2520 
2521 		ia_css_set_s3a_config(params, &default_3a_config);
2522 		ia_css_set_wb_config(params, &default_wb_config);
2523 		ia_css_set_csc_config(params, &default_cc_config);
2524 		ia_css_set_tnr_config(params, &default_tnr_config);
2525 		ia_css_set_ob_config(params, &default_ob_config);
2526 		ia_css_set_dp_config(params, &default_dp_config);
2527 
2528 		if (!IS_ISP2401) {
2529 			ia_css_set_param_exceptions(pipe_in, params);
2530 		} else {
2531 			for (i = 0; i < stream->num_pipes; i++) {
2532 				if (sh_css_select_dp_10bpp_config(stream->pipes[i],
2533 								&is_dp_10bpp) == 0) {
2534 					/* set the return value as false if both DPC and
2535 					* BDS is enabled by the user. But we do not return
2536 					* the value immediately to enable internal firmware
2537 					* feature testing. */
2538 					if (is_dp_10bpp) {
2539 						sh_css_set_dp_config(stream->pipes[i], params, &default_dp_10bpp_config);
2540 					} else {
2541 						sh_css_set_dp_config(stream->pipes[i], params, &default_dp_config);
2542 					}
2543 				} else {
2544 					retval = false;
2545 					goto exit;
2546 				}
2547 
2548 				ia_css_set_param_exceptions(stream->pipes[i], params);
2549 			}
2550 		}
2551 
2552 		ia_css_set_de_config(params, &default_de_config);
2553 		ia_css_set_gc_config(params, &default_gc_config);
2554 		ia_css_set_anr_config(params, &default_anr_config);
2555 		ia_css_set_anr2_config(params, &default_anr_thres);
2556 		ia_css_set_ce_config(params, &default_ce_config);
2557 		ia_css_set_xnr_table_config(params, &default_xnr_table);
2558 		ia_css_set_ecd_config(params, &default_ecd_config);
2559 		ia_css_set_ynr_config(params, &default_ynr_config);
2560 		ia_css_set_fc_config(params, &default_fc_config);
2561 		ia_css_set_cnr_config(params, &default_cnr_config);
2562 		ia_css_set_macc_config(params, &default_macc_config);
2563 		ia_css_set_ctc_config(params, &default_ctc_config);
2564 		ia_css_set_aa_config(params, &default_aa_config);
2565 		ia_css_set_r_gamma_config(params, &default_r_gamma_table);
2566 		ia_css_set_g_gamma_config(params, &default_g_gamma_table);
2567 		ia_css_set_b_gamma_config(params, &default_b_gamma_table);
2568 		ia_css_set_yuv2rgb_config(params, &default_yuv2rgb_cc_config);
2569 		ia_css_set_rgb2yuv_config(params, &default_rgb2yuv_cc_config);
2570 		ia_css_set_xnr_config(params, &default_xnr_config);
2571 		ia_css_set_sdis_config(params, &default_sdis_config);
2572 		ia_css_set_sdis2_config(params, &default_sdis2_config);
2573 		ia_css_set_formats_config(params, &default_formats_config);
2574 
2575 		params->fpn_config.data = NULL;
2576 		params->config_changed[IA_CSS_FPN_ID] = true;
2577 		params->fpn_config.enabled = 0;
2578 
2579 		params->motion_config = default_motion_config;
2580 		params->motion_config_changed = true;
2581 
2582 		params->morph_table = NULL;
2583 		params->morph_table_changed = true;
2584 
2585 		params->sc_table = NULL;
2586 		params->sc_table_changed = true;
2587 		params->sc_table_dirty = false;
2588 		params->sc_table_last_pipe_num = 0;
2589 
2590 		ia_css_sdis2_clear_coefficients(&params->dvs2_coefs);
2591 		params->dvs2_coef_table_changed = true;
2592 
2593 		ia_css_sdis_clear_coefficients(&params->dvs_coefs);
2594 		params->dis_coef_table_changed = true;
2595 	} else {
2596 		ia_css_set_xnr3_config(params, &stream_params->xnr3_config);
2597 
2598 		sh_css_set_nr_config(params, &stream_params->nr_config);
2599 		sh_css_set_ee_config(params, &stream_params->ee_config);
2600 		if (isp_pipe_version == SH_CSS_ISP_PIPE_VERSION_1)
2601 			sh_css_set_macc_table(params, &stream_params->macc_table);
2602 		else if (isp_pipe_version == SH_CSS_ISP_PIPE_VERSION_2_2)
2603 			sh_css_set_macc_table(params, &stream_params->macc_table);
2604 		sh_css_set_gamma_table(params, &stream_params->gc_table);
2605 		sh_css_set_ctc_table(params, &stream_params->ctc_table);
2606 		sh_css_set_baa_config(params, &stream_params->bds_config);
2607 		sh_css_set_dz_config(params, &stream_params->dz_config);
2608 		/* ------ deprecated(bz675) : from ------ */
2609 		sh_css_set_shading_settings(params, &stream_params->shading_settings);
2610 		/* ------ deprecated(bz675) : to ------ */
2611 
2612 		ia_css_set_s3a_config(params, &stream_params->s3a_config);
2613 		ia_css_set_wb_config(params, &stream_params->wb_config);
2614 		ia_css_set_csc_config(params, &stream_params->cc_config);
2615 		ia_css_set_tnr_config(params, &stream_params->tnr_config);
2616 		ia_css_set_ob_config(params, &stream_params->ob_config);
2617 		ia_css_set_dp_config(params, &stream_params->dp_config);
2618 		ia_css_set_de_config(params, &stream_params->de_config);
2619 		ia_css_set_gc_config(params, &stream_params->gc_config);
2620 		ia_css_set_anr_config(params, &stream_params->anr_config);
2621 		ia_css_set_anr2_config(params, &stream_params->anr_thres);
2622 		ia_css_set_ce_config(params, &stream_params->ce_config);
2623 		ia_css_set_xnr_table_config(params, &stream_params->xnr_table);
2624 		ia_css_set_ecd_config(params, &stream_params->ecd_config);
2625 		ia_css_set_ynr_config(params, &stream_params->ynr_config);
2626 		ia_css_set_fc_config(params, &stream_params->fc_config);
2627 		ia_css_set_cnr_config(params, &stream_params->cnr_config);
2628 		ia_css_set_macc_config(params, &stream_params->macc_config);
2629 		ia_css_set_ctc_config(params, &stream_params->ctc_config);
2630 		ia_css_set_aa_config(params, &stream_params->aa_config);
2631 		ia_css_set_r_gamma_config(params, &stream_params->r_gamma_table);
2632 		ia_css_set_g_gamma_config(params, &stream_params->g_gamma_table);
2633 		ia_css_set_b_gamma_config(params, &stream_params->b_gamma_table);
2634 		ia_css_set_yuv2rgb_config(params, &stream_params->yuv2rgb_cc_config);
2635 		ia_css_set_rgb2yuv_config(params, &stream_params->rgb2yuv_cc_config);
2636 		ia_css_set_xnr_config(params, &stream_params->xnr_config);
2637 		ia_css_set_formats_config(params, &stream_params->formats_config);
2638 
2639 		for (i = 0; i < stream->num_pipes; i++) {
2640 			if (0 ==
2641 			    sh_css_select_dp_10bpp_config(stream->pipes[i], &is_dp_10bpp)) {
2642 				/* set the return value as false if both DPC and
2643 				 * BDS is enabled by the user. But we do not return
2644 				 * the value immediately to enable internal firmware
2645 				 * feature testing. */
2646 
2647 				if (is_dp_10bpp) {
2648 					retval = false;
2649 					/* FIXME: should it ignore this error? */
2650 				}
2651 			} else {
2652 				retval = false;
2653 				goto exit;
2654 			}
2655 			if (IS_ISP2401) {
2656 				if (stream->pipes[i]->mode < IA_CSS_PIPE_ID_NUM) {
2657 					sh_css_set_dp_config(stream->pipes[i], params,
2658 							    &stream_params->pipe_dp_config[stream->pipes[i]->mode]);
2659 					ia_css_set_param_exceptions(stream->pipes[i], params);
2660 				} else {
2661 					retval = false;
2662 					goto exit;
2663 				}
2664 			}
2665 		}
2666 
2667 		if (!IS_ISP2401)
2668 			ia_css_set_param_exceptions(pipe_in, params);
2669 
2670 		params->fpn_config.data = stream_params->fpn_config.data;
2671 		params->config_changed[IA_CSS_FPN_ID] =
2672 		    stream_params->config_changed[IA_CSS_FPN_ID];
2673 		params->fpn_config.enabled = stream_params->fpn_config.enabled;
2674 
2675 		sh_css_set_motion_vector(params, &stream_params->motion_config);
2676 		sh_css_set_morph_table(params, stream_params->morph_table);
2677 
2678 		if (stream_params->sc_table) {
2679 			sh_css_update_shading_table_status(pipe_in, params);
2680 			sh_css_set_shading_table(stream, params, stream_params->sc_table);
2681 		} else {
2682 			params->sc_table = NULL;
2683 			params->sc_table_changed = true;
2684 			params->sc_table_dirty = false;
2685 			params->sc_table_last_pipe_num = 0;
2686 		}
2687 
2688 		/* Only IA_CSS_PIPE_ID_VIDEO & IA_CSS_PIPE_ID_CAPTURE will support dvs_6axis_config*/
2689 		for (i = 0; i < IA_CSS_PIPE_ID_NUM; i++) {
2690 			if (stream_params->pipe_dvs_6axis_config[i]) {
2691 				if (params->pipe_dvs_6axis_config[i]) {
2692 					copy_dvs_6axis_table(params->pipe_dvs_6axis_config[i],
2693 							     stream_params->pipe_dvs_6axis_config[i]);
2694 				} else {
2695 					params->pipe_dvs_6axis_config[i] =
2696 					    generate_dvs_6axis_table_from_config(stream_params->pipe_dvs_6axis_config[i]);
2697 				}
2698 			}
2699 		}
2700 		ia_css_set_sdis_config(params, &stream_params->dvs_coefs);
2701 		params->dis_coef_table_changed = stream_params->dis_coef_table_changed;
2702 
2703 		ia_css_set_sdis2_config(params, &stream_params->dvs2_coefs);
2704 		params->dvs2_coef_table_changed = stream_params->dvs2_coef_table_changed;
2705 		params->sensor_binning = stream_params->sensor_binning;
2706 	}
2707 
2708 exit:
2709 	return retval;
2710 }
2711 
2712 int
sh_css_params_init(void)2713 sh_css_params_init(void)
2714 {
2715 	int i, p;
2716 
2717 	IA_CSS_ENTER_PRIVATE("void");
2718 
2719 	/* TMP: tracking of paramsets */
2720 	g_param_buffer_dequeue_count = 0;
2721 	g_param_buffer_enqueue_count = 0;
2722 
2723 	for (p = 0; p < IA_CSS_PIPE_ID_NUM; p++) {
2724 		for (i = 0; i < SH_CSS_MAX_STAGES; i++) {
2725 			xmem_sp_stage_ptrs[p][i] =
2726 			ia_css_refcount_increment(-1,
2727 						  hmm_alloc(sizeof(struct sh_css_sp_stage),
2728 							    HMM_BO_PRIVATE, 0, NULL,
2729 							    ATOMISP_MAP_FLAG_CLEARED));
2730 			xmem_isp_stage_ptrs[p][i] =
2731 			ia_css_refcount_increment(-1,
2732 						  hmm_alloc(sizeof(struct sh_css_sp_stage),
2733 							    HMM_BO_PRIVATE, 0, NULL,
2734 							    ATOMISP_MAP_FLAG_CLEARED));
2735 
2736 			if ((xmem_sp_stage_ptrs[p][i] == mmgr_NULL) ||
2737 			    (xmem_isp_stage_ptrs[p][i] == mmgr_NULL)) {
2738 				sh_css_params_uninit();
2739 				IA_CSS_LEAVE_ERR_PRIVATE(-ENOMEM);
2740 				return -ENOMEM;
2741 			}
2742 		}
2743 	}
2744 
2745 	ia_css_config_gamma_table();
2746 	ia_css_config_ctc_table();
2747 	ia_css_config_rgb_gamma_tables();
2748 	ia_css_config_xnr_table();
2749 
2750 	sp_ddr_ptrs = ia_css_refcount_increment(-1,
2751 						hmm_alloc(CEIL_MUL(sizeof(struct sh_css_ddr_address_map),
2752 								   HIVE_ISP_DDR_WORD_BYTES),
2753 							  HMM_BO_PRIVATE, 0, NULL,
2754 							  ATOMISP_MAP_FLAG_CLEARED));
2755 	xmem_sp_group_ptrs = ia_css_refcount_increment(-1,
2756 						       hmm_alloc(sizeof(struct sh_css_sp_group),
2757 								 HMM_BO_PRIVATE, 0, NULL,
2758 								 ATOMISP_MAP_FLAG_CLEARED));
2759 
2760 	if ((sp_ddr_ptrs == mmgr_NULL) ||
2761 	    (xmem_sp_group_ptrs == mmgr_NULL)) {
2762 		ia_css_uninit();
2763 		IA_CSS_LEAVE_ERR_PRIVATE(-ENOMEM);
2764 		return -ENOMEM;
2765 	}
2766 	IA_CSS_LEAVE_ERR_PRIVATE(0);
2767 	return 0;
2768 }
2769 
host_lut_store(const void * lut)2770 static void host_lut_store(const void *lut)
2771 {
2772 	unsigned int i;
2773 
2774 	for (i = 0; i < N_GDC_ID; i++)
2775 		gdc_lut_store((gdc_ID_t)i, (const int (*)[HRT_GDC_N]) lut);
2776 }
2777 
2778 /* Note that allocation is in ipu address space. */
sh_css_params_alloc_gdc_lut(void)2779 inline ia_css_ptr sh_css_params_alloc_gdc_lut(void)
2780 {
2781 	return hmm_alloc(sizeof(zoom_table), HMM_BO_PRIVATE, 0, NULL, 0);
2782 }
2783 
sh_css_params_free_gdc_lut(ia_css_ptr addr)2784 inline void sh_css_params_free_gdc_lut(ia_css_ptr addr)
2785 {
2786 	if (addr != mmgr_NULL)
2787 		hmm_free(addr);
2788 }
2789 
ia_css_pipe_set_bci_scaler_lut(struct ia_css_pipe * pipe,const void * lut)2790 int ia_css_pipe_set_bci_scaler_lut(struct ia_css_pipe *pipe,
2791 	const void *lut)
2792 {
2793 	int err = 0;
2794 	bool stream_started = false;
2795 
2796 	IA_CSS_ENTER("pipe=%p lut=%p", pipe, lut);
2797 
2798 	if (!lut || !pipe) {
2799 		err = -EINVAL;
2800 		IA_CSS_LEAVE("err=%d", err);
2801 		return err;
2802 	}
2803 
2804 	/* If the pipe belongs to a stream and the stream has started, it is not
2805 	 * safe to store lut to gdc HW. If pipe->stream is NULL, then no stream is
2806 	 * created with this pipe, so it is safe to do this operation as long as
2807 	 * ia_css_init() has been called. */
2808 	if (pipe->stream && pipe->stream->started) {
2809 		ia_css_debug_dtrace(IA_CSS_DEBUG_ERROR,
2810 				    "unable to set scaler lut since stream has started\n");
2811 		stream_started = true;
2812 		err = -ENOTSUPP;
2813 	}
2814 
2815 	/* Free any existing tables. */
2816 	sh_css_params_free_gdc_lut(pipe->scaler_pp_lut);
2817 	pipe->scaler_pp_lut = mmgr_NULL;
2818 
2819 	if (!stream_started) {
2820 		if (!IS_ISP2401)
2821 			pipe->scaler_pp_lut = hmm_alloc(sizeof(zoom_table), HMM_BO_PRIVATE, 0, NULL, 0);
2822 		else
2823 			pipe->scaler_pp_lut = sh_css_params_alloc_gdc_lut();
2824 
2825 		if (pipe->scaler_pp_lut == mmgr_NULL) {
2826 			ia_css_debug_dtrace(IA_CSS_DEBUG_ERROR,
2827 					    "unable to allocate scaler_pp_lut\n");
2828 			err = -ENOMEM;
2829 		} else {
2830 			gdc_lut_convert_to_isp_format((const int(*)[HRT_GDC_N])lut,
2831 						      interleaved_lut_temp);
2832 			hmm_store(pipe->scaler_pp_lut,
2833 				   (int *)interleaved_lut_temp,
2834 				   sizeof(zoom_table));
2835 		}
2836 	}
2837 
2838 	IA_CSS_LEAVE("lut(%u) err=%d", pipe->scaler_pp_lut, err);
2839 	return err;
2840 }
2841 
2842 /* if pipe is NULL, returns default lut addr. */
sh_css_pipe_get_pp_gdc_lut(const struct ia_css_pipe * pipe)2843 ia_css_ptr sh_css_pipe_get_pp_gdc_lut(const struct ia_css_pipe *pipe)
2844 {
2845 	assert(pipe);
2846 
2847 	if (pipe->scaler_pp_lut != mmgr_NULL)
2848 		return pipe->scaler_pp_lut;
2849 	else
2850 		return sh_css_params_get_default_gdc_lut();
2851 }
2852 
sh_css_params_map_and_store_default_gdc_lut(void)2853 int sh_css_params_map_and_store_default_gdc_lut(void)
2854 {
2855 	int err = 0;
2856 
2857 	IA_CSS_ENTER_PRIVATE("void");
2858 
2859 	/* Is table already mapped? Nothing to do if it is mapped. */
2860 	if (default_gdc_lut != mmgr_NULL)
2861 		return err;
2862 
2863 	host_lut_store((void *)zoom_table);
2864 
2865 	if (!IS_ISP2401)
2866 		default_gdc_lut = hmm_alloc(sizeof(zoom_table), HMM_BO_PRIVATE, 0, NULL, 0);
2867 	else
2868 		default_gdc_lut = sh_css_params_alloc_gdc_lut();
2869 
2870 	if (default_gdc_lut == mmgr_NULL)
2871 		return -ENOMEM;
2872 
2873 	gdc_lut_convert_to_isp_format((const int(*)[HRT_GDC_N])zoom_table,
2874 				      interleaved_lut_temp);
2875 	hmm_store(default_gdc_lut, (int *)interleaved_lut_temp,
2876 		   sizeof(zoom_table));
2877 
2878 	IA_CSS_LEAVE_PRIVATE("lut(%u) err=%d", default_gdc_lut, err);
2879 	return err;
2880 }
2881 
sh_css_params_free_default_gdc_lut(void)2882 void sh_css_params_free_default_gdc_lut(void)
2883 {
2884 	IA_CSS_ENTER_PRIVATE("void");
2885 
2886 	sh_css_params_free_gdc_lut(default_gdc_lut);
2887 	default_gdc_lut = mmgr_NULL;
2888 
2889 	IA_CSS_LEAVE_PRIVATE("void");
2890 }
2891 
sh_css_params_get_default_gdc_lut(void)2892 ia_css_ptr sh_css_params_get_default_gdc_lut(void)
2893 {
2894 	return default_gdc_lut;
2895 }
2896 
free_param_set_callback(ia_css_ptr ptr)2897 static void free_param_set_callback(
2898     ia_css_ptr ptr)
2899 {
2900 	IA_CSS_ENTER_PRIVATE("void");
2901 
2902 	free_ia_css_isp_parameter_set_info(ptr);
2903 
2904 	IA_CSS_LEAVE_PRIVATE("void");
2905 }
2906 
free_buffer_callback(ia_css_ptr ptr)2907 static void free_buffer_callback(
2908     ia_css_ptr ptr)
2909 {
2910 	IA_CSS_ENTER_PRIVATE("void");
2911 
2912 	hmm_free(ptr);
2913 
2914 	IA_CSS_LEAVE_PRIVATE("void");
2915 }
2916 
2917 void
sh_css_param_clear_param_sets(void)2918 sh_css_param_clear_param_sets(void)
2919 {
2920 	IA_CSS_ENTER_PRIVATE("void");
2921 
2922 	ia_css_refcount_clear(IA_CSS_REFCOUNT_PARAM_SET_POOL, &free_param_set_callback);
2923 
2924 	IA_CSS_LEAVE_PRIVATE("void");
2925 }
2926 
2927 /*
2928  * MW: we can define hmm_free() to return a NULL
2929  * then you can write ptr = hmm_free(ptr);
2930  */
2931 #define safe_free(id, x)      \
2932 	do {                  \
2933 		ia_css_refcount_decrement(id, x);     \
2934 		(x) = mmgr_NULL;  \
2935 	} while (0)
2936 
free_map(struct sh_css_ddr_address_map * map)2937 static void free_map(struct sh_css_ddr_address_map *map)
2938 {
2939 	unsigned int i;
2940 
2941 	ia_css_ptr *addrs = (ia_css_ptr *)map;
2942 
2943 	IA_CSS_ENTER_PRIVATE("void");
2944 
2945 	/* free buffers */
2946 	for (i = 0; i < (sizeof(struct sh_css_ddr_address_map_size) /
2947 			 sizeof(size_t)); i++) {
2948 		if (addrs[i] == mmgr_NULL)
2949 			continue;
2950 		safe_free(IA_CSS_REFCOUNT_PARAM_BUFFER, addrs[i]);
2951 	}
2952 
2953 	IA_CSS_LEAVE_PRIVATE("void");
2954 }
2955 
2956 void
ia_css_stream_isp_parameters_uninit(struct ia_css_stream * stream)2957 ia_css_stream_isp_parameters_uninit(struct ia_css_stream *stream)
2958 {
2959 	int i;
2960 	struct ia_css_isp_parameters *params = stream->isp_params_configs;
2961 	struct ia_css_isp_parameters *per_frame_params =
2962 		    stream->per_frame_isp_params_configs;
2963 
2964 	IA_CSS_ENTER_PRIVATE("void");
2965 	if (!params) {
2966 		IA_CSS_LEAVE_PRIVATE("isp_param_configs is NULL");
2967 		return;
2968 	}
2969 
2970 	/* free existing ddr_ptr maps */
2971 	for (i = 0; i < IA_CSS_PIPE_ID_NUM; i++) {
2972 		free_map(&params->pipe_ddr_ptrs[i]);
2973 		if (per_frame_params)
2974 			free_map(&per_frame_params->pipe_ddr_ptrs[i]);
2975 		/* Free up theDVS table memory blocks before recomputing new table */
2976 		if (params->pipe_dvs_6axis_config[i])
2977 			free_dvs_6axis_table(&params->pipe_dvs_6axis_config[i]);
2978 		if (per_frame_params && per_frame_params->pipe_dvs_6axis_config[i])
2979 			free_dvs_6axis_table(&per_frame_params->pipe_dvs_6axis_config[i]);
2980 	}
2981 	free_map(&params->ddr_ptrs);
2982 	if (per_frame_params)
2983 		free_map(&per_frame_params->ddr_ptrs);
2984 
2985 	if (params->fpn_config.data) {
2986 		kvfree(params->fpn_config.data);
2987 		params->fpn_config.data = NULL;
2988 	}
2989 
2990 	/* Free up sc_config (temporal shading table) if it is allocated. */
2991 	if (params->sc_config) {
2992 		ia_css_shading_table_free(params->sc_config);
2993 		params->sc_config = NULL;
2994 	}
2995 	if (per_frame_params) {
2996 		if (per_frame_params->sc_config) {
2997 			ia_css_shading_table_free(per_frame_params->sc_config);
2998 			per_frame_params->sc_config = NULL;
2999 		}
3000 	}
3001 
3002 	kvfree(params);
3003 	kvfree(per_frame_params);
3004 	stream->isp_params_configs = NULL;
3005 	stream->per_frame_isp_params_configs = NULL;
3006 
3007 	IA_CSS_LEAVE_PRIVATE("void");
3008 }
3009 
3010 void
sh_css_params_uninit(void)3011 sh_css_params_uninit(void)
3012 {
3013 	unsigned int p, i;
3014 
3015 	IA_CSS_ENTER_PRIVATE("void");
3016 
3017 	ia_css_refcount_decrement(-1, sp_ddr_ptrs);
3018 	sp_ddr_ptrs = mmgr_NULL;
3019 	ia_css_refcount_decrement(-1, xmem_sp_group_ptrs);
3020 	xmem_sp_group_ptrs = mmgr_NULL;
3021 
3022 	for (p = 0; p < IA_CSS_PIPE_ID_NUM; p++)
3023 		for (i = 0; i < SH_CSS_MAX_STAGES; i++) {
3024 			ia_css_refcount_decrement(-1, xmem_sp_stage_ptrs[p][i]);
3025 			xmem_sp_stage_ptrs[p][i] = mmgr_NULL;
3026 			ia_css_refcount_decrement(-1, xmem_isp_stage_ptrs[p][i]);
3027 			xmem_isp_stage_ptrs[p][i] = mmgr_NULL;
3028 		}
3029 
3030 	/* go through the pools to clear references */
3031 	ia_css_refcount_clear(IA_CSS_REFCOUNT_PARAM_SET_POOL, &free_param_set_callback);
3032 	ia_css_refcount_clear(IA_CSS_REFCOUNT_PARAM_BUFFER, &free_buffer_callback);
3033 	ia_css_refcount_clear(-1, &free_buffer_callback);
3034 
3035 	IA_CSS_LEAVE_PRIVATE("void");
3036 }
3037 
3038 static struct ia_css_host_data *
convert_allocate_morph_plane(unsigned short * data,unsigned int width,unsigned int height,unsigned int aligned_width)3039 convert_allocate_morph_plane(
3040     unsigned short *data,
3041     unsigned int width,
3042     unsigned int height,
3043     unsigned int aligned_width)
3044 {
3045 	unsigned int i, j, padding, w;
3046 	struct ia_css_host_data *me;
3047 	unsigned int isp_data_size;
3048 	u16 *isp_data_ptr;
3049 
3050 	IA_CSS_ENTER_PRIVATE("void");
3051 
3052 	/* currently we don't have morph table interpolation yet,
3053 	 * so we allow a wider table to be used. This will be removed
3054 	 * in the future. */
3055 	if (width > aligned_width) {
3056 		padding = 0;
3057 		w = aligned_width;
3058 	} else {
3059 		padding = aligned_width - width;
3060 		w = width;
3061 	}
3062 	isp_data_size = height * (w + padding) * sizeof(uint16_t);
3063 
3064 	me = ia_css_host_data_allocate((size_t)isp_data_size);
3065 
3066 	if (!me) {
3067 		IA_CSS_LEAVE_ERR_PRIVATE(-ENOMEM);
3068 		return NULL;
3069 	}
3070 
3071 	isp_data_ptr = (uint16_t *)me->address;
3072 
3073 	memset(isp_data_ptr, 0, (size_t)isp_data_size);
3074 
3075 	for (i = 0; i < height; i++) {
3076 		for (j = 0; j < w; j++)
3077 			*isp_data_ptr++ = (uint16_t)data[j];
3078 		isp_data_ptr += padding;
3079 		data += width;
3080 	}
3081 
3082 	IA_CSS_LEAVE_PRIVATE("void");
3083 	return me;
3084 }
3085 
3086 static int
store_morph_plane(unsigned short * data,unsigned int width,unsigned int height,ia_css_ptr dest,unsigned int aligned_width)3087 store_morph_plane(
3088     unsigned short *data,
3089     unsigned int width,
3090     unsigned int height,
3091     ia_css_ptr dest,
3092     unsigned int aligned_width)
3093 {
3094 	struct ia_css_host_data *isp_data;
3095 
3096 	assert(dest != mmgr_NULL);
3097 
3098 	isp_data = convert_allocate_morph_plane(data, width, height, aligned_width);
3099 	if (!isp_data) {
3100 		IA_CSS_LEAVE_ERR_PRIVATE(-ENOMEM);
3101 		return -ENOMEM;
3102 	}
3103 	ia_css_params_store_ia_css_host_data(dest, isp_data);
3104 
3105 	ia_css_host_data_free(isp_data);
3106 	return 0;
3107 }
3108 
sh_css_update_isp_params_to_ddr(struct ia_css_isp_parameters * params,ia_css_ptr ddr_ptr)3109 static void sh_css_update_isp_params_to_ddr(
3110     struct ia_css_isp_parameters *params,
3111     ia_css_ptr ddr_ptr)
3112 {
3113 	size_t size = sizeof(params->uds);
3114 
3115 	IA_CSS_ENTER_PRIVATE("void");
3116 
3117 	assert(params);
3118 
3119 	hmm_store(ddr_ptr, &params->uds, size);
3120 	IA_CSS_LEAVE_PRIVATE("void");
3121 }
3122 
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)3123 static void sh_css_update_isp_mem_params_to_ddr(
3124     const struct ia_css_binary *binary,
3125     ia_css_ptr ddr_mem_ptr,
3126     size_t size,
3127     enum ia_css_isp_memories mem)
3128 {
3129 	const struct ia_css_host_data *params;
3130 
3131 	IA_CSS_ENTER_PRIVATE("void");
3132 
3133 	params = ia_css_isp_param_get_mem_init(&binary->mem_params,
3134 					       IA_CSS_PARAM_CLASS_PARAM, mem);
3135 	hmm_store(ddr_mem_ptr, params->address, size);
3136 
3137 	IA_CSS_LEAVE_PRIVATE("void");
3138 }
3139 
ia_css_dequeue_param_buffers(void)3140 void ia_css_dequeue_param_buffers(/*unsigned int pipe_num*/ void)
3141 {
3142 	unsigned int i;
3143 	ia_css_ptr cpy;
3144 	enum sh_css_queue_id param_queue_ids[3] = {	IA_CSS_PARAMETER_SET_QUEUE_ID,
3145 						    IA_CSS_PER_FRAME_PARAMETER_SET_QUEUE_ID,
3146 						    SH_CSS_INVALID_QUEUE_ID
3147 						  };
3148 
3149 	IA_CSS_ENTER_PRIVATE("void");
3150 
3151 	if (!sh_css_sp_is_running()) {
3152 		IA_CSS_LEAVE_PRIVATE("sp is not running");
3153 		/* SP is not running. The queues are not valid */
3154 		return;
3155 	}
3156 
3157 	for (i = 0; SH_CSS_INVALID_QUEUE_ID != param_queue_ids[i]; i++) {
3158 		cpy = (ia_css_ptr)0;
3159 		/* clean-up old copy */
3160 		while (ia_css_bufq_dequeue_buffer(param_queue_ids[i],
3161 						  (uint32_t *)&cpy) == 0) {
3162 			/* TMP: keep track of dequeued param set count
3163 			 */
3164 			g_param_buffer_dequeue_count++;
3165 			ia_css_bufq_enqueue_psys_event(
3166 			    IA_CSS_PSYS_SW_EVENT_BUFFER_DEQUEUED,
3167 			    0,
3168 			    param_queue_ids[i],
3169 			    0);
3170 
3171 			IA_CSS_LOG("dequeued param set %x from %d, release ref", cpy, 0);
3172 			free_ia_css_isp_parameter_set_info(cpy);
3173 			cpy = (ia_css_ptr)0;
3174 		}
3175 	}
3176 
3177 	IA_CSS_LEAVE_PRIVATE("void");
3178 }
3179 
3180 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)3181 process_kernel_parameters(unsigned int pipe_id,
3182 			  struct ia_css_pipeline_stage *stage,
3183 			  struct ia_css_isp_parameters *params,
3184 			  unsigned int isp_pipe_version,
3185 			  unsigned int raw_bit_depth)
3186 {
3187 	unsigned int param_id;
3188 
3189 	(void)isp_pipe_version;
3190 	(void)raw_bit_depth;
3191 
3192 	sh_css_enable_pipeline(stage->binary);
3193 
3194 	if (params->config_changed[IA_CSS_OB_ID]) {
3195 		ia_css_ob_configure(&params->stream_configs.ob,
3196 				    isp_pipe_version, raw_bit_depth);
3197 	}
3198 	if (params->config_changed[IA_CSS_S3A_ID]) {
3199 		ia_css_s3a_configure(raw_bit_depth);
3200 	}
3201 	/* Copy stage uds parameters to config, since they can differ per stage.
3202 	 */
3203 	params->crop_config.crop_pos = params->uds[stage->stage_num].crop_pos;
3204 	params->uds_config.crop_pos  = params->uds[stage->stage_num].crop_pos;
3205 	params->uds_config.uds       = params->uds[stage->stage_num].uds;
3206 	/* Call parameter process functions for all kernels */
3207 	/* Skip SC, since that is called on a temp sc table */
3208 	for (param_id = 0; param_id < IA_CSS_NUM_PARAMETER_IDS; param_id++) {
3209 		if (param_id == IA_CSS_SC_ID) continue;
3210 		if (params->config_changed[param_id])
3211 			ia_css_kernel_process_param[param_id](pipe_id, stage, params);
3212 	}
3213 }
3214 
3215 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)3216 sh_css_param_update_isp_params(struct ia_css_pipe *curr_pipe,
3217 			       struct ia_css_isp_parameters *params,
3218 			       bool commit,
3219 			       struct ia_css_pipe *pipe_in)
3220 {
3221 	int err = 0;
3222 	ia_css_ptr cpy;
3223 	int i;
3224 	unsigned int raw_bit_depth = 10;
3225 	unsigned int isp_pipe_version = SH_CSS_ISP_PIPE_VERSION_1;
3226 	bool acc_cluster_params_changed = false;
3227 	unsigned int thread_id, pipe_num;
3228 
3229 	(void)acc_cluster_params_changed;
3230 
3231 	assert(curr_pipe);
3232 
3233 	IA_CSS_ENTER_PRIVATE("pipe=%p, isp_parameters_id=%d", pipe_in, params->isp_parameters_id);
3234 	raw_bit_depth = ia_css_stream_input_format_bits_per_pixel(curr_pipe->stream);
3235 
3236 	/* now make the map available to the sp */
3237 	if (!commit) {
3238 		IA_CSS_LEAVE_ERR_PRIVATE(err);
3239 		return err;
3240 	}
3241 	/* enqueue a copies of the mem_map to
3242 	   the designated pipelines */
3243 	for (i = 0; i < curr_pipe->stream->num_pipes; i++) {
3244 		struct ia_css_pipe *pipe;
3245 		struct sh_css_ddr_address_map *cur_map;
3246 		struct sh_css_ddr_address_map_size *cur_map_size;
3247 		struct ia_css_isp_parameter_set_info isp_params_info;
3248 		struct ia_css_pipeline *pipeline;
3249 		struct ia_css_pipeline_stage *stage;
3250 
3251 		enum sh_css_queue_id queue_id;
3252 
3253 		pipe = curr_pipe->stream->pipes[i];
3254 		pipeline = ia_css_pipe_get_pipeline(pipe);
3255 		pipe_num = ia_css_pipe_get_pipe_num(pipe);
3256 		isp_pipe_version = ia_css_pipe_get_isp_pipe_version(pipe);
3257 		ia_css_pipeline_get_sp_thread_id(pipe_num, &thread_id);
3258 
3259 #if defined(SH_CSS_ENABLE_PER_FRAME_PARAMS)
3260 		ia_css_query_internal_queue_id(params->output_frame
3261 					       ? IA_CSS_BUFFER_TYPE_PER_FRAME_PARAMETER_SET
3262 					       : IA_CSS_BUFFER_TYPE_PARAMETER_SET,
3263 					       thread_id, &queue_id);
3264 #else
3265 		ia_css_query_internal_queue_id(IA_CSS_BUFFER_TYPE_PARAMETER_SET, thread_id,
3266 					       &queue_id);
3267 #endif
3268 		if (!sh_css_sp_is_running()) {
3269 			/* SP is not running. The queues are not valid */
3270 			err = -EBUSY;
3271 			break;
3272 		}
3273 		cur_map = &params->pipe_ddr_ptrs[pipeline->pipe_id];
3274 		cur_map_size = &params->pipe_ddr_ptrs_size[pipeline->pipe_id];
3275 
3276 		/* TODO: Normally, zoom and motion parameters shouldn't
3277 		 * be part of "isp_params" as it is resolution/pipe dependent
3278 		 * Therefore, move the zoom config elsewhere (e.g. shading
3279 		 * table can be taken as an example! @GC
3280 		 * */
3281 		{
3282 			/* we have to do this per pipeline because */
3283 			/* the processing is a.o. resolution dependent */
3284 			err = ia_css_process_zoom_and_motion(params,
3285 							     pipeline->stages);
3286 			if (err)
3287 				return err;
3288 		}
3289 		/* check if to actually update the parameters for this pipe */
3290 		/* When API change is implemented making good distinction between
3291 		* stream config and pipe config this skipping code can be moved out of the #ifdef */
3292 		if (pipe_in && (pipe != pipe_in)) {
3293 			IA_CSS_LOG("skipping pipe %p", pipe);
3294 			continue;
3295 		}
3296 
3297 		/* BZ 125915, should be moved till after "update other buff" */
3298 		/* update the other buffers to the pipe specific copies */
3299 		for (stage = pipeline->stages; stage; stage = stage->next) {
3300 			unsigned int mem;
3301 
3302 			if (!stage || !stage->binary)
3303 				continue;
3304 
3305 			process_kernel_parameters(pipeline->pipe_id,
3306 						  stage, params,
3307 						  isp_pipe_version, raw_bit_depth);
3308 
3309 			err = sh_css_params_write_to_ddr_internal(
3310 				  pipe,
3311 				  pipeline->pipe_id,
3312 				  params,
3313 				  stage,
3314 				  cur_map,
3315 				  cur_map_size);
3316 
3317 			if (err)
3318 				break;
3319 			for (mem = 0; mem < IA_CSS_NUM_MEMORIES; mem++) {
3320 				params->isp_mem_params_changed
3321 				[pipeline->pipe_id][stage->stage_num][mem] = false;
3322 			}
3323 		} /* for */
3324 		if (err)
3325 			break;
3326 		/* update isp_params to pipe specific copies */
3327 		if (params->isp_params_changed) {
3328 			reallocate_buffer(&cur_map->isp_param,
3329 					  &cur_map_size->isp_param,
3330 					  cur_map_size->isp_param,
3331 					  true,
3332 					  &err);
3333 			if (err)
3334 				break;
3335 			sh_css_update_isp_params_to_ddr(params, cur_map->isp_param);
3336 		}
3337 
3338 		/* last make referenced copy */
3339 		err = ref_sh_css_ddr_address_map(
3340 			  cur_map,
3341 			  &isp_params_info.mem_map);
3342 		if (err)
3343 			break;
3344 
3345 		/* Update Parameters ID */
3346 		isp_params_info.isp_parameters_id = params->isp_parameters_id;
3347 
3348 		/* Update output frame pointer */
3349 		isp_params_info.output_frame_ptr =
3350 		    (params->output_frame) ? params->output_frame->data : mmgr_NULL;
3351 
3352 		/* now write the copy to ddr */
3353 		err = write_ia_css_isp_parameter_set_info_to_ddr(&isp_params_info, &cpy);
3354 		if (err)
3355 			break;
3356 
3357 		/* enqueue the set to sp */
3358 		IA_CSS_LOG("queue param set %x to %d", cpy, thread_id);
3359 
3360 		err = ia_css_bufq_enqueue_buffer(thread_id, queue_id, (uint32_t)cpy);
3361 		if (err) {
3362 			free_ia_css_isp_parameter_set_info(cpy);
3363 #if defined(SH_CSS_ENABLE_PER_FRAME_PARAMS)
3364 			IA_CSS_LOG("pfp: FAILED to add config id %d for OF %d to q %d on thread %d",
3365 				   isp_params_info.isp_parameters_id,
3366 				   isp_params_info.output_frame_ptr,
3367 				   queue_id, thread_id);
3368 #endif
3369 			break;
3370 		} else {
3371 			/* TMP: check discrepancy between nr of enqueued
3372 			 * parameter sets and dequeued sets
3373 			 */
3374 			g_param_buffer_enqueue_count++;
3375 			assert(g_param_buffer_enqueue_count < g_param_buffer_dequeue_count + 50);
3376 			/*
3377 			 * Tell the SP which queues are not empty,
3378 			 * by sending the software event.
3379 			 */
3380 			if (!sh_css_sp_is_running()) {
3381 				/* SP is not running. The queues are not valid */
3382 				IA_CSS_LEAVE_ERR_PRIVATE(-EBUSY);
3383 				return -EBUSY;
3384 			}
3385 			ia_css_bufq_enqueue_psys_event(
3386 			    IA_CSS_PSYS_SW_EVENT_BUFFER_ENQUEUED,
3387 			    (uint8_t)thread_id,
3388 			    (uint8_t)queue_id,
3389 			    0);
3390 #if defined(SH_CSS_ENABLE_PER_FRAME_PARAMS)
3391 			IA_CSS_LOG("pfp: added config id %d for OF %d to q %d on thread %d",
3392 				   isp_params_info.isp_parameters_id,
3393 				   isp_params_info.output_frame_ptr,
3394 				   queue_id, thread_id);
3395 #endif
3396 		}
3397 		/* clean-up old copy */
3398 		ia_css_dequeue_param_buffers(/*pipe_num*/);
3399 		params->pipe_dvs_6axis_config_changed[pipeline->pipe_id] = false;
3400 	} /* end for each 'active' pipeline */
3401 	/* clear the changed flags after all params
3402 	for all pipelines have been updated */
3403 	params->isp_params_changed = false;
3404 	params->sc_table_changed = false;
3405 	params->dis_coef_table_changed = false;
3406 	params->dvs2_coef_table_changed = false;
3407 	params->morph_table_changed = false;
3408 	params->dz_config_changed = false;
3409 	params->motion_config_changed = false;
3410 	/* ------ deprecated(bz675) : from ------ */
3411 	params->shading_settings_changed = false;
3412 	/* ------ deprecated(bz675) : to ------ */
3413 
3414 	memset(&params->config_changed[0], 0, sizeof(params->config_changed));
3415 
3416 	IA_CSS_LEAVE_ERR_PRIVATE(err);
3417 	return err;
3418 }
3419 
3420 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)3421 sh_css_params_write_to_ddr_internal(
3422     struct ia_css_pipe *pipe,
3423     unsigned int pipe_id,
3424     struct ia_css_isp_parameters *params,
3425     const struct ia_css_pipeline_stage *stage,
3426     struct sh_css_ddr_address_map *ddr_map,
3427     struct sh_css_ddr_address_map_size *ddr_map_size)
3428 {
3429 	int err;
3430 	const struct ia_css_binary *binary;
3431 
3432 	unsigned int stage_num;
3433 	unsigned int mem;
3434 	bool buff_realloced;
3435 
3436 	/* struct is > 128 bytes so it should not be on stack (see checkpatch) */
3437 	static struct ia_css_macc_table converted_macc_table;
3438 
3439 	IA_CSS_ENTER_PRIVATE("void");
3440 	assert(params);
3441 	assert(ddr_map);
3442 	assert(ddr_map_size);
3443 	assert(stage);
3444 
3445 	binary = stage->binary;
3446 	assert(binary);
3447 
3448 	stage_num = stage->stage_num;
3449 
3450 	if (binary->info->sp.enable.fpnr) {
3451 		buff_realloced = reallocate_buffer(&ddr_map->fpn_tbl,
3452 						   &ddr_map_size->fpn_tbl,
3453 						   (size_t)(FPNTBL_BYTES(binary)),
3454 						   params->config_changed[IA_CSS_FPN_ID],
3455 						   &err);
3456 		if (err) {
3457 			IA_CSS_LEAVE_ERR_PRIVATE(err);
3458 			return err;
3459 		}
3460 		if (params->config_changed[IA_CSS_FPN_ID] || buff_realloced) {
3461 			if (params->fpn_config.enabled) {
3462 				err = store_fpntbl(params, ddr_map->fpn_tbl);
3463 				if (err) {
3464 					IA_CSS_LEAVE_ERR_PRIVATE(err);
3465 					return err;
3466 				}
3467 			}
3468 		}
3469 	}
3470 
3471 	if (binary->info->sp.enable.sc) {
3472 		u32 enable_conv;
3473 		size_t bytes;
3474 
3475 		if (!IS_ISP2401)
3476 			bytes = ISP2400_SCTBL_BYTES(binary);
3477 		else
3478 			bytes = ISP2401_SCTBL_BYTES(binary);
3479 
3480 		enable_conv = params->shading_settings.enable_shading_table_conversion;
3481 
3482 		buff_realloced = reallocate_buffer(&ddr_map->sc_tbl,
3483 						   &ddr_map_size->sc_tbl,
3484 						   bytes,
3485 						   params->sc_table_changed,
3486 						   &err);
3487 		if (err) {
3488 			IA_CSS_LEAVE_ERR_PRIVATE(err);
3489 			return err;
3490 		}
3491 
3492 		if (params->shading_settings_changed ||
3493 		    params->sc_table_changed || buff_realloced) {
3494 			if (enable_conv == 0) {
3495 				if (params->sc_table) {
3496 					/* store the shading table to ddr */
3497 					err = ia_css_params_store_sctbl(stage, ddr_map->sc_tbl, params->sc_table);
3498 					if (err) {
3499 						IA_CSS_LEAVE_ERR_PRIVATE(err);
3500 						return err;
3501 					}
3502 					/* set sc_config to isp */
3503 					params->sc_config = (struct ia_css_shading_table *)params->sc_table;
3504 					ia_css_kernel_process_param[IA_CSS_SC_ID](pipe_id, stage, params);
3505 					params->sc_config = NULL;
3506 				} else {
3507 					/* generate the identical shading table */
3508 					if (params->sc_config) {
3509 						ia_css_shading_table_free(params->sc_config);
3510 						params->sc_config = NULL;
3511 					}
3512 					sh_css_params_shading_id_table_generate(&params->sc_config,
3513 										binary->sctbl_width_per_color,
3514 										binary->sctbl_height);
3515 					if (!params->sc_config) {
3516 						IA_CSS_LEAVE_ERR_PRIVATE(-ENOMEM);
3517 						return -ENOMEM;
3518 					}
3519 
3520 					/* store the shading table to ddr */
3521 					err = ia_css_params_store_sctbl(stage, ddr_map->sc_tbl, params->sc_config);
3522 					if (err) {
3523 						IA_CSS_LEAVE_ERR_PRIVATE(err);
3524 						return err;
3525 					}
3526 
3527 					/* set sc_config to isp */
3528 					ia_css_kernel_process_param[IA_CSS_SC_ID](pipe_id, stage, params);
3529 
3530 					/* free the shading table */
3531 					ia_css_shading_table_free(params->sc_config);
3532 					params->sc_config = NULL;
3533 				}
3534 			} else { /* legacy */
3535 				/* ------ deprecated(bz675) : from ------ */
3536 				/* shading table is full resolution, reduce */
3537 				if (params->sc_config) {
3538 					ia_css_shading_table_free(params->sc_config);
3539 					params->sc_config = NULL;
3540 				}
3541 				prepare_shading_table(
3542 				    (const struct ia_css_shading_table *)params->sc_table,
3543 				    params->sensor_binning,
3544 				    &params->sc_config,
3545 				    binary, pipe->required_bds_factor);
3546 				if (!params->sc_config) {
3547 					IA_CSS_LEAVE_ERR_PRIVATE(-ENOMEM);
3548 					return -ENOMEM;
3549 				}
3550 
3551 				/* store the shading table to ddr */
3552 				err = ia_css_params_store_sctbl(stage, ddr_map->sc_tbl, params->sc_config);
3553 				if (err) {
3554 					IA_CSS_LEAVE_ERR_PRIVATE(err);
3555 					return err;
3556 				}
3557 
3558 				/* set sc_config to isp */
3559 				ia_css_kernel_process_param[IA_CSS_SC_ID](pipe_id, stage, params);
3560 
3561 				/* free the shading table */
3562 				ia_css_shading_table_free(params->sc_config);
3563 				params->sc_config = NULL;
3564 				/* ------ deprecated(bz675) : to ------ */
3565 			}
3566 		}
3567 	}
3568 
3569 	/* DPC configuration is made pipe specific to allow flexibility in positioning of the
3570 	 * DPC kernel. The code below sets the pipe specific configuration to
3571 	 * individual binaries. */
3572 	if (IS_ISP2401 &&
3573 	    params->pipe_dpc_config_changed[pipe_id] && binary->info->sp.enable.dpc) {
3574 		unsigned int size   =
3575 		    stage->binary->info->mem_offsets.offsets.param->dmem.dp.size;
3576 
3577 		unsigned int offset =
3578 		    stage->binary->info->mem_offsets.offsets.param->dmem.dp.offset;
3579 
3580 		if (size) {
3581 			ia_css_dp_encode((struct sh_css_isp_dp_params *)
3582 					 &binary->mem_params.params[IA_CSS_PARAM_CLASS_PARAM][IA_CSS_ISP_DMEM].address[offset],
3583 					 &params->pipe_dp_config[pipe_id], size);
3584 
3585 			params->isp_params_changed = true;
3586 			params->isp_mem_params_changed[pipe_id][stage->stage_num][IA_CSS_ISP_DMEM] =
3587 			    true;
3588 		}
3589 	}
3590 
3591 	if (params->config_changed[IA_CSS_MACC_ID] && binary->info->sp.enable.macc) {
3592 		unsigned int i, j, idx;
3593 		unsigned int idx_map[] = {
3594 			0, 1, 3, 2, 6, 7, 5, 4, 12, 13, 15, 14, 10, 11, 9, 8
3595 		};
3596 
3597 		for (i = 0; i < IA_CSS_MACC_NUM_AXES; i++) {
3598 			idx = 4 * idx_map[i];
3599 			j   = 4 * i;
3600 
3601 			if (binary->info->sp.pipeline.isp_pipe_version == SH_CSS_ISP_PIPE_VERSION_1) {
3602 				converted_macc_table.data[idx] =
3603 				    (int16_t)sDIGIT_FITTING(params->macc_table.data[j],
3604 							    13, SH_CSS_MACC_COEF_SHIFT);
3605 				converted_macc_table.data[idx + 1] =
3606 				    (int16_t)sDIGIT_FITTING(params->macc_table.data[j + 1],
3607 							    13, SH_CSS_MACC_COEF_SHIFT);
3608 				converted_macc_table.data[idx + 2] =
3609 				    (int16_t)sDIGIT_FITTING(params->macc_table.data[j + 2],
3610 							    13, SH_CSS_MACC_COEF_SHIFT);
3611 				converted_macc_table.data[idx + 3] =
3612 				    (int16_t)sDIGIT_FITTING(params->macc_table.data[j + 3],
3613 							    13, SH_CSS_MACC_COEF_SHIFT);
3614 			} else if (binary->info->sp.pipeline.isp_pipe_version ==
3615 				   SH_CSS_ISP_PIPE_VERSION_2_2) {
3616 				converted_macc_table.data[idx] =
3617 				    params->macc_table.data[j];
3618 				converted_macc_table.data[idx + 1] =
3619 				    params->macc_table.data[j + 1];
3620 				converted_macc_table.data[idx + 2] =
3621 				    params->macc_table.data[j + 2];
3622 				converted_macc_table.data[idx + 3] =
3623 				    params->macc_table.data[j + 3];
3624 			}
3625 		}
3626 		reallocate_buffer(&ddr_map->macc_tbl,
3627 				  &ddr_map_size->macc_tbl,
3628 				  ddr_map_size->macc_tbl,
3629 				  true,
3630 				  &err);
3631 		if (err) {
3632 			IA_CSS_LEAVE_ERR_PRIVATE(err);
3633 			return err;
3634 		}
3635 		hmm_store(ddr_map->macc_tbl,
3636 			   converted_macc_table.data,
3637 			   sizeof(converted_macc_table.data));
3638 	}
3639 
3640 	if (binary->info->sp.enable.dvs_6axis) {
3641 		/* because UV is packed into the Y plane, calc total
3642 		 * YYU size = /2 gives size of UV-only,
3643 		 * total YYU size = UV-only * 3.
3644 		 */
3645 		buff_realloced = reallocate_buffer(
3646 				     &ddr_map->dvs_6axis_params_y,
3647 				     &ddr_map_size->dvs_6axis_params_y,
3648 				     (size_t)((DVS_6AXIS_BYTES(binary) / 2) * 3),
3649 				     params->pipe_dvs_6axis_config_changed[pipe_id],
3650 				     &err);
3651 		if (err) {
3652 			IA_CSS_LEAVE_ERR_PRIVATE(err);
3653 			return err;
3654 		}
3655 
3656 		if (params->pipe_dvs_6axis_config_changed[pipe_id] || buff_realloced) {
3657 			const struct ia_css_frame_info *dvs_in_frame_info;
3658 
3659 			if (stage->args.delay_frames[0]) {
3660 				/*When delay frames are present(as in case of video),
3661 				they are used for dvs. Configure DVS using those params*/
3662 				dvs_in_frame_info = &stage->args.delay_frames[0]->info;
3663 			} else {
3664 				/*Otherwise, use input frame to configure DVS*/
3665 				dvs_in_frame_info = &stage->args.in_frame->info;
3666 			}
3667 
3668 			/* Generate default DVS unity table on start up*/
3669 			if (!params->pipe_dvs_6axis_config[pipe_id]) {
3670 				struct ia_css_resolution dvs_offset = {0};
3671 
3672 				if (!IS_ISP2401) {
3673 					dvs_offset.width = (PIX_SHIFT_FILTER_RUN_IN_X + binary->dvs_envelope.width) / 2;
3674 				} else {
3675 					if (binary->dvs_envelope.width || binary->dvs_envelope.height) {
3676 						dvs_offset.width  = (PIX_SHIFT_FILTER_RUN_IN_X + binary->dvs_envelope.width) / 2;
3677 					}
3678 				}
3679 				dvs_offset.height = (PIX_SHIFT_FILTER_RUN_IN_Y + binary->dvs_envelope.height) / 2;
3680 
3681 				params->pipe_dvs_6axis_config[pipe_id] =
3682 				    generate_dvs_6axis_table(&binary->out_frame_info[0].res, &dvs_offset);
3683 				if (!params->pipe_dvs_6axis_config[pipe_id]) {
3684 					IA_CSS_LEAVE_ERR_PRIVATE(-ENOMEM);
3685 					return -ENOMEM;
3686 				}
3687 				params->pipe_dvs_6axis_config_changed[pipe_id] = true;
3688 
3689 				store_dvs_6axis_config(params->pipe_dvs_6axis_config[pipe_id],
3690 						    binary,
3691 						    dvs_in_frame_info,
3692 						    ddr_map->dvs_6axis_params_y);
3693 				params->isp_params_changed = true;
3694 			}
3695 		}
3696 	}
3697 
3698 	if (binary->info->sp.enable.ca_gdc) {
3699 		unsigned int i;
3700 		ia_css_ptr *virt_addr_tetra_x[
3701 
3702 		IA_CSS_MORPH_TABLE_NUM_PLANES];
3703 		size_t *virt_size_tetra_x[
3704 
3705 		IA_CSS_MORPH_TABLE_NUM_PLANES];
3706 		ia_css_ptr *virt_addr_tetra_y[
3707 
3708 		IA_CSS_MORPH_TABLE_NUM_PLANES];
3709 		size_t *virt_size_tetra_y[
3710 
3711 		IA_CSS_MORPH_TABLE_NUM_PLANES];
3712 
3713 		virt_addr_tetra_x[0] = &ddr_map->tetra_r_x;
3714 		virt_addr_tetra_x[1] = &ddr_map->tetra_gr_x;
3715 		virt_addr_tetra_x[2] = &ddr_map->tetra_gb_x;
3716 		virt_addr_tetra_x[3] = &ddr_map->tetra_b_x;
3717 		virt_addr_tetra_x[4] = &ddr_map->tetra_ratb_x;
3718 		virt_addr_tetra_x[5] = &ddr_map->tetra_batr_x;
3719 
3720 		virt_size_tetra_x[0] = &ddr_map_size->tetra_r_x;
3721 		virt_size_tetra_x[1] = &ddr_map_size->tetra_gr_x;
3722 		virt_size_tetra_x[2] = &ddr_map_size->tetra_gb_x;
3723 		virt_size_tetra_x[3] = &ddr_map_size->tetra_b_x;
3724 		virt_size_tetra_x[4] = &ddr_map_size->tetra_ratb_x;
3725 		virt_size_tetra_x[5] = &ddr_map_size->tetra_batr_x;
3726 
3727 		virt_addr_tetra_y[0] = &ddr_map->tetra_r_y;
3728 		virt_addr_tetra_y[1] = &ddr_map->tetra_gr_y;
3729 		virt_addr_tetra_y[2] = &ddr_map->tetra_gb_y;
3730 		virt_addr_tetra_y[3] = &ddr_map->tetra_b_y;
3731 		virt_addr_tetra_y[4] = &ddr_map->tetra_ratb_y;
3732 		virt_addr_tetra_y[5] = &ddr_map->tetra_batr_y;
3733 
3734 		virt_size_tetra_y[0] = &ddr_map_size->tetra_r_y;
3735 		virt_size_tetra_y[1] = &ddr_map_size->tetra_gr_y;
3736 		virt_size_tetra_y[2] = &ddr_map_size->tetra_gb_y;
3737 		virt_size_tetra_y[3] = &ddr_map_size->tetra_b_y;
3738 		virt_size_tetra_y[4] = &ddr_map_size->tetra_ratb_y;
3739 		virt_size_tetra_y[5] = &ddr_map_size->tetra_batr_y;
3740 
3741 		buff_realloced = false;
3742 		for (i = 0; i < IA_CSS_MORPH_TABLE_NUM_PLANES; i++) {
3743 			buff_realloced |=
3744 			    reallocate_buffer(virt_addr_tetra_x[i],
3745 					    virt_size_tetra_x[i],
3746 					    (size_t)
3747 					    (MORPH_PLANE_BYTES(binary)),
3748 					    params->morph_table_changed,
3749 					    &err);
3750 			if (err) {
3751 				IA_CSS_LEAVE_ERR_PRIVATE(err);
3752 				return err;
3753 			}
3754 			buff_realloced |=
3755 			    reallocate_buffer(virt_addr_tetra_y[i],
3756 					    virt_size_tetra_y[i],
3757 					    (size_t)
3758 					    (MORPH_PLANE_BYTES(binary)),
3759 					    params->morph_table_changed,
3760 					    &err);
3761 			if (err) {
3762 				IA_CSS_LEAVE_ERR_PRIVATE(err);
3763 				return err;
3764 			}
3765 		}
3766 		if (params->morph_table_changed || buff_realloced) {
3767 			const struct ia_css_morph_table *table = params->morph_table;
3768 			struct ia_css_morph_table *id_table = NULL;
3769 
3770 			if ((table) &&
3771 			    (table->width < binary->morph_tbl_width ||
3772 			    table->height < binary->morph_tbl_height)) {
3773 				table = NULL;
3774 			}
3775 			if (!table) {
3776 				err = sh_css_params_default_morph_table(&id_table,
3777 									binary);
3778 				if (err) {
3779 					IA_CSS_LEAVE_ERR_PRIVATE(err);
3780 					return err;
3781 				}
3782 				table = id_table;
3783 			}
3784 
3785 			for (i = 0; i < IA_CSS_MORPH_TABLE_NUM_PLANES; i++) {
3786 				store_morph_plane(table->coordinates_x[i],
3787 						table->width,
3788 						table->height,
3789 						*virt_addr_tetra_x[i],
3790 						binary->morph_tbl_aligned_width);
3791 				store_morph_plane(table->coordinates_y[i],
3792 						table->width,
3793 						table->height,
3794 						*virt_addr_tetra_y[i],
3795 						binary->morph_tbl_aligned_width);
3796 			}
3797 			if (id_table)
3798 				ia_css_morph_table_free(id_table);
3799 		}
3800 	}
3801 
3802 	/* After special cases like SC, FPN since they may change parameters */
3803 	for (mem = 0; mem < N_IA_CSS_MEMORIES; mem++) {
3804 		const struct ia_css_isp_data *isp_data =
3805 		    ia_css_isp_param_get_isp_mem_init(&binary->info->sp.mem_initializers,
3806 						    IA_CSS_PARAM_CLASS_PARAM, mem);
3807 		size_t size = isp_data->size;
3808 
3809 		if (!size) continue;
3810 		buff_realloced = reallocate_buffer(&ddr_map->isp_mem_param[stage_num][mem],
3811 						&ddr_map_size->isp_mem_param[stage_num][mem],
3812 						size,
3813 						params->isp_mem_params_changed[pipe_id][stage_num][mem],
3814 						&err);
3815 		if (err) {
3816 			IA_CSS_LEAVE_ERR_PRIVATE(err);
3817 			return err;
3818 		}
3819 		if (params->isp_mem_params_changed[pipe_id][stage_num][mem] || buff_realloced) {
3820 			sh_css_update_isp_mem_params_to_ddr(binary,
3821 							    ddr_map->isp_mem_param[stage_num][mem],
3822 							    ddr_map_size->isp_mem_param[stage_num][mem], mem);
3823 		}
3824 	}
3825 
3826 	IA_CSS_LEAVE_ERR_PRIVATE(0);
3827 	return 0;
3828 }
3829 
ia_css_get_fpn_table(struct ia_css_stream * stream)3830 const struct ia_css_fpn_table *ia_css_get_fpn_table(struct ia_css_stream
3831 	*stream)
3832 {
3833 	struct ia_css_isp_parameters *params;
3834 
3835 	IA_CSS_ENTER_LEAVE("void");
3836 	assert(stream);
3837 
3838 	params = stream->isp_params_configs;
3839 
3840 	return &params->fpn_config;
3841 }
3842 
ia_css_get_shading_table(struct ia_css_stream * stream)3843 struct ia_css_shading_table *ia_css_get_shading_table(struct ia_css_stream
3844 	*stream)
3845 {
3846 	struct ia_css_shading_table *table = NULL;
3847 	struct ia_css_isp_parameters *params;
3848 
3849 	IA_CSS_ENTER("void");
3850 
3851 	assert(stream);
3852 
3853 	params = stream->isp_params_configs;
3854 	if (!params)
3855 		return NULL;
3856 
3857 	if (params->shading_settings.enable_shading_table_conversion == 0) {
3858 		if (params->sc_table) {
3859 			table = (struct ia_css_shading_table *)params->sc_table;
3860 		} else {
3861 			const struct ia_css_binary *binary
3862 			    = ia_css_stream_get_shading_correction_binary(stream);
3863 			if (binary) {
3864 				/* generate the identical shading table */
3865 				if (params->sc_config) {
3866 					ia_css_shading_table_free(params->sc_config);
3867 					params->sc_config = NULL;
3868 				}
3869 				sh_css_params_shading_id_table_generate(&params->sc_config,
3870 									binary->sctbl_width_per_color,
3871 									binary->sctbl_height);
3872 				table = params->sc_config;
3873 				/* The sc_config will be freed in the
3874 				 * ia_css_stream_isp_parameters_uninit function. */
3875 			}
3876 		}
3877 	} else {
3878 		/* ------ deprecated(bz675) : from ------ */
3879 		const struct ia_css_binary *binary
3880 		    = ia_css_stream_get_shading_correction_binary(stream);
3881 		struct ia_css_pipe *pipe;
3882 
3883 		/**********************************************************************/
3884 		/* following code is copied from function ia_css_stream_get_shading_correction_binary()
3885 		 * to match with the binary */
3886 		pipe = stream->pipes[0];
3887 
3888 		if (stream->num_pipes == 2) {
3889 			assert(stream->pipes[1]);
3890 			if (stream->pipes[1]->config.mode == IA_CSS_PIPE_MODE_VIDEO ||
3891 			    stream->pipes[1]->config.mode == IA_CSS_PIPE_MODE_PREVIEW)
3892 				pipe = stream->pipes[1];
3893 		}
3894 		/**********************************************************************/
3895 		if (binary) {
3896 			if (params->sc_config) {
3897 				ia_css_shading_table_free(params->sc_config);
3898 				params->sc_config = NULL;
3899 			}
3900 			prepare_shading_table(
3901 			    (const struct ia_css_shading_table *)params->sc_table,
3902 			    params->sensor_binning,
3903 			    &params->sc_config,
3904 			    binary, pipe->required_bds_factor);
3905 
3906 			table = params->sc_config;
3907 			/* The sc_config will be freed in the
3908 			 * ia_css_stream_isp_parameters_uninit function. */
3909 		}
3910 		/* ------ deprecated(bz675) : to ------ */
3911 	}
3912 
3913 	IA_CSS_LEAVE("table=%p", table);
3914 
3915 	return table;
3916 }
3917 
sh_css_store_sp_group_to_ddr(void)3918 ia_css_ptr sh_css_store_sp_group_to_ddr(void)
3919 {
3920 	IA_CSS_ENTER_LEAVE_PRIVATE("void");
3921 	hmm_store(xmem_sp_group_ptrs,
3922 		   &sh_css_sp_group,
3923 		   sizeof(struct sh_css_sp_group));
3924 	return xmem_sp_group_ptrs;
3925 }
3926 
sh_css_store_sp_stage_to_ddr(unsigned int pipe,unsigned int stage)3927 ia_css_ptr sh_css_store_sp_stage_to_ddr(
3928     unsigned int pipe,
3929     unsigned int stage)
3930 {
3931 	IA_CSS_ENTER_LEAVE_PRIVATE("void");
3932 	hmm_store(xmem_sp_stage_ptrs[pipe][stage],
3933 		   &sh_css_sp_stage,
3934 		   sizeof(struct sh_css_sp_stage));
3935 	return xmem_sp_stage_ptrs[pipe][stage];
3936 }
3937 
sh_css_store_isp_stage_to_ddr(unsigned int pipe,unsigned int stage)3938 ia_css_ptr sh_css_store_isp_stage_to_ddr(
3939     unsigned int pipe,
3940     unsigned int stage)
3941 {
3942 	IA_CSS_ENTER_LEAVE_PRIVATE("void");
3943 	hmm_store(xmem_isp_stage_ptrs[pipe][stage],
3944 		   &sh_css_isp_stage,
3945 		   sizeof(struct sh_css_isp_stage));
3946 	return xmem_isp_stage_ptrs[pipe][stage];
3947 }
3948 
ref_sh_css_ddr_address_map(struct sh_css_ddr_address_map * map,struct sh_css_ddr_address_map * out)3949 static int ref_sh_css_ddr_address_map(
3950     struct sh_css_ddr_address_map *map,
3951     struct sh_css_ddr_address_map *out)
3952 {
3953 	int err = 0;
3954 	unsigned int i;
3955 
3956 	/* we will use a union to copy things; overlaying an array
3957 	   with the struct; that way adding fields in the struct
3958 	   will keep things working, and we will not get type errors.
3959 	*/
3960 	union {
3961 		struct sh_css_ddr_address_map *map;
3962 		ia_css_ptr *addrs;
3963 	} in_addrs, to_addrs;
3964 
3965 	IA_CSS_ENTER_PRIVATE("void");
3966 	assert(map);
3967 	assert(out);
3968 
3969 	in_addrs.map = map;
3970 	to_addrs.map = out;
3971 
3972 	assert(sizeof(struct sh_css_ddr_address_map_size) / sizeof(size_t) ==
3973 	       sizeof(struct sh_css_ddr_address_map) / sizeof(ia_css_ptr));
3974 
3975 	/* copy map using size info */
3976 	for (i = 0; i < (sizeof(struct sh_css_ddr_address_map_size) /
3977 			 sizeof(size_t)); i++) {
3978 		if (in_addrs.addrs[i] == mmgr_NULL)
3979 			to_addrs.addrs[i] = mmgr_NULL;
3980 		else
3981 			to_addrs.addrs[i] = ia_css_refcount_increment(IA_CSS_REFCOUNT_PARAM_BUFFER,
3982 					    in_addrs.addrs[i]);
3983 	}
3984 
3985 	IA_CSS_LEAVE_ERR_PRIVATE(err);
3986 	return err;
3987 }
3988 
write_ia_css_isp_parameter_set_info_to_ddr(struct ia_css_isp_parameter_set_info * me,ia_css_ptr * out)3989 static int write_ia_css_isp_parameter_set_info_to_ddr(
3990     struct ia_css_isp_parameter_set_info *me,
3991     ia_css_ptr *out)
3992 {
3993 	int err = 0;
3994 	bool succ;
3995 
3996 	IA_CSS_ENTER_PRIVATE("void");
3997 
3998 	assert(me);
3999 	assert(out);
4000 
4001 	*out = ia_css_refcount_increment(IA_CSS_REFCOUNT_PARAM_SET_POOL,
4002 					 hmm_alloc(sizeof(struct ia_css_isp_parameter_set_info), HMM_BO_PRIVATE, 0, NULL, 0));
4003 	succ = (*out != mmgr_NULL);
4004 	if (succ)
4005 		hmm_store(*out,
4006 			   me, sizeof(struct ia_css_isp_parameter_set_info));
4007 	else
4008 		err = -ENOMEM;
4009 
4010 	IA_CSS_LEAVE_ERR_PRIVATE(err);
4011 	return err;
4012 }
4013 
4014 static int
free_ia_css_isp_parameter_set_info(ia_css_ptr ptr)4015 free_ia_css_isp_parameter_set_info(
4016     ia_css_ptr ptr)
4017 {
4018 	int err = 0;
4019 	struct ia_css_isp_parameter_set_info isp_params_info;
4020 	unsigned int i;
4021 	ia_css_ptr *addrs = (ia_css_ptr *)&isp_params_info.mem_map;
4022 
4023 	IA_CSS_ENTER_PRIVATE("ptr = %u", ptr);
4024 
4025 	/* sanity check - ptr must be valid */
4026 	if (!ia_css_refcount_is_valid(ptr)) {
4027 		IA_CSS_ERROR("%s: IA_CSS_REFCOUNT_PARAM_SET_POOL(0x%x) invalid arg", __func__,
4028 			     ptr);
4029 		err = -EINVAL;
4030 		IA_CSS_LEAVE_ERR_PRIVATE(err);
4031 		return err;
4032 	}
4033 
4034 	hmm_load(ptr, &isp_params_info.mem_map, sizeof(struct sh_css_ddr_address_map));
4035 	/* copy map using size info */
4036 	for (i = 0; i < (sizeof(struct sh_css_ddr_address_map_size) /
4037 			 sizeof(size_t)); i++) {
4038 		if (addrs[i] == mmgr_NULL)
4039 			continue;
4040 
4041 		/* sanity check - ptr must be valid */
4042 		if (!ia_css_refcount_is_valid(addrs[i])) {
4043 			IA_CSS_ERROR("%s: IA_CSS_REFCOUNT_PARAM_BUFFER(0x%x) invalid arg", __func__,
4044 				     ptr);
4045 			err = -EINVAL;
4046 			continue;
4047 		}
4048 
4049 		ia_css_refcount_decrement(IA_CSS_REFCOUNT_PARAM_BUFFER, addrs[i]);
4050 	}
4051 	ia_css_refcount_decrement(IA_CSS_REFCOUNT_PARAM_SET_POOL, ptr);
4052 
4053 	IA_CSS_LEAVE_ERR_PRIVATE(err);
4054 	return err;
4055 }
4056 
4057 /* Mark all parameters as changed to force recomputing the derived ISP parameters */
4058 void
sh_css_invalidate_params(struct ia_css_stream * stream)4059 sh_css_invalidate_params(struct ia_css_stream *stream)
4060 {
4061 	struct	ia_css_isp_parameters *params;
4062 	unsigned int i, j, mem;
4063 
4064 	IA_CSS_ENTER_PRIVATE("void");
4065 	assert(stream);
4066 
4067 	params = stream->isp_params_configs;
4068 	params->isp_params_changed = true;
4069 	for (i = 0; i < IA_CSS_PIPE_ID_NUM; i++) {
4070 		for (j = 0; j < SH_CSS_MAX_STAGES; j++) {
4071 			for (mem = 0; mem < N_IA_CSS_MEMORIES; mem++) {
4072 				params->isp_mem_params_changed[i][j][mem] = true;
4073 			}
4074 		}
4075 	}
4076 
4077 	memset(&params->config_changed[0], 1, sizeof(params->config_changed));
4078 	params->dis_coef_table_changed = true;
4079 	params->dvs2_coef_table_changed = true;
4080 	params->morph_table_changed = true;
4081 	params->sc_table_changed = true;
4082 	params->dz_config_changed = true;
4083 	params->motion_config_changed = true;
4084 
4085 	/*Free up theDVS table memory blocks before recomputing new table  */
4086 	for (i = 0; i < IA_CSS_PIPE_ID_NUM; i++) {
4087 		if (params->pipe_dvs_6axis_config[i]) {
4088 			free_dvs_6axis_table(&params->pipe_dvs_6axis_config[i]);
4089 			params->pipe_dvs_6axis_config_changed[i] = true;
4090 		}
4091 	}
4092 
4093 	IA_CSS_LEAVE_PRIVATE("void");
4094 }
4095 
4096 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)4097 sh_css_update_uds_and_crop_info(
4098     const struct ia_css_binary_info *info,
4099     const struct ia_css_frame_info *in_frame_info,
4100     const struct ia_css_frame_info *out_frame_info,
4101     const struct ia_css_resolution *dvs_env,
4102     const struct ia_css_dz_config *zoom,
4103     const struct ia_css_vector *motion_vector,
4104     struct sh_css_uds_info *uds,		/* out */
4105     struct sh_css_crop_pos *sp_out_crop_pos,	/* out */
4106 
4107     bool enable_zoom)
4108 {
4109 	IA_CSS_ENTER_PRIVATE("void");
4110 
4111 	assert(info);
4112 	assert(in_frame_info);
4113 	assert(out_frame_info);
4114 	assert(dvs_env);
4115 	assert(zoom);
4116 	assert(motion_vector);
4117 	assert(uds);
4118 	assert(sp_out_crop_pos);
4119 
4120 	uds->curr_dx   = enable_zoom ? (uint16_t)zoom->dx : HRT_GDC_N;
4121 	uds->curr_dy   = enable_zoom ? (uint16_t)zoom->dy : HRT_GDC_N;
4122 
4123 	if (info->enable.dvs_envelope) {
4124 		unsigned int crop_x = 0,
4125 			     crop_y = 0,
4126 			     uds_xc = 0,
4127 			     uds_yc = 0,
4128 			     env_width, env_height;
4129 		int half_env_x, half_env_y;
4130 		int motion_x = motion_vector->x;
4131 		int motion_y = motion_vector->y;
4132 		bool upscale_x = in_frame_info->res.width < out_frame_info->res.width;
4133 		bool upscale_y = in_frame_info->res.height < out_frame_info->res.height;
4134 
4135 		if (info->enable.uds && !info->enable.ds) {
4136 			/**
4137 			 * we calculate with the envelope that we can actually
4138 			 * use, the min dvs envelope is for the filter
4139 			 * initialization.
4140 			 */
4141 			env_width  = dvs_env->width -
4142 				     SH_CSS_MIN_DVS_ENVELOPE;
4143 			env_height = dvs_env->height -
4144 				     SH_CSS_MIN_DVS_ENVELOPE;
4145 			half_env_x = env_width / 2;
4146 			half_env_y = env_height / 2;
4147 			/**
4148 			 * for digital zoom, we use the dvs envelope and make
4149 			 * sure that we don't include the 8 leftmost pixels or
4150 			 * 8 topmost rows.
4151 			 */
4152 			if (upscale_x) {
4153 				uds_xc = (in_frame_info->res.width
4154 					  + env_width
4155 					  + SH_CSS_MIN_DVS_ENVELOPE) / 2;
4156 			} else {
4157 				uds_xc = (out_frame_info->res.width
4158 					  + env_width) / 2
4159 					 + SH_CSS_MIN_DVS_ENVELOPE;
4160 			}
4161 			if (upscale_y) {
4162 				uds_yc = (in_frame_info->res.height
4163 					  + env_height
4164 					  + SH_CSS_MIN_DVS_ENVELOPE) / 2;
4165 			} else {
4166 				uds_yc = (out_frame_info->res.height
4167 					  + env_height) / 2
4168 					 + SH_CSS_MIN_DVS_ENVELOPE;
4169 			}
4170 			/* clip the motion vector to +/- half the envelope */
4171 			motion_x = clamp(motion_x, -half_env_x, half_env_x);
4172 			motion_y = clamp(motion_y, -half_env_y, half_env_y);
4173 			uds_xc += motion_x;
4174 			uds_yc += motion_y;
4175 			/* uds can be pipelined, remove top lines */
4176 			crop_y = 2;
4177 		} else if (info->enable.ds) {
4178 			env_width  = dvs_env->width;
4179 			env_height = dvs_env->height;
4180 			half_env_x = env_width / 2;
4181 			half_env_y = env_height / 2;
4182 			/* clip the motion vector to +/- half the envelope */
4183 			motion_x = clamp(motion_x, -half_env_x, half_env_x);
4184 			motion_y = clamp(motion_y, -half_env_y, half_env_y);
4185 			/* for video with downscaling, the envelope is included
4186 			    in the input resolution. */
4187 			uds_xc = in_frame_info->res.width / 2 + motion_x;
4188 			uds_yc = in_frame_info->res.height / 2 + motion_y;
4189 			crop_x = info->pipeline.left_cropping;
4190 			/* ds == 2 (yuv_ds) can be pipelined, remove top
4191 			   lines */
4192 			if (info->enable.ds & 1)
4193 				crop_y = info->pipeline.top_cropping;
4194 			else
4195 				crop_y = 2;
4196 		} else {
4197 			/* video nodz: here we can only crop. We make sure we
4198 			   crop at least the first 8x8 pixels away. */
4199 			env_width  = dvs_env->width -
4200 				     SH_CSS_MIN_DVS_ENVELOPE;
4201 			env_height = dvs_env->height -
4202 				     SH_CSS_MIN_DVS_ENVELOPE;
4203 			half_env_x = env_width / 2;
4204 			half_env_y = env_height / 2;
4205 			motion_x = clamp(motion_x, -half_env_x, half_env_x);
4206 			motion_y = clamp(motion_y, -half_env_y, half_env_y);
4207 			crop_x = SH_CSS_MIN_DVS_ENVELOPE
4208 				 + half_env_x + motion_x;
4209 			crop_y = SH_CSS_MIN_DVS_ENVELOPE
4210 				 + half_env_y + motion_y;
4211 		}
4212 
4213 		/* Must enforce that the crop position is even */
4214 		crop_x = EVEN_FLOOR(crop_x);
4215 		crop_y = EVEN_FLOOR(crop_y);
4216 		uds_xc = EVEN_FLOOR(uds_xc);
4217 		uds_yc = EVEN_FLOOR(uds_yc);
4218 
4219 		uds->xc = (uint16_t)uds_xc;
4220 		uds->yc = (uint16_t)uds_yc;
4221 		sp_out_crop_pos->x = (uint16_t)crop_x;
4222 		sp_out_crop_pos->y = (uint16_t)crop_y;
4223 	} else {
4224 		/* for down scaling, we always use the center of the image */
4225 		uds->xc = (uint16_t)in_frame_info->res.width / 2;
4226 		uds->yc = (uint16_t)in_frame_info->res.height / 2;
4227 		sp_out_crop_pos->x = (uint16_t)info->pipeline.left_cropping;
4228 		sp_out_crop_pos->y = (uint16_t)info->pipeline.top_cropping;
4229 	}
4230 	IA_CSS_LEAVE_PRIVATE("void");
4231 }
4232 
4233 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)4234 sh_css_update_uds_and_crop_info_based_on_zoom_region(
4235     const struct ia_css_binary_info *info,
4236     const struct ia_css_frame_info *in_frame_info,
4237     const struct ia_css_frame_info *out_frame_info,
4238     const struct ia_css_resolution *dvs_env,
4239     const struct ia_css_dz_config *zoom,
4240     const struct ia_css_vector *motion_vector,
4241     struct sh_css_uds_info *uds,		/* out */
4242     struct sh_css_crop_pos *sp_out_crop_pos,	/* out */
4243     struct ia_css_resolution pipe_in_res,
4244     bool enable_zoom)
4245 {
4246 	unsigned int x0 = 0, y0 = 0, x1 = 0, y1 = 0;
4247 	int err = 0;
4248 	/* Note:
4249 	* Filter_Envelope = 0 for NND/LUT
4250 	* Filter_Envelope = 1 for BCI
4251 	* Filter_Envelope = 3 for BLI
4252 	* Currently, not considering this filter envelope because, In uds.sp.c is recalculating
4253 	* the dx/dy based on filter envelope and other information (ia_css_uds_sp_scale_params)
4254 	* Ideally, That should be done on host side not on sp side.
4255 	*/
4256 	unsigned int filter_envelope = 0;
4257 
4258 	IA_CSS_ENTER_PRIVATE("void");
4259 
4260 	assert(info);
4261 	assert(in_frame_info);
4262 	assert(out_frame_info);
4263 	assert(dvs_env);
4264 	assert(zoom);
4265 	assert(motion_vector);
4266 	assert(uds);
4267 	assert(sp_out_crop_pos);
4268 	x0 = zoom->zoom_region.origin.x;
4269 	y0 = zoom->zoom_region.origin.y;
4270 	x1 = zoom->zoom_region.resolution.width + x0;
4271 	y1 = zoom->zoom_region.resolution.height + y0;
4272 
4273 	if ((x0 > x1) || (y0 > y1) || (x1 > pipe_in_res.width) || (y1 > pipe_in_res.height))
4274 		return -EINVAL;
4275 
4276 	if (!enable_zoom) {
4277 		uds->curr_dx = HRT_GDC_N;
4278 		uds->curr_dy = HRT_GDC_N;
4279 	}
4280 
4281 	if (info->enable.dvs_envelope) {
4282 		/* Zoom region is only supported by the UDS module on ISP
4283 		 * 2 and higher. It is not supported in video mode on ISP 1 */
4284 		return -EINVAL;
4285 	} else {
4286 		if (enable_zoom) {
4287 			/* A. Calculate dx/dy based on crop region using in_frame_info
4288 			* Scale the crop region if in_frame_info to the stage is not same as
4289 			* actual effective input of the pipeline
4290 			*/
4291 			if (in_frame_info->res.width != pipe_in_res.width ||
4292 			    in_frame_info->res.height != pipe_in_res.height) {
4293 				x0 = (x0 * in_frame_info->res.width) / (pipe_in_res.width);
4294 				y0 = (y0 * in_frame_info->res.height) / (pipe_in_res.height);
4295 				x1 = (x1 * in_frame_info->res.width) / (pipe_in_res.width);
4296 				y1 = (y1 * in_frame_info->res.height) / (pipe_in_res.height);
4297 			}
4298 			uds->curr_dx =
4299 			    ((x1 - x0 - filter_envelope) * HRT_GDC_N) / in_frame_info->res.width;
4300 			uds->curr_dy =
4301 			    ((y1 - y0 - filter_envelope) * HRT_GDC_N) / in_frame_info->res.height;
4302 
4303 			/* B. Calculate xc/yc based on crop region */
4304 			uds->xc = (uint16_t)x0 + (((x1) - (x0)) / 2);
4305 			uds->yc = (uint16_t)y0 + (((y1) - (y0)) / 2);
4306 		} else {
4307 			uds->xc = (uint16_t)in_frame_info->res.width / 2;
4308 			uds->yc = (uint16_t)in_frame_info->res.height / 2;
4309 		}
4310 
4311 		ia_css_debug_dtrace(IA_CSS_DEBUG_TRACE,
4312 				    "uds->curr_dx=%d, uds->xc=%d, uds->yc=%d\n",
4313 				    uds->curr_dx, uds->xc, uds->yc);
4314 		ia_css_debug_dtrace(IA_CSS_DEBUG_TRACE, "x0=%d, y0=%d, x1=%d, y1=%d\n",
4315 				    x0, y0, x1, y1);
4316 		sp_out_crop_pos->x = (uint16_t)info->pipeline.left_cropping;
4317 		sp_out_crop_pos->y = (uint16_t)info->pipeline.top_cropping;
4318 	}
4319 	IA_CSS_LEAVE_PRIVATE("void");
4320 	return err;
4321 }
4322 
4323 struct ia_css_3a_statistics *
ia_css_3a_statistics_allocate(const struct ia_css_3a_grid_info * grid)4324 ia_css_3a_statistics_allocate(const struct ia_css_3a_grid_info *grid)
4325 {
4326 	struct ia_css_3a_statistics *me;
4327 	int grid_size;
4328 
4329 	IA_CSS_ENTER("grid=%p", grid);
4330 
4331 	assert(grid);
4332 
4333 	me = kvcalloc(1, sizeof(*me), GFP_KERNEL);
4334 	if (!me)
4335 		goto err;
4336 
4337 	me->grid = *grid;
4338 	grid_size = grid->width * grid->height;
4339 	me->data = kvmalloc(grid_size * sizeof(*me->data), GFP_KERNEL);
4340 	if (!me->data)
4341 		goto err;
4342 #if !defined(HAS_NO_HMEM)
4343 	/* No weighted histogram, no structure, treat the histogram data as a byte dump in a byte array */
4344 	me->rgby_data = kvmalloc(sizeof_hmem(HMEM0_ID), GFP_KERNEL);
4345 #else
4346 	me->rgby_data = NULL;
4347 #endif
4348 
4349 	IA_CSS_LEAVE("return=%p", me);
4350 	return me;
4351 err:
4352 	ia_css_3a_statistics_free(me);
4353 
4354 	IA_CSS_LEAVE("return=%p", NULL);
4355 	return NULL;
4356 }
4357 
4358 void
ia_css_3a_statistics_free(struct ia_css_3a_statistics * me)4359 ia_css_3a_statistics_free(struct ia_css_3a_statistics *me)
4360 {
4361 	if (me) {
4362 		kvfree(me->rgby_data);
4363 		kvfree(me->data);
4364 		kvfree(me);
4365 	}
4366 }
4367 
4368 struct ia_css_dvs_statistics *
ia_css_dvs_statistics_allocate(const struct ia_css_dvs_grid_info * grid)4369 ia_css_dvs_statistics_allocate(const struct ia_css_dvs_grid_info *grid)
4370 {
4371 	struct ia_css_dvs_statistics *me;
4372 
4373 	assert(grid);
4374 
4375 	me = kvcalloc(1, sizeof(*me), GFP_KERNEL);
4376 	if (!me)
4377 		goto err;
4378 
4379 	me->grid = *grid;
4380 	me->hor_proj = kvmalloc(grid->height * IA_CSS_DVS_NUM_COEF_TYPES *
4381 				sizeof(*me->hor_proj), GFP_KERNEL);
4382 	if (!me->hor_proj)
4383 		goto err;
4384 
4385 	me->ver_proj = kvmalloc(grid->width * IA_CSS_DVS_NUM_COEF_TYPES *
4386 				sizeof(*me->ver_proj), GFP_KERNEL);
4387 	if (!me->ver_proj)
4388 		goto err;
4389 
4390 	return me;
4391 err:
4392 	ia_css_dvs_statistics_free(me);
4393 	return NULL;
4394 }
4395 
4396 void
ia_css_dvs_statistics_free(struct ia_css_dvs_statistics * me)4397 ia_css_dvs_statistics_free(struct ia_css_dvs_statistics *me)
4398 {
4399 	if (me) {
4400 		kvfree(me->hor_proj);
4401 		kvfree(me->ver_proj);
4402 		kvfree(me);
4403 	}
4404 }
4405 
4406 struct ia_css_dvs_coefficients *
ia_css_dvs_coefficients_allocate(const struct ia_css_dvs_grid_info * grid)4407 ia_css_dvs_coefficients_allocate(const struct ia_css_dvs_grid_info *grid)
4408 {
4409 	struct ia_css_dvs_coefficients *me;
4410 
4411 	assert(grid);
4412 
4413 	me = kvcalloc(1, sizeof(*me), GFP_KERNEL);
4414 	if (!me)
4415 		goto err;
4416 
4417 	me->grid = *grid;
4418 
4419 	me->hor_coefs = kvmalloc(grid->num_hor_coefs *
4420 				 IA_CSS_DVS_NUM_COEF_TYPES *
4421 				 sizeof(*me->hor_coefs), GFP_KERNEL);
4422 	if (!me->hor_coefs)
4423 		goto err;
4424 
4425 	me->ver_coefs = kvmalloc(grid->num_ver_coefs *
4426 				 IA_CSS_DVS_NUM_COEF_TYPES *
4427 				 sizeof(*me->ver_coefs), GFP_KERNEL);
4428 	if (!me->ver_coefs)
4429 		goto err;
4430 
4431 	return me;
4432 err:
4433 	ia_css_dvs_coefficients_free(me);
4434 	return NULL;
4435 }
4436 
4437 void
ia_css_dvs_coefficients_free(struct ia_css_dvs_coefficients * me)4438 ia_css_dvs_coefficients_free(struct ia_css_dvs_coefficients *me)
4439 {
4440 	if (me) {
4441 		kvfree(me->hor_coefs);
4442 		kvfree(me->ver_coefs);
4443 		kvfree(me);
4444 	}
4445 }
4446 
4447 struct ia_css_dvs2_statistics *
ia_css_dvs2_statistics_allocate(const struct ia_css_dvs_grid_info * grid)4448 ia_css_dvs2_statistics_allocate(const struct ia_css_dvs_grid_info *grid)
4449 {
4450 	struct ia_css_dvs2_statistics *me;
4451 
4452 	assert(grid);
4453 
4454 	me = kvcalloc(1, sizeof(*me), GFP_KERNEL);
4455 	if (!me)
4456 		goto err;
4457 
4458 	me->grid = *grid;
4459 
4460 	me->hor_prod.odd_real = kvmalloc(grid->aligned_width *
4461 					 grid->aligned_height *
4462 					 sizeof(*me->hor_prod.odd_real),
4463 					 GFP_KERNEL);
4464 	if (!me->hor_prod.odd_real)
4465 		goto err;
4466 
4467 	me->hor_prod.odd_imag = kvmalloc(grid->aligned_width *
4468 					 grid->aligned_height *
4469 					 sizeof(*me->hor_prod.odd_imag),
4470 					 GFP_KERNEL);
4471 	if (!me->hor_prod.odd_imag)
4472 		goto err;
4473 
4474 	me->hor_prod.even_real = kvmalloc(grid->aligned_width *
4475 					  grid->aligned_height *
4476 					  sizeof(*me->hor_prod.even_real),
4477 					  GFP_KERNEL);
4478 	if (!me->hor_prod.even_real)
4479 		goto err;
4480 
4481 	me->hor_prod.even_imag = kvmalloc(grid->aligned_width *
4482 					  grid->aligned_height *
4483 					  sizeof(*me->hor_prod.even_imag),
4484 					  GFP_KERNEL);
4485 	if (!me->hor_prod.even_imag)
4486 		goto err;
4487 
4488 	me->ver_prod.odd_real = kvmalloc(grid->aligned_width *
4489 					 grid->aligned_height *
4490 					 sizeof(*me->ver_prod.odd_real),
4491 					 GFP_KERNEL);
4492 	if (!me->ver_prod.odd_real)
4493 		goto err;
4494 
4495 	me->ver_prod.odd_imag = kvmalloc(grid->aligned_width *
4496 					 grid->aligned_height *
4497 					 sizeof(*me->ver_prod.odd_imag),
4498 					 GFP_KERNEL);
4499 	if (!me->ver_prod.odd_imag)
4500 		goto err;
4501 
4502 	me->ver_prod.even_real = kvmalloc(grid->aligned_width *
4503 					  grid->aligned_height *
4504 					  sizeof(*me->ver_prod.even_real),
4505 					  GFP_KERNEL);
4506 	if (!me->ver_prod.even_real)
4507 		goto err;
4508 
4509 	me->ver_prod.even_imag = kvmalloc(grid->aligned_width *
4510 					  grid->aligned_height *
4511 					  sizeof(*me->ver_prod.even_imag),
4512 					  GFP_KERNEL);
4513 	if (!me->ver_prod.even_imag)
4514 		goto err;
4515 
4516 	return me;
4517 err:
4518 	ia_css_dvs2_statistics_free(me);
4519 	return NULL;
4520 }
4521 
4522 void
ia_css_dvs2_statistics_free(struct ia_css_dvs2_statistics * me)4523 ia_css_dvs2_statistics_free(struct ia_css_dvs2_statistics *me)
4524 {
4525 	if (me) {
4526 		kvfree(me->hor_prod.odd_real);
4527 		kvfree(me->hor_prod.odd_imag);
4528 		kvfree(me->hor_prod.even_real);
4529 		kvfree(me->hor_prod.even_imag);
4530 		kvfree(me->ver_prod.odd_real);
4531 		kvfree(me->ver_prod.odd_imag);
4532 		kvfree(me->ver_prod.even_real);
4533 		kvfree(me->ver_prod.even_imag);
4534 		kvfree(me);
4535 	}
4536 }
4537 
4538 struct ia_css_dvs2_coefficients *
ia_css_dvs2_coefficients_allocate(const struct ia_css_dvs_grid_info * grid)4539 ia_css_dvs2_coefficients_allocate(const struct ia_css_dvs_grid_info *grid)
4540 {
4541 	struct ia_css_dvs2_coefficients *me;
4542 
4543 	assert(grid);
4544 
4545 	me = kvcalloc(1, sizeof(*me), GFP_KERNEL);
4546 	if (!me)
4547 		goto err;
4548 
4549 	me->grid = *grid;
4550 
4551 	me->hor_coefs.odd_real = kvmalloc(grid->num_hor_coefs *
4552 					  sizeof(*me->hor_coefs.odd_real),
4553 					  GFP_KERNEL);
4554 	if (!me->hor_coefs.odd_real)
4555 		goto err;
4556 
4557 	me->hor_coefs.odd_imag = kvmalloc(grid->num_hor_coefs *
4558 					  sizeof(*me->hor_coefs.odd_imag),
4559 					  GFP_KERNEL);
4560 	if (!me->hor_coefs.odd_imag)
4561 		goto err;
4562 
4563 	me->hor_coefs.even_real = kvmalloc(grid->num_hor_coefs *
4564 					   sizeof(*me->hor_coefs.even_real),
4565 					   GFP_KERNEL);
4566 	if (!me->hor_coefs.even_real)
4567 		goto err;
4568 
4569 	me->hor_coefs.even_imag = kvmalloc(grid->num_hor_coefs *
4570 					   sizeof(*me->hor_coefs.even_imag),
4571 					   GFP_KERNEL);
4572 	if (!me->hor_coefs.even_imag)
4573 		goto err;
4574 
4575 	me->ver_coefs.odd_real = kvmalloc(grid->num_ver_coefs *
4576 					  sizeof(*me->ver_coefs.odd_real),
4577 					  GFP_KERNEL);
4578 	if (!me->ver_coefs.odd_real)
4579 		goto err;
4580 
4581 	me->ver_coefs.odd_imag = kvmalloc(grid->num_ver_coefs *
4582 					  sizeof(*me->ver_coefs.odd_imag),
4583 					  GFP_KERNEL);
4584 	if (!me->ver_coefs.odd_imag)
4585 		goto err;
4586 
4587 	me->ver_coefs.even_real = kvmalloc(grid->num_ver_coefs *
4588 					   sizeof(*me->ver_coefs.even_real),
4589 					   GFP_KERNEL);
4590 	if (!me->ver_coefs.even_real)
4591 		goto err;
4592 
4593 	me->ver_coefs.even_imag = kvmalloc(grid->num_ver_coefs *
4594 					   sizeof(*me->ver_coefs.even_imag),
4595 					   GFP_KERNEL);
4596 	if (!me->ver_coefs.even_imag)
4597 		goto err;
4598 
4599 	return me;
4600 err:
4601 	ia_css_dvs2_coefficients_free(me);
4602 	return NULL;
4603 }
4604 
4605 void
ia_css_dvs2_coefficients_free(struct ia_css_dvs2_coefficients * me)4606 ia_css_dvs2_coefficients_free(struct ia_css_dvs2_coefficients *me)
4607 {
4608 	if (me) {
4609 		kvfree(me->hor_coefs.odd_real);
4610 		kvfree(me->hor_coefs.odd_imag);
4611 		kvfree(me->hor_coefs.even_real);
4612 		kvfree(me->hor_coefs.even_imag);
4613 		kvfree(me->ver_coefs.odd_real);
4614 		kvfree(me->ver_coefs.odd_imag);
4615 		kvfree(me->ver_coefs.even_real);
4616 		kvfree(me->ver_coefs.even_imag);
4617 		kvfree(me);
4618 	}
4619 }
4620 
4621 struct ia_css_dvs_6axis_config *
ia_css_dvs2_6axis_config_allocate(const struct ia_css_stream * stream)4622 ia_css_dvs2_6axis_config_allocate(const struct ia_css_stream *stream)
4623 {
4624 	struct ia_css_dvs_6axis_config *dvs_config = NULL;
4625 	struct ia_css_isp_parameters *params = NULL;
4626 	unsigned int width_y;
4627 	unsigned int height_y;
4628 	unsigned int width_uv;
4629 	unsigned int height_uv;
4630 
4631 	assert(stream);
4632 	params = stream->isp_params_configs;
4633 
4634 	/* Backward compatibility by default consider pipe as Video*/
4635 	if (!params || !params->pipe_dvs_6axis_config[IA_CSS_PIPE_ID_VIDEO])
4636 		goto err;
4637 
4638 	dvs_config = kvcalloc(1, sizeof(struct ia_css_dvs_6axis_config),
4639 			      GFP_KERNEL);
4640 	if (!dvs_config)
4641 		goto err;
4642 
4643 	dvs_config->width_y = width_y =
4644 				  params->pipe_dvs_6axis_config[IA_CSS_PIPE_ID_VIDEO]->width_y;
4645 	dvs_config->height_y = height_y =
4646 				   params->pipe_dvs_6axis_config[IA_CSS_PIPE_ID_VIDEO]->height_y;
4647 	dvs_config->width_uv = width_uv =
4648 				   params->pipe_dvs_6axis_config[IA_CSS_PIPE_ID_VIDEO]->width_uv;
4649 	dvs_config->height_uv = height_uv =
4650 				    params->pipe_dvs_6axis_config[IA_CSS_PIPE_ID_VIDEO]->height_uv;
4651 	IA_CSS_LOG("table Y: W %d H %d", width_y, height_y);
4652 	IA_CSS_LOG("table UV: W %d H %d", width_uv, height_uv);
4653 	dvs_config->xcoords_y = kvmalloc(width_y * height_y * sizeof(uint32_t),
4654 					 GFP_KERNEL);
4655 	if (!dvs_config->xcoords_y)
4656 		goto err;
4657 
4658 	dvs_config->ycoords_y = kvmalloc(width_y * height_y * sizeof(uint32_t),
4659 					 GFP_KERNEL);
4660 	if (!dvs_config->ycoords_y)
4661 		goto err;
4662 
4663 	dvs_config->xcoords_uv = kvmalloc(width_uv * height_uv *
4664 					  sizeof(uint32_t),
4665 					  GFP_KERNEL);
4666 	if (!dvs_config->xcoords_uv)
4667 		goto err;
4668 
4669 	dvs_config->ycoords_uv = kvmalloc(width_uv * height_uv *
4670 					  sizeof(uint32_t),
4671 					  GFP_KERNEL);
4672 	if (!dvs_config->ycoords_uv)
4673 		goto err;
4674 
4675 	return dvs_config;
4676 err:
4677 	ia_css_dvs2_6axis_config_free(dvs_config);
4678 	return NULL;
4679 }
4680 
4681 void
ia_css_dvs2_6axis_config_free(struct ia_css_dvs_6axis_config * dvs_6axis_config)4682 ia_css_dvs2_6axis_config_free(struct ia_css_dvs_6axis_config *dvs_6axis_config)
4683 {
4684 	if (dvs_6axis_config) {
4685 		kvfree(dvs_6axis_config->xcoords_y);
4686 		kvfree(dvs_6axis_config->ycoords_y);
4687 		kvfree(dvs_6axis_config->xcoords_uv);
4688 		kvfree(dvs_6axis_config->ycoords_uv);
4689 		kvfree(dvs_6axis_config);
4690 	}
4691 }
4692 
4693 void
ia_css_en_dz_capt_pipe(struct ia_css_stream * stream,bool enable)4694 ia_css_en_dz_capt_pipe(struct ia_css_stream *stream, bool enable)
4695 {
4696 	struct ia_css_pipe *pipe;
4697 	struct ia_css_pipeline *pipeline;
4698 	struct ia_css_pipeline_stage *stage;
4699 	enum ia_css_pipe_id pipe_id;
4700 	int err;
4701 	int i;
4702 
4703 	if (!stream)
4704 		return;
4705 
4706 	for (i = 0; i < stream->num_pipes; i++) {
4707 		pipe = stream->pipes[i];
4708 		pipeline = ia_css_pipe_get_pipeline(pipe);
4709 		pipe_id = pipeline->pipe_id;
4710 
4711 		if (pipe_id == IA_CSS_PIPE_ID_CAPTURE) {
4712 			err = ia_css_pipeline_get_stage(pipeline, IA_CSS_BINARY_MODE_CAPTURE_PP,
4713 							&stage);
4714 			if (!err)
4715 				stage->enable_zoom = enable;
4716 			break;
4717 		}
4718 	}
4719 }
4720