Linux Audio

Check our new training course

Embedded Linux Audio

Check our new training course
with Creative Commons CC-BY-SA
lecture materials

Bootlin logo

Elixir Cross Referencer

Loading...
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
// SPDX-License-Identifier: GPL-2.0
/*
 * Support for Intel Camera Imaging ISP subsystem.
 * Copyright (c) 2015, Intel Corporation.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms and conditions of the GNU General Public License,
 * version 2, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 */

#include "hmm.h"

#include "assert_support.h"
#include "ia_css_debug.h"
#include "ia_css_sdis_types.h"
#include "sdis/common/ia_css_sdis_common.host.h"
#include "ia_css_sdis.host.h"

const struct ia_css_dvs_coefficients default_sdis_config = {
	.grid = { 0, 0, 0, 0, 0, 0, 0, 0 },
	.hor_coefs = NULL,
	.ver_coefs = NULL
};

static void
fill_row(short *private, const short *public, unsigned int width,
	 unsigned int padding)
{
	assert((int)width >= 0);
	assert((int)padding >= 0);
	memcpy(private, public, width * sizeof(short));
	memset(&private[width], 0, padding * sizeof(short));
}

void ia_css_sdis_horicoef_vmem_encode(
    struct sh_css_isp_sdis_hori_coef_tbl *to,
    const struct ia_css_dvs_coefficients *from,
    unsigned int size)
{
	unsigned int aligned_width = from->grid.aligned_width *
				     from->grid.bqs_per_grid_cell;
	unsigned int width         = from->grid.num_hor_coefs;
	int      padding       = aligned_width - width;
	unsigned int stride        = size / IA_CSS_DVS_NUM_COEF_TYPES / sizeof(short);
	unsigned int total_bytes   = aligned_width * IA_CSS_DVS_NUM_COEF_TYPES * sizeof(
					 short);
	short   *public        = from->hor_coefs;
	short   *private       = (short *)to;
	unsigned int type;

	/* Copy the table, add padding */
	assert(padding >= 0);
	assert(total_bytes <= size);
	assert(size % (IA_CSS_DVS_NUM_COEF_TYPES * ISP_VEC_NELEMS * sizeof(
			   short)) == 0);

	for (type = 0; type < IA_CSS_DVS_NUM_COEF_TYPES; type++) {
		fill_row(&private[type * stride], &public[type * width], width, padding);
	}
}

void ia_css_sdis_vertcoef_vmem_encode(
    struct sh_css_isp_sdis_vert_coef_tbl *to,
    const struct ia_css_dvs_coefficients *from,
    unsigned int size)
{
	unsigned int aligned_height = from->grid.aligned_height *
				      from->grid.bqs_per_grid_cell;
	unsigned int height         = from->grid.num_ver_coefs;
	int      padding        = aligned_height - height;
	unsigned int stride         = size / IA_CSS_DVS_NUM_COEF_TYPES / sizeof(short);
	unsigned int total_bytes    = aligned_height * IA_CSS_DVS_NUM_COEF_TYPES *
				      sizeof(short);
	short   *public         = from->ver_coefs;
	short   *private        = (short *)to;
	unsigned int type;

	/* Copy the table, add padding */
	assert(padding >= 0);
	assert(total_bytes <= size);
	assert(size % (IA_CSS_DVS_NUM_COEF_TYPES * ISP_VEC_NELEMS * sizeof(
			   short)) == 0);

	for (type = 0; type < IA_CSS_DVS_NUM_COEF_TYPES; type++) {
		fill_row(&private[type * stride], &public[type * height], height, padding);
	}
}

void ia_css_sdis_horiproj_encode(
    struct sh_css_isp_sdis_hori_proj_tbl *to,
    const struct ia_css_dvs_coefficients *from,
    unsigned int size)
{
	(void)to;
	(void)from;
	(void)size;
}

void ia_css_sdis_vertproj_encode(
    struct sh_css_isp_sdis_vert_proj_tbl *to,
    const struct ia_css_dvs_coefficients *from,
    unsigned int size)
{
	(void)to;
	(void)from;
	(void)size;
}

