xref: /linux/drivers/media/platform/qcom/iris/iris_vpu_buffer.c (revision 8cefa0ac93a8cb94ff9e16dd5327e14ec5223d28)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (c) 2022-2024 Qualcomm Innovation Center, Inc. All rights reserved.
4  */
5 
6 #include "iris_instance.h"
7 #include "iris_vpu_buffer.h"
8 #include "iris_hfi_gen1_defines.h"
9 #include "iris_hfi_gen2_defines.h"
10 
11 #define HFI_MAX_COL_FRAME 6
12 
13 #ifndef SYSTEM_LAL_TILE10
14 #define SYSTEM_LAL_TILE10 192
15 #endif
16 
17 static u32 size_h264d_hw_bin_buffer(u32 frame_width, u32 frame_height, u32 num_vpp_pipes)
18 {
19 	u32 size_yuv, size_bin_hdr, size_bin_res;
20 
21 	size_yuv = ((frame_width * frame_height) <= BIN_BUFFER_THRESHOLD) ?
22 			((BIN_BUFFER_THRESHOLD * 3) >> 1) :
23 			((frame_width * frame_height * 3) >> 1);
24 	size_bin_hdr = size_yuv * H264_CABAC_HDR_RATIO_HD_TOT;
25 	size_bin_res = size_yuv * H264_CABAC_RES_RATIO_HD_TOT;
26 	size_bin_hdr = ALIGN(size_bin_hdr / num_vpp_pipes,
27 			     DMA_ALIGNMENT) * num_vpp_pipes;
28 	size_bin_res = ALIGN(size_bin_res / num_vpp_pipes,
29 			     DMA_ALIGNMENT) * num_vpp_pipes;
30 
31 	return size_bin_hdr + size_bin_res;
32 }
33 
34 static u32 hfi_buffer_bin_h264d(u32 frame_width, u32 frame_height, u32 num_vpp_pipes)
35 {
36 	u32 n_aligned_h = ALIGN(frame_height, 16);
37 	u32 n_aligned_w = ALIGN(frame_width, 16);
38 
39 	return size_h264d_hw_bin_buffer(n_aligned_w, n_aligned_h, num_vpp_pipes);
40 }
41 
42 static u32 size_h265d_hw_bin_buffer(u32 frame_width, u32 frame_height, u32 num_vpp_pipes)
43 {
44 	u32 product = frame_width * frame_height;
45 	u32 size_yuv, size_bin_hdr, size_bin_res;
46 
47 	size_yuv = (product <= BIN_BUFFER_THRESHOLD) ?
48 		((BIN_BUFFER_THRESHOLD * 3) >> 1) : ((product * 3) >> 1);
49 	size_bin_hdr = size_yuv * H265_CABAC_HDR_RATIO_HD_TOT;
50 	size_bin_res = size_yuv * H265_CABAC_RES_RATIO_HD_TOT;
51 	size_bin_hdr = ALIGN(size_bin_hdr / num_vpp_pipes, DMA_ALIGNMENT) * num_vpp_pipes;
52 	size_bin_res = ALIGN(size_bin_res / num_vpp_pipes, DMA_ALIGNMENT) * num_vpp_pipes;
53 
54 	return size_bin_hdr + size_bin_res;
55 }
56 
57 static u32 hfi_buffer_bin_vp9d(u32 frame_width, u32 frame_height, u32 num_vpp_pipes)
58 {
59 	u32 _size_yuv = ALIGN(frame_width, 16) * ALIGN(frame_height, 16) * 3 / 2;
60 	u32 _size = ALIGN(((max_t(u32, _size_yuv, ((BIN_BUFFER_THRESHOLD * 3) >> 1)) *
61 			VPX_DECODER_FRAME_BIN_HDR_BUDGET / VPX_DECODER_FRAME_BIN_DENOMINATOR *
62 			VPX_DECODER_FRAME_CONCURENCY_LVL) / num_vpp_pipes), DMA_ALIGNMENT) +
63 			ALIGN(((max_t(u32, _size_yuv, ((BIN_BUFFER_THRESHOLD * 3) >> 1)) *
64 			VPX_DECODER_FRAME_BIN_RES_BUDGET / VPX_DECODER_FRAME_BIN_DENOMINATOR *
65 			VPX_DECODER_FRAME_CONCURENCY_LVL) / num_vpp_pipes), DMA_ALIGNMENT);
66 
67 	return _size * num_vpp_pipes;
68 }
69 
70 static u32 hfi_buffer_bin_h265d(u32 frame_width, u32 frame_height, u32 num_vpp_pipes)
71 {
72 	u32 n_aligned_w = ALIGN(frame_width, 16);
73 	u32 n_aligned_h = ALIGN(frame_height, 16);
74 
75 	return size_h265d_hw_bin_buffer(n_aligned_w, n_aligned_h, num_vpp_pipes);
76 }
77 
78 static u32 hfi_buffer_comv_h264d(u32 frame_width, u32 frame_height, u32 _comv_bufcount)
79 {
80 	u32 frame_height_in_mbs = DIV_ROUND_UP(frame_height, 16);
81 	u32 frame_width_in_mbs = DIV_ROUND_UP(frame_width, 16);
82 	u32 col_zero_aligned_width = (frame_width_in_mbs << 2);
83 	u32 col_mv_aligned_width = (frame_width_in_mbs << 7);
84 	u32 col_zero_size, size_colloc;
85 
86 	col_mv_aligned_width = ALIGN(col_mv_aligned_width, 16);
87 	col_zero_aligned_width = ALIGN(col_zero_aligned_width, 16);
88 	col_zero_size = col_zero_aligned_width *
89 			((frame_height_in_mbs + 1) >> 1);
90 	col_zero_size = ALIGN(col_zero_size, 64);
91 	col_zero_size <<= 1;
92 	col_zero_size = ALIGN(col_zero_size, 512);
93 	size_colloc = col_mv_aligned_width * ((frame_height_in_mbs + 1) >> 1);
94 	size_colloc = ALIGN(size_colloc, 64);
95 	size_colloc <<= 1;
96 	size_colloc = ALIGN(size_colloc, 512);
97 	size_colloc += (col_zero_size + SIZE_H264D_BUFTAB_T * 2);
98 
99 	return (size_colloc * (_comv_bufcount)) + 512;
100 }
101 
102 static u32 hfi_buffer_comv_h265d(u32 frame_width, u32 frame_height, u32 _comv_bufcount)
103 {
104 	u32 frame_height_in_mbs = (frame_height + 15) >> 4;
105 	u32 frame_width_in_mbs = (frame_width + 15) >> 4;
106 	u32 _size;
107 
108 	_size = ALIGN(((frame_width_in_mbs * frame_height_in_mbs) << 8), 512);
109 
110 	return (_size * (_comv_bufcount)) + 512;
111 }
112 
113 static u32 size_h264d_bse_cmd_buf(u32 frame_height)
114 {
115 	u32 height = ALIGN(frame_height, 32);
116 
117 	return min_t(u32, (DIV_ROUND_UP(height, 16) * 48), H264D_MAX_SLICE) *
118 		SIZE_H264D_BSE_CMD_PER_BUF;
119 }
120 
121 static u32 size_h265d_bse_cmd_buf(u32 frame_width, u32 frame_height)
122 {
123 	u32 _size = ALIGN(((ALIGN(frame_width, LCU_MAX_SIZE_PELS) / LCU_MIN_SIZE_PELS) *
124 			   (ALIGN(frame_height, LCU_MAX_SIZE_PELS) / LCU_MIN_SIZE_PELS)) *
125 			  NUM_HW_PIC_BUF, DMA_ALIGNMENT);
126 	_size = min_t(u32, _size, H265D_MAX_SLICE + 1);
127 	_size = 2 * _size * SIZE_H265D_BSE_CMD_PER_BUF;
128 
129 	return _size;
130 }
131 
132 static u32 hfi_buffer_persist_h265d(u32 rpu_enabled)
133 {
134 	return ALIGN((SIZE_SLIST_BUF_H265 * NUM_SLIST_BUF_H265 +
135 		      H265_NUM_FRM_INFO * H265_DISPLAY_BUF_SIZE +
136 		      H265_NUM_TILE * sizeof(u32) +
137 		      NUM_HW_PIC_BUF * SIZE_SEI_USERDATA +
138 		      rpu_enabled * NUM_HW_PIC_BUF * SIZE_DOLBY_RPU_METADATA),
139 		     DMA_ALIGNMENT);
140 }
141 
142 static inline
143 u32 hfi_iris3_vp9d_comv_size(void)
144 {
145 	return (((8192 + 63) >> 6) * ((4320 + 63) >> 6) * 8 * 8 * 2 * 8);
146 }
147 
148 static u32 hfi_buffer_persist_vp9d(void)
149 {
150 	return ALIGN(VP9_NUM_PROBABILITY_TABLE_BUF * VP9_PROB_TABLE_SIZE, DMA_ALIGNMENT) +
151 		ALIGN(hfi_iris3_vp9d_comv_size(), DMA_ALIGNMENT) +
152 		ALIGN(MAX_SUPERFRAME_HEADER_LEN, DMA_ALIGNMENT) +
153 		ALIGN(VP9_UDC_HEADER_BUF_SIZE, DMA_ALIGNMENT) +
154 		ALIGN(VP9_NUM_FRAME_INFO_BUF * CCE_TILE_OFFSET_SIZE, DMA_ALIGNMENT) +
155 		ALIGN(VP9_NUM_FRAME_INFO_BUF * VP9_FRAME_INFO_BUF_SIZE, DMA_ALIGNMENT) +
156 		HDR10_HIST_EXTRADATA_SIZE;
157 }
158 
159 static u32 size_h264d_vpp_cmd_buf(u32 frame_height)
160 {
161 	u32 size, height = ALIGN(frame_height, 32);
162 
163 	size = min_t(u32, (DIV_ROUND_UP(height, 16) * 48), H264D_MAX_SLICE) *
164 			SIZE_H264D_VPP_CMD_PER_BUF;
165 
166 	return size > VPP_CMD_MAX_SIZE ? VPP_CMD_MAX_SIZE : size;
167 }
168 
169 static u32 hfi_buffer_persist_h264d(void)
170 {
171 	return ALIGN(SIZE_SLIST_BUF_H264 * NUM_SLIST_BUF_H264 +
172 		    H264_DISPLAY_BUF_SIZE * H264_NUM_FRM_INFO +
173 		    NUM_HW_PIC_BUF * SIZE_SEI_USERDATA,
174 		    DMA_ALIGNMENT);
175 }
176 
177 static u32 hfi_buffer_non_comv_h264d(u32 frame_width, u32 frame_height, u32 num_vpp_pipes)
178 {
179 	u32 size_bse = size_h264d_bse_cmd_buf(frame_height);
180 	u32 size_vpp = size_h264d_vpp_cmd_buf(frame_height);
181 	u32 size = ALIGN(size_bse, DMA_ALIGNMENT) +
182 		ALIGN(size_vpp, DMA_ALIGNMENT) +
183 		ALIGN(SIZE_HW_PIC(SIZE_H264D_HW_PIC_T), DMA_ALIGNMENT);
184 
185 	return ALIGN(size, DMA_ALIGNMENT);
186 }
187 
188 static u32 size_h265d_vpp_cmd_buf(u32 frame_width, u32 frame_height)
189 {
190 	u32 _size = ALIGN(((ALIGN(frame_width, LCU_MAX_SIZE_PELS) / LCU_MIN_SIZE_PELS) *
191 			   (ALIGN(frame_height, LCU_MAX_SIZE_PELS) / LCU_MIN_SIZE_PELS)) *
192 			  NUM_HW_PIC_BUF, DMA_ALIGNMENT);
193 	_size = min_t(u32, _size, H265D_MAX_SLICE + 1);
194 	_size = ALIGN(_size, 4);
195 	_size = 2 * _size * SIZE_H265D_VPP_CMD_PER_BUF;
196 	if (_size > VPP_CMD_MAX_SIZE)
197 		_size = VPP_CMD_MAX_SIZE;
198 
199 	return _size;
200 }
201 
202 static u32 hfi_buffer_non_comv_h265d(u32 frame_width, u32 frame_height, u32 num_vpp_pipes)
203 {
204 	u32 _size_bse = size_h265d_bse_cmd_buf(frame_width, frame_height);
205 	u32 _size_vpp = size_h265d_vpp_cmd_buf(frame_width, frame_height);
206 	u32 _size = ALIGN(_size_bse, DMA_ALIGNMENT) +
207 		ALIGN(_size_vpp, DMA_ALIGNMENT) +
208 		ALIGN(NUM_HW_PIC_BUF * 20 * 22 * 4, DMA_ALIGNMENT) +
209 		ALIGN(2 * sizeof(u16) *
210 		(ALIGN(frame_width, LCU_MAX_SIZE_PELS) / LCU_MIN_SIZE_PELS) *
211 		(ALIGN(frame_height, LCU_MAX_SIZE_PELS) / LCU_MIN_SIZE_PELS), DMA_ALIGNMENT) +
212 		ALIGN(SIZE_HW_PIC(SIZE_H265D_HW_PIC_T), DMA_ALIGNMENT) +
213 		HDR10_HIST_EXTRADATA_SIZE;
214 
215 	return ALIGN(_size, DMA_ALIGNMENT);
216 }
217 
218 static u32 size_vpss_lb(u32 frame_width, u32 frame_height)
219 {
220 	u32 opb_lb_wr_llb_y_buffer_size, opb_lb_wr_llb_uv_buffer_size;
221 	u32 opb_wr_top_line_chroma_buffer_size;
222 	u32 opb_wr_top_line_luma_buffer_size;
223 	u32 macrotiling_size = 32;
224 
225 	opb_wr_top_line_luma_buffer_size =
226 		ALIGN(frame_width, macrotiling_size) / macrotiling_size * 256;
227 	opb_wr_top_line_luma_buffer_size =
228 		ALIGN(opb_wr_top_line_luma_buffer_size, DMA_ALIGNMENT) +
229 		(MAX_TILE_COLUMNS - 1) * 256;
230 	opb_wr_top_line_luma_buffer_size =
231 		max_t(u32, opb_wr_top_line_luma_buffer_size, (32 * ALIGN(frame_height, 8)));
232 	opb_wr_top_line_chroma_buffer_size = opb_wr_top_line_luma_buffer_size;
233 	opb_lb_wr_llb_uv_buffer_size =
234 		ALIGN((ALIGN(frame_height, 8) / (4 / 2)) * 64, 32);
235 	opb_lb_wr_llb_y_buffer_size =
236 		ALIGN((ALIGN(frame_height, 8) / (4 / 2)) * 64, 32);
237 	return opb_wr_top_line_luma_buffer_size +
238 		opb_wr_top_line_chroma_buffer_size +
239 		opb_lb_wr_llb_uv_buffer_size +
240 		opb_lb_wr_llb_y_buffer_size;
241 }
242 
243 static inline
244 u32 size_h265d_lb_fe_top_data(u32 frame_width, u32 frame_height)
245 {
246 	return MAX_FE_NBR_DATA_LUMA_LINE_BUFFER_SIZE *
247 		(ALIGN(frame_width, 64) + 8) * 2;
248 }
249 
250 static inline
251 u32 size_h265d_lb_fe_top_ctrl(u32 frame_width, u32 frame_height)
252 {
253 	return MAX_FE_NBR_CTRL_LCU64_LINE_BUFFER_SIZE *
254 		(ALIGN(frame_width, LCU_MAX_SIZE_PELS) / LCU_MIN_SIZE_PELS);
255 }
256 
257 static inline
258 u32 size_h265d_lb_fe_left_ctrl(u32 frame_width, u32 frame_height)
259 {
260 	return MAX_FE_NBR_CTRL_LCU64_LINE_BUFFER_SIZE *
261 		(ALIGN(frame_height, LCU_MAX_SIZE_PELS) / LCU_MIN_SIZE_PELS);
262 }
263 
264 static inline
265 u32 size_h265d_lb_se_top_ctrl(u32 frame_width, u32 frame_height)
266 {
267 	return (LCU_MAX_SIZE_PELS / 8 * (128 / 8)) * ((frame_width + 15) >> 4);
268 }
269 
270 static inline
271 u32 size_h265d_lb_se_left_ctrl(u32 frame_width, u32 frame_height)
272 {
273 	return max_t(u32, ((frame_height + 16 - 1) / 8) *
274 		MAX_SE_NBR_CTRL_LCU16_LINE_BUFFER_SIZE,
275 		max_t(u32, ((frame_height + 32 - 1) / 8) *
276 		MAX_SE_NBR_CTRL_LCU32_LINE_BUFFER_SIZE,
277 		((frame_height + 64 - 1) / 8) *
278 		MAX_SE_NBR_CTRL_LCU64_LINE_BUFFER_SIZE));
279 }
280 
281 static inline
282 u32 size_h265d_lb_pe_top_data(u32 frame_width, u32 frame_height)
283 {
284 	return MAX_PE_NBR_DATA_LCU64_LINE_BUFFER_SIZE *
285 		(ALIGN(frame_width, LCU_MIN_SIZE_PELS) / LCU_MIN_SIZE_PELS);
286 }
287 
288 static inline
289 u32 size_h265d_lb_vsp_top(u32 frame_width, u32 frame_height)
290 {
291 	return ((frame_width + 63) >> 6) * 128;
292 }
293 
294 static inline
295 u32 size_h265d_lb_vsp_left(u32 frame_width, u32 frame_height)
296 {
297 	return ((frame_height + 63) >> 6) * 128;
298 }
299 
300 static inline
301 u32 size_h265d_lb_recon_dma_metadata_wr(u32 frame_width, u32 frame_height)
302 {
303 	return size_h264d_lb_recon_dma_metadata_wr(frame_height);
304 }
305 
306 static inline
307 u32 size_h265d_qp(u32 frame_width, u32 frame_height)
308 {
309 	return size_h264d_qp(frame_width, frame_height);
310 }
311 
312 static inline
313 u32 hfi_buffer_line_h265d(u32 frame_width, u32 frame_height, bool is_opb, u32 num_vpp_pipes)
314 {
315 	u32 vpss_lb_size = 0, _size;
316 
317 	_size = ALIGN(size_h265d_lb_fe_top_data(frame_width, frame_height), DMA_ALIGNMENT) +
318 		ALIGN(size_h265d_lb_fe_top_ctrl(frame_width, frame_height), DMA_ALIGNMENT) +
319 		ALIGN(size_h265d_lb_fe_left_ctrl(frame_width, frame_height),
320 		      DMA_ALIGNMENT) * num_vpp_pipes +
321 		ALIGN(size_h265d_lb_se_left_ctrl(frame_width, frame_height),
322 		      DMA_ALIGNMENT) * num_vpp_pipes +
323 		ALIGN(size_h265d_lb_se_top_ctrl(frame_width, frame_height), DMA_ALIGNMENT) +
324 		ALIGN(size_h265d_lb_pe_top_data(frame_width, frame_height), DMA_ALIGNMENT) +
325 		ALIGN(size_h265d_lb_vsp_top(frame_width, frame_height), DMA_ALIGNMENT) +
326 		ALIGN(size_h265d_lb_vsp_left(frame_width, frame_height),
327 		      DMA_ALIGNMENT) * num_vpp_pipes +
328 		ALIGN(size_h265d_lb_recon_dma_metadata_wr(frame_width, frame_height),
329 		      DMA_ALIGNMENT) * 4 +
330 		ALIGN(size_h265d_qp(frame_width, frame_height), DMA_ALIGNMENT);
331 	if (is_opb)
332 		vpss_lb_size = size_vpss_lb(frame_width, frame_height);
333 
334 	return ALIGN((_size + vpss_lb_size), DMA_ALIGNMENT);
335 }
336 
337 static inline
338 u32 size_vpxd_lb_fe_left_ctrl(u32 frame_width, u32 frame_height)
339 {
340 	return max_t(u32, ((frame_height + 15) >> 4) *
341 		     MAX_FE_NBR_CTRL_LCU16_LINE_BUFFER_SIZE,
342 		     max_t(u32, ((frame_height + 31) >> 5) *
343 			   MAX_FE_NBR_CTRL_LCU32_LINE_BUFFER_SIZE,
344 			   ((frame_height + 63) >> 6) *
345 			   MAX_FE_NBR_CTRL_LCU64_LINE_BUFFER_SIZE));
346 }
347 
348 static inline
349 u32 size_vpxd_lb_fe_top_ctrl(u32 frame_width, u32 frame_height)
350 {
351 	return ((ALIGN(frame_width, 64) + 8) * 10 * 2);
352 }
353 
354 static inline
355 u32 size_vpxd_lb_se_top_ctrl(u32 frame_width, u32 frame_height)
356 {
357 	return ((frame_width + 15) >> 4) * MAX_FE_NBR_CTRL_LCU16_LINE_BUFFER_SIZE;
358 }
359 
360 static inline
361 u32 size_vpxd_lb_se_left_ctrl(u32 frame_width, u32 frame_height)
362 {
363 	return max_t(u32, ((frame_height + 15) >> 4) *
364 		     MAX_SE_NBR_CTRL_LCU16_LINE_BUFFER_SIZE,
365 		     max_t(u32, ((frame_height + 31) >> 5) *
366 			   MAX_SE_NBR_CTRL_LCU32_LINE_BUFFER_SIZE,
367 			   ((frame_height + 63) >> 6) *
368 			   MAX_SE_NBR_CTRL_LCU64_LINE_BUFFER_SIZE));
369 }
370 
371 static inline
372 u32 size_vpxd_lb_recon_dma_metadata_wr(u32 frame_width, u32 frame_height)
373 {
374 	return ALIGN((ALIGN(frame_height, 8) / (4 / 2)) * 64,
375 		BUFFER_ALIGNMENT_32_BYTES);
376 }
377 
378 static inline __maybe_unused
379 u32 size_mp2d_lb_fe_top_data(u32 frame_width, u32 frame_height)
380 {
381 	return ((ALIGN(frame_width, 16) + 8) * 10 * 2);
382 }
383 
384 static inline
385 u32 size_vp9d_lb_fe_top_data(u32 frame_width, u32 frame_height)
386 {
387 	return (ALIGN(ALIGN(frame_width, 8), 64) + 8) * 10 * 2;
388 }
389 
390 static inline
391 u32 size_vp9d_lb_pe_top_data(u32 frame_width, u32 frame_height)
392 {
393 	return ((ALIGN(ALIGN(frame_width, 8), 64) >> 6) * 176);
394 }
395 
396 static inline
397 u32 size_vp9d_lb_vsp_top(u32 frame_width, u32 frame_height)
398 {
399 	return (((ALIGN(ALIGN(frame_width, 8), 64) >> 6) * 64 * 8) + 256);
400 }
401 
402 static inline
403 u32 size_vp9d_qp(u32 frame_width, u32 frame_height)
404 {
405 	return size_h264d_qp(frame_width, frame_height);
406 }
407 
408 static inline
409 u32 hfi_iris3_vp9d_lb_size(u32 frame_width, u32 frame_height, u32 num_vpp_pipes)
410 {
411 	return ALIGN(size_vpxd_lb_fe_left_ctrl(frame_width, frame_height), DMA_ALIGNMENT) *
412 		num_vpp_pipes +
413 		ALIGN(size_vpxd_lb_se_left_ctrl(frame_width, frame_height), DMA_ALIGNMENT) *
414 		num_vpp_pipes +
415 		ALIGN(size_vp9d_lb_vsp_top(frame_width, frame_height), DMA_ALIGNMENT) +
416 		ALIGN(size_vpxd_lb_fe_top_ctrl(frame_width, frame_height), DMA_ALIGNMENT) +
417 		2 * ALIGN(size_vpxd_lb_recon_dma_metadata_wr(frame_width, frame_height),
418 			  DMA_ALIGNMENT) +
419 		ALIGN(size_vpxd_lb_se_top_ctrl(frame_width, frame_height), DMA_ALIGNMENT) +
420 		ALIGN(size_vp9d_lb_pe_top_data(frame_width, frame_height), DMA_ALIGNMENT) +
421 		ALIGN(size_vp9d_lb_fe_top_data(frame_width, frame_height), DMA_ALIGNMENT) +
422 		ALIGN(size_vp9d_qp(frame_width, frame_height), DMA_ALIGNMENT);
423 }
424 
425 static inline
426 u32 hfi_buffer_line_vp9d(u32 frame_width, u32 frame_height, u32 _yuv_bufcount_min, bool is_opb,
427 			 u32 num_vpp_pipes)
428 {
429 	u32 vpss_lb_size = 0;
430 	u32 _lb_size;
431 
432 	_lb_size = hfi_iris3_vp9d_lb_size(frame_width, frame_height, num_vpp_pipes);
433 
434 	if (is_opb)
435 		vpss_lb_size = size_vpss_lb(frame_width, frame_height);
436 
437 	return _lb_size + vpss_lb_size + 4096;
438 }
439 
440 static u32 hfi_buffer_line_h264d(u32 frame_width, u32 frame_height,
441 				 bool is_opb, u32 num_vpp_pipes)
442 {
443 	u32 vpss_lb_size = 0;
444 	u32 size;
445 
446 	size = ALIGN(size_h264d_lb_fe_top_data(frame_width), DMA_ALIGNMENT) +
447 		ALIGN(size_h264d_lb_fe_top_ctrl(frame_width), DMA_ALIGNMENT) +
448 		ALIGN(size_h264d_lb_fe_left_ctrl(frame_height), DMA_ALIGNMENT) * num_vpp_pipes +
449 		ALIGN(size_h264d_lb_se_top_ctrl(frame_width), DMA_ALIGNMENT) +
450 		ALIGN(size_h264d_lb_se_left_ctrl(frame_height), DMA_ALIGNMENT) * num_vpp_pipes +
451 		ALIGN(size_h264d_lb_pe_top_data(frame_width), DMA_ALIGNMENT) +
452 		ALIGN(size_h264d_lb_vsp_top(frame_width), DMA_ALIGNMENT) +
453 		ALIGN(size_h264d_lb_recon_dma_metadata_wr(frame_height), DMA_ALIGNMENT) * 2 +
454 		ALIGN(size_h264d_qp(frame_width, frame_height), DMA_ALIGNMENT);
455 	size = ALIGN(size, DMA_ALIGNMENT);
456 	if (is_opb)
457 		vpss_lb_size = size_vpss_lb(frame_width, frame_height);
458 
459 	return ALIGN((size + vpss_lb_size), DMA_ALIGNMENT);
460 }
461 
462 static u32 iris_vpu_dec_bin_size(struct iris_inst *inst)
463 {
464 	u32 num_vpp_pipes = inst->core->iris_platform_data->num_vpp_pipe;
465 	struct v4l2_format *f = inst->fmt_src;
466 	u32 height = f->fmt.pix_mp.height;
467 	u32 width = f->fmt.pix_mp.width;
468 
469 	if (inst->codec == V4L2_PIX_FMT_H264)
470 		return hfi_buffer_bin_h264d(width, height, num_vpp_pipes);
471 	else if (inst->codec == V4L2_PIX_FMT_HEVC)
472 		return hfi_buffer_bin_h265d(width, height, num_vpp_pipes);
473 	else if (inst->codec == V4L2_PIX_FMT_VP9)
474 		return hfi_buffer_bin_vp9d(width, height, num_vpp_pipes);
475 
476 	return 0;
477 }
478 
479 static u32 iris_vpu_dec_comv_size(struct iris_inst *inst)
480 {
481 	u32 num_comv = VIDEO_MAX_FRAME;
482 	struct v4l2_format *f = inst->fmt_src;
483 	u32 height = f->fmt.pix_mp.height;
484 	u32 width = f->fmt.pix_mp.width;
485 
486 	if (inst->codec == V4L2_PIX_FMT_H264)
487 		return hfi_buffer_comv_h264d(width, height, num_comv);
488 	else if (inst->codec == V4L2_PIX_FMT_HEVC)
489 		return hfi_buffer_comv_h265d(width, height, num_comv);
490 
491 	return 0;
492 }
493 
494 static u32 iris_vpu_dec_persist_size(struct iris_inst *inst)
495 {
496 	if (inst->codec == V4L2_PIX_FMT_H264)
497 		return hfi_buffer_persist_h264d();
498 	else if (inst->codec == V4L2_PIX_FMT_HEVC)
499 		return hfi_buffer_persist_h265d(0);
500 	else if (inst->codec == V4L2_PIX_FMT_VP9)
501 		return hfi_buffer_persist_vp9d();
502 
503 	return 0;
504 }
505 
506 static u32 iris_vpu_dec_dpb_size(struct iris_inst *inst)
507 {
508 	if (iris_split_mode_enabled(inst))
509 		return iris_get_buffer_size(inst, BUF_DPB);
510 	else
511 		return 0;
512 }
513 
514 static u32 iris_vpu_dec_non_comv_size(struct iris_inst *inst)
515 {
516 	u32 num_vpp_pipes = inst->core->iris_platform_data->num_vpp_pipe;
517 	struct v4l2_format *f = inst->fmt_src;
518 	u32 height = f->fmt.pix_mp.height;
519 	u32 width = f->fmt.pix_mp.width;
520 
521 	if (inst->codec == V4L2_PIX_FMT_H264)
522 		return hfi_buffer_non_comv_h264d(width, height, num_vpp_pipes);
523 	else if (inst->codec == V4L2_PIX_FMT_HEVC)
524 		return hfi_buffer_non_comv_h265d(width, height, num_vpp_pipes);
525 
526 	return 0;
527 }
528 
529 static u32 iris_vpu_dec_line_size(struct iris_inst *inst)
530 {
531 	u32 num_vpp_pipes = inst->core->iris_platform_data->num_vpp_pipe;
532 	struct v4l2_format *f = inst->fmt_src;
533 	u32 height = f->fmt.pix_mp.height;
534 	u32 width = f->fmt.pix_mp.width;
535 	bool is_opb = false;
536 	u32 out_min_count = inst->buffers[BUF_OUTPUT].min_count;
537 
538 	if (iris_split_mode_enabled(inst))
539 		is_opb = true;
540 
541 	if (inst->codec == V4L2_PIX_FMT_H264)
542 		return hfi_buffer_line_h264d(width, height, is_opb, num_vpp_pipes);
543 	else if (inst->codec == V4L2_PIX_FMT_HEVC)
544 		return hfi_buffer_line_h265d(width, height, is_opb, num_vpp_pipes);
545 	else if (inst->codec == V4L2_PIX_FMT_VP9)
546 		return hfi_buffer_line_vp9d(width, height, out_min_count, is_opb,
547 			num_vpp_pipes);
548 
549 	return 0;
550 }
551 
552 static u32 iris_vpu_dec_scratch1_size(struct iris_inst *inst)
553 {
554 	return iris_vpu_dec_comv_size(inst) +
555 		iris_vpu_dec_non_comv_size(inst) +
556 		iris_vpu_dec_line_size(inst);
557 }
558 
559 static inline u32 size_bin_bitstream_enc(u32 width, u32 height,
560 					 u32 rc_type)
561 {
562 	u32 aligned_height = ALIGN(height, 32);
563 	u32 aligned_width = ALIGN(width, 32);
564 	u32 frame_size = width * height * 3;
565 	u32 mbs_per_frame;
566 
567 	/*
568 	 * Encoder output size calculation: 32 Align width/height
569 	 * For resolution < 720p : YUVsize * 4
570 	 * For resolution > 720p & <= 4K : YUVsize / 2
571 	 * For resolution > 4k : YUVsize / 4
572 	 * Initially frame_size = YUVsize * 2;
573 	 */
574 
575 	mbs_per_frame = (ALIGN(aligned_height, 16) * ALIGN(aligned_width, 16)) / 256;
576 
577 	if (mbs_per_frame < NUM_MBS_720P)
578 		frame_size = frame_size << 1;
579 	else if (mbs_per_frame <= NUM_MBS_4K)
580 		frame_size = frame_size >> 2;
581 	else
582 		frame_size = frame_size >> 3;
583 
584 	if (rc_type == HFI_RATE_CONTROL_OFF || rc_type == HFI_RATE_CONTROL_CQ ||
585 	    rc_type == HFI_RC_OFF || rc_type == HFI_RC_CQ)
586 		frame_size = frame_size << 1;
587 
588 	/*
589 	 * In case of opaque color format bitdepth will be known
590 	 * with first ETB, buffers allocated already with 8 bit
591 	 * won't be sufficient for 10 bit
592 	 * calculate size considering 10-bit by default
593 	 * For 10-bit cases size = size * 1.25
594 	 */
595 	frame_size *= 5;
596 	frame_size /= 4;
597 
598 	return ALIGN(frame_size, SZ_4K);
599 }
600 
601 static inline u32 hfi_buffer_bin_enc(u32 width, u32 height,
602 				     u32 work_mode, u32 lcu_size,
603 				     u32 num_vpp_pipes, u32 rc_type)
604 {
605 	u32 sao_bin_buffer_size, padded_bin_size, bitstream_size;
606 	u32 total_bitbin_buffers, size_single_pipe, bitbin_size;
607 	u32 aligned_height = ALIGN(height, lcu_size);
608 	u32 aligned_width = ALIGN(width, lcu_size);
609 
610 	bitstream_size = size_bin_bitstream_enc(width, height, rc_type);
611 	bitstream_size = ALIGN(bitstream_size, 256);
612 
613 	if (work_mode == STAGE_2) {
614 		total_bitbin_buffers = 3;
615 		bitbin_size = bitstream_size * 17 / 10;
616 		bitbin_size = ALIGN(bitbin_size, 256);
617 	} else {
618 		total_bitbin_buffers = 1;
619 		bitstream_size = aligned_width * aligned_height * 3;
620 		bitbin_size = ALIGN(bitstream_size, 256);
621 	}
622 
623 	if (num_vpp_pipes > 2)
624 		size_single_pipe = bitbin_size / 2;
625 	else
626 		size_single_pipe = bitbin_size;
627 
628 	size_single_pipe = ALIGN(size_single_pipe, 256);
629 	sao_bin_buffer_size = (64 * (((width + 32) * (height + 32)) >> 10)) + 384;
630 	padded_bin_size = ALIGN(size_single_pipe, 256);
631 	size_single_pipe = sao_bin_buffer_size + padded_bin_size;
632 	size_single_pipe = ALIGN(size_single_pipe, 256);
633 	bitbin_size = size_single_pipe * num_vpp_pipes;
634 
635 	return ALIGN(bitbin_size, 256) * total_bitbin_buffers + 512;
636 }
637 
638 static u32 iris_vpu_enc_bin_size(struct iris_inst *inst)
639 {
640 	u32 num_vpp_pipes = inst->core->iris_platform_data->num_vpp_pipe;
641 	u32 stage = inst->fw_caps[STAGE].value;
642 	struct v4l2_format *f = inst->fmt_dst;
643 	u32 height = f->fmt.pix_mp.height;
644 	u32 width = f->fmt.pix_mp.width;
645 	u32 lcu_size;
646 
647 	if (inst->codec == V4L2_PIX_FMT_HEVC)
648 		lcu_size = 32;
649 	else
650 		lcu_size = 16;
651 
652 	return hfi_buffer_bin_enc(width, height, stage, lcu_size,
653 				  num_vpp_pipes, inst->hfi_rc_type);
654 }
655 
656 static inline
657 u32 hfi_buffer_comv_enc(u32 frame_width, u32 frame_height, u32 lcu_size,
658 			u32 num_recon, u32 standard)
659 {
660 	u32 height_in_lcus = ((frame_height) + (lcu_size) - 1) / (lcu_size);
661 	u32 width_in_lcus = ((frame_width) + (lcu_size) - 1) / (lcu_size);
662 	u32 num_lcu_in_frame = width_in_lcus * height_in_lcus;
663 	u32 mb_height = ((frame_height) + 15) >> 4;
664 	u32 mb_width = ((frame_width) + 15) >> 4;
665 	u32 size_colloc_mv, size_colloc_rc;
666 
667 	size_colloc_mv = (standard == HFI_CODEC_ENCODE_HEVC) ?
668 		(16 * ((num_lcu_in_frame << 2) + 32)) :
669 		(3 * 16 * (width_in_lcus * height_in_lcus + 32));
670 	size_colloc_mv = ALIGN(size_colloc_mv, 256) * num_recon;
671 	size_colloc_rc = (((mb_width + 7) >> 3) * 16 * 2 * mb_height);
672 	size_colloc_rc = ALIGN(size_colloc_rc, 256) * HFI_MAX_COL_FRAME;
673 
674 	return size_colloc_mv + size_colloc_rc;
675 }
676 
677 static u32 iris_vpu_enc_comv_size(struct iris_inst *inst)
678 {
679 	struct v4l2_format *f = inst->fmt_dst;
680 	u32 height = f->fmt.pix_mp.height;
681 	u32 width = f->fmt.pix_mp.width;
682 	u32 num_recon = 1;
683 	u32 lcu_size = 16;
684 
685 	if (inst->codec == V4L2_PIX_FMT_HEVC) {
686 		lcu_size = 32;
687 		return hfi_buffer_comv_enc(width, height, lcu_size,
688 					   num_recon + 1, HFI_CODEC_ENCODE_HEVC);
689 	}
690 
691 	return hfi_buffer_comv_enc(width, height, lcu_size,
692 				   num_recon + 1, HFI_CODEC_ENCODE_AVC);
693 }
694 
695 static inline
696 u32 size_frame_rc_buf_size(u32 standard, u32 frame_height_coded,
697 			   u32 num_vpp_pipes_enc)
698 {
699 	u32 size = 0;
700 
701 	size = (standard == HFI_CODEC_ENCODE_HEVC) ?
702 		(256 + 16 * (14 + ((((frame_height_coded) >> 5) + 7) >> 3))) :
703 		(256 + 16 * (14 + ((((frame_height_coded) >> 4) + 7) >> 3)));
704 	size *= 11;
705 
706 	if (num_vpp_pipes_enc > 1)
707 		size = ALIGN(size, 256) * num_vpp_pipes_enc;
708 
709 	return ALIGN(size, 512) * HFI_MAX_COL_FRAME;
710 }
711 
712 static inline
713 u32 size_enc_slice_info_buf(u32 num_lcu_in_frame)
714 {
715 	return ALIGN((256 + (num_lcu_in_frame << 4)), 256);
716 }
717 
718 static inline u32 enc_bitcnt_buf_size(u32 num_lcu_in_frame)
719 {
720 	return ALIGN((256 + (4 * (num_lcu_in_frame))), 256);
721 }
722 
723 static inline u32 enc_bitmap_buf_size(u32 num_lcu_in_frame)
724 {
725 	return ALIGN((256 + ((num_lcu_in_frame) >> 3)), 256);
726 }
727 
728 static inline u32 size_override_buf(u32 num_lcumb)
729 {
730 	return ALIGN(((16 * (((num_lcumb) + 7) >> 3))), 256) * 2;
731 }
732 
733 static inline u32 size_ir_buf(u32 num_lcu_in_frame)
734 {
735 	return ALIGN((((((num_lcu_in_frame) << 1) + 7) & (~7)) * 3), 256);
736 }
737 
738 static inline
739 u32 size_linebuff_data(bool is_ten_bit, u32 frame_width_coded)
740 {
741 	return is_ten_bit ?
742 		(((((10 * (frame_width_coded) + 1024) + (256 - 1)) &
743 		   (~(256 - 1))) * 1) +
744 		 (((((10 * (frame_width_coded) + 1024) >> 1) + (256 - 1)) &
745 		   (~(256 - 1))) * 2)) :
746 		(((((8 * (frame_width_coded) + 1024) + (256 - 1)) &
747 		   (~(256 - 1))) * 1) +
748 		 (((((8 * (frame_width_coded) + 1024) >> 1) + (256 - 1)) &
749 		   (~(256 - 1))) * 2));
750 }
751 
752 static inline
753 u32 size_left_linebuff_ctrl(u32 standard, u32 frame_height_coded,
754 			    u32 num_vpp_pipes_enc)
755 {
756 	u32 size = 0;
757 
758 	size = standard == HFI_CODEC_ENCODE_HEVC ?
759 		(((frame_height_coded) +
760 		 (32)) / 32 * 4 * 16) :
761 		(((frame_height_coded) + 15) / 16 * 5 * 16);
762 
763 	if ((num_vpp_pipes_enc) > 1) {
764 		size += 512;
765 		size = ALIGN(size, 512) *
766 			num_vpp_pipes_enc;
767 	}
768 
769 	return ALIGN(size, 256);
770 }
771 
772 static inline
773 u32 size_left_linebuff_recon_pix(bool is_ten_bit, u32 frame_height_coded,
774 				 u32 num_vpp_pipes_enc)
775 {
776 	return (((is_ten_bit + 1) * 2 * (frame_height_coded) + 256) +
777 		(256 << (num_vpp_pipes_enc - 1)) - 1) &
778 		(~((256 << (num_vpp_pipes_enc - 1)) - 1)) * 1;
779 }
780 
781 static inline
782 u32 size_top_linebuff_ctrl_fe(u32 frame_width_coded, u32 standard)
783 {
784 	return standard == HFI_CODEC_ENCODE_HEVC ?
785 		ALIGN((64 * ((frame_width_coded) >> 5)), 256) :
786 		ALIGN((256 + 16 * ((frame_width_coded) >> 4)), 256);
787 }
788 
789 static inline
790 u32 size_left_linebuff_ctrl_fe(u32 frame_height_coded, u32 num_vpp_pipes_enc)
791 {
792 	return (((256 + 64 * ((frame_height_coded) >> 4)) +
793 		 (256 << (num_vpp_pipes_enc - 1)) - 1) &
794 		 (~((256 << (num_vpp_pipes_enc - 1)) - 1)) * 1) *
795 		num_vpp_pipes_enc;
796 }
797 
798 static inline
799 u32 size_left_linebuff_metadata_recon_y(u32 frame_height_coded,
800 					bool is_ten_bit,
801 					u32 num_vpp_pipes_enc)
802 {
803 	return ALIGN(((256 + 64 * ((frame_height_coded) /
804 		  (8 * (is_ten_bit ? 4 : 8))))), 256) * num_vpp_pipes_enc;
805 }
806 
807 static inline
808 u32 size_left_linebuff_metadata_recon_uv(u32 frame_height_coded,
809 					 bool is_ten_bit,
810 					 u32 num_vpp_pipes_enc)
811 {
812 	return ALIGN(((256 + 64 * ((frame_height_coded) /
813 		  (4 * (is_ten_bit ? 4 : 8))))), 256) * num_vpp_pipes_enc;
814 }
815 
816 static inline
817 u32 size_linebuff_recon_pix(bool is_ten_bit, u32 frame_width_coded)
818 {
819 	return ALIGN(((is_ten_bit ? 3 : 2) * (frame_width_coded)), 256);
820 }
821 
822 static inline
823 u32 size_line_buf_ctrl(u32 frame_width_coded)
824 {
825 	return ALIGN(frame_width_coded, 256);
826 }
827 
828 static inline
829 u32 size_line_buf_ctrl_id2(u32 frame_width_coded)
830 {
831 	return ALIGN(frame_width_coded, 256);
832 }
833 
834 static inline u32 size_line_buf_sde(u32 frame_width_coded)
835 {
836 	return ALIGN((256 + (16 * ((frame_width_coded) >> 4))), 256);
837 }
838 
839 static inline
840 u32 size_vpss_line_buf(u32 num_vpp_pipes_enc, u32 frame_height_coded,
841 		       u32 frame_width_coded)
842 {
843 	return ALIGN(((((((8192) >> 2) << 5) * (num_vpp_pipes_enc)) + 64) +
844 		      (((((max_t(u32, (frame_width_coded),
845 				 (frame_height_coded)) + 3) >> 2) << 5) + 256) * 16)), 256);
846 }
847 
848 static inline
849 u32 size_top_line_buf_first_stg_sao(u32 frame_width_coded)
850 {
851 	return ALIGN((16 * ((frame_width_coded) >> 5)), 256);
852 }
853 
854 static inline
855 u32 size_enc_ref_buffer(u32 frame_width, u32 frame_height)
856 {
857 	u32 u_chroma_buffer_height = ALIGN(frame_height >> 1, 32);
858 	u32 u_buffer_height = ALIGN(frame_height, 32);
859 	u32 u_buffer_width = ALIGN(frame_width, 32);
860 
861 	return (u_buffer_height + u_chroma_buffer_height) * u_buffer_width;
862 }
863 
864 static inline
865 u32 size_enc_ten_bit_ref_buffer(u32 frame_width, u32 frame_height)
866 {
867 	u32 ref_luma_stride_in_bytes = ((frame_width + SYSTEM_LAL_TILE10 - 1) / SYSTEM_LAL_TILE10) *
868 		SYSTEM_LAL_TILE10;
869 	u32 ref_buf_height = (frame_height + (32 - 1)) & (~(32 - 1));
870 	u32 u_ref_stride, luma_size;
871 	u32 ref_chrm_height_in_bytes;
872 	u32 chroma_size;
873 
874 	u_ref_stride = 4 * (ref_luma_stride_in_bytes / 3);
875 	u_ref_stride = (u_ref_stride + (128 - 1)) & (~(128 - 1));
876 	luma_size = ref_buf_height * u_ref_stride;
877 	luma_size = (luma_size + (4096 - 1)) & (~(4096 - 1));
878 
879 	ref_chrm_height_in_bytes = (((frame_height + 1) >> 1) + (32 - 1)) & (~(32 - 1));
880 	chroma_size = u_ref_stride * ref_chrm_height_in_bytes;
881 	chroma_size = (chroma_size + (4096 - 1)) & (~(4096 - 1));
882 
883 	return luma_size + chroma_size;
884 }
885 
886 static inline
887 u32 hfi_ubwc_calc_metadata_plane_stride(u32 frame_width,
888 					u32 metadata_stride_multiple,
889 					u32 tile_width_in_pels)
890 {
891 	return ALIGN(((frame_width + (tile_width_in_pels - 1)) / tile_width_in_pels),
892 		     metadata_stride_multiple);
893 }
894 
895 static inline
896 u32 hfi_ubwc_metadata_plane_bufheight(u32 frame_height,
897 				      u32 metadata_height_multiple,
898 				      u32 tile_height_in_pels)
899 {
900 	return ALIGN(((frame_height + (tile_height_in_pels - 1)) / tile_height_in_pels),
901 		     metadata_height_multiple);
902 }
903 
904 static inline
905 u32 hfi_ubwc_metadata_plane_buffer_size(u32 _metadata_tride, u32 _metadata_buf_height)
906 {
907 	return ALIGN(_metadata_tride * _metadata_buf_height, 4096);
908 }
909 
910 static inline
911 u32 hfi_buffer_non_comv_enc(u32 frame_width, u32 frame_height,
912 			    u32 num_vpp_pipes_enc, u32 lcu_size, u32 standard)
913 {
914 	u32 height_in_lcus = ((frame_height) + (lcu_size) - 1) / (lcu_size);
915 	u32 width_in_lcus = ((frame_width) + (lcu_size) - 1) / (lcu_size);
916 	u32 num_lcu_in_frame = width_in_lcus * height_in_lcus;
917 	u32 frame_height_coded = height_in_lcus * (lcu_size);
918 	u32 frame_width_coded = width_in_lcus * (lcu_size);
919 	u32 num_lcumb, frame_rc_buf_size;
920 
921 	num_lcumb = (frame_height_coded / lcu_size) *
922 		((frame_width_coded + lcu_size * 8) / lcu_size);
923 	frame_rc_buf_size = size_frame_rc_buf_size(standard, frame_height_coded,
924 						   num_vpp_pipes_enc);
925 	return size_enc_slice_info_buf(num_lcu_in_frame) +
926 		SIZE_SLICE_CMD_BUFFER +
927 		SIZE_SPS_PPS_SLICE_HDR +
928 		frame_rc_buf_size +
929 		enc_bitcnt_buf_size(num_lcu_in_frame) +
930 		enc_bitmap_buf_size(num_lcu_in_frame) +
931 		SIZE_BSE_SLICE_CMD_BUF +
932 		SIZE_LAMBDA_LUT +
933 		size_override_buf(num_lcumb) +
934 		size_ir_buf(num_lcu_in_frame);
935 }
936 
937 static u32 iris_vpu_enc_non_comv_size(struct iris_inst *inst)
938 {
939 	u32 num_vpp_pipes = inst->core->iris_platform_data->num_vpp_pipe;
940 	struct v4l2_format *f = inst->fmt_dst;
941 	u32 height = f->fmt.pix_mp.height;
942 	u32 width = f->fmt.pix_mp.width;
943 	u32 lcu_size = 16;
944 
945 	if (inst->codec == V4L2_PIX_FMT_HEVC) {
946 		lcu_size = 32;
947 		return hfi_buffer_non_comv_enc(width, height, num_vpp_pipes,
948 					       lcu_size, HFI_CODEC_ENCODE_HEVC) +
949 					       SIZE_ONE_SLICE_BUF;
950 	}
951 
952 	return hfi_buffer_non_comv_enc(width, height, num_vpp_pipes,
953 				       lcu_size, HFI_CODEC_ENCODE_AVC);
954 }
955 
956 static inline
957 u32 hfi_buffer_line_enc(u32 frame_width, u32 frame_height, bool is_ten_bit,
958 			u32 num_vpp_pipes_enc, u32 lcu_size, u32 standard)
959 {
960 	u32 width_in_lcus = ((frame_width) + (lcu_size) - 1) / (lcu_size);
961 	u32 height_in_lcus = ((frame_height) + (lcu_size) - 1) / (lcu_size);
962 	u32 frame_height_coded = height_in_lcus * (lcu_size);
963 	u32 frame_width_coded = width_in_lcus * (lcu_size);
964 	u32 line_buff_data_size, left_line_buff_ctrl_size;
965 	u32 left_line_buff_metadata_recon__uv__size;
966 	u32 left_line_buff_metadata_recon__y__size;
967 	u32 left_line_buff_recon_pix_size;
968 	u32 top_line_buff_ctrl_fe_size;
969 	u32 line_buff_recon_pix_size;
970 
971 	line_buff_data_size = size_linebuff_data(is_ten_bit, frame_width_coded);
972 	left_line_buff_ctrl_size =
973 		size_left_linebuff_ctrl(standard, frame_height_coded, num_vpp_pipes_enc);
974 	left_line_buff_recon_pix_size =
975 		size_left_linebuff_recon_pix(is_ten_bit, frame_height_coded,
976 					     num_vpp_pipes_enc);
977 	top_line_buff_ctrl_fe_size =
978 		size_top_linebuff_ctrl_fe(frame_width_coded, standard);
979 	left_line_buff_metadata_recon__y__size =
980 		size_left_linebuff_metadata_recon_y(frame_height_coded, is_ten_bit,
981 						    num_vpp_pipes_enc);
982 	left_line_buff_metadata_recon__uv__size =
983 		size_left_linebuff_metadata_recon_uv(frame_height_coded, is_ten_bit,
984 						     num_vpp_pipes_enc);
985 	line_buff_recon_pix_size = size_linebuff_recon_pix(is_ten_bit, frame_width_coded);
986 
987 	return size_line_buf_ctrl(frame_width_coded) +
988 		size_line_buf_ctrl_id2(frame_width_coded) +
989 		line_buff_data_size +
990 		left_line_buff_ctrl_size +
991 		left_line_buff_recon_pix_size +
992 		top_line_buff_ctrl_fe_size +
993 		left_line_buff_metadata_recon__y__size +
994 		left_line_buff_metadata_recon__uv__size +
995 		line_buff_recon_pix_size +
996 		size_left_linebuff_ctrl_fe(frame_height_coded, num_vpp_pipes_enc) +
997 		size_line_buf_sde(frame_width_coded) +
998 		size_vpss_line_buf(num_vpp_pipes_enc, frame_height_coded, frame_width_coded) +
999 		size_top_line_buf_first_stg_sao(frame_width_coded);
1000 }
1001 
1002 static u32 iris_vpu_enc_line_size(struct iris_inst *inst)
1003 {
1004 	u32 num_vpp_pipes = inst->core->iris_platform_data->num_vpp_pipe;
1005 	struct v4l2_format *f = inst->fmt_dst;
1006 	u32 height = f->fmt.pix_mp.height;
1007 	u32 width = f->fmt.pix_mp.width;
1008 	u32 lcu_size = 16;
1009 
1010 	if (inst->codec == V4L2_PIX_FMT_HEVC) {
1011 		lcu_size = 32;
1012 		return hfi_buffer_line_enc(width, height, 0, num_vpp_pipes,
1013 					   lcu_size, HFI_CODEC_ENCODE_HEVC);
1014 	}
1015 
1016 	return hfi_buffer_line_enc(width, height, 0, num_vpp_pipes,
1017 				   lcu_size, HFI_CODEC_ENCODE_AVC);
1018 }
1019 
1020 static inline
1021 u32 hfi_buffer_dpb_enc(u32 frame_width, u32 frame_height, bool is_ten_bit)
1022 {
1023 	u32 metadata_stride, metadata_buf_height, meta_size_y, meta_size_c;
1024 	u32 ten_bit_ref_buf_size = 0, ref_buf_size = 0;
1025 	u32 size;
1026 
1027 	if (!is_ten_bit) {
1028 		ref_buf_size = size_enc_ref_buffer(frame_width, frame_height);
1029 		metadata_stride =
1030 			hfi_ubwc_calc_metadata_plane_stride(frame_width, 64,
1031 							    HFI_COL_FMT_NV12C_Y_TILE_WIDTH);
1032 		metadata_buf_height =
1033 			hfi_ubwc_metadata_plane_bufheight(frame_height, 16,
1034 							  HFI_COL_FMT_NV12C_Y_TILE_HEIGHT);
1035 		meta_size_y =
1036 			hfi_ubwc_metadata_plane_buffer_size(metadata_stride, metadata_buf_height);
1037 		meta_size_c =
1038 			hfi_ubwc_metadata_plane_buffer_size(metadata_stride, metadata_buf_height);
1039 		size = ref_buf_size + meta_size_y + meta_size_c;
1040 	} else {
1041 		ten_bit_ref_buf_size = size_enc_ten_bit_ref_buffer(frame_width, frame_height);
1042 		metadata_stride =
1043 			hfi_ubwc_calc_metadata_plane_stride(frame_width,
1044 							    IRIS_METADATA_STRIDE_MULTIPLE,
1045 							    HFI_COL_FMT_TP10C_Y_TILE_WIDTH);
1046 		metadata_buf_height =
1047 			hfi_ubwc_metadata_plane_bufheight(frame_height,
1048 							  IRIS_METADATA_HEIGHT_MULTIPLE,
1049 							  HFI_COL_FMT_TP10C_Y_TILE_HEIGHT);
1050 		meta_size_y =
1051 			hfi_ubwc_metadata_plane_buffer_size(metadata_stride, metadata_buf_height);
1052 		meta_size_c =
1053 			hfi_ubwc_metadata_plane_buffer_size(metadata_stride, metadata_buf_height);
1054 		size = ten_bit_ref_buf_size + meta_size_y + meta_size_c;
1055 	}
1056 
1057 	return size;
1058 }
1059 
1060 static u32 iris_vpu_enc_arp_size(struct iris_inst *inst)
1061 {
1062 	return HFI_BUFFER_ARP_ENC;
1063 }
1064 
1065 inline bool is_scaling_enabled(struct iris_inst *inst)
1066 {
1067 	return inst->crop.left != inst->compose.left ||
1068 		inst->crop.top != inst->compose.top ||
1069 		inst->crop.width != inst->compose.width ||
1070 		inst->crop.height != inst->compose.height;
1071 }
1072 
1073 static inline
1074 u32 hfi_buffer_vpss_enc(u32 dswidth, u32 dsheight, bool ds_enable,
1075 			u32 blur, bool is_ten_bit)
1076 {
1077 	if (ds_enable || blur)
1078 		return hfi_buffer_dpb_enc(dswidth, dsheight, is_ten_bit);
1079 
1080 	return 0;
1081 }
1082 
1083 static inline u32 hfi_buffer_scratch1_enc(u32 frame_width, u32 frame_height,
1084 					  u32 lcu_size, u32 num_ref,
1085 					  bool ten_bit, u32 num_vpp_pipes,
1086 					  bool is_h265)
1087 {
1088 	u32 line_buf_ctrl_size, line_buf_data_size, leftline_buf_ctrl_size;
1089 	u32 line_buf_sde_size, sps_pps_slice_hdr, topline_buf_ctrl_size_FE;
1090 	u32 leftline_buf_ctrl_size_FE, line_buf_recon_pix_size;
1091 	u32 leftline_buf_recon_pix_size, lambda_lut_size, override_buffer_size;
1092 	u32 col_mv_buf_size, vpp_reg_buffer_size, ir_buffer_size;
1093 	u32 vpss_line_buf, leftline_buf_meta_recony, h265e_colrcbuf_size;
1094 	u32 h265e_framerc_bufsize, h265e_lcubitcnt_bufsize;
1095 	u32 h265e_lcubitmap_bufsize, se_stats_bufsize;
1096 	u32 bse_reg_buffer_size, bse_slice_cmd_buffer_size, slice_info_bufsize;
1097 	u32 line_buf_ctrl_size_buffid2, slice_cmd_buffer_size;
1098 	u32 width_lcu_num, height_lcu_num, width_coded, height_coded;
1099 	u32 frame_num_lcu, linebuf_meta_recon_uv, topline_bufsize_fe_1stg_sao;
1100 	u32 vpss_line_buffer_size_1;
1101 	u32 bit_depth, num_lcu_mb;
1102 
1103 	width_lcu_num = (frame_width + lcu_size - 1) / lcu_size;
1104 	height_lcu_num = (frame_height + lcu_size - 1) / lcu_size;
1105 	frame_num_lcu = width_lcu_num * height_lcu_num;
1106 	width_coded = width_lcu_num * lcu_size;
1107 	height_coded = height_lcu_num * lcu_size;
1108 	num_lcu_mb = (height_coded / lcu_size) *
1109 		     ((width_coded + lcu_size * 8) / lcu_size);
1110 	slice_info_bufsize = 256 + (frame_num_lcu << 4);
1111 	slice_info_bufsize = ALIGN(slice_info_bufsize, 256);
1112 	line_buf_ctrl_size = ALIGN(width_coded, 256);
1113 	line_buf_ctrl_size_buffid2 = ALIGN(width_coded, 256);
1114 
1115 	bit_depth = ten_bit ? 10 : 8;
1116 	line_buf_data_size =
1117 		(((((bit_depth * width_coded + 1024) + (256 - 1)) &
1118 		   (~(256 - 1))) * 1) +
1119 		 (((((bit_depth * width_coded + 1024) >> 1) + (256 - 1)) &
1120 		   (~(256 - 1))) * 2));
1121 
1122 	leftline_buf_ctrl_size = is_h265 ? ((height_coded + 32) / 32 * 4 * 16) :
1123 					   ((height_coded + 15) / 16 * 5 * 16);
1124 
1125 	if (num_vpp_pipes > 1) {
1126 		leftline_buf_ctrl_size += 512;
1127 		leftline_buf_ctrl_size =
1128 			ALIGN(leftline_buf_ctrl_size, 512) * num_vpp_pipes;
1129 	}
1130 
1131 	leftline_buf_ctrl_size = ALIGN(leftline_buf_ctrl_size, 256);
1132 	leftline_buf_recon_pix_size =
1133 		(((ten_bit + 1) * 2 * (height_coded) + 256) +
1134 		 (256 << (num_vpp_pipes - 1)) - 1) &
1135 		(~((256 << (num_vpp_pipes - 1)) - 1)) * 1;
1136 
1137 	topline_buf_ctrl_size_FE = is_h265 ? (64 * (width_coded >> 5)) :
1138 					     (256 + 16 * (width_coded >> 4));
1139 	topline_buf_ctrl_size_FE = ALIGN(topline_buf_ctrl_size_FE, 256);
1140 	leftline_buf_ctrl_size_FE =
1141 		(((256 + 64 * (height_coded >> 4)) +
1142 		  (256 << (num_vpp_pipes - 1)) - 1) &
1143 		 (~((256 << (num_vpp_pipes - 1)) - 1)) * 1) *
1144 		num_vpp_pipes;
1145 	leftline_buf_meta_recony =
1146 		(256 + 64 * ((height_coded) / (8 * (ten_bit ? 4 : 8))));
1147 	leftline_buf_meta_recony = ALIGN(leftline_buf_meta_recony, 256);
1148 	leftline_buf_meta_recony = leftline_buf_meta_recony * num_vpp_pipes;
1149 	linebuf_meta_recon_uv =
1150 		(256 + 64 * ((height_coded) / (4 * (ten_bit ? 4 : 8))));
1151 	linebuf_meta_recon_uv = ALIGN(linebuf_meta_recon_uv, 256);
1152 	linebuf_meta_recon_uv = linebuf_meta_recon_uv * num_vpp_pipes;
1153 	line_buf_recon_pix_size = ((ten_bit ? 3 : 2) * width_coded);
1154 	line_buf_recon_pix_size = ALIGN(line_buf_recon_pix_size, 256);
1155 	slice_cmd_buffer_size = ALIGN(20480, 256);
1156 	sps_pps_slice_hdr = 2048 + 4096;
1157 	col_mv_buf_size =
1158 		is_h265 ? (16 * ((frame_num_lcu << 2) + 32)) :
1159 			  (3 * 16 * (width_lcu_num * height_lcu_num + 32));
1160 	col_mv_buf_size = ALIGN(col_mv_buf_size, 256) * (num_ref + 1);
1161 	h265e_colrcbuf_size =
1162 		(((width_lcu_num + 7) >> 3) * 16 * 2 * height_lcu_num);
1163 	if (num_vpp_pipes > 1)
1164 		h265e_colrcbuf_size =
1165 			ALIGN(h265e_colrcbuf_size, 256) * num_vpp_pipes;
1166 
1167 	h265e_colrcbuf_size =
1168 		ALIGN(h265e_colrcbuf_size, 256) * HFI_MAX_COL_FRAME;
1169 	h265e_framerc_bufsize =
1170 		(is_h265) ?
1171 			(256 + 16 * (14 + (((height_coded >> 5) + 7) >> 3))) :
1172 			(256 + 16 * (14 + (((height_coded >> 4) + 7) >> 3)));
1173 	h265e_framerc_bufsize *= 6;
1174 	if (num_vpp_pipes > 1)
1175 		h265e_framerc_bufsize =
1176 			ALIGN(h265e_framerc_bufsize, 256) * num_vpp_pipes;
1177 
1178 	h265e_framerc_bufsize =
1179 		ALIGN(h265e_framerc_bufsize, 512) * HFI_MAX_COL_FRAME;
1180 	h265e_lcubitcnt_bufsize = 256 + 4 * frame_num_lcu;
1181 	h265e_lcubitcnt_bufsize = ALIGN(h265e_lcubitcnt_bufsize, 256);
1182 	h265e_lcubitmap_bufsize = 256 + (frame_num_lcu >> 3);
1183 	h265e_lcubitmap_bufsize = ALIGN(h265e_lcubitmap_bufsize, 256);
1184 	line_buf_sde_size = 256 + 16 * (width_coded >> 4);
1185 	line_buf_sde_size = ALIGN(line_buf_sde_size, 256);
1186 	if ((width_coded * height_coded) > (4096 * 2160))
1187 		se_stats_bufsize = 0;
1188 	else if ((width_coded * height_coded) > (1920 * 1088))
1189 		se_stats_bufsize = (40 * 4 * frame_num_lcu + 256 + 256);
1190 	else
1191 		se_stats_bufsize = (1024 * frame_num_lcu + 256 + 256);
1192 
1193 	se_stats_bufsize = ALIGN(se_stats_bufsize, 256) * 2;
1194 	bse_slice_cmd_buffer_size = (((8192 << 2) + 7) & (~7)) * 6;
1195 	bse_reg_buffer_size = (((512 << 3) + 7) & (~7)) * 4;
1196 	vpp_reg_buffer_size = (((2048 << 3) + 31) & (~31)) * 10;
1197 	lambda_lut_size = 256 * 11;
1198 	override_buffer_size = 16 * ((num_lcu_mb + 7) >> 3);
1199 	override_buffer_size = ALIGN(override_buffer_size, 256) * 2;
1200 	ir_buffer_size = (((frame_num_lcu << 1) + 7) & (~7)) * 3;
1201 	vpss_line_buffer_size_1 = (((8192 >> 2) << 5) * num_vpp_pipes) + 64;
1202 	vpss_line_buf =
1203 		(((((max(width_coded, height_coded) + 3) >> 2) << 5) + 256) *
1204 		 16) +
1205 		vpss_line_buffer_size_1;
1206 	topline_bufsize_fe_1stg_sao = 16 * (width_coded >> 5);
1207 	topline_bufsize_fe_1stg_sao = ALIGN(topline_bufsize_fe_1stg_sao, 256);
1208 
1209 	return line_buf_ctrl_size + line_buf_data_size +
1210 	       line_buf_ctrl_size_buffid2 + leftline_buf_ctrl_size +
1211 	       vpss_line_buf + col_mv_buf_size + topline_buf_ctrl_size_FE +
1212 	       leftline_buf_ctrl_size_FE + line_buf_recon_pix_size +
1213 	       leftline_buf_recon_pix_size + leftline_buf_meta_recony +
1214 	       linebuf_meta_recon_uv + h265e_colrcbuf_size +
1215 	       h265e_framerc_bufsize + h265e_lcubitcnt_bufsize +
1216 	       h265e_lcubitmap_bufsize + line_buf_sde_size +
1217 	       topline_bufsize_fe_1stg_sao + override_buffer_size +
1218 	       bse_reg_buffer_size + vpp_reg_buffer_size + sps_pps_slice_hdr +
1219 	       slice_cmd_buffer_size + bse_slice_cmd_buffer_size +
1220 	       ir_buffer_size + slice_info_bufsize + lambda_lut_size +
1221 	       se_stats_bufsize + 1024;
1222 }
1223 
1224 static u32 iris_vpu_enc_scratch1_size(struct iris_inst *inst)
1225 {
1226 	u32 num_vpp_pipes = inst->core->iris_platform_data->num_vpp_pipe;
1227 	struct v4l2_format *f = inst->fmt_dst;
1228 	u32 frame_height = f->fmt.pix_mp.height;
1229 	u32 frame_width = f->fmt.pix_mp.width;
1230 	u32 num_ref = 1;
1231 	u32 lcu_size;
1232 	bool is_h265;
1233 
1234 	if (inst->codec == V4L2_PIX_FMT_H264) {
1235 		lcu_size = 16;
1236 		is_h265 = false;
1237 	} else if (inst->codec == V4L2_PIX_FMT_HEVC) {
1238 		lcu_size = 32;
1239 		is_h265 = true;
1240 	} else {
1241 		return 0;
1242 	}
1243 
1244 	return hfi_buffer_scratch1_enc(frame_width, frame_height, lcu_size,
1245 				       num_ref, false, num_vpp_pipes, is_h265);
1246 }
1247 
1248 static inline u32 ubwc_metadata_plane_stride(u32 width,
1249 					     u32 metadata_stride_multi,
1250 					     u32 tile_width_pels)
1251 {
1252 	return ALIGN(((width + (tile_width_pels - 1)) / tile_width_pels),
1253 		     metadata_stride_multi);
1254 }
1255 
1256 static inline u32 ubwc_metadata_plane_bufheight(u32 height,
1257 						u32 metadata_height_multi,
1258 						u32 tile_height_pels)
1259 {
1260 	return ALIGN(((height + (tile_height_pels - 1)) / tile_height_pels),
1261 		     metadata_height_multi);
1262 }
1263 
1264 static inline u32 ubwc_metadata_plane_buffer_size(u32 metadata_stride,
1265 						  u32 metadata_buf_height)
1266 {
1267 	return ALIGN(metadata_stride * metadata_buf_height, SZ_4K);
1268 }
1269 
1270 static inline u32 hfi_buffer_scratch2_enc(u32 frame_width, u32 frame_height,
1271 					  u32 num_ref, bool ten_bit)
1272 {
1273 	u32 aligned_width, aligned_height, chroma_height, ref_buf_height;
1274 	u32 metadata_stride, meta_buf_height, meta_size_y, meta_size_c;
1275 	u32 ref_luma_stride_bytes, ref_chroma_height_bytes;
1276 	u32 ref_buf_size, ref_stride;
1277 	u32 luma_size, chroma_size;
1278 	u32 size;
1279 
1280 	if (!ten_bit) {
1281 		aligned_height = ALIGN(frame_height, 32);
1282 		chroma_height = frame_height >> 1;
1283 		chroma_height = ALIGN(chroma_height, 32);
1284 		aligned_width = ALIGN(frame_width, 128);
1285 		metadata_stride =
1286 			ubwc_metadata_plane_stride(frame_width, 64, 32);
1287 		meta_buf_height =
1288 			ubwc_metadata_plane_bufheight(frame_height, 16, 8);
1289 		meta_size_y = ubwc_metadata_plane_buffer_size(metadata_stride,
1290 							      meta_buf_height);
1291 		meta_size_c = ubwc_metadata_plane_buffer_size(metadata_stride,
1292 							      meta_buf_height);
1293 		size = (aligned_height + chroma_height) * aligned_width +
1294 		       meta_size_y + meta_size_c;
1295 		size = (size * (num_ref + 3)) + 4096;
1296 	} else {
1297 		ref_buf_height = (frame_height + (32 - 1)) & (~(32 - 1));
1298 		ref_luma_stride_bytes = ((frame_width + 192 - 1) / 192) * 192;
1299 		ref_stride = 4 * (ref_luma_stride_bytes / 3);
1300 		ref_stride = (ref_stride + (128 - 1)) & (~(128 - 1));
1301 		luma_size = ref_buf_height * ref_stride;
1302 		ref_chroma_height_bytes =
1303 			(((frame_height + 1) >> 1) + (32 - 1)) & (~(32 - 1));
1304 		chroma_size = ref_stride * ref_chroma_height_bytes;
1305 		luma_size = (luma_size + (SZ_4K - 1)) & (~(SZ_4K - 1));
1306 		chroma_size = (chroma_size + (SZ_4K - 1)) & (~(SZ_4K - 1));
1307 		ref_buf_size = luma_size + chroma_size;
1308 		metadata_stride =
1309 			ubwc_metadata_plane_stride(frame_width, 64, 48);
1310 		meta_buf_height =
1311 			ubwc_metadata_plane_bufheight(frame_height, 16, 4);
1312 		meta_size_y = ubwc_metadata_plane_buffer_size(metadata_stride,
1313 							      meta_buf_height);
1314 		meta_size_c = ubwc_metadata_plane_buffer_size(metadata_stride,
1315 							      meta_buf_height);
1316 		size = ref_buf_size + meta_size_y + meta_size_c;
1317 		size = (size * (num_ref + 3)) + 4096;
1318 	}
1319 
1320 	return size;
1321 }
1322 
1323 static u32 iris_vpu_enc_scratch2_size(struct iris_inst *inst)
1324 {
1325 	struct v4l2_format *f = inst->fmt_dst;
1326 	u32 frame_width = f->fmt.pix_mp.width;
1327 	u32 frame_height = f->fmt.pix_mp.height;
1328 	u32 num_ref = 1;
1329 
1330 	return hfi_buffer_scratch2_enc(frame_width, frame_height, num_ref,
1331 				       false);
1332 }
1333 
1334 static u32 iris_vpu_enc_vpss_size(struct iris_inst *inst)
1335 {
1336 	u32 ds_enable = is_scaling_enabled(inst);
1337 	struct v4l2_format *f = inst->fmt_dst;
1338 	u32 height = f->fmt.pix_mp.height;
1339 	u32 width = f->fmt.pix_mp.width;
1340 
1341 	return hfi_buffer_vpss_enc(width, height, ds_enable, 0, 0);
1342 }
1343 
1344 static int output_min_count(struct iris_inst *inst)
1345 {
1346 	int output_min_count = 4;
1347 
1348 	/* fw_min_count > 0 indicates reconfig event has already arrived */
1349 	if (inst->fw_min_count) {
1350 		if (iris_split_mode_enabled(inst) && inst->codec == V4L2_PIX_FMT_VP9)
1351 			return min_t(u32, 4, inst->fw_min_count);
1352 		else
1353 			return inst->fw_min_count;
1354 	}
1355 
1356 	if (inst->codec == V4L2_PIX_FMT_VP9)
1357 		output_min_count = 9;
1358 
1359 	return output_min_count;
1360 }
1361 
1362 struct iris_vpu_buf_type_handle {
1363 	enum iris_buffer_type type;
1364 	u32 (*handle)(struct iris_inst *inst);
1365 };
1366 
1367 int iris_vpu_buf_size(struct iris_inst *inst, enum iris_buffer_type buffer_type)
1368 {
1369 	const struct iris_vpu_buf_type_handle *buf_type_handle_arr = NULL;
1370 	u32 size = 0, buf_type_handle_size = 0, i;
1371 
1372 	static const struct iris_vpu_buf_type_handle dec_internal_buf_type_handle[] = {
1373 		{BUF_BIN,         iris_vpu_dec_bin_size             },
1374 		{BUF_COMV,        iris_vpu_dec_comv_size            },
1375 		{BUF_NON_COMV,    iris_vpu_dec_non_comv_size        },
1376 		{BUF_LINE,        iris_vpu_dec_line_size            },
1377 		{BUF_PERSIST,     iris_vpu_dec_persist_size         },
1378 		{BUF_DPB,         iris_vpu_dec_dpb_size             },
1379 		{BUF_SCRATCH_1,   iris_vpu_dec_scratch1_size        },
1380 	};
1381 
1382 	static const struct iris_vpu_buf_type_handle enc_internal_buf_type_handle[] = {
1383 		{BUF_BIN,         iris_vpu_enc_bin_size             },
1384 		{BUF_COMV,        iris_vpu_enc_comv_size            },
1385 		{BUF_NON_COMV,    iris_vpu_enc_non_comv_size        },
1386 		{BUF_LINE,        iris_vpu_enc_line_size            },
1387 		{BUF_ARP,         iris_vpu_enc_arp_size             },
1388 		{BUF_VPSS,        iris_vpu_enc_vpss_size            },
1389 		{BUF_SCRATCH_1,   iris_vpu_enc_scratch1_size        },
1390 		{BUF_SCRATCH_2,   iris_vpu_enc_scratch2_size        },
1391 	};
1392 
1393 	if (inst->domain == DECODER) {
1394 		buf_type_handle_size = ARRAY_SIZE(dec_internal_buf_type_handle);
1395 		buf_type_handle_arr = dec_internal_buf_type_handle;
1396 	} else if (inst->domain == ENCODER) {
1397 		buf_type_handle_size = ARRAY_SIZE(enc_internal_buf_type_handle);
1398 		buf_type_handle_arr = enc_internal_buf_type_handle;
1399 	}
1400 
1401 	for (i = 0; i < buf_type_handle_size; i++) {
1402 		if (buf_type_handle_arr[i].type == buffer_type) {
1403 			size = buf_type_handle_arr[i].handle(inst);
1404 			break;
1405 		}
1406 	}
1407 
1408 	return size;
1409 }
1410 
1411 static u32 internal_buffer_count(struct iris_inst *inst,
1412 				 enum iris_buffer_type buffer_type)
1413 {
1414 	if (buffer_type == BUF_BIN || buffer_type == BUF_LINE ||
1415 	    buffer_type == BUF_PERSIST) {
1416 		return 1;
1417 	} else if (buffer_type == BUF_COMV || buffer_type == BUF_NON_COMV) {
1418 		if (inst->codec == V4L2_PIX_FMT_H264 || inst->codec == V4L2_PIX_FMT_HEVC)
1419 			return 1;
1420 	}
1421 	return 0;
1422 }
1423 
1424 static inline int iris_vpu_dpb_count(struct iris_inst *inst)
1425 {
1426 	if (iris_split_mode_enabled(inst)) {
1427 		return inst->fw_min_count ?
1428 			inst->fw_min_count : inst->buffers[BUF_OUTPUT].min_count;
1429 	}
1430 
1431 	return 0;
1432 }
1433 
1434 int iris_vpu_buf_count(struct iris_inst *inst, enum iris_buffer_type buffer_type)
1435 {
1436 	switch (buffer_type) {
1437 	case BUF_INPUT:
1438 		return MIN_BUFFERS;
1439 	case BUF_OUTPUT:
1440 		if (inst->domain == ENCODER)
1441 			return MIN_BUFFERS;
1442 		else
1443 			return output_min_count(inst);
1444 	case BUF_BIN:
1445 	case BUF_COMV:
1446 	case BUF_NON_COMV:
1447 	case BUF_LINE:
1448 	case BUF_PERSIST:
1449 		return internal_buffer_count(inst, buffer_type);
1450 	case BUF_SCRATCH_1:
1451 	case BUF_SCRATCH_2:
1452 	case BUF_VPSS:
1453 	case BUF_ARP:
1454 		return 1; /* internal buffer count needed by firmware is 1 */
1455 	case BUF_DPB:
1456 		return iris_vpu_dpb_count(inst);
1457 	default:
1458 		return 0;
1459 	}
1460 }
1461