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