void ia_css_get_isp_dis_coefficients(
    struct ia_css_stream *stream,
    short *horizontal_coefficients,
    short *vertical_coefficients)
{
	struct ia_css_isp_parameters *params;
	unsigned int hor_num_isp, ver_num_isp;
	unsigned int hor_num_3a,  ver_num_3a;
	int i;
	struct ia_css_binary *dvs_binary;

	IA_CSS_ENTER("void");

	assert(horizontal_coefficients);
	assert(vertical_coefficients);

	params = stream->isp_params_configs;

	/* Only video pipe supports DVS */
	dvs_binary = ia_css_stream_get_dvs_binary(stream);
	if (!dvs_binary)
		return;

	hor_num_isp = dvs_binary->dis.coef.pad.width;
	ver_num_isp = dvs_binary->dis.coef.pad.height;
	hor_num_3a  = dvs_binary->dis.coef.dim.width;
	ver_num_3a  = dvs_binary->dis.coef.dim.height;

	for (i = 0; i < IA_CSS_DVS_NUM_COEF_TYPES; i++) {
		fill_row(&horizontal_coefficients[i * hor_num_isp],
			 &params->dvs_coefs.hor_coefs[i * hor_num_3a], hor_num_3a,
			 hor_num_isp - hor_num_3a);
	}
	for (i = 0; i < SH_CSS_DIS_VER_NUM_COEF_TYPES(dvs_binary); i++) {
		fill_row(&vertical_coefficients[i * ver_num_isp],
			 &params->dvs_coefs.ver_coefs[i * ver_num_3a], ver_num_3a,
			 ver_num_isp - ver_num_3a);
	}

	IA_CSS_LEAVE("void");
}

size_t
ia_css_sdis_hor_coef_tbl_bytes(
    const struct ia_css_binary *binary)
{
	if (binary->info->sp.pipeline.isp_pipe_version == 1)
		return sizeof(short) * IA_CSS_DVS_NUM_COEF_TYPES  * binary->dis.coef.pad.width;
	else
		return sizeof(short) * IA_CSS_DVS2_NUM_COEF_TYPES * binary->dis.coef.pad.width;
}

size_t
ia_css_sdis_ver_coef_tbl_bytes(
    const struct ia_css_binary *binary)
{
	return sizeof(short) * SH_CSS_DIS_VER_NUM_COEF_TYPES(binary) *
	       binary->dis.coef.pad.height;
}

void
ia_css_sdis_init_info(
    struct ia_css_sdis_info *dis,
    unsigned int sc_3a_dis_width,
    unsigned int sc_3a_dis_padded_width,
    unsigned int sc_3a_dis_height,
    unsigned int isp_pipe_version,
    unsigned int enabled)
{
	if (!enabled) {
		*dis = (struct ia_css_sdis_info) { };
		return;
	}

	dis->deci_factor_log2 = SH_CSS_DIS_DECI_FACTOR_LOG2;

	dis->grid.dim.width  =
	    _ISP_BQS(sc_3a_dis_width) >> SH_CSS_DIS_DECI_FACTOR_LOG2;
	dis->grid.dim.height =
	    _ISP_BQS(sc_3a_dis_height) >> SH_CSS_DIS_DECI_FACTOR_LOG2;
	dis->grid.pad.width  =
	    CEIL_SHIFT(_ISP_BQS(sc_3a_dis_padded_width), SH_CSS_DIS_DECI_FACTOR_LOG2);
	dis->grid.pad.height =
	    CEIL_SHIFT(_ISP_BQS(sc_3a_dis_height), SH_CSS_DIS_DECI_FACTOR_LOG2);

	dis->coef.dim.width  =
	    (_ISP_BQS(sc_3a_dis_width)  >> SH_CSS_DIS_DECI_FACTOR_LOG2) <<
	    SH_CSS_DIS_DECI_FACTOR_LOG2;
	dis->coef.dim.height =
	    (_ISP_BQS(sc_3a_dis_height) >> SH_CSS_DIS_DECI_FACTOR_LOG2) <<
	    SH_CSS_DIS_DECI_FACTOR_LOG2;
	dis->coef.pad.width  =
	    __ISP_SDIS_HOR_COEF_NUM_VECS(sc_3a_dis_padded_width) * ISP_VEC_NELEMS;
	dis->coef.pad.height =
	    __ISP_SDIS_VER_COEF_NUM_VECS(sc_3a_dis_height) * ISP_VEC_NELEMS;
	if (isp_pipe_version == 1) {
		dis->proj.dim.width  =
		    _ISP_BQS(sc_3a_dis_height) >> SH_CSS_DIS_DECI_FACTOR_LOG2;
		dis->proj.dim.height =
		    _ISP_BQS(sc_3a_dis_width)  >> SH_CSS_DIS_DECI_FACTOR_LOG2;
	} else {
		dis->proj.dim.width  =
		    (_ISP_BQS(sc_3a_dis_width)  >> SH_CSS_DIS_DECI_FACTOR_LOG2) *
		    (_ISP_BQS(sc_3a_dis_height) >> SH_CSS_DIS_DECI_FACTOR_LOG2);
		dis->proj.dim.height =
		    (_ISP_BQS(sc_3a_dis_width)  >> SH_CSS_DIS_DECI_FACTOR_LOG2) *
		    (_ISP_BQS(sc_3a_dis_height) >> SH_CSS_DIS_DECI_FACTOR_LOG2);
	}
	dis->proj.pad.width  =
	    __ISP_SDIS_HOR_PROJ_NUM_ISP(sc_3a_dis_padded_width,
					sc_3a_dis_height,
					SH_CSS_DIS_DECI_FACTOR_LOG2,
					isp_pipe_version);
	dis->proj.pad.height =
	    __ISP_SDIS_VER_PROJ_NUM_ISP(sc_3a_dis_padded_width,
					SH_CSS_DIS_DECI_FACTOR_LOG2);
}

