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