xref: /netbsd-src/sys/external/bsd/drm2/dist/drm/amd/display/dc/dml/dcn20/amdgpu_display_rq_dlg_calc_20.c (revision 41ec02673d281bbb3d38e6c78504ce6e30c228c1)
1 /*	$NetBSD: amdgpu_display_rq_dlg_calc_20.c,v 1.2 2021/12/18 23:45:04 riastradh Exp $	*/
2 
3 /*
4  * Copyright 2018 Advanced Micro Devices, Inc.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
20  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22  * OTHER DEALINGS IN THE SOFTWARE.
23  *
24  * Authors: AMD
25  *
26  */
27 
28 #include <sys/cdefs.h>
29 __KERNEL_RCSID(0, "$NetBSD: amdgpu_display_rq_dlg_calc_20.c,v 1.2 2021/12/18 23:45:04 riastradh Exp $");
30 
31 #include "../display_mode_lib.h"
32 #include "../display_mode_vba.h"
33 #include "display_rq_dlg_calc_20.h"
34 
35 // Function: dml20_rq_dlg_get_rq_params
36 //  Calculate requestor related parameters that register definition agnostic
37 //  (i.e. this layer does try to separate real values from register definition)
38 // Input:
39 //  pipe_src_param - pipe source configuration (e.g. vp, pitch, etc.)
40 // Output:
41 //  rq_param - values that can be used to setup RQ (e.g. swath_height, plane1_addr, etc.)
42 //
43 static void dml20_rq_dlg_get_rq_params(
44 		struct display_mode_lib *mode_lib,
45 		display_rq_params_st * rq_param,
46 		const display_pipe_source_params_st pipe_src_param);
47 
48 // Function: dml20_rq_dlg_get_dlg_params
49 //  Calculate deadline related parameters
50 //
51 static void dml20_rq_dlg_get_dlg_params(struct display_mode_lib *mode_lib,
52 		const display_e2e_pipe_params_st *e2e_pipe_param,
53 		const unsigned int num_pipes,
54 		const unsigned int pipe_idx,
55 		display_dlg_regs_st *disp_dlg_regs,
56 		display_ttu_regs_st *disp_ttu_regs,
57 		const display_rq_dlg_params_st rq_dlg_param,
58 		const display_dlg_sys_params_st dlg_sys_param,
59 		const bool cstate_en,
60 		const bool pstate_en);
61 /*
62  * NOTE:
63  *   This file is gcc-parseable HW gospel, coming straight from HW engineers.
64  *
65  * It doesn't adhere to Linux kernel style and sometimes will do things in odd
66  * ways. Unless there is something clearly wrong with it the code should
67  * remain as-is as it provides us with a guarantee from HW that it is correct.
68  */
69 
70 static void calculate_ttu_cursor(struct display_mode_lib *mode_lib,
71 		double *refcyc_per_req_delivery_pre_cur,
72 		double *refcyc_per_req_delivery_cur,
73 		double refclk_freq_in_mhz,
74 		double ref_freq_to_pix_freq,
75 		double hscale_pixel_rate_l,
76 		double hscl_ratio,
77 		double vratio_pre_l,
78 		double vratio_l,
79 		unsigned int cur_width,
80 		enum cursor_bpp cur_bpp);
81 
82 #include "../dml_inline_defs.h"
83 
get_bytes_per_element(enum source_format_class source_format,bool is_chroma)84 static unsigned int get_bytes_per_element(enum source_format_class source_format, bool is_chroma)
85 {
86 	unsigned int ret_val = 0;
87 
88 	if (source_format == dm_444_16) {
89 		if (!is_chroma)
90 			ret_val = 2;
91 	} else if (source_format == dm_444_32) {
92 		if (!is_chroma)
93 			ret_val = 4;
94 	} else if (source_format == dm_444_64) {
95 		if (!is_chroma)
96 			ret_val = 8;
97 	} else if (source_format == dm_420_8) {
98 		if (is_chroma)
99 			ret_val = 2;
100 		else
101 			ret_val = 1;
102 	} else if (source_format == dm_420_10) {
103 		if (is_chroma)
104 			ret_val = 4;
105 		else
106 			ret_val = 2;
107 	} else if (source_format == dm_444_8) {
108 		ret_val = 1;
109 	}
110 	return ret_val;
111 }
112 
is_dual_plane(enum source_format_class source_format)113 static bool is_dual_plane(enum source_format_class source_format)
114 {
115 	bool ret_val = false;
116 
117 	if ((source_format == dm_420_8) || (source_format == dm_420_10))
118 		ret_val = true;
119 
120 	return ret_val;
121 }
122 
get_refcyc_per_delivery(struct display_mode_lib * mode_lib,double refclk_freq_in_mhz,double pclk_freq_in_mhz,bool odm_combine,unsigned int recout_width,unsigned int hactive,double vratio,double hscale_pixel_rate,unsigned int delivery_width,unsigned int req_per_swath_ub)123 static double get_refcyc_per_delivery(struct display_mode_lib *mode_lib,
124 		double refclk_freq_in_mhz,
125 		double pclk_freq_in_mhz,
126 		bool odm_combine,
127 		unsigned int recout_width,
128 		unsigned int hactive,
129 		double vratio,
130 		double hscale_pixel_rate,
131 		unsigned int delivery_width,
132 		unsigned int req_per_swath_ub)
133 {
134 	double refcyc_per_delivery = 0.0;
135 
136 	if (vratio <= 1.0) {
137 		if (odm_combine)
138 			refcyc_per_delivery = (double) refclk_freq_in_mhz
139 					* dml_min((double) recout_width, (double) hactive / 2.0)
140 					/ pclk_freq_in_mhz / (double) req_per_swath_ub;
141 		else
142 			refcyc_per_delivery = (double) refclk_freq_in_mhz * (double) recout_width
143 					/ pclk_freq_in_mhz / (double) req_per_swath_ub;
144 	} else {
145 		refcyc_per_delivery = (double) refclk_freq_in_mhz * (double) delivery_width
146 				/ (double) hscale_pixel_rate / (double) req_per_swath_ub;
147 	}
148 
149 	dml_print("DML_DLG: %s: refclk_freq_in_mhz = %3.2f\n", __func__, refclk_freq_in_mhz);
150 	dml_print("DML_DLG: %s: pclk_freq_in_mhz   = %3.2f\n", __func__, pclk_freq_in_mhz);
151 	dml_print("DML_DLG: %s: recout_width       = %d\n", __func__, recout_width);
152 	dml_print("DML_DLG: %s: vratio             = %3.2f\n", __func__, vratio);
153 	dml_print("DML_DLG: %s: req_per_swath_ub   = %d\n", __func__, req_per_swath_ub);
154 	dml_print("DML_DLG: %s: refcyc_per_delivery= %3.2f\n", __func__, refcyc_per_delivery);
155 
156 	return refcyc_per_delivery;
157 
158 }
159 
get_blk_size_bytes(const enum source_macro_tile_size tile_size)160 static unsigned int get_blk_size_bytes(const enum source_macro_tile_size tile_size)
161 {
162 	if (tile_size == dm_256k_tile)
163 		return (256 * 1024);
164 	else if (tile_size == dm_64k_tile)
165 		return (64 * 1024);
166 	else
167 		return (4 * 1024);
168 }
169 
extract_rq_sizing_regs(struct display_mode_lib * mode_lib,display_data_rq_regs_st * rq_regs,const display_data_rq_sizing_params_st rq_sizing)170 static void extract_rq_sizing_regs(struct display_mode_lib *mode_lib,
171 		display_data_rq_regs_st *rq_regs,
172 		const display_data_rq_sizing_params_st rq_sizing)
173 {
174 	dml_print("DML_DLG: %s: rq_sizing param\n", __func__);
175 	print__data_rq_sizing_params_st(mode_lib, rq_sizing);
176 
177 	rq_regs->chunk_size = dml_log2(rq_sizing.chunk_bytes) - 10;
178 
179 	if (rq_sizing.min_chunk_bytes == 0)
180 		rq_regs->min_chunk_size = 0;
181 	else
182 		rq_regs->min_chunk_size = dml_log2(rq_sizing.min_chunk_bytes) - 8 + 1;
183 
184 	rq_regs->meta_chunk_size = dml_log2(rq_sizing.meta_chunk_bytes) - 10;
185 	if (rq_sizing.min_meta_chunk_bytes == 0)
186 		rq_regs->min_meta_chunk_size = 0;
187 	else
188 		rq_regs->min_meta_chunk_size = dml_log2(rq_sizing.min_meta_chunk_bytes) - 6 + 1;
189 
190 	rq_regs->dpte_group_size = dml_log2(rq_sizing.dpte_group_bytes) - 6;
191 	rq_regs->mpte_group_size = dml_log2(rq_sizing.mpte_group_bytes) - 6;
192 }
193 
extract_rq_regs(struct display_mode_lib * mode_lib,display_rq_regs_st * rq_regs,const display_rq_params_st rq_param)194 static void extract_rq_regs(struct display_mode_lib *mode_lib,
195 		display_rq_regs_st *rq_regs,
196 		const display_rq_params_st rq_param)
197 {
198 	unsigned int detile_buf_size_in_bytes = mode_lib->ip.det_buffer_size_kbytes * 1024;
199 	unsigned int detile_buf_plane1_addr = 0;
200 
201 	extract_rq_sizing_regs(mode_lib, &(rq_regs->rq_regs_l), rq_param.sizing.rq_l);
202 
203 	rq_regs->rq_regs_l.pte_row_height_linear = dml_floor(dml_log2(rq_param.dlg.rq_l.dpte_row_height),
204 			1) - 3;
205 
206 	if (rq_param.yuv420) {
207 		extract_rq_sizing_regs(mode_lib, &(rq_regs->rq_regs_c), rq_param.sizing.rq_c);
208 		rq_regs->rq_regs_c.pte_row_height_linear = dml_floor(dml_log2(rq_param.dlg.rq_c.dpte_row_height),
209 				1) - 3;
210 	}
211 
212 	rq_regs->rq_regs_l.swath_height = dml_log2(rq_param.dlg.rq_l.swath_height);
213 	rq_regs->rq_regs_c.swath_height = dml_log2(rq_param.dlg.rq_c.swath_height);
214 
215 	// TODO: take the max between luma, chroma chunk size?
216 	// okay for now, as we are setting chunk_bytes to 8kb anyways
217 	if (rq_param.sizing.rq_l.chunk_bytes >= 32 * 1024) { //32kb
218 		rq_regs->drq_expansion_mode = 0;
219 	} else {
220 		rq_regs->drq_expansion_mode = 2;
221 	}
222 	rq_regs->prq_expansion_mode = 1;
223 	rq_regs->mrq_expansion_mode = 1;
224 	rq_regs->crq_expansion_mode = 1;
225 
226 	if (rq_param.yuv420) {
227 		if ((double) rq_param.misc.rq_l.stored_swath_bytes
228 				/ (double) rq_param.misc.rq_c.stored_swath_bytes <= 1.5) {
229 			detile_buf_plane1_addr = (detile_buf_size_in_bytes / 2.0 / 64.0); // half to chroma
230 		} else {
231 			detile_buf_plane1_addr = dml_round_to_multiple((unsigned int) ((2.0 * detile_buf_size_in_bytes) / 3.0),
232 					256,
233 					0) / 64.0; // 2/3 to chroma
234 		}
235 	}
236 	rq_regs->plane1_base_address = detile_buf_plane1_addr;
237 }
238 
handle_det_buf_split(struct display_mode_lib * mode_lib,display_rq_params_st * rq_param,const display_pipe_source_params_st pipe_src_param)239 static void handle_det_buf_split(struct display_mode_lib *mode_lib,
240 		display_rq_params_st *rq_param,
241 		const display_pipe_source_params_st pipe_src_param)
242 {
243 	unsigned int total_swath_bytes = 0;
244 	unsigned int swath_bytes_l = 0;
245 	unsigned int swath_bytes_c = 0;
246 	unsigned int full_swath_bytes_packed_l = 0;
247 	unsigned int full_swath_bytes_packed_c = 0;
248 	bool req128_l = false;
249 	bool req128_c = false;
250 	bool surf_linear = (pipe_src_param.sw_mode == dm_sw_linear);
251 	bool surf_vert = (pipe_src_param.source_scan == dm_vert);
252 	unsigned int log2_swath_height_l = 0;
253 	unsigned int log2_swath_height_c = 0;
254 	unsigned int detile_buf_size_in_bytes = mode_lib->ip.det_buffer_size_kbytes * 1024;
255 
256 	full_swath_bytes_packed_l = rq_param->misc.rq_l.full_swath_bytes;
257 	full_swath_bytes_packed_c = rq_param->misc.rq_c.full_swath_bytes;
258 
259 	if (rq_param->yuv420_10bpc) {
260 		full_swath_bytes_packed_l = dml_round_to_multiple(rq_param->misc.rq_l.full_swath_bytes * 2 / 3,
261 				256,
262 				1) + 256;
263 		full_swath_bytes_packed_c = dml_round_to_multiple(rq_param->misc.rq_c.full_swath_bytes * 2 / 3,
264 				256,
265 				1) + 256;
266 	}
267 
268 	if (rq_param->yuv420) {
269 		total_swath_bytes = 2 * full_swath_bytes_packed_l + 2 * full_swath_bytes_packed_c;
270 
271 		if (total_swath_bytes <= detile_buf_size_in_bytes) { //full 256b request
272 			req128_l = false;
273 			req128_c = false;
274 			swath_bytes_l = full_swath_bytes_packed_l;
275 			swath_bytes_c = full_swath_bytes_packed_c;
276 		} else { //128b request (for luma only for yuv420 8bpc)
277 			req128_l = true;
278 			req128_c = false;
279 			swath_bytes_l = full_swath_bytes_packed_l / 2;
280 			swath_bytes_c = full_swath_bytes_packed_c;
281 		}
282 		// Note: assumption, the config that pass in will fit into
283 		//       the detiled buffer.
284 	} else {
285 		total_swath_bytes = 2 * full_swath_bytes_packed_l;
286 
287 		if (total_swath_bytes <= detile_buf_size_in_bytes)
288 			req128_l = false;
289 		else
290 			req128_l = true;
291 
292 		swath_bytes_l = total_swath_bytes;
293 		swath_bytes_c = 0;
294 	}
295 	rq_param->misc.rq_l.stored_swath_bytes = swath_bytes_l;
296 	rq_param->misc.rq_c.stored_swath_bytes = swath_bytes_c;
297 
298 	if (surf_linear) {
299 		log2_swath_height_l = 0;
300 		log2_swath_height_c = 0;
301 	} else if (!surf_vert) {
302 		log2_swath_height_l = dml_log2(rq_param->misc.rq_l.blk256_height) - req128_l;
303 		log2_swath_height_c = dml_log2(rq_param->misc.rq_c.blk256_height) - req128_c;
304 	} else {
305 		log2_swath_height_l = dml_log2(rq_param->misc.rq_l.blk256_width) - req128_l;
306 		log2_swath_height_c = dml_log2(rq_param->misc.rq_c.blk256_width) - req128_c;
307 	}
308 	rq_param->dlg.rq_l.swath_height = 1 << log2_swath_height_l;
309 	rq_param->dlg.rq_c.swath_height = 1 << log2_swath_height_c;
310 
311 	dml_print("DML_DLG: %s: req128_l = %0d\n", __func__, req128_l);
312 	dml_print("DML_DLG: %s: req128_c = %0d\n", __func__, req128_c);
313 	dml_print("DML_DLG: %s: full_swath_bytes_packed_l = %0d\n",
314 			__func__,
315 			full_swath_bytes_packed_l);
316 	dml_print("DML_DLG: %s: full_swath_bytes_packed_c = %0d\n",
317 			__func__,
318 			full_swath_bytes_packed_c);
319 }
320 
get_meta_and_pte_attr(struct display_mode_lib * mode_lib,display_data_rq_dlg_params_st * rq_dlg_param,display_data_rq_misc_params_st * rq_misc_param,display_data_rq_sizing_params_st * rq_sizing_param,unsigned int vp_width,unsigned int vp_height,unsigned int data_pitch,unsigned int meta_pitch,unsigned int source_format,unsigned int tiling,unsigned int macro_tile_size,unsigned int source_scan,unsigned int is_chroma)321 static void get_meta_and_pte_attr(struct display_mode_lib *mode_lib,
322 		display_data_rq_dlg_params_st *rq_dlg_param,
323 		display_data_rq_misc_params_st *rq_misc_param,
324 		display_data_rq_sizing_params_st *rq_sizing_param,
325 		unsigned int vp_width,
326 		unsigned int vp_height,
327 		unsigned int data_pitch,
328 		unsigned int meta_pitch,
329 		unsigned int source_format,
330 		unsigned int tiling,
331 		unsigned int macro_tile_size,
332 		unsigned int source_scan,
333 		unsigned int is_chroma)
334 {
335 	bool surf_linear = (tiling == dm_sw_linear);
336 	bool surf_vert = (source_scan == dm_vert);
337 
338 	unsigned int bytes_per_element;
339 	unsigned int bytes_per_element_y = get_bytes_per_element((enum source_format_class)(source_format),
340 			false);
341 	unsigned int bytes_per_element_c = get_bytes_per_element((enum source_format_class)(source_format),
342 			true);
343 
344 	unsigned int blk256_width = 0;
345 	unsigned int blk256_height = 0;
346 
347 	unsigned int blk256_width_y = 0;
348 	unsigned int blk256_height_y = 0;
349 	unsigned int blk256_width_c = 0;
350 	unsigned int blk256_height_c = 0;
351 	unsigned int log2_bytes_per_element;
352 	unsigned int log2_blk256_width;
353 	unsigned int log2_blk256_height;
354 	unsigned int blk_bytes;
355 	unsigned int log2_blk_bytes;
356 	unsigned int log2_blk_height;
357 	unsigned int log2_blk_width;
358 	unsigned int log2_meta_req_bytes;
359 	unsigned int log2_meta_req_height;
360 	unsigned int log2_meta_req_width;
361 	unsigned int meta_req_width;
362 	unsigned int meta_req_height;
363 	unsigned int log2_meta_row_height;
364 	unsigned int meta_row_width_ub;
365 	unsigned int log2_meta_chunk_bytes;
366 	unsigned int log2_meta_chunk_height;
367 
368 	//full sized meta chunk width in unit of data elements
369 	unsigned int log2_meta_chunk_width;
370 	unsigned int log2_min_meta_chunk_bytes;
371 	unsigned int min_meta_chunk_width;
372 	unsigned int meta_chunk_width;
373 	unsigned int meta_chunk_per_row_int;
374 	unsigned int meta_row_remainder;
375 	unsigned int meta_chunk_threshold;
376 	unsigned int meta_blk_bytes;
377 	unsigned int meta_blk_height;
378 	unsigned int meta_blk_width;
379 	unsigned int meta_surface_bytes;
380 	unsigned int vmpg_bytes;
381 	unsigned int meta_pte_req_per_frame_ub;
382 	unsigned int meta_pte_bytes_per_frame_ub;
383 	const unsigned int log2_vmpg_bytes = dml_log2(mode_lib->soc.vmm_page_size_bytes);
384 	const unsigned int dpte_buf_in_pte_reqs = mode_lib->ip.dpte_buffer_size_in_pte_reqs_luma;
385 	const unsigned int pde_proc_buffer_size_64k_reqs =
386 			mode_lib->ip.pde_proc_buffer_size_64k_reqs;
387 
388 	unsigned int log2_vmpg_height = 0;
389 	unsigned int log2_vmpg_width = 0;
390 	unsigned int log2_dpte_req_height_ptes = 0;
391 	unsigned int log2_dpte_req_height = 0;
392 	unsigned int log2_dpte_req_width = 0;
393 	unsigned int log2_dpte_row_height_linear = 0;
394 	unsigned int log2_dpte_row_height = 0;
395 	unsigned int log2_dpte_group_width = 0;
396 	unsigned int dpte_row_width_ub = 0;
397 	unsigned int dpte_req_height = 0;
398 	unsigned int dpte_req_width = 0;
399 	unsigned int dpte_group_width = 0;
400 	unsigned int log2_dpte_group_bytes = 0;
401 	unsigned int log2_dpte_group_length = 0;
402 	unsigned int pde_buf_entries;
403 	bool yuv420 = (source_format == dm_420_8 || source_format == dm_420_10);
404 
405 	Calculate256BBlockSizes((enum source_format_class)(source_format),
406 			(enum dm_swizzle_mode)(tiling),
407 			bytes_per_element_y,
408 			bytes_per_element_c,
409 			&blk256_height_y,
410 			&blk256_height_c,
411 			&blk256_width_y,
412 			&blk256_width_c);
413 
414 	if (!is_chroma) {
415 		blk256_width = blk256_width_y;
416 		blk256_height = blk256_height_y;
417 		bytes_per_element = bytes_per_element_y;
418 	} else {
419 		blk256_width = blk256_width_c;
420 		blk256_height = blk256_height_c;
421 		bytes_per_element = bytes_per_element_c;
422 	}
423 
424 	log2_bytes_per_element = dml_log2(bytes_per_element);
425 
426 	dml_print("DML_DLG: %s: surf_linear        = %d\n", __func__, surf_linear);
427 	dml_print("DML_DLG: %s: surf_vert          = %d\n", __func__, surf_vert);
428 	dml_print("DML_DLG: %s: blk256_width       = %d\n", __func__, blk256_width);
429 	dml_print("DML_DLG: %s: blk256_height      = %d\n", __func__, blk256_height);
430 
431 	log2_blk256_width = dml_log2((double) blk256_width);
432 	log2_blk256_height = dml_log2((double) blk256_height);
433 	blk_bytes = surf_linear ?
434 			256 : get_blk_size_bytes((enum source_macro_tile_size) macro_tile_size);
435 	log2_blk_bytes = dml_log2((double) blk_bytes);
436 	log2_blk_height = 0;
437 	log2_blk_width = 0;
438 
439 	// remember log rule
440 	// "+" in log is multiply
441 	// "-" in log is divide
442 	// "/2" is like square root
443 	// blk is vertical biased
444 	if (tiling != dm_sw_linear)
445 		log2_blk_height = log2_blk256_height
446 				+ dml_ceil((double) (log2_blk_bytes - 8) / 2.0, 1);
447 	else
448 		log2_blk_height = 0;  // blk height of 1
449 
450 	log2_blk_width = log2_blk_bytes - log2_bytes_per_element - log2_blk_height;
451 
452 	if (!surf_vert) {
453 		rq_dlg_param->swath_width_ub = dml_round_to_multiple(vp_width - 1, blk256_width, 1)
454 				+ blk256_width;
455 		rq_dlg_param->req_per_swath_ub = rq_dlg_param->swath_width_ub >> log2_blk256_width;
456 	} else {
457 		rq_dlg_param->swath_width_ub = dml_round_to_multiple(vp_height - 1, blk256_height, 1)
458 				+ blk256_height;
459 		rq_dlg_param->req_per_swath_ub = rq_dlg_param->swath_width_ub >> log2_blk256_height;
460 	}
461 
462 	if (!surf_vert)
463 		rq_misc_param->full_swath_bytes = rq_dlg_param->swath_width_ub * blk256_height
464 				* bytes_per_element;
465 	else
466 		rq_misc_param->full_swath_bytes = rq_dlg_param->swath_width_ub * blk256_width
467 				* bytes_per_element;
468 
469 	rq_misc_param->blk256_height = blk256_height;
470 	rq_misc_param->blk256_width = blk256_width;
471 
472 	// -------
473 	// meta
474 	// -------
475 	log2_meta_req_bytes = 6; // meta request is 64b and is 8x8byte meta element
476 
477 	// each 64b meta request for dcn is 8x8 meta elements and
478 	// a meta element covers one 256b block of the the data surface.
479 	log2_meta_req_height = log2_blk256_height + 3; // meta req is 8x8 byte, each byte represent 1 blk256
480 	log2_meta_req_width = log2_meta_req_bytes + 8 - log2_bytes_per_element
481 			- log2_meta_req_height;
482 	meta_req_width = 1 << log2_meta_req_width;
483 	meta_req_height = 1 << log2_meta_req_height;
484 	log2_meta_row_height = 0;
485 	meta_row_width_ub = 0;
486 
487 	// the dimensions of a meta row are meta_row_width x meta_row_height in elements.
488 	// calculate upper bound of the meta_row_width
489 	if (!surf_vert) {
490 		log2_meta_row_height = log2_meta_req_height;
491 		meta_row_width_ub = dml_round_to_multiple(vp_width - 1, meta_req_width, 1)
492 				+ meta_req_width;
493 		rq_dlg_param->meta_req_per_row_ub = meta_row_width_ub / meta_req_width;
494 	} else {
495 		log2_meta_row_height = log2_meta_req_width;
496 		meta_row_width_ub = dml_round_to_multiple(vp_height - 1, meta_req_height, 1)
497 				+ meta_req_height;
498 		rq_dlg_param->meta_req_per_row_ub = meta_row_width_ub / meta_req_height;
499 	}
500 	rq_dlg_param->meta_bytes_per_row_ub = rq_dlg_param->meta_req_per_row_ub * 64;
501 
502 	rq_dlg_param->meta_row_height = 1 << log2_meta_row_height;
503 
504 	log2_meta_chunk_bytes = dml_log2(rq_sizing_param->meta_chunk_bytes);
505 	log2_meta_chunk_height = log2_meta_row_height;
506 
507 	//full sized meta chunk width in unit of data elements
508 	log2_meta_chunk_width = log2_meta_chunk_bytes + 8 - log2_bytes_per_element
509 			- log2_meta_chunk_height;
510 	log2_min_meta_chunk_bytes = dml_log2(rq_sizing_param->min_meta_chunk_bytes);
511 	min_meta_chunk_width = 1
512 			<< (log2_min_meta_chunk_bytes + 8 - log2_bytes_per_element
513 					- log2_meta_chunk_height);
514 	meta_chunk_width = 1 << log2_meta_chunk_width;
515 	meta_chunk_per_row_int = (unsigned int) (meta_row_width_ub / meta_chunk_width);
516 	meta_row_remainder = meta_row_width_ub % meta_chunk_width;
517 	meta_chunk_threshold = 0;
518 	meta_blk_bytes = 4096;
519 	meta_blk_height = blk256_height * 64;
520 	meta_blk_width = meta_blk_bytes * 256 / bytes_per_element / meta_blk_height;
521 	meta_surface_bytes = meta_pitch
522 			* (dml_round_to_multiple(vp_height - 1, meta_blk_height, 1) + meta_blk_height)
523 			* bytes_per_element / 256;
524 	vmpg_bytes = mode_lib->soc.vmm_page_size_bytes;
525 	meta_pte_req_per_frame_ub = (dml_round_to_multiple(meta_surface_bytes - vmpg_bytes,
526 			8 * vmpg_bytes,
527 			1) + 8 * vmpg_bytes) / (8 * vmpg_bytes);
528 	meta_pte_bytes_per_frame_ub = meta_pte_req_per_frame_ub * 64; //64B mpte request
529 	rq_dlg_param->meta_pte_bytes_per_frame_ub = meta_pte_bytes_per_frame_ub;
530 
531 	dml_print("DML_DLG: %s: meta_blk_height             = %d\n", __func__, meta_blk_height);
532 	dml_print("DML_DLG: %s: meta_blk_width              = %d\n", __func__, meta_blk_width);
533 	dml_print("DML_DLG: %s: meta_surface_bytes          = %d\n", __func__, meta_surface_bytes);
534 	dml_print("DML_DLG: %s: meta_pte_req_per_frame_ub   = %d\n",
535 			__func__,
536 			meta_pte_req_per_frame_ub);
537 	dml_print("DML_DLG: %s: meta_pte_bytes_per_frame_ub = %d\n",
538 			__func__,
539 			meta_pte_bytes_per_frame_ub);
540 
541 	if (!surf_vert)
542 		meta_chunk_threshold = 2 * min_meta_chunk_width - meta_req_width;
543 	else
544 		meta_chunk_threshold = 2 * min_meta_chunk_width - meta_req_height;
545 
546 	if (meta_row_remainder <= meta_chunk_threshold)
547 		rq_dlg_param->meta_chunks_per_row_ub = meta_chunk_per_row_int + 1;
548 	else
549 		rq_dlg_param->meta_chunks_per_row_ub = meta_chunk_per_row_int + 2;
550 
551 	// ------
552 	// dpte
553 	// ------
554 	if (surf_linear) {
555 		log2_vmpg_height = 0;   // one line high
556 	} else {
557 		log2_vmpg_height = (log2_vmpg_bytes - 8) / 2 + log2_blk256_height;
558 	}
559 	log2_vmpg_width = log2_vmpg_bytes - log2_bytes_per_element - log2_vmpg_height;
560 
561 	// only 3 possible shapes for dpte request in dimensions of ptes: 8x1, 4x2, 2x4.
562 	if (surf_linear) { //one 64B PTE request returns 8 PTEs
563 		log2_dpte_req_height_ptes = 0;
564 		log2_dpte_req_width = log2_vmpg_width + 3;
565 		log2_dpte_req_height = 0;
566 	} else if (log2_blk_bytes == 12) { //4KB tile means 4kB page size
567 		//one 64B req gives 8x1 PTEs for 4KB tile
568 		log2_dpte_req_height_ptes = 0;
569 		log2_dpte_req_width = log2_blk_width + 3;
570 		log2_dpte_req_height = log2_blk_height + 0;
571 	} else if ((log2_blk_bytes >= 16) && (log2_vmpg_bytes == 12)) { // tile block >= 64KB
572 		//two 64B reqs of 2x4 PTEs give 16 PTEs to cover 64KB
573 		log2_dpte_req_height_ptes = 4;
574 		log2_dpte_req_width = log2_blk256_width + 4; // log2_64KB_width
575 		log2_dpte_req_height = log2_blk256_height + 4; // log2_64KB_height
576 	} else { //64KB page size and must 64KB tile block
577 		 //one 64B req gives 8x1 PTEs for 64KB tile
578 		log2_dpte_req_height_ptes = 0;
579 		log2_dpte_req_width = log2_blk_width + 3;
580 		log2_dpte_req_height = log2_blk_height + 0;
581 	}
582 
583 	// The dpte request dimensions in data elements is dpte_req_width x dpte_req_height
584 	// log2_vmpg_width is how much 1 pte represent, now calculating how much a 64b pte req represent
585 	// That depends on the pte shape (i.e. 8x1, 4x2, 2x4)
586 	//log2_dpte_req_height    = log2_vmpg_height + log2_dpte_req_height_ptes;
587 	//log2_dpte_req_width     = log2_vmpg_width + log2_dpte_req_width_ptes;
588 	dpte_req_height = 1 << log2_dpte_req_height;
589 	dpte_req_width = 1 << log2_dpte_req_width;
590 
591 	// calculate pitch dpte row buffer can hold
592 	// round the result down to a power of two.
593 	pde_buf_entries = yuv420 ? (pde_proc_buffer_size_64k_reqs >> 1) : pde_proc_buffer_size_64k_reqs;
594 	if (surf_linear) {
595 		unsigned int dpte_row_height;
596 
597 		log2_dpte_row_height_linear = dml_floor(dml_log2(dml_min(64 * 1024 * pde_buf_entries
598 										/ bytes_per_element,
599 								dpte_buf_in_pte_reqs
600 										* dpte_req_width)
601 								/ data_pitch),
602 				1);
603 
604 		ASSERT(log2_dpte_row_height_linear >= 3);
605 
606 		if (log2_dpte_row_height_linear > 7)
607 			log2_dpte_row_height_linear = 7;
608 
609 		log2_dpte_row_height = log2_dpte_row_height_linear;
610 		// For linear, the dpte row is pitch dependent and the pte requests wrap at the pitch boundary.
611 		// the dpte_row_width_ub is the upper bound of data_pitch*dpte_row_height in elements with this unique buffering.
612 		dpte_row_height = 1 << log2_dpte_row_height;
613 		dpte_row_width_ub = dml_round_to_multiple(data_pitch * dpte_row_height - 1,
614 				dpte_req_width,
615 				1) + dpte_req_width;
616 		rq_dlg_param->dpte_req_per_row_ub = dpte_row_width_ub / dpte_req_width;
617 	} else {
618 		// the upper bound of the dpte_row_width without dependency on viewport position follows.
619 		// for tiled mode, row height is the same as req height and row store up to vp size upper bound
620 		if (!surf_vert) {
621 			log2_dpte_row_height = log2_dpte_req_height;
622 			dpte_row_width_ub = dml_round_to_multiple(vp_width - 1, dpte_req_width, 1)
623 					+ dpte_req_width;
624 			rq_dlg_param->dpte_req_per_row_ub = dpte_row_width_ub / dpte_req_width;
625 		} else {
626 			log2_dpte_row_height =
627 					(log2_blk_width < log2_dpte_req_width) ?
628 							log2_blk_width : log2_dpte_req_width;
629 			dpte_row_width_ub = dml_round_to_multiple(vp_height - 1, dpte_req_height, 1)
630 					+ dpte_req_height;
631 			rq_dlg_param->dpte_req_per_row_ub = dpte_row_width_ub / dpte_req_height;
632 		}
633 	}
634 	if (log2_blk_bytes >= 16 && log2_vmpg_bytes == 12) // tile block >= 64KB
635 		rq_dlg_param->dpte_bytes_per_row_ub = rq_dlg_param->dpte_req_per_row_ub * 128; //2*64B dpte request
636 	else
637 		rq_dlg_param->dpte_bytes_per_row_ub = rq_dlg_param->dpte_req_per_row_ub * 64; //64B dpte request
638 
639 	rq_dlg_param->dpte_row_height = 1 << log2_dpte_row_height;
640 
641 	// the dpte_group_bytes is reduced for the specific case of vertical
642 	// access of a tile surface that has dpte request of 8x1 ptes.
643 	if (!surf_linear & (log2_dpte_req_height_ptes == 0) & surf_vert) //reduced, in this case, will have page fault within a group
644 		rq_sizing_param->dpte_group_bytes = 512;
645 	else
646 		//full size
647 		rq_sizing_param->dpte_group_bytes = 2048;
648 
649 	//since pte request size is 64byte, the number of data pte requests per full sized group is as follows.
650 	log2_dpte_group_bytes = dml_log2(rq_sizing_param->dpte_group_bytes);
651 	log2_dpte_group_length = log2_dpte_group_bytes - 6; //length in 64b requests
652 
653 	// full sized data pte group width in elements
654 	if (!surf_vert)
655 		log2_dpte_group_width = log2_dpte_group_length + log2_dpte_req_width;
656 	else
657 		log2_dpte_group_width = log2_dpte_group_length + log2_dpte_req_height;
658 
659 	//But if the tile block >=64KB and the page size is 4KB, then each dPTE request is 2*64B
660 	if ((log2_blk_bytes >= 16) && (log2_vmpg_bytes == 12)) // tile block >= 64KB
661 		log2_dpte_group_width = log2_dpte_group_width - 1;
662 
663 	dpte_group_width = 1 << log2_dpte_group_width;
664 
665 	// since dpte groups are only aligned to dpte_req_width and not dpte_group_width,
666 	// the upper bound for the dpte groups per row is as follows.
667 	rq_dlg_param->dpte_groups_per_row_ub = dml_ceil((double) dpte_row_width_ub / dpte_group_width,
668 			1);
669 }
670 
get_surf_rq_param(struct display_mode_lib * mode_lib,display_data_rq_sizing_params_st * rq_sizing_param,display_data_rq_dlg_params_st * rq_dlg_param,display_data_rq_misc_params_st * rq_misc_param,const display_pipe_source_params_st pipe_src_param,bool is_chroma)671 static void get_surf_rq_param(struct display_mode_lib *mode_lib,
672 		display_data_rq_sizing_params_st *rq_sizing_param,
673 		display_data_rq_dlg_params_st *rq_dlg_param,
674 		display_data_rq_misc_params_st *rq_misc_param,
675 		const display_pipe_source_params_st pipe_src_param,
676 		bool is_chroma)
677 {
678 	bool mode_422 = false;
679 	unsigned int vp_width = 0;
680 	unsigned int vp_height = 0;
681 	unsigned int data_pitch = 0;
682 	unsigned int meta_pitch = 0;
683 	unsigned int ppe = mode_422 ? 2 : 1;
684 
685 	// TODO check if ppe apply for both luma and chroma in 422 case
686 	if (is_chroma) {
687 		vp_width = pipe_src_param.viewport_width_c / ppe;
688 		vp_height = pipe_src_param.viewport_height_c;
689 		data_pitch = pipe_src_param.data_pitch_c;
690 		meta_pitch = pipe_src_param.meta_pitch_c;
691 	} else {
692 		vp_width = pipe_src_param.viewport_width / ppe;
693 		vp_height = pipe_src_param.viewport_height;
694 		data_pitch = pipe_src_param.data_pitch;
695 		meta_pitch = pipe_src_param.meta_pitch;
696 	}
697 
698 	rq_sizing_param->chunk_bytes = 8192;
699 
700 	if (rq_sizing_param->chunk_bytes == 64 * 1024)
701 		rq_sizing_param->min_chunk_bytes = 0;
702 	else
703 		rq_sizing_param->min_chunk_bytes = 1024;
704 
705 	rq_sizing_param->meta_chunk_bytes = 2048;
706 	rq_sizing_param->min_meta_chunk_bytes = 256;
707 
708 	rq_sizing_param->mpte_group_bytes = 2048;
709 
710 	get_meta_and_pte_attr(mode_lib,
711 			rq_dlg_param,
712 			rq_misc_param,
713 			rq_sizing_param,
714 			vp_width,
715 			vp_height,
716 			data_pitch,
717 			meta_pitch,
718 			pipe_src_param.source_format,
719 			pipe_src_param.sw_mode,
720 			pipe_src_param.macro_tile_size,
721 			pipe_src_param.source_scan,
722 			is_chroma);
723 }
724 
dml20_rq_dlg_get_rq_params(struct display_mode_lib * mode_lib,display_rq_params_st * rq_param,const display_pipe_source_params_st pipe_src_param)725 static void dml20_rq_dlg_get_rq_params(struct display_mode_lib *mode_lib,
726 		display_rq_params_st *rq_param,
727 		const display_pipe_source_params_st pipe_src_param)
728 {
729 	// get param for luma surface
730 	rq_param->yuv420 = pipe_src_param.source_format == dm_420_8
731 			|| pipe_src_param.source_format == dm_420_10;
732 	rq_param->yuv420_10bpc = pipe_src_param.source_format == dm_420_10;
733 
734 	get_surf_rq_param(mode_lib,
735 			&(rq_param->sizing.rq_l),
736 			&(rq_param->dlg.rq_l),
737 			&(rq_param->misc.rq_l),
738 			pipe_src_param,
739 			0);
740 
741 	if (is_dual_plane((enum source_format_class)(pipe_src_param.source_format))) {
742 		// get param for chroma surface
743 		get_surf_rq_param(mode_lib,
744 				&(rq_param->sizing.rq_c),
745 				&(rq_param->dlg.rq_c),
746 				&(rq_param->misc.rq_c),
747 				pipe_src_param,
748 				1);
749 	}
750 
751 	// calculate how to split the det buffer space between luma and chroma
752 	handle_det_buf_split(mode_lib, rq_param, pipe_src_param);
753 	print__rq_params_st(mode_lib, *rq_param);
754 }
755 
dml20_rq_dlg_get_rq_reg(struct display_mode_lib * mode_lib,display_rq_regs_st * rq_regs,const display_pipe_params_st pipe_param)756 void dml20_rq_dlg_get_rq_reg(struct display_mode_lib *mode_lib,
757 		display_rq_regs_st *rq_regs,
758 		const display_pipe_params_st pipe_param)
759 {
760 	display_rq_params_st rq_param = {0};
761 
762 	memset(rq_regs, 0, sizeof(*rq_regs));
763 	dml20_rq_dlg_get_rq_params(mode_lib, &rq_param, pipe_param.src);
764 	extract_rq_regs(mode_lib, rq_regs, rq_param);
765 
766 	print__rq_regs_st(mode_lib, *rq_regs);
767 }
768 
769 // Note: currently taken in as is.
770 // Nice to decouple code from hw register implement and extract code that are repeated for luma and chroma.
dml20_rq_dlg_get_dlg_params(struct display_mode_lib * mode_lib,const display_e2e_pipe_params_st * e2e_pipe_param,const unsigned int num_pipes,const unsigned int pipe_idx,display_dlg_regs_st * disp_dlg_regs,display_ttu_regs_st * disp_ttu_regs,const display_rq_dlg_params_st rq_dlg_param,const display_dlg_sys_params_st dlg_sys_param,const bool cstate_en,const bool pstate_en)771 static void dml20_rq_dlg_get_dlg_params(struct display_mode_lib *mode_lib,
772 		const display_e2e_pipe_params_st *e2e_pipe_param,
773 		const unsigned int num_pipes,
774 		const unsigned int pipe_idx,
775 		display_dlg_regs_st *disp_dlg_regs,
776 		display_ttu_regs_st *disp_ttu_regs,
777 		const display_rq_dlg_params_st rq_dlg_param,
778 		const display_dlg_sys_params_st dlg_sys_param,
779 		const bool cstate_en,
780 		const bool pstate_en)
781 {
782 	const display_pipe_source_params_st *src = &e2e_pipe_param[pipe_idx].pipe.src;
783 	const display_pipe_dest_params_st *dst = &e2e_pipe_param[pipe_idx].pipe.dest;
784 	const display_output_params_st *dout = &e2e_pipe_param[pipe_idx].dout;
785 	const display_clocks_and_cfg_st *clks = &e2e_pipe_param[pipe_idx].clks_cfg;
786 	const scaler_ratio_depth_st *scl = &e2e_pipe_param[pipe_idx].pipe.scale_ratio_depth;
787 	const scaler_taps_st *taps = &e2e_pipe_param[pipe_idx].pipe.scale_taps;
788 
789 	// -------------------------
790 	// Section 1.15.2.1: OTG dependent Params
791 	// -------------------------
792 	// Timing
793 	unsigned int htotal = dst->htotal;
794 //    unsigned int hblank_start = dst.hblank_start; // TODO: Remove
795 	unsigned int hblank_end = dst->hblank_end;
796 	unsigned int vblank_start = dst->vblank_start;
797 	unsigned int vblank_end = dst->vblank_end;
798 	unsigned int min_vblank = mode_lib->ip.min_vblank_lines;
799 
800 	double dppclk_freq_in_mhz = clks->dppclk_mhz;
801 	double dispclk_freq_in_mhz = clks->dispclk_mhz;
802 	double refclk_freq_in_mhz = clks->refclk_mhz;
803 	double pclk_freq_in_mhz = dst->pixel_rate_mhz;
804 	bool interlaced = dst->interlaced;
805 
806 	double ref_freq_to_pix_freq = refclk_freq_in_mhz / pclk_freq_in_mhz;
807 
808 	double min_dcfclk_mhz;
809 	double t_calc_us;
810 	double min_ttu_vblank;
811 
812 	double min_dst_y_ttu_vblank;
813 	unsigned int dlg_vblank_start;
814 	bool dual_plane;
815 	bool mode_422;
816 	unsigned int access_dir;
817 	unsigned int vp_height_l;
818 	unsigned int vp_width_l;
819 	unsigned int vp_height_c;
820 	unsigned int vp_width_c;
821 
822 	// Scaling
823 	unsigned int htaps_l;
824 	unsigned int htaps_c;
825 	double hratio_l;
826 	double hratio_c;
827 	double vratio_l;
828 	double vratio_c;
829 	bool scl_enable;
830 
831 	double line_time_in_us;
832 	//    double vinit_l;
833 	//    double vinit_c;
834 	//    double vinit_bot_l;
835 	//    double vinit_bot_c;
836 
837 	//    unsigned int swath_height_l;
838 	unsigned int swath_width_ub_l;
839 	//    unsigned int dpte_bytes_per_row_ub_l;
840 	unsigned int dpte_groups_per_row_ub_l;
841 	//    unsigned int meta_pte_bytes_per_frame_ub_l;
842 	//    unsigned int meta_bytes_per_row_ub_l;
843 
844 	//    unsigned int swath_height_c;
845 	unsigned int swath_width_ub_c;
846 	//   unsigned int dpte_bytes_per_row_ub_c;
847 	unsigned int dpte_groups_per_row_ub_c;
848 
849 	unsigned int meta_chunks_per_row_ub_l;
850 	unsigned int meta_chunks_per_row_ub_c;
851 	unsigned int vupdate_offset;
852 	unsigned int vupdate_width;
853 	unsigned int vready_offset;
854 
855 	unsigned int dppclk_delay_subtotal;
856 	unsigned int dispclk_delay_subtotal;
857 	unsigned int pixel_rate_delay_subtotal;
858 
859 	unsigned int vstartup_start;
860 	unsigned int dst_x_after_scaler;
861 	unsigned int dst_y_after_scaler;
862 	double line_wait;
863 	double dst_y_prefetch;
864 	double dst_y_per_vm_vblank;
865 	double dst_y_per_row_vblank;
866 	double dst_y_per_vm_flip;
867 	double dst_y_per_row_flip;
868 	double min_dst_y_per_vm_vblank;
869 	double min_dst_y_per_row_vblank;
870 	double lsw;
871 	double vratio_pre_l;
872 	double vratio_pre_c;
873 	unsigned int req_per_swath_ub_l;
874 	unsigned int req_per_swath_ub_c;
875 	unsigned int meta_row_height_l;
876 	unsigned int meta_row_height_c;
877 	unsigned int swath_width_pixels_ub_l;
878 	unsigned int swath_width_pixels_ub_c;
879 	unsigned int scaler_rec_in_width_l;
880 	unsigned int scaler_rec_in_width_c;
881 	unsigned int dpte_row_height_l;
882 	unsigned int dpte_row_height_c;
883 	double hscale_pixel_rate_l;
884 	double hscale_pixel_rate_c;
885 	double min_hratio_fact_l;
886 	double min_hratio_fact_c;
887 	double refcyc_per_line_delivery_pre_l;
888 	double refcyc_per_line_delivery_pre_c;
889 	double refcyc_per_line_delivery_l;
890 	double refcyc_per_line_delivery_c;
891 
892 	double refcyc_per_req_delivery_pre_l;
893 	double refcyc_per_req_delivery_pre_c;
894 	double refcyc_per_req_delivery_l;
895 	double refcyc_per_req_delivery_c;
896 
897 	unsigned int full_recout_width;
898 	double xfc_transfer_delay;
899 	double xfc_precharge_delay;
900 	double xfc_remote_surface_flip_latency;
901 	double xfc_dst_y_delta_drq_limit;
902 	double xfc_prefetch_margin;
903 	double refcyc_per_req_delivery_pre_cur0;
904 	double refcyc_per_req_delivery_cur0;
905 	double refcyc_per_req_delivery_pre_cur1;
906 	double refcyc_per_req_delivery_cur1;
907 
908 	memset(disp_dlg_regs, 0, sizeof(*disp_dlg_regs));
909 	memset(disp_ttu_regs, 0, sizeof(*disp_ttu_regs));
910 
911 	dml_print("DML_DLG: %s:  cstate_en = %d\n", __func__, cstate_en);
912 	dml_print("DML_DLG: %s:  pstate_en = %d\n", __func__, pstate_en);
913 
914 	dml_print("DML_DLG: %s: dppclk_freq_in_mhz     = %3.2f\n", __func__, dppclk_freq_in_mhz);
915 	dml_print("DML_DLG: %s: dispclk_freq_in_mhz    = %3.2f\n", __func__, dispclk_freq_in_mhz);
916 	dml_print("DML_DLG: %s: refclk_freq_in_mhz     = %3.2f\n", __func__, refclk_freq_in_mhz);
917 	dml_print("DML_DLG: %s: pclk_freq_in_mhz       = %3.2f\n", __func__, pclk_freq_in_mhz);
918 	dml_print("DML_DLG: %s: interlaced             = %d\n", __func__, interlaced);
919 	ASSERT(ref_freq_to_pix_freq < 4.0);
920 
921 	disp_dlg_regs->ref_freq_to_pix_freq =
922 			(unsigned int) (ref_freq_to_pix_freq * dml_pow(2, 19));
923 	disp_dlg_regs->refcyc_per_htotal = (unsigned int) (ref_freq_to_pix_freq * (double) htotal
924 			* dml_pow(2, 8));
925 	disp_dlg_regs->dlg_vblank_end = interlaced ? (vblank_end / 2) : vblank_end; // 15 bits
926 	disp_dlg_regs->refcyc_h_blank_end = (unsigned int) ((double) hblank_end
927 			* (double) ref_freq_to_pix_freq);
928 	ASSERT(disp_dlg_regs->refcyc_h_blank_end < (unsigned int) dml_pow(2, 13));
929 
930 	min_dcfclk_mhz = dlg_sys_param.deepsleep_dcfclk_mhz;
931 	t_calc_us = get_tcalc(mode_lib, e2e_pipe_param, num_pipes);
932 	min_ttu_vblank = get_min_ttu_vblank(mode_lib, e2e_pipe_param, num_pipes, pipe_idx);
933 
934 	min_dst_y_ttu_vblank = min_ttu_vblank * pclk_freq_in_mhz / (double) htotal;
935 	dlg_vblank_start = interlaced ? (vblank_start / 2) : vblank_start;
936 
937 	disp_dlg_regs->min_dst_y_next_start = (unsigned int) ((double) dlg_vblank_start * dml_pow(2, 2));
938 	ASSERT(disp_dlg_regs->min_dst_y_next_start < (unsigned int) dml_pow(2, 18));
939 
940 	dml_print("DML_DLG: %s: min_dcfclk_mhz                         = %3.2f\n",
941 			__func__,
942 			min_dcfclk_mhz);
943 	dml_print("DML_DLG: %s: min_ttu_vblank                         = %3.2f\n",
944 			__func__,
945 			min_ttu_vblank);
946 	dml_print("DML_DLG: %s: min_dst_y_ttu_vblank                   = %3.2f\n",
947 			__func__,
948 			min_dst_y_ttu_vblank);
949 	dml_print("DML_DLG: %s: t_calc_us                              = %3.2f\n",
950 			__func__,
951 			t_calc_us);
952 	dml_print("DML_DLG: %s: disp_dlg_regs->min_dst_y_next_start    = 0x%0x\n",
953 			__func__,
954 			disp_dlg_regs->min_dst_y_next_start);
955 	dml_print("DML_DLG: %s: ref_freq_to_pix_freq                   = %3.2f\n",
956 			__func__,
957 			ref_freq_to_pix_freq);
958 
959 	// -------------------------
960 	// Section 1.15.2.2: Prefetch, Active and TTU
961 	// -------------------------
962 	// Prefetch Calc
963 	// Source
964 //             dcc_en              = src.dcc;
965 	dual_plane = is_dual_plane((enum source_format_class)(src->source_format));
966 	mode_422 = false; // TODO
967 	access_dir = (src->source_scan == dm_vert); // vp access direction: horizontal or vertical accessed
968 //      bytes_per_element_l = get_bytes_per_element(source_format_class(src.source_format), 0);
969 //      bytes_per_element_c = get_bytes_per_element(source_format_class(src.source_format), 1);
970 	vp_height_l = src->viewport_height;
971 	vp_width_l = src->viewport_width;
972 	vp_height_c = src->viewport_height_c;
973 	vp_width_c = src->viewport_width_c;
974 
975 	// Scaling
976 	htaps_l = taps->htaps;
977 	htaps_c = taps->htaps_c;
978 	hratio_l = scl->hscl_ratio;
979 	hratio_c = scl->hscl_ratio_c;
980 	vratio_l = scl->vscl_ratio;
981 	vratio_c = scl->vscl_ratio_c;
982 	scl_enable = scl->scl_enable;
983 
984 	line_time_in_us = (htotal / pclk_freq_in_mhz);
985 //     vinit_l         = scl.vinit;
986 //     vinit_c         = scl.vinit_c;
987 //     vinit_bot_l     = scl.vinit_bot;
988 //     vinit_bot_c     = scl.vinit_bot_c;
989 
990 //    unsigned int swath_height_l                 = rq_dlg_param.rq_l.swath_height;
991 	swath_width_ub_l = rq_dlg_param.rq_l.swath_width_ub;
992 //    unsigned int dpte_bytes_per_row_ub_l        = rq_dlg_param.rq_l.dpte_bytes_per_row_ub;
993 	dpte_groups_per_row_ub_l = rq_dlg_param.rq_l.dpte_groups_per_row_ub;
994 //    unsigned int meta_pte_bytes_per_frame_ub_l  = rq_dlg_param.rq_l.meta_pte_bytes_per_frame_ub;
995 //    unsigned int meta_bytes_per_row_ub_l        = rq_dlg_param.rq_l.meta_bytes_per_row_ub;
996 
997 //    unsigned int swath_height_c                 = rq_dlg_param.rq_c.swath_height;
998 	swath_width_ub_c = rq_dlg_param.rq_c.swath_width_ub;
999 	//   dpte_bytes_per_row_ub_c        = rq_dlg_param.rq_c.dpte_bytes_per_row_ub;
1000 	dpte_groups_per_row_ub_c = rq_dlg_param.rq_c.dpte_groups_per_row_ub;
1001 
1002 	meta_chunks_per_row_ub_l = rq_dlg_param.rq_l.meta_chunks_per_row_ub;
1003 	meta_chunks_per_row_ub_c = rq_dlg_param.rq_c.meta_chunks_per_row_ub;
1004 	vupdate_offset = dst->vupdate_offset;
1005 	vupdate_width = dst->vupdate_width;
1006 	vready_offset = dst->vready_offset;
1007 
1008 	dppclk_delay_subtotal = mode_lib->ip.dppclk_delay_subtotal;
1009 	dispclk_delay_subtotal = mode_lib->ip.dispclk_delay_subtotal;
1010 
1011 	if (scl_enable)
1012 		dppclk_delay_subtotal += mode_lib->ip.dppclk_delay_scl;
1013 	else
1014 		dppclk_delay_subtotal += mode_lib->ip.dppclk_delay_scl_lb_only;
1015 
1016 	dppclk_delay_subtotal += mode_lib->ip.dppclk_delay_cnvc_formatter
1017 			+ src->num_cursors * mode_lib->ip.dppclk_delay_cnvc_cursor;
1018 
1019 	if (dout->dsc_enable) {
1020 		double dsc_delay = get_dsc_delay(mode_lib, e2e_pipe_param, num_pipes, pipe_idx);
1021 
1022 		dispclk_delay_subtotal += dsc_delay;
1023 	}
1024 
1025 	pixel_rate_delay_subtotal = dppclk_delay_subtotal * pclk_freq_in_mhz / dppclk_freq_in_mhz
1026 			+ dispclk_delay_subtotal * pclk_freq_in_mhz / dispclk_freq_in_mhz;
1027 
1028 	vstartup_start = dst->vstartup_start;
1029 	if (interlaced) {
1030 		if (vstartup_start / 2.0
1031 				- (double) (vready_offset + vupdate_width + vupdate_offset) / htotal
1032 				<= vblank_end / 2.0)
1033 			disp_dlg_regs->vready_after_vcount0 = 1;
1034 		else
1035 			disp_dlg_regs->vready_after_vcount0 = 0;
1036 	} else {
1037 		if (vstartup_start
1038 				- (double) (vready_offset + vupdate_width + vupdate_offset) / htotal
1039 				<= vblank_end)
1040 			disp_dlg_regs->vready_after_vcount0 = 1;
1041 		else
1042 			disp_dlg_regs->vready_after_vcount0 = 0;
1043 	}
1044 
1045 	// TODO: Where is this coming from?
1046 	if (interlaced)
1047 		vstartup_start = vstartup_start / 2;
1048 
1049 	// TODO: What if this min_vblank doesn't match the value in the dml_config_settings.cpp?
1050 	if (vstartup_start >= min_vblank) {
1051 		dml_print("WARNING: DML_DLG: %s: vblank_start=%d vblank_end=%d\n",
1052 				__func__,
1053 				vblank_start,
1054 				vblank_end);
1055 		dml_print("WARNING: DML_DLG: %s: vstartup_start=%d should be less than min_vblank=%d\n",
1056 				__func__,
1057 				vstartup_start,
1058 				min_vblank);
1059 		min_vblank = vstartup_start + 1;
1060 		dml_print("WARNING: DML_DLG: %s: vstartup_start=%d should be less than min_vblank=%d\n",
1061 				__func__,
1062 				vstartup_start,
1063 				min_vblank);
1064 	}
1065 
1066 	dst_x_after_scaler = get_dst_x_after_scaler(mode_lib, e2e_pipe_param, num_pipes, pipe_idx);
1067 	dst_y_after_scaler = get_dst_y_after_scaler(mode_lib, e2e_pipe_param, num_pipes, pipe_idx);
1068 
1069 	dml_print("DML_DLG: %s: htotal                                 = %d\n", __func__, htotal);
1070 	dml_print("DML_DLG: %s: pixel_rate_delay_subtotal              = %d\n",
1071 			__func__,
1072 			pixel_rate_delay_subtotal);
1073 	dml_print("DML_DLG: %s: dst_x_after_scaler                     = %d\n",
1074 			__func__,
1075 			dst_x_after_scaler);
1076 	dml_print("DML_DLG: %s: dst_y_after_scaler                     = %d\n",
1077 			__func__,
1078 			dst_y_after_scaler);
1079 
1080 	// Lwait
1081 	line_wait = mode_lib->soc.urgent_latency_us;
1082 	if (cstate_en)
1083 		line_wait = dml_max(mode_lib->soc.sr_enter_plus_exit_time_us, line_wait);
1084 	if (pstate_en)
1085 		line_wait = dml_max(mode_lib->soc.dram_clock_change_latency_us
1086 						+ mode_lib->soc.urgent_latency_us,
1087 				line_wait);
1088 	line_wait = line_wait / line_time_in_us;
1089 
1090 	dst_y_prefetch = get_dst_y_prefetch(mode_lib, e2e_pipe_param, num_pipes, pipe_idx);
1091 	dml_print("DML_DLG: %s: dst_y_prefetch (after rnd) = %3.2f\n", __func__, dst_y_prefetch);
1092 
1093 	dst_y_per_vm_vblank = get_dst_y_per_vm_vblank(mode_lib,
1094 			e2e_pipe_param,
1095 			num_pipes,
1096 			pipe_idx);
1097 	dst_y_per_row_vblank = get_dst_y_per_row_vblank(mode_lib,
1098 			e2e_pipe_param,
1099 			num_pipes,
1100 			pipe_idx);
1101 	dst_y_per_vm_flip = get_dst_y_per_vm_flip(mode_lib, e2e_pipe_param, num_pipes, pipe_idx);
1102 	dst_y_per_row_flip = get_dst_y_per_row_flip(mode_lib, e2e_pipe_param, num_pipes, pipe_idx);
1103 
1104 	min_dst_y_per_vm_vblank = 8.0;
1105 	min_dst_y_per_row_vblank = 16.0;
1106 
1107 	// magic!
1108 	if (htotal <= 75) {
1109 		min_vblank = 300;
1110 		min_dst_y_per_vm_vblank = 100.0;
1111 		min_dst_y_per_row_vblank = 100.0;
1112 	}
1113 
1114 	dml_print("DML_DLG: %s: dst_y_per_vm_vblank    = %3.2f\n", __func__, dst_y_per_vm_vblank);
1115 	dml_print("DML_DLG: %s: dst_y_per_row_vblank   = %3.2f\n", __func__, dst_y_per_row_vblank);
1116 
1117 	ASSERT(dst_y_per_vm_vblank < min_dst_y_per_vm_vblank);
1118 	ASSERT(dst_y_per_row_vblank < min_dst_y_per_row_vblank);
1119 
1120 	ASSERT(dst_y_prefetch > (dst_y_per_vm_vblank + dst_y_per_row_vblank));
1121 	lsw = dst_y_prefetch - (dst_y_per_vm_vblank + dst_y_per_row_vblank);
1122 
1123 	dml_print("DML_DLG: %s: lsw = %3.2f\n", __func__, lsw);
1124 
1125 	vratio_pre_l = get_vratio_prefetch_l(mode_lib, e2e_pipe_param, num_pipes, pipe_idx);
1126 	vratio_pre_c = get_vratio_prefetch_c(mode_lib, e2e_pipe_param, num_pipes, pipe_idx);
1127 
1128 	dml_print("DML_DLG: %s: vratio_pre_l=%3.2f\n", __func__, vratio_pre_l);
1129 	dml_print("DML_DLG: %s: vratio_pre_c=%3.2f\n", __func__, vratio_pre_c);
1130 
1131 	// Active
1132 	req_per_swath_ub_l = rq_dlg_param.rq_l.req_per_swath_ub;
1133 	req_per_swath_ub_c = rq_dlg_param.rq_c.req_per_swath_ub;
1134 	meta_row_height_l = rq_dlg_param.rq_l.meta_row_height;
1135 	meta_row_height_c = rq_dlg_param.rq_c.meta_row_height;
1136 	swath_width_pixels_ub_l = 0;
1137 	swath_width_pixels_ub_c = 0;
1138 	scaler_rec_in_width_l = 0;
1139 	scaler_rec_in_width_c = 0;
1140 	dpte_row_height_l = rq_dlg_param.rq_l.dpte_row_height;
1141 	dpte_row_height_c = rq_dlg_param.rq_c.dpte_row_height;
1142 
1143 	if (mode_422) {
1144 		swath_width_pixels_ub_l = swath_width_ub_l * 2;  // *2 for 2 pixel per element
1145 		swath_width_pixels_ub_c = swath_width_ub_c * 2;
1146 	} else {
1147 		swath_width_pixels_ub_l = swath_width_ub_l * 1;
1148 		swath_width_pixels_ub_c = swath_width_ub_c * 1;
1149 	}
1150 
1151 	hscale_pixel_rate_l = 0.;
1152 	hscale_pixel_rate_c = 0.;
1153 	min_hratio_fact_l = 1.0;
1154 	min_hratio_fact_c = 1.0;
1155 
1156 	if (htaps_l <= 1)
1157 		min_hratio_fact_l = 2.0;
1158 	else if (htaps_l <= 6) {
1159 		if ((hratio_l * 2.0) > 4.0)
1160 			min_hratio_fact_l = 4.0;
1161 		else
1162 			min_hratio_fact_l = hratio_l * 2.0;
1163 	} else {
1164 		if (hratio_l > 4.0)
1165 			min_hratio_fact_l = 4.0;
1166 		else
1167 			min_hratio_fact_l = hratio_l;
1168 	}
1169 
1170 	hscale_pixel_rate_l = min_hratio_fact_l * dppclk_freq_in_mhz;
1171 
1172 	if (htaps_c <= 1)
1173 		min_hratio_fact_c = 2.0;
1174 	else if (htaps_c <= 6) {
1175 		if ((hratio_c * 2.0) > 4.0)
1176 			min_hratio_fact_c = 4.0;
1177 		else
1178 			min_hratio_fact_c = hratio_c * 2.0;
1179 	} else {
1180 		if (hratio_c > 4.0)
1181 			min_hratio_fact_c = 4.0;
1182 		else
1183 			min_hratio_fact_c = hratio_c;
1184 	}
1185 
1186 	hscale_pixel_rate_c = min_hratio_fact_c * dppclk_freq_in_mhz;
1187 
1188 	refcyc_per_line_delivery_pre_l = 0.;
1189 	refcyc_per_line_delivery_pre_c = 0.;
1190 	refcyc_per_line_delivery_l = 0.;
1191 	refcyc_per_line_delivery_c = 0.;
1192 
1193 	refcyc_per_req_delivery_pre_l = 0.;
1194 	refcyc_per_req_delivery_pre_c = 0.;
1195 	refcyc_per_req_delivery_l = 0.;
1196 	refcyc_per_req_delivery_c = 0.;
1197 
1198 	full_recout_width = 0;
1199 	// In ODM
1200 	if (src->is_hsplit) {
1201 		// This "hack"  is only allowed (and valid) for MPC combine. In ODM
1202 		// combine, you MUST specify the full_recout_width...according to Oswin
1203 		if (dst->full_recout_width == 0 && !dst->odm_combine) {
1204 			dml_print("DML_DLG: %s: Warning: full_recout_width not set in hsplit mode\n",
1205 					__func__);
1206 			full_recout_width = dst->recout_width * 2; // assume half split for dcn1
1207 		} else
1208 			full_recout_width = dst->full_recout_width;
1209 	} else
1210 		full_recout_width = dst->recout_width;
1211 
1212 	// As of DCN2, mpc_combine and odm_combine are mutually exclusive
1213 	refcyc_per_line_delivery_pre_l = get_refcyc_per_delivery(mode_lib,
1214 			refclk_freq_in_mhz,
1215 			pclk_freq_in_mhz,
1216 			dst->odm_combine,
1217 			full_recout_width,
1218 			dst->hactive,
1219 			vratio_pre_l,
1220 			hscale_pixel_rate_l,
1221 			swath_width_pixels_ub_l,
1222 			1); // per line
1223 
1224 	refcyc_per_line_delivery_l = get_refcyc_per_delivery(mode_lib,
1225 			refclk_freq_in_mhz,
1226 			pclk_freq_in_mhz,
1227 			dst->odm_combine,
1228 			full_recout_width,
1229 			dst->hactive,
1230 			vratio_l,
1231 			hscale_pixel_rate_l,
1232 			swath_width_pixels_ub_l,
1233 			1); // per line
1234 
1235 	dml_print("DML_DLG: %s: full_recout_width              = %d\n",
1236 			__func__,
1237 			full_recout_width);
1238 	dml_print("DML_DLG: %s: hscale_pixel_rate_l            = %3.2f\n",
1239 			__func__,
1240 			hscale_pixel_rate_l);
1241 	dml_print("DML_DLG: %s: refcyc_per_line_delivery_pre_l = %3.2f\n",
1242 			__func__,
1243 			refcyc_per_line_delivery_pre_l);
1244 	dml_print("DML_DLG: %s: refcyc_per_line_delivery_l     = %3.2f\n",
1245 			__func__,
1246 			refcyc_per_line_delivery_l);
1247 
1248 	if (dual_plane) {
1249 		refcyc_per_line_delivery_pre_c = get_refcyc_per_delivery(mode_lib,
1250 				refclk_freq_in_mhz,
1251 				pclk_freq_in_mhz,
1252 				dst->odm_combine,
1253 				full_recout_width,
1254 				dst->hactive,
1255 				vratio_pre_c,
1256 				hscale_pixel_rate_c,
1257 				swath_width_pixels_ub_c,
1258 				1); // per line
1259 
1260 		refcyc_per_line_delivery_c = get_refcyc_per_delivery(mode_lib,
1261 				refclk_freq_in_mhz,
1262 				pclk_freq_in_mhz,
1263 				dst->odm_combine,
1264 				full_recout_width,
1265 				dst->hactive,
1266 				vratio_c,
1267 				hscale_pixel_rate_c,
1268 				swath_width_pixels_ub_c,
1269 				1);  // per line
1270 
1271 		dml_print("DML_DLG: %s: refcyc_per_line_delivery_pre_c = %3.2f\n",
1272 				__func__,
1273 				refcyc_per_line_delivery_pre_c);
1274 		dml_print("DML_DLG: %s: refcyc_per_line_delivery_c     = %3.2f\n",
1275 				__func__,
1276 				refcyc_per_line_delivery_c);
1277 	}
1278 
1279 	// TTU - Luma / Chroma
1280 	if (access_dir) {  // vertical access
1281 		scaler_rec_in_width_l = vp_height_l;
1282 		scaler_rec_in_width_c = vp_height_c;
1283 	} else {
1284 		scaler_rec_in_width_l = vp_width_l;
1285 		scaler_rec_in_width_c = vp_width_c;
1286 	}
1287 
1288 	refcyc_per_req_delivery_pre_l = get_refcyc_per_delivery(mode_lib,
1289 			refclk_freq_in_mhz,
1290 			pclk_freq_in_mhz,
1291 			dst->odm_combine,
1292 			full_recout_width,
1293 			dst->hactive,
1294 			vratio_pre_l,
1295 			hscale_pixel_rate_l,
1296 			scaler_rec_in_width_l,
1297 			req_per_swath_ub_l);  // per req
1298 	refcyc_per_req_delivery_l = get_refcyc_per_delivery(mode_lib,
1299 			refclk_freq_in_mhz,
1300 			pclk_freq_in_mhz,
1301 			dst->odm_combine,
1302 			full_recout_width,
1303 			dst->hactive,
1304 			vratio_l,
1305 			hscale_pixel_rate_l,
1306 			scaler_rec_in_width_l,
1307 			req_per_swath_ub_l);  // per req
1308 
1309 	dml_print("DML_DLG: %s: refcyc_per_req_delivery_pre_l = %3.2f\n",
1310 			__func__,
1311 			refcyc_per_req_delivery_pre_l);
1312 	dml_print("DML_DLG: %s: refcyc_per_req_delivery_l     = %3.2f\n",
1313 			__func__,
1314 			refcyc_per_req_delivery_l);
1315 
1316 	ASSERT(refcyc_per_req_delivery_pre_l < dml_pow(2, 13));
1317 	ASSERT(refcyc_per_req_delivery_l < dml_pow(2, 13));
1318 
1319 	if (dual_plane) {
1320 		refcyc_per_req_delivery_pre_c = get_refcyc_per_delivery(mode_lib,
1321 				refclk_freq_in_mhz,
1322 				pclk_freq_in_mhz,
1323 				dst->odm_combine,
1324 				full_recout_width,
1325 				dst->hactive,
1326 				vratio_pre_c,
1327 				hscale_pixel_rate_c,
1328 				scaler_rec_in_width_c,
1329 				req_per_swath_ub_c);  // per req
1330 		refcyc_per_req_delivery_c = get_refcyc_per_delivery(mode_lib,
1331 				refclk_freq_in_mhz,
1332 				pclk_freq_in_mhz,
1333 				dst->odm_combine,
1334 				full_recout_width,
1335 				dst->hactive,
1336 				vratio_c,
1337 				hscale_pixel_rate_c,
1338 				scaler_rec_in_width_c,
1339 				req_per_swath_ub_c);  // per req
1340 
1341 		dml_print("DML_DLG: %s: refcyc_per_req_delivery_pre_c = %3.2f\n",
1342 				__func__,
1343 				refcyc_per_req_delivery_pre_c);
1344 		dml_print("DML_DLG: %s: refcyc_per_req_delivery_c     = %3.2f\n",
1345 				__func__,
1346 				refcyc_per_req_delivery_c);
1347 
1348 		ASSERT(refcyc_per_req_delivery_pre_c < dml_pow(2, 13));
1349 		ASSERT(refcyc_per_req_delivery_c < dml_pow(2, 13));
1350 	}
1351 
1352 	// XFC
1353 	xfc_transfer_delay = get_xfc_transfer_delay(mode_lib, e2e_pipe_param, num_pipes, pipe_idx);
1354 	xfc_precharge_delay = get_xfc_precharge_delay(mode_lib,
1355 			e2e_pipe_param,
1356 			num_pipes,
1357 			pipe_idx);
1358 	xfc_remote_surface_flip_latency = get_xfc_remote_surface_flip_latency(mode_lib,
1359 			e2e_pipe_param,
1360 			num_pipes,
1361 			pipe_idx);
1362 	xfc_dst_y_delta_drq_limit = xfc_remote_surface_flip_latency;
1363 	xfc_prefetch_margin = get_xfc_prefetch_margin(mode_lib,
1364 			e2e_pipe_param,
1365 			num_pipes,
1366 			pipe_idx);
1367 
1368 	// TTU - Cursor
1369 	refcyc_per_req_delivery_pre_cur0 = 0.0;
1370 	refcyc_per_req_delivery_cur0 = 0.0;
1371 	if (src->num_cursors > 0) {
1372 		calculate_ttu_cursor(mode_lib,
1373 				&refcyc_per_req_delivery_pre_cur0,
1374 				&refcyc_per_req_delivery_cur0,
1375 				refclk_freq_in_mhz,
1376 				ref_freq_to_pix_freq,
1377 				hscale_pixel_rate_l,
1378 				scl->hscl_ratio,
1379 				vratio_pre_l,
1380 				vratio_l,
1381 				src->cur0_src_width,
1382 				(enum cursor_bpp)(src->cur0_bpp));
1383 	}
1384 
1385 	refcyc_per_req_delivery_pre_cur1 = 0.0;
1386 	refcyc_per_req_delivery_cur1 = 0.0;
1387 	if (src->num_cursors > 1) {
1388 		calculate_ttu_cursor(mode_lib,
1389 				&refcyc_per_req_delivery_pre_cur1,
1390 				&refcyc_per_req_delivery_cur1,
1391 				refclk_freq_in_mhz,
1392 				ref_freq_to_pix_freq,
1393 				hscale_pixel_rate_l,
1394 				scl->hscl_ratio,
1395 				vratio_pre_l,
1396 				vratio_l,
1397 				src->cur1_src_width,
1398 				(enum cursor_bpp)(src->cur1_bpp));
1399 	}
1400 
1401 	// TTU - Misc
1402 	// all hard-coded
1403 
1404 	// Assignment to register structures
1405 	disp_dlg_regs->dst_y_after_scaler = dst_y_after_scaler; // in terms of line
1406 	disp_dlg_regs->refcyc_x_after_scaler = dst_x_after_scaler * ref_freq_to_pix_freq; // in terms of refclk
1407 	ASSERT(disp_dlg_regs->refcyc_x_after_scaler < (unsigned int) dml_pow(2, 13));
1408 	disp_dlg_regs->dst_y_prefetch = (unsigned int) (dst_y_prefetch * dml_pow(2, 2));
1409 	disp_dlg_regs->dst_y_per_vm_vblank = (unsigned int) (dst_y_per_vm_vblank * dml_pow(2, 2));
1410 	disp_dlg_regs->dst_y_per_row_vblank = (unsigned int) (dst_y_per_row_vblank * dml_pow(2, 2));
1411 	disp_dlg_regs->dst_y_per_vm_flip = (unsigned int) (dst_y_per_vm_flip * dml_pow(2, 2));
1412 	disp_dlg_regs->dst_y_per_row_flip = (unsigned int) (dst_y_per_row_flip * dml_pow(2, 2));
1413 
1414 	disp_dlg_regs->vratio_prefetch = (unsigned int) (vratio_pre_l * dml_pow(2, 19));
1415 	disp_dlg_regs->vratio_prefetch_c = (unsigned int) (vratio_pre_c * dml_pow(2, 19));
1416 
1417 	disp_dlg_regs->refcyc_per_pte_group_vblank_l =
1418 			(unsigned int) (dst_y_per_row_vblank * (double) htotal
1419 					* ref_freq_to_pix_freq / (double) dpte_groups_per_row_ub_l);
1420 	ASSERT(disp_dlg_regs->refcyc_per_pte_group_vblank_l < (unsigned int) dml_pow(2, 13));
1421 
1422 	if (dual_plane) {
1423 		disp_dlg_regs->refcyc_per_pte_group_vblank_c = (unsigned int) (dst_y_per_row_vblank
1424 				* (double) htotal * ref_freq_to_pix_freq
1425 				/ (double) dpte_groups_per_row_ub_c);
1426 		ASSERT(disp_dlg_regs->refcyc_per_pte_group_vblank_c
1427 						< (unsigned int) dml_pow(2, 13));
1428 	}
1429 
1430 	disp_dlg_regs->refcyc_per_meta_chunk_vblank_l =
1431 			(unsigned int) (dst_y_per_row_vblank * (double) htotal
1432 					* ref_freq_to_pix_freq / (double) meta_chunks_per_row_ub_l);
1433 	ASSERT(disp_dlg_regs->refcyc_per_meta_chunk_vblank_l < (unsigned int) dml_pow(2, 13));
1434 
1435 	disp_dlg_regs->refcyc_per_meta_chunk_vblank_c =
1436 			disp_dlg_regs->refcyc_per_meta_chunk_vblank_l; // dcc for 4:2:0 is not supported in dcn1.0.  assigned to be the same as _l for now
1437 
1438 	disp_dlg_regs->refcyc_per_pte_group_flip_l = (unsigned int) (dst_y_per_row_flip * htotal
1439 			* ref_freq_to_pix_freq) / dpte_groups_per_row_ub_l;
1440 	disp_dlg_regs->refcyc_per_meta_chunk_flip_l = (unsigned int) (dst_y_per_row_flip * htotal
1441 			* ref_freq_to_pix_freq) / meta_chunks_per_row_ub_l;
1442 
1443 	if (dual_plane) {
1444 		disp_dlg_regs->refcyc_per_pte_group_flip_c = (unsigned int) (dst_y_per_row_flip
1445 				* htotal * ref_freq_to_pix_freq) / dpte_groups_per_row_ub_c;
1446 		disp_dlg_regs->refcyc_per_meta_chunk_flip_c = (unsigned int) (dst_y_per_row_flip
1447 				* htotal * ref_freq_to_pix_freq) / meta_chunks_per_row_ub_c;
1448 	}
1449 
1450 	disp_dlg_regs->dst_y_per_pte_row_nom_l = (unsigned int) ((double) dpte_row_height_l
1451 			/ (double) vratio_l * dml_pow(2, 2));
1452 	ASSERT(disp_dlg_regs->dst_y_per_pte_row_nom_l < (unsigned int) dml_pow(2, 17));
1453 
1454 	if (dual_plane) {
1455 		disp_dlg_regs->dst_y_per_pte_row_nom_c = (unsigned int) ((double) dpte_row_height_c
1456 				/ (double) vratio_c * dml_pow(2, 2));
1457 		if (disp_dlg_regs->dst_y_per_pte_row_nom_c >= (unsigned int) dml_pow(2, 17)) {
1458 			dml_print("DML_DLG: %s: Warning dst_y_per_pte_row_nom_c %u larger than supported by register format U15.2 %u\n",
1459 					__func__,
1460 					disp_dlg_regs->dst_y_per_pte_row_nom_c,
1461 					(unsigned int) dml_pow(2, 17) - 1);
1462 		}
1463 	}
1464 
1465 	disp_dlg_regs->dst_y_per_meta_row_nom_l = (unsigned int) ((double) meta_row_height_l
1466 			/ (double) vratio_l * dml_pow(2, 2));
1467 	ASSERT(disp_dlg_regs->dst_y_per_meta_row_nom_l < (unsigned int) dml_pow(2, 17));
1468 
1469 	disp_dlg_regs->dst_y_per_meta_row_nom_c = disp_dlg_regs->dst_y_per_meta_row_nom_l; // TODO: dcc for 4:2:0 is not supported in dcn1.0.  assigned to be the same as _l for now
1470 
1471 	disp_dlg_regs->refcyc_per_pte_group_nom_l = (unsigned int) ((double) dpte_row_height_l
1472 			/ (double) vratio_l * (double) htotal * ref_freq_to_pix_freq
1473 			/ (double) dpte_groups_per_row_ub_l);
1474 	if (disp_dlg_regs->refcyc_per_pte_group_nom_l >= (unsigned int) dml_pow(2, 23))
1475 		disp_dlg_regs->refcyc_per_pte_group_nom_l = dml_pow(2, 23) - 1;
1476 	disp_dlg_regs->refcyc_per_meta_chunk_nom_l = (unsigned int) ((double) meta_row_height_l
1477 			/ (double) vratio_l * (double) htotal * ref_freq_to_pix_freq
1478 			/ (double) meta_chunks_per_row_ub_l);
1479 	if (disp_dlg_regs->refcyc_per_meta_chunk_nom_l >= (unsigned int) dml_pow(2, 23))
1480 		disp_dlg_regs->refcyc_per_meta_chunk_nom_l = dml_pow(2, 23) - 1;
1481 
1482 	if (dual_plane) {
1483 		disp_dlg_regs->refcyc_per_pte_group_nom_c =
1484 				(unsigned int) ((double) dpte_row_height_c / (double) vratio_c
1485 						* (double) htotal * ref_freq_to_pix_freq
1486 						/ (double) dpte_groups_per_row_ub_c);
1487 		if (disp_dlg_regs->refcyc_per_pte_group_nom_c >= (unsigned int) dml_pow(2, 23))
1488 			disp_dlg_regs->refcyc_per_pte_group_nom_c = dml_pow(2, 23) - 1;
1489 
1490 		// TODO: Is this the right calculation? Does htotal need to be halved?
1491 		disp_dlg_regs->refcyc_per_meta_chunk_nom_c =
1492 				(unsigned int) ((double) meta_row_height_c / (double) vratio_c
1493 						* (double) htotal * ref_freq_to_pix_freq
1494 						/ (double) meta_chunks_per_row_ub_c);
1495 		if (disp_dlg_regs->refcyc_per_meta_chunk_nom_c >= (unsigned int) dml_pow(2, 23))
1496 			disp_dlg_regs->refcyc_per_meta_chunk_nom_c = dml_pow(2, 23) - 1;
1497 	}
1498 
1499 	disp_dlg_regs->refcyc_per_line_delivery_pre_l = (unsigned int) dml_floor(refcyc_per_line_delivery_pre_l,
1500 			1);
1501 	disp_dlg_regs->refcyc_per_line_delivery_l = (unsigned int) dml_floor(refcyc_per_line_delivery_l,
1502 			1);
1503 	ASSERT(disp_dlg_regs->refcyc_per_line_delivery_pre_l < (unsigned int) dml_pow(2, 13));
1504 	ASSERT(disp_dlg_regs->refcyc_per_line_delivery_l < (unsigned int) dml_pow(2, 13));
1505 
1506 	disp_dlg_regs->refcyc_per_line_delivery_pre_c = (unsigned int) dml_floor(refcyc_per_line_delivery_pre_c,
1507 			1);
1508 	disp_dlg_regs->refcyc_per_line_delivery_c = (unsigned int) dml_floor(refcyc_per_line_delivery_c,
1509 			1);
1510 	ASSERT(disp_dlg_regs->refcyc_per_line_delivery_pre_c < (unsigned int) dml_pow(2, 13));
1511 	ASSERT(disp_dlg_regs->refcyc_per_line_delivery_c < (unsigned int) dml_pow(2, 13));
1512 
1513 	disp_dlg_regs->chunk_hdl_adjust_cur0 = 3;
1514 	disp_dlg_regs->dst_y_offset_cur0 = 0;
1515 	disp_dlg_regs->chunk_hdl_adjust_cur1 = 3;
1516 	disp_dlg_regs->dst_y_offset_cur1 = 0;
1517 
1518 	disp_dlg_regs->xfc_reg_transfer_delay = xfc_transfer_delay;
1519 	disp_dlg_regs->xfc_reg_precharge_delay = xfc_precharge_delay;
1520 	disp_dlg_regs->xfc_reg_remote_surface_flip_latency = xfc_remote_surface_flip_latency;
1521 	disp_dlg_regs->xfc_reg_prefetch_margin = dml_ceil(xfc_prefetch_margin * refclk_freq_in_mhz,
1522 			1);
1523 
1524 	// slave has to have this value also set to off
1525 	if (src->xfc_enable && !src->xfc_slave)
1526 		disp_dlg_regs->dst_y_delta_drq_limit = dml_ceil(xfc_dst_y_delta_drq_limit, 1);
1527 	else
1528 		disp_dlg_regs->dst_y_delta_drq_limit = 0x7fff; // off
1529 
1530 	disp_ttu_regs->refcyc_per_req_delivery_pre_l = (unsigned int) (refcyc_per_req_delivery_pre_l
1531 			* dml_pow(2, 10));
1532 	disp_ttu_regs->refcyc_per_req_delivery_l = (unsigned int) (refcyc_per_req_delivery_l
1533 			* dml_pow(2, 10));
1534 	disp_ttu_regs->refcyc_per_req_delivery_pre_c = (unsigned int) (refcyc_per_req_delivery_pre_c
1535 			* dml_pow(2, 10));
1536 	disp_ttu_regs->refcyc_per_req_delivery_c = (unsigned int) (refcyc_per_req_delivery_c
1537 			* dml_pow(2, 10));
1538 	disp_ttu_regs->refcyc_per_req_delivery_pre_cur0 =
1539 			(unsigned int) (refcyc_per_req_delivery_pre_cur0 * dml_pow(2, 10));
1540 	disp_ttu_regs->refcyc_per_req_delivery_cur0 = (unsigned int) (refcyc_per_req_delivery_cur0
1541 			* dml_pow(2, 10));
1542 	disp_ttu_regs->refcyc_per_req_delivery_pre_cur1 =
1543 			(unsigned int) (refcyc_per_req_delivery_pre_cur1 * dml_pow(2, 10));
1544 	disp_ttu_regs->refcyc_per_req_delivery_cur1 = (unsigned int) (refcyc_per_req_delivery_cur1
1545 			* dml_pow(2, 10));
1546 	disp_ttu_regs->qos_level_low_wm = 0;
1547 	ASSERT(disp_ttu_regs->qos_level_low_wm < dml_pow(2, 14));
1548 	disp_ttu_regs->qos_level_high_wm = (unsigned int) (4.0 * (double) htotal
1549 			* ref_freq_to_pix_freq);
1550 	/*ASSERT(disp_ttu_regs->qos_level_high_wm < dml_pow(2, 14));*/
1551 
1552 	disp_ttu_regs->qos_level_flip = 14;
1553 	disp_ttu_regs->qos_level_fixed_l = 8;
1554 	disp_ttu_regs->qos_level_fixed_c = 8;
1555 	disp_ttu_regs->qos_level_fixed_cur0 = 8;
1556 	disp_ttu_regs->qos_ramp_disable_l = 0;
1557 	disp_ttu_regs->qos_ramp_disable_c = 0;
1558 	disp_ttu_regs->qos_ramp_disable_cur0 = 0;
1559 
1560 	disp_ttu_regs->min_ttu_vblank = min_ttu_vblank * refclk_freq_in_mhz;
1561 	ASSERT(disp_ttu_regs->min_ttu_vblank < dml_pow(2, 24));
1562 
1563 	print__ttu_regs_st(mode_lib, *disp_ttu_regs);
1564 	print__dlg_regs_st(mode_lib, *disp_dlg_regs);
1565 }
1566 
dml20_rq_dlg_get_dlg_reg(struct display_mode_lib * mode_lib,display_dlg_regs_st * dlg_regs,display_ttu_regs_st * ttu_regs,display_e2e_pipe_params_st * e2e_pipe_param,const unsigned int num_pipes,const unsigned int pipe_idx,const bool cstate_en,const bool pstate_en,const bool vm_en,const bool ignore_viewport_pos,const bool immediate_flip_support)1567 void dml20_rq_dlg_get_dlg_reg(struct display_mode_lib *mode_lib,
1568 		display_dlg_regs_st *dlg_regs,
1569 		display_ttu_regs_st *ttu_regs,
1570 		display_e2e_pipe_params_st *e2e_pipe_param,
1571 		const unsigned int num_pipes,
1572 		const unsigned int pipe_idx,
1573 		const bool cstate_en,
1574 		const bool pstate_en,
1575 		const bool vm_en,
1576 		const bool ignore_viewport_pos,
1577 		const bool immediate_flip_support)
1578 {
1579 	display_rq_params_st rq_param = {0};
1580 	display_dlg_sys_params_st dlg_sys_param = {0};
1581 
1582 	// Get watermark and Tex.
1583 	dlg_sys_param.t_urg_wm_us = get_wm_urgent(mode_lib, e2e_pipe_param, num_pipes);
1584 	dlg_sys_param.deepsleep_dcfclk_mhz = get_clk_dcf_deepsleep(mode_lib,
1585 			e2e_pipe_param,
1586 			num_pipes);
1587 	dlg_sys_param.t_extra_us = get_urgent_extra_latency(mode_lib, e2e_pipe_param, num_pipes);
1588 	dlg_sys_param.mem_trip_us = get_wm_memory_trip(mode_lib, e2e_pipe_param, num_pipes);
1589 	dlg_sys_param.t_mclk_wm_us = get_wm_dram_clock_change(mode_lib, e2e_pipe_param, num_pipes);
1590 	dlg_sys_param.t_sr_wm_us = get_wm_stutter_enter_exit(mode_lib, e2e_pipe_param, num_pipes);
1591 	dlg_sys_param.total_flip_bw = get_total_immediate_flip_bw(mode_lib,
1592 			e2e_pipe_param,
1593 			num_pipes);
1594 	dlg_sys_param.total_flip_bytes = get_total_immediate_flip_bytes(mode_lib,
1595 			e2e_pipe_param,
1596 			num_pipes);
1597 	dlg_sys_param.t_srx_delay_us = mode_lib->ip.dcfclk_cstate_latency
1598 			/ dlg_sys_param.deepsleep_dcfclk_mhz; // TODO: Deprecated
1599 
1600 	print__dlg_sys_params_st(mode_lib, dlg_sys_param);
1601 
1602 	// system parameter calculation done
1603 
1604 	dml_print("DML_DLG: Calculation for pipe[%d] start\n\n", pipe_idx);
1605 	dml20_rq_dlg_get_rq_params(mode_lib, &rq_param, e2e_pipe_param[pipe_idx].pipe.src);
1606 	dml20_rq_dlg_get_dlg_params(mode_lib,
1607 			e2e_pipe_param,
1608 			num_pipes,
1609 			pipe_idx,
1610 			dlg_regs,
1611 			ttu_regs,
1612 			rq_param.dlg,
1613 			dlg_sys_param,
1614 			cstate_en,
1615 			pstate_en);
1616 	dml_print("DML_DLG: Calculation for pipe[%d] end\n", pipe_idx);
1617 }
1618 
calculate_ttu_cursor(struct display_mode_lib * mode_lib,double * refcyc_per_req_delivery_pre_cur,double * refcyc_per_req_delivery_cur,double refclk_freq_in_mhz,double ref_freq_to_pix_freq,double hscale_pixel_rate_l,double hscl_ratio,double vratio_pre_l,double vratio_l,unsigned int cur_width,enum cursor_bpp cur_bpp)1619 static void calculate_ttu_cursor(struct display_mode_lib *mode_lib,
1620 		double *refcyc_per_req_delivery_pre_cur,
1621 		double *refcyc_per_req_delivery_cur,
1622 		double refclk_freq_in_mhz,
1623 		double ref_freq_to_pix_freq,
1624 		double hscale_pixel_rate_l,
1625 		double hscl_ratio,
1626 		double vratio_pre_l,
1627 		double vratio_l,
1628 		unsigned int cur_width,
1629 		enum cursor_bpp cur_bpp)
1630 {
1631 	unsigned int cur_src_width = cur_width;
1632 	unsigned int cur_req_size = 0;
1633 	unsigned int cur_req_width = 0;
1634 	double cur_width_ub = 0.0;
1635 	double cur_req_per_width = 0.0;
1636 	double hactive_cur = 0.0;
1637 
1638 	ASSERT(cur_src_width <= 256);
1639 
1640 	*refcyc_per_req_delivery_pre_cur = 0.0;
1641 	*refcyc_per_req_delivery_cur = 0.0;
1642 	if (cur_src_width > 0) {
1643 		unsigned int cur_bit_per_pixel = 0;
1644 
1645 		if (cur_bpp == dm_cur_2bit) {
1646 			cur_req_size = 64; // byte
1647 			cur_bit_per_pixel = 2;
1648 		} else { // 32bit
1649 			cur_bit_per_pixel = 32;
1650 			if (cur_src_width >= 1 && cur_src_width <= 16)
1651 				cur_req_size = 64;
1652 			else if (cur_src_width >= 17 && cur_src_width <= 31)
1653 				cur_req_size = 128;
1654 			else
1655 				cur_req_size = 256;
1656 		}
1657 
1658 		cur_req_width = (double) cur_req_size / ((double) cur_bit_per_pixel / 8.0);
1659 		cur_width_ub = dml_ceil((double) cur_src_width / (double) cur_req_width, 1)
1660 				* (double) cur_req_width;
1661 		cur_req_per_width = cur_width_ub / (double) cur_req_width;
1662 		hactive_cur = (double) cur_src_width / hscl_ratio; // TODO: oswin to think about what to do for cursor
1663 
1664 		if (vratio_pre_l <= 1.0) {
1665 			*refcyc_per_req_delivery_pre_cur = hactive_cur * ref_freq_to_pix_freq
1666 					/ (double) cur_req_per_width;
1667 		} else {
1668 			*refcyc_per_req_delivery_pre_cur = (double) refclk_freq_in_mhz
1669 					* (double) cur_src_width / hscale_pixel_rate_l
1670 					/ (double) cur_req_per_width;
1671 		}
1672 
1673 		ASSERT(*refcyc_per_req_delivery_pre_cur < dml_pow(2, 13));
1674 
1675 		if (vratio_l <= 1.0) {
1676 			*refcyc_per_req_delivery_cur = hactive_cur * ref_freq_to_pix_freq
1677 					/ (double) cur_req_per_width;
1678 		} else {
1679 			*refcyc_per_req_delivery_cur = (double) refclk_freq_in_mhz
1680 					* (double) cur_src_width / hscale_pixel_rate_l
1681 					/ (double) cur_req_per_width;
1682 		}
1683 
1684 		dml_print("DML_DLG: %s: cur_req_width                     = %d\n",
1685 				__func__,
1686 				cur_req_width);
1687 		dml_print("DML_DLG: %s: cur_width_ub                      = %3.2f\n",
1688 				__func__,
1689 				cur_width_ub);
1690 		dml_print("DML_DLG: %s: cur_req_per_width                 = %3.2f\n",
1691 				__func__,
1692 				cur_req_per_width);
1693 		dml_print("DML_DLG: %s: hactive_cur                       = %3.2f\n",
1694 				__func__,
1695 				hactive_cur);
1696 		dml_print("DML_DLG: %s: refcyc_per_req_delivery_pre_cur   = %3.2f\n",
1697 				__func__,
1698 				*refcyc_per_req_delivery_pre_cur);
1699 		dml_print("DML_DLG: %s: refcyc_per_req_delivery_cur       = %3.2f\n",
1700 				__func__,
1701 				*refcyc_per_req_delivery_cur);
1702 
1703 		ASSERT(*refcyc_per_req_delivery_cur < dml_pow(2, 13));
1704 	}
1705 }
1706