void ia_css_sdis_clear_coefficients(
    struct ia_css_dvs_coefficients *dvs_coefs)
{
	dvs_coefs->hor_coefs = NULL;
	dvs_coefs->ver_coefs = NULL;
}

int
ia_css_get_dvs_statistics(
    struct ia_css_dvs_statistics	       *host_stats,
    const struct ia_css_isp_dvs_statistics *isp_stats) {
	struct ia_css_isp_dvs_statistics_map *map;
	int ret = 0;

	IA_CSS_ENTER("host_stats=%p, isp_stats=%p", host_stats, isp_stats);

	assert(host_stats);
	assert(isp_stats);

	map = ia_css_isp_dvs_statistics_map_allocate(isp_stats, NULL);
	if (map)
	{
		hmm_load(isp_stats->data_ptr, map->data_ptr, isp_stats->size);
		ia_css_translate_dvs_statistics(host_stats, map);
		ia_css_isp_dvs_statistics_map_free(map);
	} else
	{
		IA_CSS_ERROR("out of memory");
		ret = -ENOMEM;
	}

	IA_CSS_LEAVE_ERR(ret);
	return ret;
}

void
ia_css_translate_dvs_statistics(
    struct ia_css_dvs_statistics               *host_stats,
    const struct ia_css_isp_dvs_statistics_map *isp_stats)
{
	unsigned int hor_num_isp, ver_num_isp, hor_num_dvs, ver_num_dvs, i;
	s32 *hor_ptr_dvs, *ver_ptr_dvs, *hor_ptr_isp, *ver_ptr_isp;

	assert(host_stats);
	assert(host_stats->hor_proj);
	assert(host_stats->ver_proj);
	assert(isp_stats);
	assert(isp_stats->hor_proj);
	assert(isp_stats->ver_proj);

	IA_CSS_ENTER("hproj=%p, vproj=%p, haddr=%p, vaddr=%p",
		     host_stats->hor_proj, host_stats->ver_proj,
		     isp_stats->hor_proj, isp_stats->ver_proj);

	hor_num_isp = host_stats->grid.aligned_height;
	ver_num_isp = host_stats->grid.aligned_width;
	hor_ptr_isp = isp_stats->hor_proj;
	ver_ptr_isp = isp_stats->ver_proj;
	hor_num_dvs = host_stats->grid.height;
	ver_num_dvs = host_stats->grid.width;
	hor_ptr_dvs = host_stats->hor_proj;
	ver_ptr_dvs = host_stats->ver_proj;

	for (i = 0; i < IA_CSS_DVS_NUM_COEF_TYPES; i++) {
		memcpy(hor_ptr_dvs, hor_ptr_isp, hor_num_dvs * sizeof(int32_t));
		hor_ptr_isp += hor_num_isp;
		hor_ptr_dvs += hor_num_dvs;

		memcpy(ver_ptr_dvs, ver_ptr_isp, ver_num_dvs * sizeof(int32_t));
		ver_ptr_isp += ver_num_isp;
		ver_ptr_dvs += ver_num_dvs;
	}

	IA_CSS_LEAVE("void");
}

