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