struct ia_css_isp_dvs_statistics *
ia_css_isp_dvs_statistics_allocate(
    const struct ia_css_dvs_grid_info *grid)
{
	struct ia_css_isp_dvs_statistics *me;
	int hor_size, ver_size;

	assert(grid);

	IA_CSS_ENTER("grid=%p", grid);

	if (!grid->enable)
		return NULL;

	me = kvcalloc(1, sizeof(*me), GFP_KERNEL);
	if (!me)
		goto err;

	hor_size = CEIL_MUL(sizeof(int) * IA_CSS_DVS_NUM_COEF_TYPES *
			    grid->aligned_height,
			    HIVE_ISP_DDR_WORD_BYTES);
	ver_size = CEIL_MUL(sizeof(int) * IA_CSS_DVS_NUM_COEF_TYPES *
			    grid->aligned_width,
			    HIVE_ISP_DDR_WORD_BYTES);

	me->size = hor_size + ver_size;
	me->data_ptr = hmm_alloc(me->size, HMM_BO_PRIVATE, 0, NULL, 0);
	if (me->data_ptr == mmgr_NULL)
		goto err;
	me->hor_size = hor_size;
	me->hor_proj = me->data_ptr;
	me->ver_size = ver_size;
	me->ver_proj = me->data_ptr + hor_size;

	IA_CSS_LEAVE("return=%p", me);

	return me;
err:
	ia_css_isp_dvs_statistics_free(me);

	IA_CSS_LEAVE("return=%p", NULL);

	return NULL;
}

struct ia_css_isp_dvs_statistics_map *
ia_css_isp_dvs_statistics_map_allocate(
    const struct ia_css_isp_dvs_statistics *isp_stats,
    void *data_ptr)
{
	struct ia_css_isp_dvs_statistics_map *me;
	/* Windows compiler does not like adding sizes to a void *
	 * so we use a local char * instead. */
	char *base_ptr;

	me = kvmalloc(sizeof(*me), GFP_KERNEL);
	if (!me) {
		IA_CSS_LOG("cannot allocate memory");
		goto err;
	}

	me->data_ptr = data_ptr;
	me->data_allocated = !data_ptr;

	if (!me->data_ptr) {
		me->data_ptr = kvmalloc(isp_stats->size, GFP_KERNEL);
		if (!me->data_ptr) {
			IA_CSS_LOG("cannot allocate memory");
			goto err;
		}
	}
	base_ptr = me->data_ptr;

	me->size = isp_stats->size;
	/* GCC complains when we assign a char * to a void *, so these
	 * casts are necessary unfortunately. */
	me->hor_proj = (void *)base_ptr;
	me->ver_proj = (void *)(base_ptr + isp_stats->hor_size);

	return me;
err:
	if (me)
		kvfree(me);
	return NULL;
}

void
ia_css_isp_dvs_statistics_map_free(struct ia_css_isp_dvs_statistics_map *me)
{
	if (me) {
		if (me->data_allocated)
			kvfree(me->data_ptr);
		kvfree(me);
	}
}

void
ia_css_isp_dvs_statistics_free(struct ia_css_isp_dvs_statistics *me)
{
	if (me) {
		hmm_free(me->data_ptr);
		kvfree(me);
	}
}

void ia_css_sdis_horicoef_debug_dtrace(
    const struct ia_css_dvs_coefficients *config, unsigned int level)
{
	(void)config;
	(void)level;
}

void ia_css_sdis_vertcoef_debug_dtrace(
    const struct ia_css_dvs_coefficients *config, unsigned int level)
{
	(void)config;
	(void)level;
}

void ia_css_sdis_horiproj_debug_dtrace(
    const struct ia_css_dvs_coefficients *config, unsigned int level)
{
	(void)config;
	(void)level;
}

void ia_css_sdis_vertproj_debug_dtrace(
    const struct ia_css_dvs_coefficients *config, unsigned int level)
{
	(void)config;
	(void)level;
}