1 /*
2  * Copyright 2017 Advanced Micro Devices, Inc.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20  * OTHER DEALINGS IN THE SOFTWARE.
21  *
22  * Authors: AMD
23  *
24  */
25 
26 #ifdef CONFIG_DRM_AMD_DC_DCN2_0
27 
28 #include "../display_mode_lib.h"
29 #include "../dml_inline_defs.h"
30 #include "../display_mode_vba.h"
31 #include "display_mode_vba_21.h"
32 
33 
34 /*
35  * NOTE:
36  *   This file is gcc-parsable HW gospel, coming straight from HW engineers.
37  *
38  * It doesn't adhere to Linux kernel style and sometimes will do things in odd
39  * ways. Unless there is something clearly wrong with it the code should
40  * remain as-is as it provides us with a guarantee from HW that it is correct.
41  */
42 typedef struct {
43 	double DPPCLK;
44 	double DISPCLK;
45 	double PixelClock;
46 	double DCFCLKDeepSleep;
47 	unsigned int DPPPerPlane;
48 	bool ScalerEnabled;
49 	enum scan_direction_class SourceScan;
50 	unsigned int BlockWidth256BytesY;
51 	unsigned int BlockHeight256BytesY;
52 	unsigned int BlockWidth256BytesC;
53 	unsigned int BlockHeight256BytesC;
54 	unsigned int InterlaceEnable;
55 	unsigned int NumberOfCursors;
56 	unsigned int VBlank;
57 	unsigned int HTotal;
58 } Pipe;
59 
60 typedef struct {
61 	bool Enable;
62 	unsigned int MaxPageTableLevels;
63 	unsigned int CachedPageTableLevels;
64 } HostVM;
65 
66 #define BPP_INVALID 0
67 #define BPP_BLENDED_PIPE 0xffffffff
68 #define DCN21_MAX_DSC_IMAGE_WIDTH 5184
69 
70 static void DisplayPipeConfiguration(struct display_mode_lib *mode_lib);
71 static void DISPCLKDPPCLKDCFCLKDeepSleepPrefetchParametersWatermarksAndPerformanceCalculation(
72 		struct display_mode_lib *mode_lib);
73 static unsigned int dscceComputeDelay(
74 		unsigned int bpc,
75 		double bpp,
76 		unsigned int sliceWidth,
77 		unsigned int numSlices,
78 		enum output_format_class pixelFormat);
79 static unsigned int dscComputeDelay(enum output_format_class pixelFormat);
80 // Super monster function with some 45 argument
81 static bool CalculatePrefetchSchedule(
82 		struct display_mode_lib *mode_lib,
83 		double PercentOfIdealDRAMFabricAndSDPPortBWReceivedAfterUrgLatencyPixelMixedWithVMData,
84 		double PercentOfIdealDRAMFabricAndSDPPortBWReceivedAfterUrgLatencyVMDataOnly,
85 		Pipe *myPipe,
86 		unsigned int DSCDelay,
87 		double DPPCLKDelaySubtotal,
88 		double DPPCLKDelaySCL,
89 		double DPPCLKDelaySCLLBOnly,
90 		double DPPCLKDelayCNVCFormater,
91 		double DPPCLKDelayCNVCCursor,
92 		double DISPCLKDelaySubtotal,
93 		unsigned int ScalerRecoutWidth,
94 		enum output_format_class OutputFormat,
95 		unsigned int MaxInterDCNTileRepeaters,
96 		unsigned int VStartup,
97 		unsigned int MaxVStartup,
98 		unsigned int GPUVMPageTableLevels,
99 		bool GPUVMEnable,
100 		HostVM *myHostVM,
101 		bool DynamicMetadataEnable,
102 		int DynamicMetadataLinesBeforeActiveRequired,
103 		unsigned int DynamicMetadataTransmittedBytes,
104 		bool DCCEnable,
105 		double UrgentLatency,
106 		double UrgentExtraLatency,
107 		double TCalc,
108 		unsigned int PDEAndMetaPTEBytesFrame,
109 		unsigned int MetaRowByte,
110 		unsigned int PixelPTEBytesPerRow,
111 		double PrefetchSourceLinesY,
112 		unsigned int SwathWidthY,
113 		double BytePerPixelDETY,
114 		double VInitPreFillY,
115 		unsigned int MaxNumSwathY,
116 		double PrefetchSourceLinesC,
117 		double BytePerPixelDETC,
118 		double VInitPreFillC,
119 		unsigned int MaxNumSwathC,
120 		unsigned int SwathHeightY,
121 		unsigned int SwathHeightC,
122 		double TWait,
123 		bool XFCEnabled,
124 		double XFCRemoteSurfaceFlipDelay,
125 		bool ProgressiveToInterlaceUnitInOPP,
126 		double *DSTXAfterScaler,
127 		double *DSTYAfterScaler,
128 		double *DestinationLinesForPrefetch,
129 		double *PrefetchBandwidth,
130 		double *DestinationLinesToRequestVMInVBlank,
131 		double *DestinationLinesToRequestRowInVBlank,
132 		double *VRatioPrefetchY,
133 		double *VRatioPrefetchC,
134 		double *RequiredPrefetchPixDataBWLuma,
135 		double *RequiredPrefetchPixDataBWChroma,
136 		unsigned int *VStartupRequiredWhenNotEnoughTimeForDynamicMetadata,
137 		double *Tno_bw,
138 		double *prefetch_vmrow_bw,
139 		unsigned int *swath_width_luma_ub,
140 		unsigned int *swath_width_chroma_ub,
141 		unsigned int *VUpdateOffsetPix,
142 		double *VUpdateWidthPix,
143 		double *VReadyOffsetPix);
144 static double RoundToDFSGranularityUp(double Clock, double VCOSpeed);
145 static double RoundToDFSGranularityDown(double Clock, double VCOSpeed);
146 static double CalculateDCCConfiguration(
147 		bool                 DCCEnabled,
148 		bool                 DCCProgrammingAssumesScanDirectionUnknown,
149 		unsigned int         ViewportWidth,
150 		unsigned int         ViewportHeight,
151 		double               DETBufferSize,
152 		unsigned int         RequestHeight256Byte,
153 		unsigned int         SwathHeight,
154 		enum dm_swizzle_mode TilingFormat,
155 		unsigned int         BytePerPixel,
156 		enum scan_direction_class ScanOrientation,
157 		unsigned int        *MaxUncompressedBlock,
158 		unsigned int        *MaxCompressedBlock,
159 		unsigned int        *Independent64ByteBlock);
160 static double CalculatePrefetchSourceLines(
161 		struct display_mode_lib *mode_lib,
162 		double VRatio,
163 		double vtaps,
164 		bool Interlace,
165 		bool ProgressiveToInterlaceUnitInOPP,
166 		unsigned int SwathHeight,
167 		unsigned int ViewportYStart,
168 		double *VInitPreFill,
169 		unsigned int *MaxNumSwath);
170 static unsigned int CalculateVMAndRowBytes(
171 		struct display_mode_lib *mode_lib,
172 		bool DCCEnable,
173 		unsigned int BlockHeight256Bytes,
174 		unsigned int BlockWidth256Bytes,
175 		enum source_format_class SourcePixelFormat,
176 		unsigned int SurfaceTiling,
177 		unsigned int BytePerPixel,
178 		enum scan_direction_class ScanDirection,
179 		unsigned int ViewportWidth,
180 		unsigned int ViewportHeight,
181 		unsigned int SwathWidthY,
182 		bool GPUVMEnable,
183 		bool HostVMEnable,
184 		unsigned int HostVMMaxPageTableLevels,
185 		unsigned int HostVMCachedPageTableLevels,
186 		unsigned int VMMPageSize,
187 		unsigned int PTEBufferSizeInRequests,
188 		unsigned int Pitch,
189 		unsigned int DCCMetaPitch,
190 		unsigned int *MacroTileWidth,
191 		unsigned int *MetaRowByte,
192 		unsigned int *PixelPTEBytesPerRow,
193 		bool *PTEBufferSizeNotExceeded,
194 		unsigned int *dpte_row_width_ub,
195 		unsigned int *dpte_row_height,
196 		unsigned int *MetaRequestWidth,
197 		unsigned int *MetaRequestHeight,
198 		unsigned int *meta_row_width,
199 		unsigned int *meta_row_height,
200 		unsigned int *vm_group_bytes,
201 		long         *dpte_group_bytes,
202 		unsigned int *PixelPTEReqWidth,
203 		unsigned int *PixelPTEReqHeight,
204 		unsigned int *PTERequestSize,
205 		unsigned int *DPDE0BytesFrame,
206 		unsigned int *MetaPTEBytesFrame);
207 
208 static double CalculateTWait(
209 		unsigned int PrefetchMode,
210 		double DRAMClockChangeLatency,
211 		double UrgentLatency,
212 		double SREnterPlusExitTime);
213 static double CalculateRemoteSurfaceFlipDelay(
214 		struct display_mode_lib *mode_lib,
215 		double VRatio,
216 		double SwathWidth,
217 		double Bpp,
218 		double LineTime,
219 		double XFCTSlvVupdateOffset,
220 		double XFCTSlvVupdateWidth,
221 		double XFCTSlvVreadyOffset,
222 		double XFCXBUFLatencyTolerance,
223 		double XFCFillBWOverhead,
224 		double XFCSlvChunkSize,
225 		double XFCBusTransportTime,
226 		double TCalc,
227 		double TWait,
228 		double *SrcActiveDrainRate,
229 		double *TInitXFill,
230 		double *TslvChk);
231 static void CalculateActiveRowBandwidth(
232 		bool GPUVMEnable,
233 		enum source_format_class SourcePixelFormat,
234 		double VRatio,
235 		bool DCCEnable,
236 		double LineTime,
237 		unsigned int MetaRowByteLuma,
238 		unsigned int MetaRowByteChroma,
239 		unsigned int meta_row_height_luma,
240 		unsigned int meta_row_height_chroma,
241 		unsigned int PixelPTEBytesPerRowLuma,
242 		unsigned int PixelPTEBytesPerRowChroma,
243 		unsigned int dpte_row_height_luma,
244 		unsigned int dpte_row_height_chroma,
245 		double *meta_row_bw,
246 		double *dpte_row_bw);
247 static void CalculateFlipSchedule(
248 		struct display_mode_lib *mode_lib,
249 		double PercentOfIdealDRAMFabricAndSDPPortBWReceivedAfterUrgLatencyPixelMixedWithVMData,
250 		double PercentOfIdealDRAMFabricAndSDPPortBWReceivedAfterUrgLatencyVMDataOnly,
251 		double UrgentExtraLatency,
252 		double UrgentLatency,
253 		unsigned int GPUVMMaxPageTableLevels,
254 		bool HostVMEnable,
255 		unsigned int HostVMMaxPageTableLevels,
256 		unsigned int HostVMCachedPageTableLevels,
257 		bool GPUVMEnable,
258 		double PDEAndMetaPTEBytesPerFrame,
259 		double MetaRowBytes,
260 		double DPTEBytesPerRow,
261 		double BandwidthAvailableForImmediateFlip,
262 		unsigned int TotImmediateFlipBytes,
263 		enum source_format_class SourcePixelFormat,
264 		double LineTime,
265 		double VRatio,
266 		double Tno_bw,
267 		bool DCCEnable,
268 		unsigned int dpte_row_height,
269 		unsigned int meta_row_height,
270 		unsigned int dpte_row_height_chroma,
271 		unsigned int meta_row_height_chroma,
272 		double *DestinationLinesToRequestVMInImmediateFlip,
273 		double *DestinationLinesToRequestRowInImmediateFlip,
274 		double *final_flip_bw,
275 		bool *ImmediateFlipSupportedForPipe);
276 static double CalculateWriteBackDelay(
277 		enum source_format_class WritebackPixelFormat,
278 		double WritebackHRatio,
279 		double WritebackVRatio,
280 		unsigned int WritebackLumaHTaps,
281 		unsigned int WritebackLumaVTaps,
282 		unsigned int WritebackChromaHTaps,
283 		unsigned int WritebackChromaVTaps,
284 		unsigned int WritebackDestinationWidth);
285 static void CalculateWatermarksAndDRAMSpeedChangeSupport(
286 		struct display_mode_lib *mode_lib,
287 		unsigned int PrefetchMode,
288 		unsigned int NumberOfActivePlanes,
289 		unsigned int MaxLineBufferLines,
290 		unsigned int LineBufferSize,
291 		unsigned int DPPOutputBufferPixels,
292 		double DETBufferSizeInKByte,
293 		unsigned int WritebackInterfaceLumaBufferSize,
294 		unsigned int WritebackInterfaceChromaBufferSize,
295 		double DCFCLK,
296 		double UrgentOutOfOrderReturn,
297 		double ReturnBW,
298 		bool GPUVMEnable,
299 		long dpte_group_bytes[],
300 		unsigned int MetaChunkSize,
301 		double UrgentLatency,
302 		double ExtraLatency,
303 		double WritebackLatency,
304 		double WritebackChunkSize,
305 		double SOCCLK,
306 		double DRAMClockChangeLatency,
307 		double SRExitTime,
308 		double SREnterPlusExitTime,
309 		double DCFCLKDeepSleep,
310 		int DPPPerPlane[],
311 		bool DCCEnable[],
312 		double DPPCLK[],
313 		unsigned int SwathWidthSingleDPPY[],
314 		unsigned int SwathHeightY[],
315 		double ReadBandwidthPlaneLuma[],
316 		unsigned int SwathHeightC[],
317 		double ReadBandwidthPlaneChroma[],
318 		unsigned int LBBitPerPixel[],
319 		unsigned int SwathWidthY[],
320 		double HRatio[],
321 		unsigned int vtaps[],
322 		unsigned int VTAPsChroma[],
323 		double VRatio[],
324 		unsigned int HTotal[],
325 		double PixelClock[],
326 		unsigned int BlendingAndTiming[],
327 		double BytePerPixelDETY[],
328 		double BytePerPixelDETC[],
329 		bool WritebackEnable[],
330 		enum source_format_class WritebackPixelFormat[],
331 		double WritebackDestinationWidth[],
332 		double WritebackDestinationHeight[],
333 		double WritebackSourceHeight[],
334 		enum clock_change_support *DRAMClockChangeSupport,
335 		double *UrgentWatermark,
336 		double *WritebackUrgentWatermark,
337 		double *DRAMClockChangeWatermark,
338 		double *WritebackDRAMClockChangeWatermark,
339 		double *StutterExitWatermark,
340 		double *StutterEnterPlusExitWatermark,
341 		double *MinActiveDRAMClockChangeLatencySupported);
342 static void CalculateDCFCLKDeepSleep(
343 		struct display_mode_lib *mode_lib,
344 		unsigned int NumberOfActivePlanes,
345 		double BytePerPixelDETY[],
346 		double BytePerPixelDETC[],
347 		double VRatio[],
348 		unsigned int SwathWidthY[],
349 		int DPPPerPlane[],
350 		double HRatio[],
351 		double PixelClock[],
352 		double PSCL_THROUGHPUT[],
353 		double PSCL_THROUGHPUT_CHROMA[],
354 		double DPPCLK[],
355 		double *DCFCLKDeepSleep);
356 static void CalculateDETBufferSize(
357 		double DETBufferSizeInKByte,
358 		unsigned int SwathHeightY,
359 		unsigned int SwathHeightC,
360 		double *DETBufferSizeY,
361 		double *DETBufferSizeC);
362 static void CalculateUrgentBurstFactor(
363 		unsigned int DETBufferSizeInKByte,
364 		unsigned int SwathHeightY,
365 		unsigned int SwathHeightC,
366 		unsigned int SwathWidthY,
367 		double LineTime,
368 		double UrgentLatency,
369 		double CursorBufferSize,
370 		unsigned int CursorWidth,
371 		unsigned int CursorBPP,
372 		double VRatio,
373 		double VRatioPreY,
374 		double VRatioPreC,
375 		double BytePerPixelInDETY,
376 		double BytePerPixelInDETC,
377 		double *UrgentBurstFactorCursor,
378 		double *UrgentBurstFactorCursorPre,
379 		double *UrgentBurstFactorLuma,
380 		double *UrgentBurstFactorLumaPre,
381 		double *UrgentBurstFactorChroma,
382 		double *UrgentBurstFactorChromaPre,
383 		unsigned int *NotEnoughUrgentLatencyHiding,
384 		unsigned int *NotEnoughUrgentLatencyHidingPre);
385 
386 static void CalculatePixelDeliveryTimes(
387 		unsigned int           NumberOfActivePlanes,
388 		double                 VRatio[],
389 		double                 VRatioPrefetchY[],
390 		double                 VRatioPrefetchC[],
391 		unsigned int           swath_width_luma_ub[],
392 		unsigned int           swath_width_chroma_ub[],
393 		int                    DPPPerPlane[],
394 		double                 HRatio[],
395 		double                 PixelClock[],
396 		double                 PSCL_THROUGHPUT[],
397 		double                 PSCL_THROUGHPUT_CHROMA[],
398 		double                 DPPCLK[],
399 		double                 BytePerPixelDETC[],
400 		enum scan_direction_class SourceScan[],
401 		unsigned int           BlockWidth256BytesY[],
402 		unsigned int           BlockHeight256BytesY[],
403 		unsigned int           BlockWidth256BytesC[],
404 		unsigned int           BlockHeight256BytesC[],
405 		double                 DisplayPipeLineDeliveryTimeLuma[],
406 		double                 DisplayPipeLineDeliveryTimeChroma[],
407 		double                 DisplayPipeLineDeliveryTimeLumaPrefetch[],
408 		double                 DisplayPipeLineDeliveryTimeChromaPrefetch[],
409 		double                 DisplayPipeRequestDeliveryTimeLuma[],
410 		double                 DisplayPipeRequestDeliveryTimeChroma[],
411 		double                 DisplayPipeRequestDeliveryTimeLumaPrefetch[],
412 		double                 DisplayPipeRequestDeliveryTimeChromaPrefetch[]);
413 
414 static void CalculateMetaAndPTETimes(
415 		unsigned int           NumberOfActivePlanes,
416 		bool                   GPUVMEnable,
417 		unsigned int           MetaChunkSize,
418 		unsigned int           MinMetaChunkSizeBytes,
419 		unsigned int           GPUVMMaxPageTableLevels,
420 		unsigned int           HTotal[],
421 		double                 VRatio[],
422 		double                 VRatioPrefetchY[],
423 		double                 VRatioPrefetchC[],
424 		double                 DestinationLinesToRequestRowInVBlank[],
425 		double                 DestinationLinesToRequestRowInImmediateFlip[],
426 		double                 DestinationLinesToRequestVMInVBlank[],
427 		double                 DestinationLinesToRequestVMInImmediateFlip[],
428 		bool                   DCCEnable[],
429 		double                 PixelClock[],
430 		double                 BytePerPixelDETY[],
431 		double                 BytePerPixelDETC[],
432 		enum scan_direction_class SourceScan[],
433 		unsigned int           dpte_row_height[],
434 		unsigned int           dpte_row_height_chroma[],
435 		unsigned int           meta_row_width[],
436 		unsigned int           meta_row_height[],
437 		unsigned int           meta_req_width[],
438 		unsigned int           meta_req_height[],
439 		long                   dpte_group_bytes[],
440 		unsigned int           PTERequestSizeY[],
441 		unsigned int           PTERequestSizeC[],
442 		unsigned int           PixelPTEReqWidthY[],
443 		unsigned int           PixelPTEReqHeightY[],
444 		unsigned int           PixelPTEReqWidthC[],
445 		unsigned int           PixelPTEReqHeightC[],
446 		unsigned int           dpte_row_width_luma_ub[],
447 		unsigned int           dpte_row_width_chroma_ub[],
448 		unsigned int           vm_group_bytes[],
449 		unsigned int           dpde0_bytes_per_frame_ub_l[],
450 		unsigned int           dpde0_bytes_per_frame_ub_c[],
451 		unsigned int           meta_pte_bytes_per_frame_ub_l[],
452 		unsigned int           meta_pte_bytes_per_frame_ub_c[],
453 		double                 DST_Y_PER_PTE_ROW_NOM_L[],
454 		double                 DST_Y_PER_PTE_ROW_NOM_C[],
455 		double                 DST_Y_PER_META_ROW_NOM_L[],
456 		double                 TimePerMetaChunkNominal[],
457 		double                 TimePerMetaChunkVBlank[],
458 		double                 TimePerMetaChunkFlip[],
459 		double                 time_per_pte_group_nom_luma[],
460 		double                 time_per_pte_group_vblank_luma[],
461 		double                 time_per_pte_group_flip_luma[],
462 		double                 time_per_pte_group_nom_chroma[],
463 		double                 time_per_pte_group_vblank_chroma[],
464 		double                 time_per_pte_group_flip_chroma[],
465 		double                 TimePerVMGroupVBlank[],
466 		double                 TimePerVMGroupFlip[],
467 		double                 TimePerVMRequestVBlank[],
468 		double                 TimePerVMRequestFlip[]);
469 
470 static double CalculateExtraLatency(
471 		double UrgentRoundTripAndOutOfOrderLatency,
472 		int TotalNumberOfActiveDPP,
473 		int PixelChunkSizeInKByte,
474 		int TotalNumberOfDCCActiveDPP,
475 		int MetaChunkSize,
476 		double ReturnBW,
477 		bool GPUVMEnable,
478 		bool HostVMEnable,
479 		int NumberOfActivePlanes,
480 		int NumberOfDPP[],
481 		long dpte_group_bytes[],
482 		double PercentOfIdealDRAMFabricAndSDPPortBWReceivedAfterUrgLatencyPixelMixedWithVMData,
483 		double PercentOfIdealDRAMFabricAndSDPPortBWReceivedAfterUrgLatencyVMDataOnly,
484 		int HostVMMaxPageTableLevels,
485 		int HostVMCachedPageTableLevels);
486 
487 void dml21_recalculate(struct display_mode_lib *mode_lib)
488 {
489 	ModeSupportAndSystemConfiguration(mode_lib);
490 	PixelClockAdjustmentForProgressiveToInterlaceUnit(mode_lib);
491 	DisplayPipeConfiguration(mode_lib);
492 	DISPCLKDPPCLKDCFCLKDeepSleepPrefetchParametersWatermarksAndPerformanceCalculation(mode_lib);
493 }
494 
495 static unsigned int dscceComputeDelay(
496 		unsigned int bpc,
497 		double bpp,
498 		unsigned int sliceWidth,
499 		unsigned int numSlices,
500 		enum output_format_class pixelFormat)
501 {
502 	// valid bpc         = source bits per component in the set of {8, 10, 12}
503 	// valid bpp         = increments of 1/16 of a bit
504 	//                    min = 6/7/8 in N420/N422/444, respectively
505 	//                    max = such that compression is 1:1
506 	//valid sliceWidth  = number of pixels per slice line, must be less than or equal to 5184/numSlices (or 4096/numSlices in 420 mode)
507 	//valid numSlices   = number of slices in the horiziontal direction per DSC engine in the set of {1, 2, 3, 4}
508 	//valid pixelFormat = pixel/color format in the set of {:N444_RGB, :S422, :N422, :N420}
509 
510 	// fixed value
511 	unsigned int rcModelSize = 8192;
512 
513 	// N422/N420 operate at 2 pixels per clock
514 	unsigned int pixelsPerClock, lstall, D, initalXmitDelay, w, S, ix, wx, p, l0, a, ax, l,
515 			Delay, pixels;
516 
517 	if (pixelFormat == dm_n422 || pixelFormat == dm_420)
518 		pixelsPerClock = 2;
519 	// #all other modes operate at 1 pixel per clock
520 	else
521 		pixelsPerClock = 1;
522 
523 	//initial transmit delay as per PPS
524 	initalXmitDelay = dml_round(rcModelSize / 2.0 / bpp / pixelsPerClock);
525 
526 	//compute ssm delay
527 	if (bpc == 8)
528 		D = 81;
529 	else if (bpc == 10)
530 		D = 89;
531 	else
532 		D = 113;
533 
534 	//divide by pixel per cycle to compute slice width as seen by DSC
535 	w = sliceWidth / pixelsPerClock;
536 
537 	//422 mode has an additional cycle of delay
538 	if (pixelFormat == dm_s422)
539 		S = 1;
540 	else
541 		S = 0;
542 
543 	//main calculation for the dscce
544 	ix = initalXmitDelay + 45;
545 	wx = (w + 2) / 3;
546 	p = 3 * wx - w;
547 	l0 = ix / w;
548 	a = ix + p * l0;
549 	ax = (a + 2) / 3 + D + 6 + 1;
550 	l = (ax + wx - 1) / wx;
551 	if ((ix % w) == 0 && p != 0)
552 		lstall = 1;
553 	else
554 		lstall = 0;
555 	Delay = l * wx * (numSlices - 1) + ax + S + lstall + 22;
556 
557 	//dsc processes 3 pixel containers per cycle and a container can contain 1 or 2 pixels
558 	pixels = Delay * 3 * pixelsPerClock;
559 	return pixels;
560 }
561 
562 static unsigned int dscComputeDelay(enum output_format_class pixelFormat)
563 {
564 	unsigned int Delay = 0;
565 
566 	if (pixelFormat == dm_420) {
567 		//   sfr
568 		Delay = Delay + 2;
569 		//   dsccif
570 		Delay = Delay + 0;
571 		//   dscc - input deserializer
572 		Delay = Delay + 3;
573 		//   dscc gets pixels every other cycle
574 		Delay = Delay + 2;
575 		//   dscc - input cdc fifo
576 		Delay = Delay + 12;
577 		//   dscc gets pixels every other cycle
578 		Delay = Delay + 13;
579 		//   dscc - cdc uncertainty
580 		Delay = Delay + 2;
581 		//   dscc - output cdc fifo
582 		Delay = Delay + 7;
583 		//   dscc gets pixels every other cycle
584 		Delay = Delay + 3;
585 		//   dscc - cdc uncertainty
586 		Delay = Delay + 2;
587 		//   dscc - output serializer
588 		Delay = Delay + 1;
589 		//   sft
590 		Delay = Delay + 1;
591 	} else if (pixelFormat == dm_n422) {
592 		//   sfr
593 		Delay = Delay + 2;
594 		//   dsccif
595 		Delay = Delay + 1;
596 		//   dscc - input deserializer
597 		Delay = Delay + 5;
598 		//  dscc - input cdc fifo
599 		Delay = Delay + 25;
600 		//   dscc - cdc uncertainty
601 		Delay = Delay + 2;
602 		//   dscc - output cdc fifo
603 		Delay = Delay + 10;
604 		//   dscc - cdc uncertainty
605 		Delay = Delay + 2;
606 		//   dscc - output serializer
607 		Delay = Delay + 1;
608 		//   sft
609 		Delay = Delay + 1;
610 	} else {
611 		//   sfr
612 		Delay = Delay + 2;
613 		//   dsccif
614 		Delay = Delay + 0;
615 		//   dscc - input deserializer
616 		Delay = Delay + 3;
617 		//   dscc - input cdc fifo
618 		Delay = Delay + 12;
619 		//   dscc - cdc uncertainty
620 		Delay = Delay + 2;
621 		//   dscc - output cdc fifo
622 		Delay = Delay + 7;
623 		//   dscc - output serializer
624 		Delay = Delay + 1;
625 		//   dscc - cdc uncertainty
626 		Delay = Delay + 2;
627 		//   sft
628 		Delay = Delay + 1;
629 	}
630 
631 	return Delay;
632 }
633 
634 static bool CalculatePrefetchSchedule(
635 		struct display_mode_lib *mode_lib,
636 		double PercentOfIdealDRAMFabricAndSDPPortBWReceivedAfterUrgLatencyPixelMixedWithVMData,
637 		double PercentOfIdealDRAMFabricAndSDPPortBWReceivedAfterUrgLatencyVMDataOnly,
638 		Pipe *myPipe,
639 		unsigned int DSCDelay,
640 		double DPPCLKDelaySubtotal,
641 		double DPPCLKDelaySCL,
642 		double DPPCLKDelaySCLLBOnly,
643 		double DPPCLKDelayCNVCFormater,
644 		double DPPCLKDelayCNVCCursor,
645 		double DISPCLKDelaySubtotal,
646 		unsigned int ScalerRecoutWidth,
647 		enum output_format_class OutputFormat,
648 		unsigned int MaxInterDCNTileRepeaters,
649 		unsigned int VStartup,
650 		unsigned int MaxVStartup,
651 		unsigned int GPUVMPageTableLevels,
652 		bool GPUVMEnable,
653 		HostVM *myHostVM,
654 		bool DynamicMetadataEnable,
655 		int DynamicMetadataLinesBeforeActiveRequired,
656 		unsigned int DynamicMetadataTransmittedBytes,
657 		bool DCCEnable,
658 		double UrgentLatency,
659 		double UrgentExtraLatency,
660 		double TCalc,
661 		unsigned int PDEAndMetaPTEBytesFrame,
662 		unsigned int MetaRowByte,
663 		unsigned int PixelPTEBytesPerRow,
664 		double PrefetchSourceLinesY,
665 		unsigned int SwathWidthY,
666 		double BytePerPixelDETY,
667 		double VInitPreFillY,
668 		unsigned int MaxNumSwathY,
669 		double PrefetchSourceLinesC,
670 		double BytePerPixelDETC,
671 		double VInitPreFillC,
672 		unsigned int MaxNumSwathC,
673 		unsigned int SwathHeightY,
674 		unsigned int SwathHeightC,
675 		double TWait,
676 		bool XFCEnabled,
677 		double XFCRemoteSurfaceFlipDelay,
678 		bool ProgressiveToInterlaceUnitInOPP,
679 		double *DSTXAfterScaler,
680 		double *DSTYAfterScaler,
681 		double *DestinationLinesForPrefetch,
682 		double *PrefetchBandwidth,
683 		double *DestinationLinesToRequestVMInVBlank,
684 		double *DestinationLinesToRequestRowInVBlank,
685 		double *VRatioPrefetchY,
686 		double *VRatioPrefetchC,
687 		double *RequiredPrefetchPixDataBWLuma,
688 		double *RequiredPrefetchPixDataBWChroma,
689 		unsigned int *VStartupRequiredWhenNotEnoughTimeForDynamicMetadata,
690 		double *Tno_bw,
691 		double *prefetch_vmrow_bw,
692 		unsigned int *swath_width_luma_ub,
693 		unsigned int *swath_width_chroma_ub,
694 		unsigned int *VUpdateOffsetPix,
695 		double *VUpdateWidthPix,
696 		double *VReadyOffsetPix)
697 {
698 	bool MyError = false;
699 	unsigned int DPPCycles, DISPCLKCycles;
700 	double DSTTotalPixelsAfterScaler, TotalRepeaterDelayTime;
701 	double Tdm, LineTime, Tsetup;
702 	double dst_y_prefetch_equ;
703 	double Tsw_oto;
704 	double prefetch_bw_oto;
705 	double Tvm_oto;
706 	double Tr0_oto;
707 	double Tvm_oto_lines;
708 	double Tr0_oto_lines;
709 	double Tsw_oto_lines;
710 	double dst_y_prefetch_oto;
711 	double TimeForFetchingMetaPTE = 0;
712 	double TimeForFetchingRowInVBlank = 0;
713 	double LinesToRequestPrefetchPixelData = 0;
714 	double HostVMInefficiencyFactor;
715 	unsigned int HostVMDynamicLevels;
716 
717 	if (GPUVMEnable == true && myHostVM->Enable == true) {
718 		HostVMInefficiencyFactor =
719 				PercentOfIdealDRAMFabricAndSDPPortBWReceivedAfterUrgLatencyPixelMixedWithVMData
720 						/ PercentOfIdealDRAMFabricAndSDPPortBWReceivedAfterUrgLatencyVMDataOnly;
721 		HostVMDynamicLevels = myHostVM->MaxPageTableLevels
722 				- myHostVM->CachedPageTableLevels;
723 	} else {
724 		HostVMInefficiencyFactor = 1;
725 		HostVMDynamicLevels = 0;
726 	}
727 
728 	if (myPipe->ScalerEnabled)
729 		DPPCycles = DPPCLKDelaySubtotal + DPPCLKDelaySCL;
730 	else
731 		DPPCycles = DPPCLKDelaySubtotal + DPPCLKDelaySCLLBOnly;
732 
733 	DPPCycles = DPPCycles + DPPCLKDelayCNVCFormater + myPipe->NumberOfCursors * DPPCLKDelayCNVCCursor;
734 
735 	DISPCLKCycles = DISPCLKDelaySubtotal;
736 
737 	if (myPipe->DPPCLK == 0.0 || myPipe->DISPCLK == 0.0)
738 		return true;
739 
740 	*DSTXAfterScaler = DPPCycles * myPipe->PixelClock / myPipe->DPPCLK
741 			+ DISPCLKCycles * myPipe->PixelClock / myPipe->DISPCLK + DSCDelay;
742 
743 	if (myPipe->DPPPerPlane > 1)
744 		*DSTXAfterScaler = *DSTXAfterScaler + ScalerRecoutWidth;
745 
746 	if (OutputFormat == dm_420 || (myPipe->InterlaceEnable && ProgressiveToInterlaceUnitInOPP))
747 		*DSTYAfterScaler = 1;
748 	else
749 		*DSTYAfterScaler = 0;
750 
751 	DSTTotalPixelsAfterScaler = ((double) (*DSTYAfterScaler * myPipe->HTotal)) + *DSTXAfterScaler;
752 	*DSTYAfterScaler = dml_floor(DSTTotalPixelsAfterScaler / myPipe->HTotal, 1);
753 	*DSTXAfterScaler = DSTTotalPixelsAfterScaler - ((double) (*DSTYAfterScaler * myPipe->HTotal));
754 
755 	*VUpdateOffsetPix = dml_ceil(myPipe->HTotal / 4.0, 1);
756 	TotalRepeaterDelayTime = MaxInterDCNTileRepeaters * (2.0 / myPipe->DPPCLK + 3.0 / myPipe->DISPCLK);
757 	*VUpdateWidthPix = (14.0 / myPipe->DCFCLKDeepSleep + 12.0 / myPipe->DPPCLK + TotalRepeaterDelayTime)
758 			* myPipe->PixelClock;
759 
760 	*VReadyOffsetPix = dml_max(
761 			150.0 / myPipe->DPPCLK,
762 			TotalRepeaterDelayTime + 20.0 / myPipe->DCFCLKDeepSleep + 10.0 / myPipe->DPPCLK)
763 			* myPipe->PixelClock;
764 
765 	Tsetup = (double) (*VUpdateOffsetPix + *VUpdateWidthPix + *VReadyOffsetPix) / myPipe->PixelClock;
766 
767 	LineTime = (double) myPipe->HTotal / myPipe->PixelClock;
768 
769 	if (DynamicMetadataEnable) {
770 		double Tdmbf, Tdmec, Tdmsks;
771 
772 		Tdm = dml_max(0.0, UrgentExtraLatency - TCalc);
773 		Tdmbf = DynamicMetadataTransmittedBytes / 4.0 / myPipe->DISPCLK;
774 		Tdmec = LineTime;
775 		if (DynamicMetadataLinesBeforeActiveRequired == -1)
776 			Tdmsks = myPipe->VBlank * LineTime / 2.0;
777 		else
778 			Tdmsks = DynamicMetadataLinesBeforeActiveRequired * LineTime;
779 		if (myPipe->InterlaceEnable && !ProgressiveToInterlaceUnitInOPP)
780 			Tdmsks = Tdmsks / 2;
781 		if (VStartup * LineTime
782 				< Tsetup + TWait + UrgentExtraLatency + Tdmbf + Tdmec + Tdmsks) {
783 			MyError = true;
784 			*VStartupRequiredWhenNotEnoughTimeForDynamicMetadata = (Tsetup + TWait
785 					+ UrgentExtraLatency + Tdmbf + Tdmec + Tdmsks) / LineTime;
786 		} else
787 			*VStartupRequiredWhenNotEnoughTimeForDynamicMetadata = 0.0;
788 	} else
789 		Tdm = 0;
790 
791 	if (GPUVMEnable) {
792 		if (GPUVMPageTableLevels >= 3)
793 			*Tno_bw = UrgentExtraLatency + UrgentLatency * ((GPUVMPageTableLevels - 2) * (myHostVM->MaxPageTableLevels + 1) - 1);
794 		else
795 			*Tno_bw = 0;
796 	} else if (!DCCEnable)
797 		*Tno_bw = LineTime;
798 	else
799 		*Tno_bw = LineTime / 4;
800 
801 	dst_y_prefetch_equ = VStartup - dml_max(TCalc + TWait, XFCRemoteSurfaceFlipDelay) / LineTime
802 			- (Tsetup + Tdm) / LineTime
803 			- (*DSTYAfterScaler + *DSTXAfterScaler / myPipe->HTotal);
804 
805 	Tsw_oto = dml_max(PrefetchSourceLinesY, PrefetchSourceLinesC) * LineTime;
806 
807 	if (myPipe->SourceScan == dm_horz) {
808 		*swath_width_luma_ub = dml_ceil(SwathWidthY - 1, myPipe->BlockWidth256BytesY) + myPipe->BlockWidth256BytesY;
809 		*swath_width_chroma_ub = dml_ceil(SwathWidthY / 2 - 1, myPipe->BlockWidth256BytesC) + myPipe->BlockWidth256BytesC;
810 	} else {
811 		*swath_width_luma_ub = dml_ceil(SwathWidthY - 1, myPipe->BlockHeight256BytesY) + myPipe->BlockHeight256BytesY;
812 		*swath_width_chroma_ub = dml_ceil(SwathWidthY / 2 - 1, myPipe->BlockHeight256BytesC) + myPipe->BlockHeight256BytesC;
813 	}
814 
815 	prefetch_bw_oto = (PrefetchSourceLinesY * *swath_width_luma_ub * dml_ceil(BytePerPixelDETY, 1) + PrefetchSourceLinesC * *swath_width_chroma_ub * dml_ceil(BytePerPixelDETC, 2)) / Tsw_oto;
816 
817 
818 	if (GPUVMEnable == true) {
819 		Tvm_oto = dml_max(*Tno_bw + PDEAndMetaPTEBytesFrame * HostVMInefficiencyFactor / prefetch_bw_oto,
820 				dml_max(UrgentExtraLatency + UrgentLatency * (GPUVMPageTableLevels * (HostVMDynamicLevels + 1) - 1),
821 					LineTime / 4.0));
822 	} else
823 		Tvm_oto = LineTime / 4.0;
824 
825 	if ((GPUVMEnable == true || DCCEnable == true)) {
826 		Tr0_oto = dml_max(
827 				(MetaRowByte + PixelPTEBytesPerRow * HostVMInefficiencyFactor) / prefetch_bw_oto,
828 				dml_max(UrgentLatency * (HostVMDynamicLevels + 1), dml_max(LineTime - Tvm_oto, LineTime / 4)));
829 	} else
830 		Tr0_oto = (LineTime - Tvm_oto) / 2.0;
831 
832 	Tvm_oto_lines = dml_ceil(4 * Tvm_oto / LineTime, 1) / 4.0;
833 	Tr0_oto_lines = dml_ceil(4 * Tr0_oto / LineTime, 1) / 4.0;
834 	Tsw_oto_lines = dml_ceil(4 * Tsw_oto / LineTime, 1) / 4.0;
835 	dst_y_prefetch_oto = Tvm_oto_lines + 2 * Tr0_oto_lines + Tsw_oto_lines + 0.75;
836 
837 	dst_y_prefetch_equ = dml_floor(4.0 * (dst_y_prefetch_equ + 0.125), 1) / 4.0;
838 
839 	if (dst_y_prefetch_oto < dst_y_prefetch_equ)
840 		*DestinationLinesForPrefetch = dst_y_prefetch_oto;
841 	else
842 		*DestinationLinesForPrefetch = dst_y_prefetch_equ;
843 
844 	dml_print("DML: VStartup: %d\n", VStartup);
845 	dml_print("DML: TCalc: %f\n", TCalc);
846 	dml_print("DML: TWait: %f\n", TWait);
847 	dml_print("DML: XFCRemoteSurfaceFlipDelay: %f\n", XFCRemoteSurfaceFlipDelay);
848 	dml_print("DML: LineTime: %f\n", LineTime);
849 	dml_print("DML: Tsetup: %f\n", Tsetup);
850 	dml_print("DML: Tdm: %f\n", Tdm);
851 	dml_print("DML: DSTYAfterScaler: %f\n", *DSTYAfterScaler);
852 	dml_print("DML: DSTXAfterScaler: %f\n", *DSTXAfterScaler);
853 	dml_print("DML: HTotal: %d\n", myPipe->HTotal);
854 
855 	*PrefetchBandwidth = 0;
856 	*DestinationLinesToRequestVMInVBlank = 0;
857 	*DestinationLinesToRequestRowInVBlank = 0;
858 	*VRatioPrefetchY = 0;
859 	*VRatioPrefetchC = 0;
860 	*RequiredPrefetchPixDataBWLuma = 0;
861 	if (*DestinationLinesForPrefetch > 1) {
862 		double PrefetchBandwidth1 = (PDEAndMetaPTEBytesFrame * HostVMInefficiencyFactor + 2 * MetaRowByte
863 				+ 2 * PixelPTEBytesPerRow * HostVMInefficiencyFactor
864 				+ PrefetchSourceLinesY * *swath_width_luma_ub * dml_ceil(BytePerPixelDETY, 1)
865 				+ PrefetchSourceLinesC * *swath_width_chroma_ub * dml_ceil(BytePerPixelDETC, 2))
866 				/ (*DestinationLinesForPrefetch * LineTime - *Tno_bw);
867 
868 		double PrefetchBandwidth2 = (PDEAndMetaPTEBytesFrame *
869 				HostVMInefficiencyFactor + PrefetchSourceLinesY *
870 				*swath_width_luma_ub * dml_ceil(BytePerPixelDETY, 1) +
871 				PrefetchSourceLinesC * *swath_width_chroma_ub *
872 				dml_ceil(BytePerPixelDETC, 2)) /
873 				(*DestinationLinesForPrefetch * LineTime - *Tno_bw - 2 *
874 				UrgentLatency * (1 + HostVMDynamicLevels));
875 
876 		double PrefetchBandwidth3 = (2 * MetaRowByte + 2 * PixelPTEBytesPerRow
877 				* HostVMInefficiencyFactor + PrefetchSourceLinesY *
878 				*swath_width_luma_ub * dml_ceil(BytePerPixelDETY, 1) +
879 				PrefetchSourceLinesC * *swath_width_chroma_ub *
880 				dml_ceil(BytePerPixelDETC, 2)) /
881 				(*DestinationLinesForPrefetch * LineTime -
882 				UrgentExtraLatency - UrgentLatency * (GPUVMPageTableLevels
883 				* (HostVMDynamicLevels + 1) - 1));
884 
885 		double PrefetchBandwidth4 = (PrefetchSourceLinesY * *swath_width_luma_ub *
886 				dml_ceil(BytePerPixelDETY, 1) + PrefetchSourceLinesC *
887 				*swath_width_chroma_ub * dml_ceil(BytePerPixelDETC, 2)) /
888 				(*DestinationLinesForPrefetch * LineTime -
889 				UrgentExtraLatency - UrgentLatency * (GPUVMPageTableLevels
890 				* (HostVMDynamicLevels + 1) - 1) - 2 * UrgentLatency *
891 				(1 + HostVMDynamicLevels));
892 
893 		if (VStartup == MaxVStartup && (PrefetchBandwidth1 > 4 * prefetch_bw_oto) && (*DestinationLinesForPrefetch - dml_ceil(Tsw_oto_lines, 1) / 4.0 - 0.75) * LineTime - *Tno_bw > 0) {
894 			PrefetchBandwidth1 = (PDEAndMetaPTEBytesFrame * HostVMInefficiencyFactor + 2 * MetaRowByte + 2 * PixelPTEBytesPerRow * HostVMInefficiencyFactor) / ((*DestinationLinesForPrefetch - dml_ceil(Tsw_oto_lines, 1) / 4.0 - 0.75) * LineTime - *Tno_bw);
895 		}
896 		if (*Tno_bw + PDEAndMetaPTEBytesFrame * HostVMInefficiencyFactor / PrefetchBandwidth1 >= UrgentExtraLatency + UrgentLatency * (GPUVMPageTableLevels * (HostVMDynamicLevels + 1) - 1) && (MetaRowByte + PixelPTEBytesPerRow * HostVMInefficiencyFactor) / PrefetchBandwidth1 >= UrgentLatency * (1 + HostVMDynamicLevels)) {
897 			*PrefetchBandwidth = PrefetchBandwidth1;
898 		} else if (*Tno_bw + PDEAndMetaPTEBytesFrame * HostVMInefficiencyFactor / PrefetchBandwidth2 >= UrgentExtraLatency + UrgentLatency * (GPUVMPageTableLevels * (HostVMDynamicLevels + 1) - 1) && (MetaRowByte + PixelPTEBytesPerRow * HostVMInefficiencyFactor) / PrefetchBandwidth2 < UrgentLatency * (1 + HostVMDynamicLevels)) {
899 			*PrefetchBandwidth = PrefetchBandwidth2;
900 		} else if (*Tno_bw + PDEAndMetaPTEBytesFrame * HostVMInefficiencyFactor / PrefetchBandwidth3 < UrgentExtraLatency + UrgentLatency * (GPUVMPageTableLevels * (HostVMDynamicLevels + 1) - 1) && (MetaRowByte + PixelPTEBytesPerRow * HostVMInefficiencyFactor) / PrefetchBandwidth3 >= UrgentLatency * (1 + HostVMDynamicLevels)) {
901 			*PrefetchBandwidth = PrefetchBandwidth3;
902 		} else {
903 			*PrefetchBandwidth = PrefetchBandwidth4;
904 		}
905 
906 		if (GPUVMEnable) {
907 			TimeForFetchingMetaPTE = dml_max(*Tno_bw + (double) PDEAndMetaPTEBytesFrame * HostVMInefficiencyFactor / *PrefetchBandwidth,
908 					dml_max(UrgentExtraLatency + UrgentLatency * (GPUVMPageTableLevels * (HostVMDynamicLevels + 1) - 1), LineTime / 4));
909 		} else {
910 // 5/30/2018 - This was an optimization requested from Sy but now NumberOfCursors is no longer a factor
911 //             so if this needs to be reinstated, then it should be officially done in the VBA code as well.
912 //			if (mode_lib->NumberOfCursors > 0 || XFCEnabled)
913 				TimeForFetchingMetaPTE = LineTime / 4;
914 //			else
915 //				TimeForFetchingMetaPTE = 0.0;
916 		}
917 
918 		if ((GPUVMEnable == true || DCCEnable == true)) {
919 			TimeForFetchingRowInVBlank =
920 					dml_max(
921 							(MetaRowByte + PixelPTEBytesPerRow * HostVMInefficiencyFactor)
922 									/ *PrefetchBandwidth,
923 							dml_max(
924 									UrgentLatency * (1 + HostVMDynamicLevels),
925 									dml_max(
926 											(LineTime
927 													- TimeForFetchingMetaPTE) / 2.0,
928 											LineTime
929 													/ 4.0)));
930 		} else {
931 // See note above dated 5/30/2018
932 //			if (NumberOfCursors > 0 || XFCEnabled)
933 				TimeForFetchingRowInVBlank = (LineTime - TimeForFetchingMetaPTE) / 2.0;
934 //			else // TODO: Did someone else add this??
935 //				TimeForFetchingRowInVBlank = 0.0;
936 		}
937 
938 		*DestinationLinesToRequestVMInVBlank = dml_ceil(4.0 * TimeForFetchingMetaPTE / LineTime, 1.0) / 4.0;
939 
940 		*DestinationLinesToRequestRowInVBlank = dml_ceil(4.0 * TimeForFetchingRowInVBlank / LineTime, 1.0) / 4.0;
941 
942 		LinesToRequestPrefetchPixelData = *DestinationLinesForPrefetch
943 // See note above dated 5/30/2018
944 //						- ((NumberOfCursors > 0 || GPUVMEnable || DCCEnable) ?
945 						- ((GPUVMEnable || DCCEnable) ?
946 								(*DestinationLinesToRequestVMInVBlank + 2 * *DestinationLinesToRequestRowInVBlank) :
947 								0.0); // TODO: Did someone else add this??
948 
949 		if (LinesToRequestPrefetchPixelData > 0) {
950 
951 			*VRatioPrefetchY = (double) PrefetchSourceLinesY
952 					/ LinesToRequestPrefetchPixelData;
953 			*VRatioPrefetchY = dml_max(*VRatioPrefetchY, 1.0);
954 			if ((SwathHeightY > 4) && (VInitPreFillY > 3)) {
955 				if (LinesToRequestPrefetchPixelData > (VInitPreFillY - 3.0) / 2.0) {
956 					*VRatioPrefetchY =
957 							dml_max(
958 									(double) PrefetchSourceLinesY
959 											/ LinesToRequestPrefetchPixelData,
960 									(double) MaxNumSwathY
961 											* SwathHeightY
962 											/ (LinesToRequestPrefetchPixelData
963 													- (VInitPreFillY
964 															- 3.0)
965 															/ 2.0));
966 					*VRatioPrefetchY = dml_max(*VRatioPrefetchY, 1.0);
967 				} else {
968 					MyError = true;
969 					*VRatioPrefetchY = 0;
970 				}
971 			}
972 
973 			*VRatioPrefetchC = (double) PrefetchSourceLinesC
974 					/ LinesToRequestPrefetchPixelData;
975 			*VRatioPrefetchC = dml_max(*VRatioPrefetchC, 1.0);
976 
977 			if ((SwathHeightC > 4)) {
978 				if (LinesToRequestPrefetchPixelData > (VInitPreFillC - 3.0) / 2.0) {
979 					*VRatioPrefetchC =
980 							dml_max(
981 									*VRatioPrefetchC,
982 									(double) MaxNumSwathC
983 											* SwathHeightC
984 											/ (LinesToRequestPrefetchPixelData
985 													- (VInitPreFillC
986 															- 3.0)
987 															/ 2.0));
988 					*VRatioPrefetchC = dml_max(*VRatioPrefetchC, 1.0);
989 				} else {
990 					MyError = true;
991 					*VRatioPrefetchC = 0;
992 				}
993 			}
994 
995 			*RequiredPrefetchPixDataBWLuma = myPipe->DPPPerPlane
996 					* (double) PrefetchSourceLinesY / LinesToRequestPrefetchPixelData
997 					* dml_ceil(BytePerPixelDETY, 1)
998 					* *swath_width_luma_ub / LineTime;
999 			*RequiredPrefetchPixDataBWChroma = myPipe->DPPPerPlane
1000 					* (double) PrefetchSourceLinesC / LinesToRequestPrefetchPixelData
1001 					* dml_ceil(BytePerPixelDETC, 2)
1002 					* *swath_width_chroma_ub / LineTime;
1003 		} else {
1004 			MyError = true;
1005 			*VRatioPrefetchY = 0;
1006 			*VRatioPrefetchC = 0;
1007 			*RequiredPrefetchPixDataBWLuma = 0;
1008 			*RequiredPrefetchPixDataBWChroma = 0;
1009 		}
1010 
1011 		dml_print("DML: Tvm: %fus\n", TimeForFetchingMetaPTE);
1012 		dml_print("DML: Tr0: %fus\n", TimeForFetchingRowInVBlank);
1013 		dml_print("DML: Tsw: %fus\n", (double)(*DestinationLinesForPrefetch) * LineTime - TimeForFetchingMetaPTE - TimeForFetchingRowInVBlank);
1014 		dml_print("DML: Tpre: %fus\n", (double)(*DestinationLinesForPrefetch) * LineTime);
1015 		dml_print("DML: row_bytes = dpte_row_bytes (per_pipe) = PixelPTEBytesPerRow = : %d\n", PixelPTEBytesPerRow);
1016 
1017 	} else {
1018 		MyError = true;
1019 	}
1020 
1021 	{
1022 		double prefetch_vm_bw;
1023 		double prefetch_row_bw;
1024 
1025 		if (PDEAndMetaPTEBytesFrame == 0) {
1026 			prefetch_vm_bw = 0;
1027 		} else if (*DestinationLinesToRequestVMInVBlank > 0) {
1028 			prefetch_vm_bw = PDEAndMetaPTEBytesFrame * HostVMInefficiencyFactor / (*DestinationLinesToRequestVMInVBlank * LineTime);
1029 		} else {
1030 			prefetch_vm_bw = 0;
1031 			MyError = true;
1032 		}
1033 		if (MetaRowByte + PixelPTEBytesPerRow == 0) {
1034 			prefetch_row_bw = 0;
1035 		} else if (*DestinationLinesToRequestRowInVBlank > 0) {
1036 			prefetch_row_bw = (MetaRowByte + PixelPTEBytesPerRow * HostVMInefficiencyFactor) / (*DestinationLinesToRequestRowInVBlank * LineTime);
1037 		} else {
1038 			prefetch_row_bw = 0;
1039 			MyError = true;
1040 		}
1041 
1042 		*prefetch_vmrow_bw = dml_max(prefetch_vm_bw, prefetch_row_bw);
1043 	}
1044 
1045 	if (MyError) {
1046 		*PrefetchBandwidth = 0;
1047 		TimeForFetchingMetaPTE = 0;
1048 		TimeForFetchingRowInVBlank = 0;
1049 		*DestinationLinesToRequestVMInVBlank = 0;
1050 		*DestinationLinesToRequestRowInVBlank = 0;
1051 		*DestinationLinesForPrefetch = 0;
1052 		LinesToRequestPrefetchPixelData = 0;
1053 		*VRatioPrefetchY = 0;
1054 		*VRatioPrefetchC = 0;
1055 		*RequiredPrefetchPixDataBWLuma = 0;
1056 		*RequiredPrefetchPixDataBWChroma = 0;
1057 	}
1058 
1059 	return MyError;
1060 }
1061 
1062 static double RoundToDFSGranularityUp(double Clock, double VCOSpeed)
1063 {
1064 	return VCOSpeed * 4 / dml_floor(VCOSpeed * 4 / Clock, 1);
1065 }
1066 
1067 static double RoundToDFSGranularityDown(double Clock, double VCOSpeed)
1068 {
1069 	return VCOSpeed * 4 / dml_ceil(VCOSpeed * 4 / Clock, 1);
1070 }
1071 
1072 static double CalculateDCCConfiguration(
1073 		bool DCCEnabled,
1074 		bool DCCProgrammingAssumesScanDirectionUnknown,
1075 		unsigned int ViewportWidth,
1076 		unsigned int ViewportHeight,
1077 		double DETBufferSize,
1078 		unsigned int RequestHeight256Byte,
1079 		unsigned int SwathHeight,
1080 		enum dm_swizzle_mode TilingFormat,
1081 		unsigned int BytePerPixel,
1082 		enum scan_direction_class ScanOrientation,
1083 		unsigned int *MaxUncompressedBlock,
1084 		unsigned int *MaxCompressedBlock,
1085 		unsigned int *Independent64ByteBlock)
1086 {
1087 	double MaximumDCCCompressionSurface = 0.0;
1088 	enum {
1089 		REQ_256Bytes,
1090 		REQ_128BytesNonContiguous,
1091 		REQ_128BytesContiguous,
1092 		REQ_NA
1093 	} Request = REQ_NA;
1094 
1095 	if (DCCEnabled == true) {
1096 		if (DCCProgrammingAssumesScanDirectionUnknown == true) {
1097 			if (DETBufferSize >= RequestHeight256Byte * ViewportWidth * BytePerPixel
1098 					&& DETBufferSize
1099 							>= 256 / RequestHeight256Byte
1100 									* ViewportHeight) {
1101 				Request = REQ_256Bytes;
1102 			} else if ((DETBufferSize
1103 					< RequestHeight256Byte * ViewportWidth * BytePerPixel
1104 					&& (BytePerPixel == 2 || BytePerPixel == 4))
1105 					|| (DETBufferSize
1106 							< 256 / RequestHeight256Byte
1107 									* ViewportHeight
1108 							&& BytePerPixel == 8
1109 							&& (TilingFormat == dm_sw_4kb_d
1110 									|| TilingFormat
1111 											== dm_sw_4kb_d_x
1112 									|| TilingFormat
1113 											== dm_sw_var_d
1114 									|| TilingFormat
1115 											== dm_sw_var_d_x
1116 									|| TilingFormat
1117 											== dm_sw_64kb_d
1118 									|| TilingFormat
1119 											== dm_sw_64kb_d_x
1120 									|| TilingFormat
1121 											== dm_sw_64kb_d_t
1122 									|| TilingFormat
1123 											== dm_sw_64kb_r_x))) {
1124 				Request = REQ_128BytesNonContiguous;
1125 			} else {
1126 				Request = REQ_128BytesContiguous;
1127 			}
1128 		} else {
1129 			if (BytePerPixel == 1) {
1130 				if (ScanOrientation == dm_vert || SwathHeight == 16) {
1131 					Request = REQ_256Bytes;
1132 				} else {
1133 					Request = REQ_128BytesContiguous;
1134 				}
1135 			} else if (BytePerPixel == 2) {
1136 				if ((ScanOrientation == dm_vert && SwathHeight == 16) || (ScanOrientation != dm_vert && SwathHeight == 8)) {
1137 					Request = REQ_256Bytes;
1138 				} else if (ScanOrientation == dm_vert) {
1139 					Request = REQ_128BytesContiguous;
1140 				} else {
1141 					Request = REQ_128BytesNonContiguous;
1142 				}
1143 			} else if (BytePerPixel == 4) {
1144 				if (SwathHeight == 8) {
1145 					Request = REQ_256Bytes;
1146 				} else if (ScanOrientation == dm_vert) {
1147 					Request = REQ_128BytesContiguous;
1148 				} else {
1149 					Request = REQ_128BytesNonContiguous;
1150 				}
1151 			} else if (BytePerPixel == 8) {
1152 				if (TilingFormat == dm_sw_4kb_d || TilingFormat == dm_sw_4kb_d_x
1153 						|| TilingFormat == dm_sw_var_d
1154 						|| TilingFormat == dm_sw_var_d_x
1155 						|| TilingFormat == dm_sw_64kb_d
1156 						|| TilingFormat == dm_sw_64kb_d_x
1157 						|| TilingFormat == dm_sw_64kb_d_t
1158 						|| TilingFormat == dm_sw_64kb_r_x) {
1159 					if ((ScanOrientation == dm_vert && SwathHeight == 8)
1160 							|| (ScanOrientation != dm_vert
1161 									&& SwathHeight == 4)) {
1162 						Request = REQ_256Bytes;
1163 					} else if (ScanOrientation != dm_vert) {
1164 						Request = REQ_128BytesContiguous;
1165 					} else {
1166 						Request = REQ_128BytesNonContiguous;
1167 					}
1168 				} else {
1169 					if (ScanOrientation != dm_vert || SwathHeight == 8) {
1170 						Request = REQ_256Bytes;
1171 					} else {
1172 						Request = REQ_128BytesContiguous;
1173 					}
1174 				}
1175 			}
1176 		}
1177 	} else {
1178 		Request = REQ_NA;
1179 	}
1180 
1181 	if (Request == REQ_256Bytes) {
1182 		*MaxUncompressedBlock = 256;
1183 		*MaxCompressedBlock = 256;
1184 		*Independent64ByteBlock = false;
1185 		MaximumDCCCompressionSurface = 4.0;
1186 	} else if (Request == REQ_128BytesContiguous) {
1187 		*MaxUncompressedBlock = 128;
1188 		*MaxCompressedBlock = 128;
1189 		*Independent64ByteBlock = false;
1190 		MaximumDCCCompressionSurface = 2.0;
1191 	} else if (Request == REQ_128BytesNonContiguous) {
1192 		*MaxUncompressedBlock = 256;
1193 		*MaxCompressedBlock = 64;
1194 		*Independent64ByteBlock = true;
1195 		MaximumDCCCompressionSurface = 4.0;
1196 	} else {
1197 		*MaxUncompressedBlock = 0;
1198 		*MaxCompressedBlock = 0;
1199 		*Independent64ByteBlock = 0;
1200 		MaximumDCCCompressionSurface = 0.0;
1201 	}
1202 
1203 	return MaximumDCCCompressionSurface;
1204 }
1205 
1206 static double CalculatePrefetchSourceLines(
1207 		struct display_mode_lib *mode_lib,
1208 		double VRatio,
1209 		double vtaps,
1210 		bool Interlace,
1211 		bool ProgressiveToInterlaceUnitInOPP,
1212 		unsigned int SwathHeight,
1213 		unsigned int ViewportYStart,
1214 		double *VInitPreFill,
1215 		unsigned int *MaxNumSwath)
1216 {
1217 	unsigned int MaxPartialSwath;
1218 
1219 	if (ProgressiveToInterlaceUnitInOPP)
1220 		*VInitPreFill = dml_floor((VRatio + vtaps + 1) / 2.0, 1);
1221 	else
1222 		*VInitPreFill = dml_floor((VRatio + vtaps + 1 + Interlace * 0.5 * VRatio) / 2.0, 1);
1223 
1224 	if (!mode_lib->vba.IgnoreViewportPositioning) {
1225 
1226 		*MaxNumSwath = dml_ceil((*VInitPreFill - 1.0) / SwathHeight, 1) + 1.0;
1227 
1228 		if (*VInitPreFill > 1.0)
1229 			MaxPartialSwath = (unsigned int) (*VInitPreFill - 2) % SwathHeight;
1230 		else
1231 			MaxPartialSwath = (unsigned int) (*VInitPreFill + SwathHeight - 2)
1232 					% SwathHeight;
1233 		MaxPartialSwath = dml_max(1U, MaxPartialSwath);
1234 
1235 	} else {
1236 
1237 		if (ViewportYStart != 0)
1238 			dml_print(
1239 					"WARNING DML: using viewport y position of 0 even though actual viewport y position is non-zero in prefetch source lines calculation\n");
1240 
1241 		*MaxNumSwath = dml_ceil(*VInitPreFill / SwathHeight, 1);
1242 
1243 		if (*VInitPreFill > 1.0)
1244 			MaxPartialSwath = (unsigned int) (*VInitPreFill - 1) % SwathHeight;
1245 		else
1246 			MaxPartialSwath = (unsigned int) (*VInitPreFill + SwathHeight - 1)
1247 					% SwathHeight;
1248 	}
1249 
1250 	return *MaxNumSwath * SwathHeight + MaxPartialSwath;
1251 }
1252 
1253 static unsigned int CalculateVMAndRowBytes(
1254 		struct display_mode_lib *mode_lib,
1255 		bool DCCEnable,
1256 		unsigned int BlockHeight256Bytes,
1257 		unsigned int BlockWidth256Bytes,
1258 		enum source_format_class SourcePixelFormat,
1259 		unsigned int SurfaceTiling,
1260 		unsigned int BytePerPixel,
1261 		enum scan_direction_class ScanDirection,
1262 		unsigned int ViewportWidth,
1263 		unsigned int ViewportHeight,
1264 		unsigned int SwathWidth,
1265 		bool GPUVMEnable,
1266 		bool HostVMEnable,
1267 		unsigned int HostVMMaxPageTableLevels,
1268 		unsigned int HostVMCachedPageTableLevels,
1269 		unsigned int VMMPageSize,
1270 		unsigned int PTEBufferSizeInRequests,
1271 		unsigned int Pitch,
1272 		unsigned int DCCMetaPitch,
1273 		unsigned int *MacroTileWidth,
1274 		unsigned int *MetaRowByte,
1275 		unsigned int *PixelPTEBytesPerRow,
1276 		bool *PTEBufferSizeNotExceeded,
1277 		unsigned int *dpte_row_width_ub,
1278 		unsigned int *dpte_row_height,
1279 		unsigned int *MetaRequestWidth,
1280 		unsigned int *MetaRequestHeight,
1281 		unsigned int *meta_row_width,
1282 		unsigned int *meta_row_height,
1283 		unsigned int *vm_group_bytes,
1284 		long         *dpte_group_bytes,
1285 		unsigned int *PixelPTEReqWidth,
1286 		unsigned int *PixelPTEReqHeight,
1287 		unsigned int *PTERequestSize,
1288 		unsigned int *DPDE0BytesFrame,
1289 		unsigned int *MetaPTEBytesFrame)
1290 {
1291 	unsigned int MPDEBytesFrame;
1292 	unsigned int DCCMetaSurfaceBytes;
1293 	unsigned int MacroTileSizeBytes;
1294 	unsigned int MacroTileHeight;
1295 	unsigned int ExtraDPDEBytesFrame;
1296 	unsigned int PDEAndMetaPTEBytesFrame;
1297 	unsigned int PixelPTEReqHeightPTEs;
1298 
1299 	if (DCCEnable == true) {
1300 		*MetaRequestHeight = 8 * BlockHeight256Bytes;
1301 		*MetaRequestWidth = 8 * BlockWidth256Bytes;
1302 		if (ScanDirection == dm_horz) {
1303 			*meta_row_height = *MetaRequestHeight;
1304 			*meta_row_width = dml_ceil((double) SwathWidth - 1, *MetaRequestWidth)
1305 					+ *MetaRequestWidth;
1306 			*MetaRowByte = *meta_row_width * *MetaRequestHeight * BytePerPixel / 256.0;
1307 		} else {
1308 			*meta_row_height = *MetaRequestWidth;
1309 			*meta_row_width = dml_ceil((double) SwathWidth - 1, *MetaRequestHeight)
1310 					+ *MetaRequestHeight;
1311 			*MetaRowByte = *meta_row_width * *MetaRequestWidth * BytePerPixel / 256.0;
1312 		}
1313 		if (ScanDirection == dm_horz) {
1314 			DCCMetaSurfaceBytes = DCCMetaPitch
1315 					* (dml_ceil(ViewportHeight - 1, 64 * BlockHeight256Bytes)
1316 							+ 64 * BlockHeight256Bytes) * BytePerPixel
1317 					/ 256;
1318 		} else {
1319 			DCCMetaSurfaceBytes = DCCMetaPitch
1320 					* (dml_ceil(
1321 							(double) ViewportHeight - 1,
1322 							64 * BlockHeight256Bytes)
1323 							+ 64 * BlockHeight256Bytes) * BytePerPixel
1324 					/ 256;
1325 		}
1326 		if (GPUVMEnable == true) {
1327 			*MetaPTEBytesFrame = (dml_ceil(
1328 					(double) (DCCMetaSurfaceBytes - VMMPageSize)
1329 							/ (8 * VMMPageSize),
1330 					1) + 1) * 64;
1331 			MPDEBytesFrame = 128 * ((mode_lib->vba.GPUVMMaxPageTableLevels + 1) * (mode_lib->vba.HostVMMaxPageTableLevels + 1) - 2);
1332 		} else {
1333 			*MetaPTEBytesFrame = 0;
1334 			MPDEBytesFrame = 0;
1335 		}
1336 	} else {
1337 		*MetaPTEBytesFrame = 0;
1338 		MPDEBytesFrame = 0;
1339 		*MetaRowByte = 0;
1340 	}
1341 
1342 	if (SurfaceTiling == dm_sw_linear || SurfaceTiling == dm_sw_gfx7_2d_thin_gl || SurfaceTiling == dm_sw_gfx7_2d_thin_lvp) {
1343 		MacroTileSizeBytes = 256;
1344 		MacroTileHeight = BlockHeight256Bytes;
1345 	} else if (SurfaceTiling == dm_sw_4kb_s || SurfaceTiling == dm_sw_4kb_s_x
1346 			|| SurfaceTiling == dm_sw_4kb_d || SurfaceTiling == dm_sw_4kb_d_x) {
1347 		MacroTileSizeBytes = 4096;
1348 		MacroTileHeight = 4 * BlockHeight256Bytes;
1349 	} else if (SurfaceTiling == dm_sw_64kb_s || SurfaceTiling == dm_sw_64kb_s_t
1350 			|| SurfaceTiling == dm_sw_64kb_s_x || SurfaceTiling == dm_sw_64kb_d
1351 			|| SurfaceTiling == dm_sw_64kb_d_t || SurfaceTiling == dm_sw_64kb_d_x
1352 			|| SurfaceTiling == dm_sw_64kb_r_x) {
1353 		MacroTileSizeBytes = 65536;
1354 		MacroTileHeight = 16 * BlockHeight256Bytes;
1355 	} else {
1356 		MacroTileSizeBytes = 262144;
1357 		MacroTileHeight = 32 * BlockHeight256Bytes;
1358 	}
1359 	*MacroTileWidth = MacroTileSizeBytes / BytePerPixel / MacroTileHeight;
1360 
1361 	if (GPUVMEnable == true && (mode_lib->vba.GPUVMMaxPageTableLevels + 1) * (mode_lib->vba.HostVMMaxPageTableLevels + 1) > 2) {
1362 		if (ScanDirection == dm_horz) {
1363 			*DPDE0BytesFrame = 64 * (dml_ceil(((Pitch * (dml_ceil(ViewportHeight - 1, MacroTileHeight) + MacroTileHeight) * BytePerPixel) - MacroTileSizeBytes) / (8 * 2097152), 1) + 1);
1364 		} else {
1365 			*DPDE0BytesFrame = 64 * (dml_ceil(((Pitch * (dml_ceil((double) SwathWidth - 1, MacroTileHeight) + MacroTileHeight) * BytePerPixel) - MacroTileSizeBytes) / (8 * 2097152), 1) + 1);
1366 		}
1367 		ExtraDPDEBytesFrame = 128 * ((mode_lib->vba.GPUVMMaxPageTableLevels + 1) * (mode_lib->vba.HostVMMaxPageTableLevels + 1) - 3);
1368 	} else {
1369 		*DPDE0BytesFrame = 0;
1370 		ExtraDPDEBytesFrame = 0;
1371 	}
1372 
1373 	PDEAndMetaPTEBytesFrame = *MetaPTEBytesFrame + MPDEBytesFrame + *DPDE0BytesFrame
1374 			+ ExtraDPDEBytesFrame;
1375 
1376 	if (HostVMEnable == true) {
1377 		PDEAndMetaPTEBytesFrame = PDEAndMetaPTEBytesFrame * (1 + 8 * (HostVMMaxPageTableLevels - HostVMCachedPageTableLevels));
1378 	}
1379 
1380 	if (GPUVMEnable == true) {
1381 		double FractionOfPTEReturnDrop;
1382 
1383 		if (SurfaceTiling == dm_sw_linear) {
1384 			PixelPTEReqHeightPTEs = 1;
1385 			*PixelPTEReqHeight = 1;
1386 			*PixelPTEReqWidth = 8.0 * VMMPageSize / BytePerPixel;
1387 			*PTERequestSize = 64;
1388 			FractionOfPTEReturnDrop = 0;
1389 		} else if (MacroTileSizeBytes == 4096) {
1390 			PixelPTEReqHeightPTEs = 1;
1391 			*PixelPTEReqHeight = MacroTileHeight;
1392 			*PixelPTEReqWidth = 8 * *MacroTileWidth;
1393 			*PTERequestSize = 64;
1394 			if (ScanDirection == dm_horz)
1395 				FractionOfPTEReturnDrop = 0;
1396 			else
1397 				FractionOfPTEReturnDrop = 7 / 8;
1398 		} else if (VMMPageSize == 4096 && MacroTileSizeBytes > 4096) {
1399 			PixelPTEReqHeightPTEs = 16;
1400 			*PixelPTEReqHeight = 16 * BlockHeight256Bytes;
1401 			*PixelPTEReqWidth = 16 * BlockWidth256Bytes;
1402 			*PTERequestSize = 128;
1403 			FractionOfPTEReturnDrop = 0;
1404 		} else {
1405 			PixelPTEReqHeightPTEs = 1;
1406 			*PixelPTEReqHeight = MacroTileHeight;
1407 			*PixelPTEReqWidth = 8 * *MacroTileWidth;
1408 			*PTERequestSize = 64;
1409 			FractionOfPTEReturnDrop = 0;
1410 		}
1411 
1412 		if (SurfaceTiling == dm_sw_linear) {
1413 			*dpte_row_height = dml_min(128,
1414 					1 << (unsigned int) dml_floor(
1415 						dml_log2(
1416 							(double) PTEBufferSizeInRequests * *PixelPTEReqWidth / Pitch),
1417 						1));
1418 			*dpte_row_width_ub = (dml_ceil((double) (Pitch * *dpte_row_height - 1) / *PixelPTEReqWidth, 1) + 1) * *PixelPTEReqWidth;
1419 			*PixelPTEBytesPerRow = *dpte_row_width_ub / *PixelPTEReqWidth * *PTERequestSize;
1420 		} else if (ScanDirection == dm_horz) {
1421 			*dpte_row_height = *PixelPTEReqHeight;
1422 			*dpte_row_width_ub = (dml_ceil((double) (SwathWidth - 1) / *PixelPTEReqWidth, 1) + 1) * *PixelPTEReqWidth;
1423 			*PixelPTEBytesPerRow = *dpte_row_width_ub / *PixelPTEReqWidth * *PTERequestSize;
1424 		} else {
1425 			*dpte_row_height = dml_min(*PixelPTEReqWidth, *MacroTileWidth);
1426 			*dpte_row_width_ub = (dml_ceil((double) (SwathWidth - 1) / *PixelPTEReqHeight, 1) + 1) * *PixelPTEReqHeight;
1427 			*PixelPTEBytesPerRow = *dpte_row_width_ub / *PixelPTEReqHeight * *PTERequestSize;
1428 		}
1429 		if (*PixelPTEBytesPerRow * (1 - FractionOfPTEReturnDrop)
1430 				<= 64 * PTEBufferSizeInRequests) {
1431 			*PTEBufferSizeNotExceeded = true;
1432 		} else {
1433 			*PTEBufferSizeNotExceeded = false;
1434 		}
1435 	} else {
1436 		*PixelPTEBytesPerRow = 0;
1437 		*PTEBufferSizeNotExceeded = true;
1438 	}
1439 	dml_print("DML: vm_bytes = meta_pte_bytes_per_frame (per_pipe) = MetaPTEBytesFrame = : %d\n", *MetaPTEBytesFrame);
1440 
1441 	if (HostVMEnable == true) {
1442 		*PixelPTEBytesPerRow = *PixelPTEBytesPerRow * (1 + 8 * (HostVMMaxPageTableLevels - HostVMCachedPageTableLevels));
1443 	}
1444 
1445 	if (HostVMEnable == true) {
1446 		*vm_group_bytes = 512;
1447 		*dpte_group_bytes = 512;
1448 	} else if (GPUVMEnable == true) {
1449 		*vm_group_bytes = 2048;
1450 		if (SurfaceTiling != dm_sw_linear && PixelPTEReqHeightPTEs == 1 && ScanDirection != dm_horz) {
1451 			*dpte_group_bytes = 512;
1452 		} else {
1453 			*dpte_group_bytes = 2048;
1454 		}
1455 	} else {
1456 		*vm_group_bytes = 0;
1457 		*dpte_group_bytes = 0;
1458 	}
1459 
1460 	return PDEAndMetaPTEBytesFrame;
1461 }
1462 
1463 static void DISPCLKDPPCLKDCFCLKDeepSleepPrefetchParametersWatermarksAndPerformanceCalculation(
1464 		struct display_mode_lib *mode_lib)
1465 {
1466 	struct vba_vars_st *locals = &mode_lib->vba;
1467 	unsigned int j, k;
1468 
1469 	mode_lib->vba.WritebackDISPCLK = 0.0;
1470 	mode_lib->vba.DISPCLKWithRamping = 0;
1471 	mode_lib->vba.DISPCLKWithoutRamping = 0;
1472 	mode_lib->vba.GlobalDPPCLK = 0.0;
1473 
1474 	// DISPCLK and DPPCLK Calculation
1475 	//
1476 	for (k = 0; k < mode_lib->vba.NumberOfActivePlanes; ++k) {
1477 		if (mode_lib->vba.WritebackEnable[k]) {
1478 			mode_lib->vba.WritebackDISPCLK =
1479 					dml_max(
1480 							mode_lib->vba.WritebackDISPCLK,
1481 							CalculateWriteBackDISPCLK(
1482 									mode_lib->vba.WritebackPixelFormat[k],
1483 									mode_lib->vba.PixelClock[k],
1484 									mode_lib->vba.WritebackHRatio[k],
1485 									mode_lib->vba.WritebackVRatio[k],
1486 									mode_lib->vba.WritebackLumaHTaps[k],
1487 									mode_lib->vba.WritebackLumaVTaps[k],
1488 									mode_lib->vba.WritebackChromaHTaps[k],
1489 									mode_lib->vba.WritebackChromaVTaps[k],
1490 									mode_lib->vba.WritebackDestinationWidth[k],
1491 									mode_lib->vba.HTotal[k],
1492 									mode_lib->vba.WritebackChromaLineBufferWidth));
1493 		}
1494 	}
1495 
1496 	for (k = 0; k < mode_lib->vba.NumberOfActivePlanes; ++k) {
1497 		if (mode_lib->vba.HRatio[k] > 1) {
1498 			locals->PSCL_THROUGHPUT_LUMA[k] = dml_min(
1499 					mode_lib->vba.MaxDCHUBToPSCLThroughput,
1500 					mode_lib->vba.MaxPSCLToLBThroughput
1501 							* mode_lib->vba.HRatio[k]
1502 							/ dml_ceil(
1503 									mode_lib->vba.htaps[k]
1504 											/ 6.0,
1505 									1));
1506 		} else {
1507 			locals->PSCL_THROUGHPUT_LUMA[k] = dml_min(
1508 					mode_lib->vba.MaxDCHUBToPSCLThroughput,
1509 					mode_lib->vba.MaxPSCLToLBThroughput);
1510 		}
1511 
1512 		mode_lib->vba.DPPCLKUsingSingleDPPLuma =
1513 				mode_lib->vba.PixelClock[k]
1514 						* dml_max(
1515 								mode_lib->vba.vtaps[k] / 6.0
1516 										* dml_min(
1517 												1.0,
1518 												mode_lib->vba.HRatio[k]),
1519 								dml_max(
1520 										mode_lib->vba.HRatio[k]
1521 												* mode_lib->vba.VRatio[k]
1522 												/ locals->PSCL_THROUGHPUT_LUMA[k],
1523 										1.0));
1524 
1525 		if ((mode_lib->vba.htaps[k] > 6 || mode_lib->vba.vtaps[k] > 6)
1526 				&& mode_lib->vba.DPPCLKUsingSingleDPPLuma
1527 						< 2 * mode_lib->vba.PixelClock[k]) {
1528 			mode_lib->vba.DPPCLKUsingSingleDPPLuma = 2 * mode_lib->vba.PixelClock[k];
1529 		}
1530 
1531 		if ((mode_lib->vba.SourcePixelFormat[k] != dm_420_8
1532 				&& mode_lib->vba.SourcePixelFormat[k] != dm_420_10)) {
1533 			locals->PSCL_THROUGHPUT_CHROMA[k] = 0.0;
1534 			locals->DPPCLKUsingSingleDPP[k] =
1535 					mode_lib->vba.DPPCLKUsingSingleDPPLuma;
1536 		} else {
1537 			if (mode_lib->vba.HRatio[k] > 1) {
1538 				locals->PSCL_THROUGHPUT_CHROMA[k] =
1539 						dml_min(
1540 								mode_lib->vba.MaxDCHUBToPSCLThroughput,
1541 								mode_lib->vba.MaxPSCLToLBThroughput
1542 										* mode_lib->vba.HRatio[k]
1543 										/ 2
1544 										/ dml_ceil(
1545 												mode_lib->vba.HTAPsChroma[k]
1546 														/ 6.0,
1547 												1.0));
1548 			} else {
1549 				locals->PSCL_THROUGHPUT_CHROMA[k] = dml_min(
1550 						mode_lib->vba.MaxDCHUBToPSCLThroughput,
1551 						mode_lib->vba.MaxPSCLToLBThroughput);
1552 			}
1553 			mode_lib->vba.DPPCLKUsingSingleDPPChroma =
1554 					mode_lib->vba.PixelClock[k]
1555 							* dml_max(
1556 									mode_lib->vba.VTAPsChroma[k]
1557 											/ 6.0
1558 											* dml_min(
1559 													1.0,
1560 													mode_lib->vba.HRatio[k]
1561 															/ 2),
1562 									dml_max(
1563 											mode_lib->vba.HRatio[k]
1564 													* mode_lib->vba.VRatio[k]
1565 													/ 4
1566 													/ locals->PSCL_THROUGHPUT_CHROMA[k],
1567 											1.0));
1568 
1569 			if ((mode_lib->vba.HTAPsChroma[k] > 6 || mode_lib->vba.VTAPsChroma[k] > 6)
1570 					&& mode_lib->vba.DPPCLKUsingSingleDPPChroma
1571 							< 2 * mode_lib->vba.PixelClock[k]) {
1572 				mode_lib->vba.DPPCLKUsingSingleDPPChroma = 2
1573 						* mode_lib->vba.PixelClock[k];
1574 			}
1575 
1576 			locals->DPPCLKUsingSingleDPP[k] = dml_max(
1577 					mode_lib->vba.DPPCLKUsingSingleDPPLuma,
1578 					mode_lib->vba.DPPCLKUsingSingleDPPChroma);
1579 		}
1580 	}
1581 
1582 	for (k = 0; k < mode_lib->vba.NumberOfActivePlanes; ++k) {
1583 		if (mode_lib->vba.BlendingAndTiming[k] != k)
1584 			continue;
1585 		if (mode_lib->vba.ODMCombineEnabled[k]) {
1586 			mode_lib->vba.DISPCLKWithRamping =
1587 					dml_max(
1588 							mode_lib->vba.DISPCLKWithRamping,
1589 							mode_lib->vba.PixelClock[k] / 2
1590 									* (1
1591 											+ mode_lib->vba.DISPCLKDPPCLKDSCCLKDownSpreading
1592 													/ 100)
1593 									* (1
1594 											+ mode_lib->vba.DISPCLKRampingMargin
1595 													/ 100));
1596 			mode_lib->vba.DISPCLKWithoutRamping =
1597 					dml_max(
1598 							mode_lib->vba.DISPCLKWithoutRamping,
1599 							mode_lib->vba.PixelClock[k] / 2
1600 									* (1
1601 											+ mode_lib->vba.DISPCLKDPPCLKDSCCLKDownSpreading
1602 													/ 100));
1603 		} else if (!mode_lib->vba.ODMCombineEnabled[k]) {
1604 			mode_lib->vba.DISPCLKWithRamping =
1605 					dml_max(
1606 							mode_lib->vba.DISPCLKWithRamping,
1607 							mode_lib->vba.PixelClock[k]
1608 									* (1
1609 											+ mode_lib->vba.DISPCLKDPPCLKDSCCLKDownSpreading
1610 													/ 100)
1611 									* (1
1612 											+ mode_lib->vba.DISPCLKRampingMargin
1613 													/ 100));
1614 			mode_lib->vba.DISPCLKWithoutRamping =
1615 					dml_max(
1616 							mode_lib->vba.DISPCLKWithoutRamping,
1617 							mode_lib->vba.PixelClock[k]
1618 									* (1
1619 											+ mode_lib->vba.DISPCLKDPPCLKDSCCLKDownSpreading
1620 													/ 100));
1621 		}
1622 	}
1623 
1624 	mode_lib->vba.DISPCLKWithRamping = dml_max(
1625 			mode_lib->vba.DISPCLKWithRamping,
1626 			mode_lib->vba.WritebackDISPCLK);
1627 	mode_lib->vba.DISPCLKWithoutRamping = dml_max(
1628 			mode_lib->vba.DISPCLKWithoutRamping,
1629 			mode_lib->vba.WritebackDISPCLK);
1630 
1631 	ASSERT(mode_lib->vba.DISPCLKDPPCLKVCOSpeed != 0);
1632 	mode_lib->vba.DISPCLKWithRampingRoundedToDFSGranularity = RoundToDFSGranularityUp(
1633 			mode_lib->vba.DISPCLKWithRamping,
1634 			mode_lib->vba.DISPCLKDPPCLKVCOSpeed);
1635 	mode_lib->vba.DISPCLKWithoutRampingRoundedToDFSGranularity = RoundToDFSGranularityUp(
1636 			mode_lib->vba.DISPCLKWithoutRamping,
1637 			mode_lib->vba.DISPCLKDPPCLKVCOSpeed);
1638 	mode_lib->vba.MaxDispclkRoundedToDFSGranularity = RoundToDFSGranularityDown(
1639 			mode_lib->vba.soc.clock_limits[mode_lib->vba.soc.num_states - 1].dispclk_mhz,
1640 			mode_lib->vba.DISPCLKDPPCLKVCOSpeed);
1641 	if (mode_lib->vba.DISPCLKWithoutRampingRoundedToDFSGranularity
1642 			> mode_lib->vba.MaxDispclkRoundedToDFSGranularity) {
1643 		mode_lib->vba.DISPCLK_calculated =
1644 				mode_lib->vba.DISPCLKWithoutRampingRoundedToDFSGranularity;
1645 	} else if (mode_lib->vba.DISPCLKWithRampingRoundedToDFSGranularity
1646 			> mode_lib->vba.MaxDispclkRoundedToDFSGranularity) {
1647 		mode_lib->vba.DISPCLK_calculated = mode_lib->vba.MaxDispclkRoundedToDFSGranularity;
1648 	} else {
1649 		mode_lib->vba.DISPCLK_calculated =
1650 				mode_lib->vba.DISPCLKWithRampingRoundedToDFSGranularity;
1651 	}
1652 	DTRACE("   dispclk_mhz (calculated) = %f", mode_lib->vba.DISPCLK_calculated);
1653 
1654 	for (k = 0; k < mode_lib->vba.NumberOfActivePlanes; ++k) {
1655 		mode_lib->vba.DPPCLK_calculated[k] = locals->DPPCLKUsingSingleDPP[k]
1656 				/ mode_lib->vba.DPPPerPlane[k]
1657 				* (1 + mode_lib->vba.DISPCLKDPPCLKDSCCLKDownSpreading / 100);
1658 		mode_lib->vba.GlobalDPPCLK = dml_max(
1659 				mode_lib->vba.GlobalDPPCLK,
1660 				mode_lib->vba.DPPCLK_calculated[k]);
1661 	}
1662 	mode_lib->vba.GlobalDPPCLK = RoundToDFSGranularityUp(
1663 			mode_lib->vba.GlobalDPPCLK,
1664 			mode_lib->vba.DISPCLKDPPCLKVCOSpeed);
1665 	for (k = 0; k < mode_lib->vba.NumberOfActivePlanes; ++k) {
1666 		mode_lib->vba.DPPCLK_calculated[k] = mode_lib->vba.GlobalDPPCLK / 255
1667 				* dml_ceil(
1668 						mode_lib->vba.DPPCLK_calculated[k] * 255
1669 								/ mode_lib->vba.GlobalDPPCLK,
1670 						1);
1671 		DTRACE("   dppclk_mhz[%i] (calculated) = %f", k, mode_lib->vba.DPPCLK_calculated[k]);
1672 	}
1673 
1674 	// Urgent and B P-State/DRAM Clock Change Watermark
1675 	DTRACE("   dcfclk_mhz         = %f", mode_lib->vba.DCFCLK);
1676 	DTRACE("   return_bw_to_dcn   = %f", mode_lib->vba.ReturnBandwidthToDCN);
1677 	DTRACE("   return_bus_bw      = %f", mode_lib->vba.ReturnBW);
1678 
1679 	for (k = 0; k < mode_lib->vba.NumberOfActivePlanes; ++k) {
1680 		bool MainPlaneDoesODMCombine = false;
1681 
1682 		if (mode_lib->vba.SourceScan[k] == dm_horz)
1683 			locals->SwathWidthSingleDPPY[k] = mode_lib->vba.ViewportWidth[k];
1684 		else
1685 			locals->SwathWidthSingleDPPY[k] = mode_lib->vba.ViewportHeight[k];
1686 
1687 		if (mode_lib->vba.ODMCombineEnabled[k] == true)
1688 			MainPlaneDoesODMCombine = true;
1689 		for (j = 0; j < mode_lib->vba.NumberOfActivePlanes; ++j)
1690 			if (mode_lib->vba.BlendingAndTiming[k] == j
1691 					&& mode_lib->vba.ODMCombineEnabled[j] == true)
1692 				MainPlaneDoesODMCombine = true;
1693 
1694 		if (MainPlaneDoesODMCombine == true)
1695 			locals->SwathWidthY[k] = dml_min(
1696 					(double) locals->SwathWidthSingleDPPY[k],
1697 					dml_round(
1698 							mode_lib->vba.HActive[k] / 2.0
1699 									* mode_lib->vba.HRatio[k]));
1700 		else
1701 			locals->SwathWidthY[k] = locals->SwathWidthSingleDPPY[k]
1702 					/ mode_lib->vba.DPPPerPlane[k];
1703 	}
1704 
1705 	for (k = 0; k < mode_lib->vba.NumberOfActivePlanes; ++k) {
1706 		if (mode_lib->vba.SourcePixelFormat[k] == dm_444_64) {
1707 			locals->BytePerPixelDETY[k] = 8;
1708 			locals->BytePerPixelDETC[k] = 0;
1709 		} else if (mode_lib->vba.SourcePixelFormat[k] == dm_444_32) {
1710 			locals->BytePerPixelDETY[k] = 4;
1711 			locals->BytePerPixelDETC[k] = 0;
1712 		} else if (mode_lib->vba.SourcePixelFormat[k] == dm_444_16 || mode_lib->vba.SourcePixelFormat[k] == dm_mono_16) {
1713 			locals->BytePerPixelDETY[k] = 2;
1714 			locals->BytePerPixelDETC[k] = 0;
1715 		} else if (mode_lib->vba.SourcePixelFormat[k] == dm_444_8 || mode_lib->vba.SourcePixelFormat[k] == dm_mono_8) {
1716 			locals->BytePerPixelDETY[k] = 1;
1717 			locals->BytePerPixelDETC[k] = 0;
1718 		} else if (mode_lib->vba.SourcePixelFormat[k] == dm_420_8) {
1719 			locals->BytePerPixelDETY[k] = 1;
1720 			locals->BytePerPixelDETC[k] = 2;
1721 		} else { // dm_420_10
1722 			locals->BytePerPixelDETY[k] = 4.0 / 3.0;
1723 			locals->BytePerPixelDETC[k] = 8.0 / 3.0;
1724 		}
1725 	}
1726 
1727 	mode_lib->vba.TotalDataReadBandwidth = 0.0;
1728 	for (k = 0; k < mode_lib->vba.NumberOfActivePlanes; ++k) {
1729 		locals->ReadBandwidthPlaneLuma[k] = locals->SwathWidthSingleDPPY[k]
1730 				* dml_ceil(locals->BytePerPixelDETY[k], 1)
1731 				/ (mode_lib->vba.HTotal[k] / mode_lib->vba.PixelClock[k])
1732 				* mode_lib->vba.VRatio[k];
1733 		locals->ReadBandwidthPlaneChroma[k] = locals->SwathWidthSingleDPPY[k]
1734 				/ 2 * dml_ceil(locals->BytePerPixelDETC[k], 2)
1735 				/ (mode_lib->vba.HTotal[k] / mode_lib->vba.PixelClock[k])
1736 				* mode_lib->vba.VRatio[k] / 2;
1737 		DTRACE(
1738 				"   read_bw[%i] = %fBps",
1739 				k,
1740 				locals->ReadBandwidthPlaneLuma[k]
1741 						+ locals->ReadBandwidthPlaneChroma[k]);
1742 		mode_lib->vba.TotalDataReadBandwidth += locals->ReadBandwidthPlaneLuma[k]
1743 				+ locals->ReadBandwidthPlaneChroma[k];
1744 	}
1745 
1746 	// DCFCLK Deep Sleep
1747 	CalculateDCFCLKDeepSleep(
1748 		mode_lib,
1749 		mode_lib->vba.NumberOfActivePlanes,
1750 		locals->BytePerPixelDETY,
1751 		locals->BytePerPixelDETC,
1752 		mode_lib->vba.VRatio,
1753 		locals->SwathWidthY,
1754 		mode_lib->vba.DPPPerPlane,
1755 		mode_lib->vba.HRatio,
1756 		mode_lib->vba.PixelClock,
1757 		locals->PSCL_THROUGHPUT_LUMA,
1758 		locals->PSCL_THROUGHPUT_CHROMA,
1759 		locals->DPPCLK,
1760 		&mode_lib->vba.DCFCLKDeepSleep);
1761 
1762 	// DSCCLK
1763 	for (k = 0; k < mode_lib->vba.NumberOfActivePlanes; ++k) {
1764 		if ((mode_lib->vba.BlendingAndTiming[k] != k) || !mode_lib->vba.DSCEnabled[k]) {
1765 			locals->DSCCLK_calculated[k] = 0.0;
1766 		} else {
1767 			if (mode_lib->vba.OutputFormat[k] == dm_420
1768 					|| mode_lib->vba.OutputFormat[k] == dm_n422)
1769 				mode_lib->vba.DSCFormatFactor = 2;
1770 			else
1771 				mode_lib->vba.DSCFormatFactor = 1;
1772 			if (mode_lib->vba.ODMCombineEnabled[k])
1773 				locals->DSCCLK_calculated[k] =
1774 						mode_lib->vba.PixelClockBackEnd[k] / 6
1775 								/ mode_lib->vba.DSCFormatFactor
1776 								/ (1
1777 										- mode_lib->vba.DISPCLKDPPCLKDSCCLKDownSpreading
1778 												/ 100);
1779 			else
1780 				locals->DSCCLK_calculated[k] =
1781 						mode_lib->vba.PixelClockBackEnd[k] / 3
1782 								/ mode_lib->vba.DSCFormatFactor
1783 								/ (1
1784 										- mode_lib->vba.DISPCLKDPPCLKDSCCLKDownSpreading
1785 												/ 100);
1786 		}
1787 	}
1788 
1789 	// DSC Delay
1790 	// TODO
1791 	for (k = 0; k < mode_lib->vba.NumberOfActivePlanes; ++k) {
1792 		double bpp = mode_lib->vba.OutputBpp[k];
1793 		unsigned int slices = mode_lib->vba.NumberOfDSCSlices[k];
1794 
1795 		if (mode_lib->vba.DSCEnabled[k] && bpp != 0) {
1796 			if (!mode_lib->vba.ODMCombineEnabled[k]) {
1797 				locals->DSCDelay[k] =
1798 						dscceComputeDelay(
1799 								mode_lib->vba.DSCInputBitPerComponent[k],
1800 								bpp,
1801 								dml_ceil(
1802 										(double) mode_lib->vba.HActive[k]
1803 												/ mode_lib->vba.NumberOfDSCSlices[k],
1804 										1),
1805 								slices,
1806 								mode_lib->vba.OutputFormat[k])
1807 								+ dscComputeDelay(
1808 										mode_lib->vba.OutputFormat[k]);
1809 			} else {
1810 				locals->DSCDelay[k] =
1811 						2
1812 								* (dscceComputeDelay(
1813 										mode_lib->vba.DSCInputBitPerComponent[k],
1814 										bpp,
1815 										dml_ceil(
1816 												(double) mode_lib->vba.HActive[k]
1817 														/ mode_lib->vba.NumberOfDSCSlices[k],
1818 												1),
1819 										slices / 2.0,
1820 										mode_lib->vba.OutputFormat[k])
1821 										+ dscComputeDelay(
1822 												mode_lib->vba.OutputFormat[k]));
1823 			}
1824 			locals->DSCDelay[k] = locals->DSCDelay[k]
1825 					* mode_lib->vba.PixelClock[k]
1826 					/ mode_lib->vba.PixelClockBackEnd[k];
1827 		} else {
1828 			locals->DSCDelay[k] = 0;
1829 		}
1830 	}
1831 
1832 	for (k = 0; k < mode_lib->vba.NumberOfActivePlanes; ++k)
1833 		for (j = 0; j < mode_lib->vba.NumberOfActivePlanes; ++j) // NumberOfPlanes
1834 			if (j != k && mode_lib->vba.BlendingAndTiming[k] == j
1835 					&& mode_lib->vba.DSCEnabled[j])
1836 				locals->DSCDelay[k] = locals->DSCDelay[j];
1837 
1838 	// Prefetch
1839 	for (k = 0; k < mode_lib->vba.NumberOfActivePlanes; ++k) {
1840 		unsigned int PDEAndMetaPTEBytesFrameY;
1841 		unsigned int PixelPTEBytesPerRowY;
1842 		unsigned int MetaRowByteY;
1843 		unsigned int MetaRowByteC;
1844 		unsigned int PDEAndMetaPTEBytesFrameC;
1845 		unsigned int PixelPTEBytesPerRowC;
1846 		bool         PTEBufferSizeNotExceededY;
1847 		bool         PTEBufferSizeNotExceededC;
1848 
1849 		Calculate256BBlockSizes(
1850 				mode_lib->vba.SourcePixelFormat[k],
1851 				mode_lib->vba.SurfaceTiling[k],
1852 				dml_ceil(locals->BytePerPixelDETY[k], 1),
1853 				dml_ceil(locals->BytePerPixelDETC[k], 2),
1854 				&locals->BlockHeight256BytesY[k],
1855 				&locals->BlockHeight256BytesC[k],
1856 				&locals->BlockWidth256BytesY[k],
1857 				&locals->BlockWidth256BytesC[k]);
1858 
1859 		locals->PrefetchSourceLinesY[k] = CalculatePrefetchSourceLines(
1860 				mode_lib,
1861 				mode_lib->vba.VRatio[k],
1862 				mode_lib->vba.vtaps[k],
1863 				mode_lib->vba.Interlace[k],
1864 				mode_lib->vba.ProgressiveToInterlaceUnitInOPP,
1865 				mode_lib->vba.SwathHeightY[k],
1866 				mode_lib->vba.ViewportYStartY[k],
1867 				&locals->VInitPreFillY[k],
1868 				&locals->MaxNumSwathY[k]);
1869 
1870 		if ((mode_lib->vba.SourcePixelFormat[k] != dm_444_64
1871 				&& mode_lib->vba.SourcePixelFormat[k] != dm_444_32
1872 				&& mode_lib->vba.SourcePixelFormat[k] != dm_444_16
1873 				&& mode_lib->vba.SourcePixelFormat[k] != dm_444_8)) {
1874 			PDEAndMetaPTEBytesFrameC =
1875 					CalculateVMAndRowBytes(
1876 							mode_lib,
1877 							mode_lib->vba.DCCEnable[k],
1878 							locals->BlockHeight256BytesC[k],
1879 							locals->BlockWidth256BytesC[k],
1880 							mode_lib->vba.SourcePixelFormat[k],
1881 							mode_lib->vba.SurfaceTiling[k],
1882 							dml_ceil(
1883 									locals->BytePerPixelDETC[k],
1884 									2),
1885 							mode_lib->vba.SourceScan[k],
1886 							mode_lib->vba.ViewportWidth[k] / 2,
1887 							mode_lib->vba.ViewportHeight[k] / 2,
1888 							locals->SwathWidthY[k] / 2,
1889 							mode_lib->vba.GPUVMEnable,
1890 							mode_lib->vba.HostVMEnable,
1891 							mode_lib->vba.HostVMMaxPageTableLevels,
1892 							mode_lib->vba.HostVMCachedPageTableLevels,
1893 							mode_lib->vba.VMMPageSize,
1894 							mode_lib->vba.PTEBufferSizeInRequestsChroma,
1895 							mode_lib->vba.PitchC[k],
1896 							mode_lib->vba.DCCMetaPitchC[k],
1897 							&locals->MacroTileWidthC[k],
1898 							&MetaRowByteC,
1899 							&PixelPTEBytesPerRowC,
1900 							&PTEBufferSizeNotExceededC,
1901 							&locals->dpte_row_width_chroma_ub[k],
1902 							&locals->dpte_row_height_chroma[k],
1903 							&locals->meta_req_width_chroma[k],
1904 							&locals->meta_req_height_chroma[k],
1905 							&locals->meta_row_width_chroma[k],
1906 							&locals->meta_row_height_chroma[k],
1907 							&locals->vm_group_bytes_chroma,
1908 							&locals->dpte_group_bytes_chroma,
1909 							&locals->PixelPTEReqWidthC[k],
1910 							&locals->PixelPTEReqHeightC[k],
1911 							&locals->PTERequestSizeC[k],
1912 							&locals->dpde0_bytes_per_frame_ub_c[k],
1913 							&locals->meta_pte_bytes_per_frame_ub_c[k]);
1914 
1915 			locals->PrefetchSourceLinesC[k] = CalculatePrefetchSourceLines(
1916 					mode_lib,
1917 					mode_lib->vba.VRatio[k] / 2,
1918 					mode_lib->vba.VTAPsChroma[k],
1919 					mode_lib->vba.Interlace[k],
1920 					mode_lib->vba.ProgressiveToInterlaceUnitInOPP,
1921 					mode_lib->vba.SwathHeightC[k],
1922 					mode_lib->vba.ViewportYStartC[k],
1923 					&locals->VInitPreFillC[k],
1924 					&locals->MaxNumSwathC[k]);
1925 		} else {
1926 			PixelPTEBytesPerRowC = 0;
1927 			PDEAndMetaPTEBytesFrameC = 0;
1928 			MetaRowByteC = 0;
1929 			locals->MaxNumSwathC[k] = 0;
1930 			locals->PrefetchSourceLinesC[k] = 0;
1931 			locals->PTEBufferSizeInRequestsForLuma = mode_lib->vba.PTEBufferSizeInRequestsLuma + mode_lib->vba.PTEBufferSizeInRequestsChroma;
1932 		}
1933 
1934 		PDEAndMetaPTEBytesFrameY = CalculateVMAndRowBytes(
1935 				mode_lib,
1936 				mode_lib->vba.DCCEnable[k],
1937 				locals->BlockHeight256BytesY[k],
1938 				locals->BlockWidth256BytesY[k],
1939 				mode_lib->vba.SourcePixelFormat[k],
1940 				mode_lib->vba.SurfaceTiling[k],
1941 				dml_ceil(locals->BytePerPixelDETY[k], 1),
1942 				mode_lib->vba.SourceScan[k],
1943 				mode_lib->vba.ViewportWidth[k],
1944 				mode_lib->vba.ViewportHeight[k],
1945 				locals->SwathWidthY[k],
1946 				mode_lib->vba.GPUVMEnable,
1947 				mode_lib->vba.HostVMEnable,
1948 				mode_lib->vba.HostVMMaxPageTableLevels,
1949 				mode_lib->vba.HostVMCachedPageTableLevels,
1950 				mode_lib->vba.VMMPageSize,
1951 				locals->PTEBufferSizeInRequestsForLuma,
1952 				mode_lib->vba.PitchY[k],
1953 				mode_lib->vba.DCCMetaPitchY[k],
1954 				&locals->MacroTileWidthY[k],
1955 				&MetaRowByteY,
1956 				&PixelPTEBytesPerRowY,
1957 				&PTEBufferSizeNotExceededY,
1958 				&locals->dpte_row_width_luma_ub[k],
1959 				&locals->dpte_row_height[k],
1960 				&locals->meta_req_width[k],
1961 				&locals->meta_req_height[k],
1962 				&locals->meta_row_width[k],
1963 				&locals->meta_row_height[k],
1964 				&locals->vm_group_bytes[k],
1965 				&locals->dpte_group_bytes[k],
1966 				&locals->PixelPTEReqWidthY[k],
1967 				&locals->PixelPTEReqHeightY[k],
1968 				&locals->PTERequestSizeY[k],
1969 				&locals->dpde0_bytes_per_frame_ub_l[k],
1970 				&locals->meta_pte_bytes_per_frame_ub_l[k]);
1971 
1972 		locals->PixelPTEBytesPerRow[k] = PixelPTEBytesPerRowY + PixelPTEBytesPerRowC;
1973 		locals->PDEAndMetaPTEBytesFrame[k] = PDEAndMetaPTEBytesFrameY
1974 				+ PDEAndMetaPTEBytesFrameC;
1975 		locals->MetaRowByte[k] = MetaRowByteY + MetaRowByteC;
1976 
1977 		CalculateActiveRowBandwidth(
1978 				mode_lib->vba.GPUVMEnable,
1979 				mode_lib->vba.SourcePixelFormat[k],
1980 				mode_lib->vba.VRatio[k],
1981 				mode_lib->vba.DCCEnable[k],
1982 				mode_lib->vba.HTotal[k] / mode_lib->vba.PixelClock[k],
1983 				MetaRowByteY,
1984 				MetaRowByteC,
1985 				locals->meta_row_height[k],
1986 				locals->meta_row_height_chroma[k],
1987 				PixelPTEBytesPerRowY,
1988 				PixelPTEBytesPerRowC,
1989 				locals->dpte_row_height[k],
1990 				locals->dpte_row_height_chroma[k],
1991 				&locals->meta_row_bw[k],
1992 				&locals->dpte_row_bw[k]);
1993 	}
1994 
1995 	mode_lib->vba.TotalDCCActiveDPP = 0;
1996 	mode_lib->vba.TotalActiveDPP = 0;
1997 	for (k = 0; k < mode_lib->vba.NumberOfActivePlanes; ++k) {
1998 		mode_lib->vba.TotalActiveDPP = mode_lib->vba.TotalActiveDPP
1999 				+ mode_lib->vba.DPPPerPlane[k];
2000 		if (mode_lib->vba.DCCEnable[k])
2001 			mode_lib->vba.TotalDCCActiveDPP = mode_lib->vba.TotalDCCActiveDPP
2002 					+ mode_lib->vba.DPPPerPlane[k];
2003 	}
2004 
2005 	mode_lib->vba.UrgentOutOfOrderReturnPerChannel = dml_max3(
2006 			mode_lib->vba.UrgentOutOfOrderReturnPerChannelPixelDataOnly,
2007 			mode_lib->vba.UrgentOutOfOrderReturnPerChannelPixelMixedWithVMData,
2008 			mode_lib->vba.UrgentOutOfOrderReturnPerChannelVMDataOnly);
2009 
2010 	mode_lib->vba.UrgentRoundTripAndOutOfOrderLatency =
2011 			(mode_lib->vba.RoundTripPingLatencyCycles + 32) / mode_lib->vba.DCFCLK
2012 					+ mode_lib->vba.UrgentOutOfOrderReturnPerChannel
2013 							* mode_lib->vba.NumberOfChannels
2014 							/ mode_lib->vba.ReturnBW;
2015 
2016 	mode_lib->vba.UrgentExtraLatency = CalculateExtraLatency(
2017 			mode_lib->vba.UrgentRoundTripAndOutOfOrderLatency,
2018 			mode_lib->vba.TotalActiveDPP,
2019 			mode_lib->vba.PixelChunkSizeInKByte,
2020 			mode_lib->vba.TotalDCCActiveDPP,
2021 			mode_lib->vba.MetaChunkSize,
2022 			mode_lib->vba.ReturnBW,
2023 			mode_lib->vba.GPUVMEnable,
2024 			mode_lib->vba.HostVMEnable,
2025 			mode_lib->vba.NumberOfActivePlanes,
2026 			mode_lib->vba.DPPPerPlane,
2027 			locals->dpte_group_bytes,
2028 			mode_lib->vba.PercentOfIdealDRAMFabricAndSDPPortBWReceivedAfterUrgLatencyPixelMixedWithVMData,
2029 			mode_lib->vba.PercentOfIdealDRAMFabricAndSDPPortBWReceivedAfterUrgLatencyVMDataOnly,
2030 			mode_lib->vba.HostVMMaxPageTableLevels,
2031 			mode_lib->vba.HostVMCachedPageTableLevels);
2032 
2033 
2034 	mode_lib->vba.TCalc = 24.0 / mode_lib->vba.DCFCLKDeepSleep;
2035 
2036 	for (k = 0; k < mode_lib->vba.NumberOfActivePlanes; ++k) {
2037 		if (mode_lib->vba.BlendingAndTiming[k] == k) {
2038 			if (mode_lib->vba.WritebackEnable[k] == true) {
2039 				locals->WritebackDelay[mode_lib->vba.VoltageLevel][k] =
2040 						mode_lib->vba.WritebackLatency
2041 								+ CalculateWriteBackDelay(
2042 										mode_lib->vba.WritebackPixelFormat[k],
2043 										mode_lib->vba.WritebackHRatio[k],
2044 										mode_lib->vba.WritebackVRatio[k],
2045 										mode_lib->vba.WritebackLumaHTaps[k],
2046 										mode_lib->vba.WritebackLumaVTaps[k],
2047 										mode_lib->vba.WritebackChromaHTaps[k],
2048 										mode_lib->vba.WritebackChromaVTaps[k],
2049 										mode_lib->vba.WritebackDestinationWidth[k])
2050 										/ mode_lib->vba.DISPCLK;
2051 			} else
2052 				locals->WritebackDelay[mode_lib->vba.VoltageLevel][k] = 0;
2053 			for (j = 0; j < mode_lib->vba.NumberOfActivePlanes; ++j) {
2054 				if (mode_lib->vba.BlendingAndTiming[j] == k
2055 						&& mode_lib->vba.WritebackEnable[j] == true) {
2056 					locals->WritebackDelay[mode_lib->vba.VoltageLevel][k] =
2057 							dml_max(
2058 									locals->WritebackDelay[mode_lib->vba.VoltageLevel][k],
2059 									mode_lib->vba.WritebackLatency
2060 											+ CalculateWriteBackDelay(
2061 													mode_lib->vba.WritebackPixelFormat[j],
2062 													mode_lib->vba.WritebackHRatio[j],
2063 													mode_lib->vba.WritebackVRatio[j],
2064 													mode_lib->vba.WritebackLumaHTaps[j],
2065 													mode_lib->vba.WritebackLumaVTaps[j],
2066 													mode_lib->vba.WritebackChromaHTaps[j],
2067 													mode_lib->vba.WritebackChromaVTaps[j],
2068 													mode_lib->vba.WritebackDestinationWidth[j])
2069 													/ mode_lib->vba.DISPCLK);
2070 				}
2071 			}
2072 		}
2073 	}
2074 
2075 	for (k = 0; k < mode_lib->vba.NumberOfActivePlanes; ++k)
2076 		for (j = 0; j < mode_lib->vba.NumberOfActivePlanes; ++j)
2077 			if (mode_lib->vba.BlendingAndTiming[k] == j)
2078 				locals->WritebackDelay[mode_lib->vba.VoltageLevel][k] =
2079 						locals->WritebackDelay[mode_lib->vba.VoltageLevel][j];
2080 
2081 	mode_lib->vba.VStartupLines = 13;
2082 	for (k = 0; k < mode_lib->vba.NumberOfActivePlanes; ++k) {
2083 		locals->MaxVStartupLines[k] = mode_lib->vba.VTotal[k] - mode_lib->vba.VActive[k] - dml_max(1.0, dml_ceil(locals->WritebackDelay[mode_lib->vba.VoltageLevel][k] / (mode_lib->vba.HTotal[k] / mode_lib->vba.PixelClock[k]), 1));
2084 	}
2085 
2086 	for (k = 0; k < mode_lib->vba.NumberOfActivePlanes; ++k)
2087 		locals->MaximumMaxVStartupLines = dml_max(locals->MaximumMaxVStartupLines, locals->MaxVStartupLines[k]);
2088 
2089 	// We don't really care to iterate between the various prefetch modes
2090 	//mode_lib->vba.PrefetchERROR = CalculateMinAndMaxPrefetchMode(mode_lib->vba.AllowDRAMSelfRefreshOrDRAMClockChangeInVblank, &mode_lib->vba.MinPrefetchMode, &mode_lib->vba.MaxPrefetchMode);
2091 	mode_lib->vba.UrgentLatency = dml_max3(mode_lib->vba.UrgentLatencyPixelDataOnly, mode_lib->vba.UrgentLatencyPixelMixedWithVMData, mode_lib->vba.UrgentLatencyVMDataOnly);
2092 
2093 	do {
2094 		double MaxTotalRDBandwidth = 0;
2095 		double MaxTotalRDBandwidthNoUrgentBurst = 0;
2096 		bool DestinationLineTimesForPrefetchLessThan2 = false;
2097 		bool VRatioPrefetchMoreThan4 = false;
2098 		double TWait = CalculateTWait(
2099 				mode_lib->vba.PrefetchMode[mode_lib->vba.VoltageLevel][mode_lib->vba.maxMpcComb],
2100 				mode_lib->vba.DRAMClockChangeLatency,
2101 				mode_lib->vba.UrgentLatency,
2102 				mode_lib->vba.SREnterPlusExitTime);
2103 
2104 		for (k = 0; k < mode_lib->vba.NumberOfActivePlanes; ++k) {
2105 			Pipe   myPipe;
2106 			HostVM myHostVM;
2107 
2108 			if (mode_lib->vba.XFCEnabled[k] == true) {
2109 				mode_lib->vba.XFCRemoteSurfaceFlipDelay =
2110 						CalculateRemoteSurfaceFlipDelay(
2111 								mode_lib,
2112 								mode_lib->vba.VRatio[k],
2113 								locals->SwathWidthY[k],
2114 								dml_ceil(
2115 										locals->BytePerPixelDETY[k],
2116 										1),
2117 								mode_lib->vba.HTotal[k]
2118 										/ mode_lib->vba.PixelClock[k],
2119 								mode_lib->vba.XFCTSlvVupdateOffset,
2120 								mode_lib->vba.XFCTSlvVupdateWidth,
2121 								mode_lib->vba.XFCTSlvVreadyOffset,
2122 								mode_lib->vba.XFCXBUFLatencyTolerance,
2123 								mode_lib->vba.XFCFillBWOverhead,
2124 								mode_lib->vba.XFCSlvChunkSize,
2125 								mode_lib->vba.XFCBusTransportTime,
2126 								mode_lib->vba.TCalc,
2127 								TWait,
2128 								&mode_lib->vba.SrcActiveDrainRate,
2129 								&mode_lib->vba.TInitXFill,
2130 								&mode_lib->vba.TslvChk);
2131 			} else {
2132 				mode_lib->vba.XFCRemoteSurfaceFlipDelay = 0;
2133 			}
2134 
2135 			myPipe.DPPCLK = locals->DPPCLK[k];
2136 			myPipe.DISPCLK = mode_lib->vba.DISPCLK;
2137 			myPipe.PixelClock = mode_lib->vba.PixelClock[k];
2138 			myPipe.DCFCLKDeepSleep = mode_lib->vba.DCFCLKDeepSleep;
2139 			myPipe.DPPPerPlane = mode_lib->vba.DPPPerPlane[k];
2140 			myPipe.ScalerEnabled = mode_lib->vba.ScalerEnabled[k];
2141 			myPipe.SourceScan = mode_lib->vba.SourceScan[k];
2142 			myPipe.BlockWidth256BytesY = locals->BlockWidth256BytesY[k];
2143 			myPipe.BlockHeight256BytesY = locals->BlockHeight256BytesY[k];
2144 			myPipe.BlockWidth256BytesC = locals->BlockWidth256BytesC[k];
2145 			myPipe.BlockHeight256BytesC = locals->BlockHeight256BytesC[k];
2146 			myPipe.InterlaceEnable = mode_lib->vba.Interlace[k];
2147 			myPipe.NumberOfCursors = mode_lib->vba.NumberOfCursors[k];
2148 			myPipe.VBlank = mode_lib->vba.VTotal[k] - mode_lib->vba.VActive[k];
2149 			myPipe.HTotal = mode_lib->vba.HTotal[k];
2150 
2151 
2152 			myHostVM.Enable = mode_lib->vba.HostVMEnable;
2153 			myHostVM.MaxPageTableLevels = mode_lib->vba.HostVMMaxPageTableLevels;
2154 			myHostVM.CachedPageTableLevels = mode_lib->vba.HostVMCachedPageTableLevels;
2155 
2156 			mode_lib->vba.ErrorResult[k] =
2157 					CalculatePrefetchSchedule(
2158 							mode_lib,
2159 							mode_lib->vba.PercentOfIdealDRAMFabricAndSDPPortBWReceivedAfterUrgLatencyPixelMixedWithVMData,
2160 							mode_lib->vba.PercentOfIdealDRAMFabricAndSDPPortBWReceivedAfterUrgLatencyVMDataOnly,
2161 							&myPipe,
2162 							locals->DSCDelay[k],
2163 							mode_lib->vba.DPPCLKDelaySubtotal,
2164 							mode_lib->vba.DPPCLKDelaySCL,
2165 							mode_lib->vba.DPPCLKDelaySCLLBOnly,
2166 							mode_lib->vba.DPPCLKDelayCNVCFormater,
2167 							mode_lib->vba.DPPCLKDelayCNVCCursor,
2168 							mode_lib->vba.DISPCLKDelaySubtotal,
2169 							(unsigned int) (locals->SwathWidthY[k]
2170 									/ mode_lib->vba.HRatio[k]),
2171 							mode_lib->vba.OutputFormat[k],
2172 							mode_lib->vba.MaxInterDCNTileRepeaters,
2173 							dml_min(mode_lib->vba.VStartupLines, locals->MaxVStartupLines[k]),
2174 							locals->MaxVStartupLines[k],
2175 							mode_lib->vba.GPUVMMaxPageTableLevels,
2176 							mode_lib->vba.GPUVMEnable,
2177 							&myHostVM,
2178 							mode_lib->vba.DynamicMetadataEnable[k],
2179 							mode_lib->vba.DynamicMetadataLinesBeforeActiveRequired[k],
2180 							mode_lib->vba.DynamicMetadataTransmittedBytes[k],
2181 							mode_lib->vba.DCCEnable[k],
2182 							mode_lib->vba.UrgentLatency,
2183 							mode_lib->vba.UrgentExtraLatency,
2184 							mode_lib->vba.TCalc,
2185 							locals->PDEAndMetaPTEBytesFrame[k],
2186 							locals->MetaRowByte[k],
2187 							locals->PixelPTEBytesPerRow[k],
2188 							locals->PrefetchSourceLinesY[k],
2189 							locals->SwathWidthY[k],
2190 							locals->BytePerPixelDETY[k],
2191 							locals->VInitPreFillY[k],
2192 							locals->MaxNumSwathY[k],
2193 							locals->PrefetchSourceLinesC[k],
2194 							locals->BytePerPixelDETC[k],
2195 							locals->VInitPreFillC[k],
2196 							locals->MaxNumSwathC[k],
2197 							mode_lib->vba.SwathHeightY[k],
2198 							mode_lib->vba.SwathHeightC[k],
2199 							TWait,
2200 							mode_lib->vba.XFCEnabled[k],
2201 							mode_lib->vba.XFCRemoteSurfaceFlipDelay,
2202 							mode_lib->vba.ProgressiveToInterlaceUnitInOPP,
2203 							&locals->DSTXAfterScaler[k],
2204 							&locals->DSTYAfterScaler[k],
2205 							&locals->DestinationLinesForPrefetch[k],
2206 							&locals->PrefetchBandwidth[k],
2207 							&locals->DestinationLinesToRequestVMInVBlank[k],
2208 							&locals->DestinationLinesToRequestRowInVBlank[k],
2209 							&locals->VRatioPrefetchY[k],
2210 							&locals->VRatioPrefetchC[k],
2211 							&locals->RequiredPrefetchPixDataBWLuma[k],
2212 							&locals->RequiredPrefetchPixDataBWChroma[k],
2213 							&locals->VStartupRequiredWhenNotEnoughTimeForDynamicMetadata,
2214 							&locals->Tno_bw[k],
2215 							&locals->prefetch_vmrow_bw[k],
2216 							&locals->swath_width_luma_ub[k],
2217 							&locals->swath_width_chroma_ub[k],
2218 							&mode_lib->vba.VUpdateOffsetPix[k],
2219 							&mode_lib->vba.VUpdateWidthPix[k],
2220 							&mode_lib->vba.VReadyOffsetPix[k]);
2221 			if (mode_lib->vba.BlendingAndTiming[k] == k) {
2222 				locals->VStartup[k] = dml_min(
2223 						mode_lib->vba.VStartupLines,
2224 						locals->MaxVStartupLines[k]);
2225 				if (locals->VStartupRequiredWhenNotEnoughTimeForDynamicMetadata
2226 						!= 0) {
2227 					locals->VStartup[k] =
2228 							locals->VStartupRequiredWhenNotEnoughTimeForDynamicMetadata;
2229 				}
2230 			} else {
2231 				locals->VStartup[k] =
2232 						dml_min(
2233 								mode_lib->vba.VStartupLines,
2234 								locals->MaxVStartupLines[mode_lib->vba.BlendingAndTiming[k]]);
2235 			}
2236 		}
2237 
2238 		for (k = 0; k < mode_lib->vba.NumberOfActivePlanes; ++k) {
2239 			unsigned int m;
2240 
2241 			locals->cursor_bw[k] = 0;
2242 			locals->cursor_bw_pre[k] = 0;
2243 			for (m = 0; m < mode_lib->vba.NumberOfCursors[k]; m++) {
2244 				locals->cursor_bw[k] += mode_lib->vba.CursorWidth[k][m] * mode_lib->vba.CursorBPP[k][m] / 8.0 / (mode_lib->vba.HTotal[k] / mode_lib->vba.PixelClock[k]) * mode_lib->vba.VRatio[k];
2245 				locals->cursor_bw_pre[k] += mode_lib->vba.CursorWidth[k][m] * mode_lib->vba.CursorBPP[k][m] / 8.0 / (mode_lib->vba.HTotal[k] / mode_lib->vba.PixelClock[k]) * locals->VRatioPrefetchY[k];
2246 			}
2247 
2248 			CalculateUrgentBurstFactor(
2249 					mode_lib->vba.DETBufferSizeInKByte,
2250 					mode_lib->vba.SwathHeightY[k],
2251 					mode_lib->vba.SwathHeightC[k],
2252 					locals->SwathWidthY[k],
2253 					mode_lib->vba.HTotal[k] /
2254 					mode_lib->vba.PixelClock[k],
2255 					mode_lib->vba.UrgentLatency,
2256 					mode_lib->vba.CursorBufferSize,
2257 					mode_lib->vba.CursorWidth[k][0] + mode_lib->vba.CursorWidth[k][1],
2258 					dml_max(mode_lib->vba.CursorBPP[k][0], mode_lib->vba.CursorBPP[k][1]),
2259 					mode_lib->vba.VRatio[k],
2260 					locals->VRatioPrefetchY[k],
2261 					locals->VRatioPrefetchC[k],
2262 					locals->BytePerPixelDETY[k],
2263 					locals->BytePerPixelDETC[k],
2264 					&locals->UrgentBurstFactorCursor[k],
2265 					&locals->UrgentBurstFactorCursorPre[k],
2266 					&locals->UrgentBurstFactorLuma[k],
2267 					&locals->UrgentBurstFactorLumaPre[k],
2268 					&locals->UrgentBurstFactorChroma[k],
2269 					&locals->UrgentBurstFactorChromaPre[k],
2270 					&locals->NotEnoughUrgentLatencyHiding,
2271 					&locals->NotEnoughUrgentLatencyHidingPre);
2272 
2273 			if (mode_lib->vba.UseUrgentBurstBandwidth == false) {
2274 				locals->UrgentBurstFactorLuma[k] = 1;
2275 				locals->UrgentBurstFactorChroma[k] = 1;
2276 				locals->UrgentBurstFactorCursor[k] = 1;
2277 				locals->UrgentBurstFactorLumaPre[k] = 1;
2278 				locals->UrgentBurstFactorChromaPre[k] = 1;
2279 				locals->UrgentBurstFactorCursorPre[k] = 1;
2280 			}
2281 
2282 			MaxTotalRDBandwidth = MaxTotalRDBandwidth +
2283 				dml_max3(locals->prefetch_vmrow_bw[k],
2284 					locals->ReadBandwidthPlaneLuma[k] * locals->UrgentBurstFactorLuma[k]
2285 					+ locals->ReadBandwidthPlaneChroma[k] * locals->UrgentBurstFactorChroma[k] + locals->cursor_bw[k]
2286 					* locals->UrgentBurstFactorCursor[k] + locals->meta_row_bw[k] + locals->dpte_row_bw[k],
2287 					locals->RequiredPrefetchPixDataBWLuma[k] * locals->UrgentBurstFactorLumaPre[k] + locals->RequiredPrefetchPixDataBWChroma[k]
2288 					* locals->UrgentBurstFactorChromaPre[k] + locals->cursor_bw_pre[k] * locals->UrgentBurstFactorCursorPre[k]);
2289 
2290 			MaxTotalRDBandwidthNoUrgentBurst = MaxTotalRDBandwidthNoUrgentBurst +
2291 				dml_max3(locals->prefetch_vmrow_bw[k],
2292 					locals->ReadBandwidthPlaneLuma[k] + locals->ReadBandwidthPlaneChroma[k] + locals->cursor_bw[k]
2293 					+ locals->meta_row_bw[k] + locals->dpte_row_bw[k],
2294 					locals->RequiredPrefetchPixDataBWLuma[k] + locals->RequiredPrefetchPixDataBWChroma[k] + locals->cursor_bw_pre[k]);
2295 
2296 			if (locals->DestinationLinesForPrefetch[k] < 2)
2297 				DestinationLineTimesForPrefetchLessThan2 = true;
2298 			if (locals->VRatioPrefetchY[k] > 4 || locals->VRatioPrefetchC[k] > 4)
2299 				VRatioPrefetchMoreThan4 = true;
2300 		}
2301 		mode_lib->vba.FractionOfUrgentBandwidth = MaxTotalRDBandwidthNoUrgentBurst / mode_lib->vba.ReturnBW;
2302 
2303 		if (MaxTotalRDBandwidth <= mode_lib->vba.ReturnBW && locals->NotEnoughUrgentLatencyHiding == 0 && locals->NotEnoughUrgentLatencyHidingPre == 0 && !VRatioPrefetchMoreThan4
2304 				&& !DestinationLineTimesForPrefetchLessThan2)
2305 			mode_lib->vba.PrefetchModeSupported = true;
2306 		else {
2307 			mode_lib->vba.PrefetchModeSupported = false;
2308 			dml_print(
2309 					"DML: CalculatePrefetchSchedule ***failed***. Bandwidth violation. Results are NOT valid\n");
2310 		}
2311 
2312 		if (mode_lib->vba.PrefetchModeSupported == true) {
2313 			mode_lib->vba.BandwidthAvailableForImmediateFlip = mode_lib->vba.ReturnBW;
2314 			for (k = 0; k < mode_lib->vba.NumberOfActivePlanes; ++k) {
2315 				mode_lib->vba.BandwidthAvailableForImmediateFlip =
2316 						mode_lib->vba.BandwidthAvailableForImmediateFlip
2317 							- dml_max(
2318 								locals->ReadBandwidthPlaneLuma[k] * locals->UrgentBurstFactorLuma[k]
2319 								+ locals->ReadBandwidthPlaneChroma[k] * locals->UrgentBurstFactorChroma[k]
2320 								+ locals->cursor_bw[k] * locals->UrgentBurstFactorCursor[k],
2321 								locals->RequiredPrefetchPixDataBWLuma[k] * locals->UrgentBurstFactorLumaPre[k] +
2322 								locals->RequiredPrefetchPixDataBWChroma[k] * locals->UrgentBurstFactorChromaPre[k] +
2323 								locals->cursor_bw_pre[k] * locals->UrgentBurstFactorCursorPre[k]);
2324 			}
2325 
2326 			mode_lib->vba.TotImmediateFlipBytes = 0;
2327 			for (k = 0; k < mode_lib->vba.NumberOfActivePlanes; ++k) {
2328 				mode_lib->vba.TotImmediateFlipBytes = mode_lib->vba.TotImmediateFlipBytes + locals->PDEAndMetaPTEBytesFrame[k] + locals->MetaRowByte[k] + locals->PixelPTEBytesPerRow[k];
2329 			}
2330 			for (k = 0; k < mode_lib->vba.NumberOfActivePlanes; ++k) {
2331 				CalculateFlipSchedule(
2332 						mode_lib,
2333 						mode_lib->vba.PercentOfIdealDRAMFabricAndSDPPortBWReceivedAfterUrgLatencyPixelMixedWithVMData,
2334 						mode_lib->vba.PercentOfIdealDRAMFabricAndSDPPortBWReceivedAfterUrgLatencyVMDataOnly,
2335 						mode_lib->vba.UrgentExtraLatency,
2336 						mode_lib->vba.UrgentLatency,
2337 						mode_lib->vba.GPUVMMaxPageTableLevels,
2338 						mode_lib->vba.HostVMEnable,
2339 						mode_lib->vba.HostVMMaxPageTableLevels,
2340 						mode_lib->vba.HostVMCachedPageTableLevels,
2341 						mode_lib->vba.GPUVMEnable,
2342 						locals->PDEAndMetaPTEBytesFrame[k],
2343 						locals->MetaRowByte[k],
2344 						locals->PixelPTEBytesPerRow[k],
2345 						mode_lib->vba.BandwidthAvailableForImmediateFlip,
2346 						mode_lib->vba.TotImmediateFlipBytes,
2347 						mode_lib->vba.SourcePixelFormat[k],
2348 						mode_lib->vba.HTotal[k] / mode_lib->vba.PixelClock[k],
2349 						mode_lib->vba.VRatio[k],
2350 						locals->Tno_bw[k],
2351 						mode_lib->vba.DCCEnable[k],
2352 						locals->dpte_row_height[k],
2353 						locals->meta_row_height[k],
2354 						locals->dpte_row_height_chroma[k],
2355 						locals->meta_row_height_chroma[k],
2356 						&locals->DestinationLinesToRequestVMInImmediateFlip[k],
2357 						&locals->DestinationLinesToRequestRowInImmediateFlip[k],
2358 						&locals->final_flip_bw[k],
2359 						&locals->ImmediateFlipSupportedForPipe[k]);
2360 			}
2361 			mode_lib->vba.total_dcn_read_bw_with_flip = 0.0;
2362 			mode_lib->vba.total_dcn_read_bw_with_flip_no_urgent_burst = 0.0;
2363 			for (k = 0; k < mode_lib->vba.NumberOfActivePlanes; ++k) {
2364 				mode_lib->vba.total_dcn_read_bw_with_flip =
2365 						mode_lib->vba.total_dcn_read_bw_with_flip + dml_max3(
2366 							locals->prefetch_vmrow_bw[k],
2367 							locals->final_flip_bw[k] + locals->ReadBandwidthLuma[k] * locals->UrgentBurstFactorLuma[k]
2368 							+ locals->ReadBandwidthChroma[k] * locals->UrgentBurstFactorChroma[k] + locals->cursor_bw[k] * locals->UrgentBurstFactorCursor[k],
2369 							locals->final_flip_bw[k] + locals->RequiredPrefetchPixDataBWLuma[k] * locals->UrgentBurstFactorLumaPre[k]
2370 							+ locals->RequiredPrefetchPixDataBWChroma[k] * locals->UrgentBurstFactorChromaPre[k]
2371 							+ locals->cursor_bw_pre[k] * locals->UrgentBurstFactorCursorPre[k]);
2372 				mode_lib->vba.total_dcn_read_bw_with_flip_no_urgent_burst =
2373 				mode_lib->vba.total_dcn_read_bw_with_flip_no_urgent_burst +
2374 					dml_max3(locals->prefetch_vmrow_bw[k],
2375 						locals->final_flip_bw[k] + locals->ReadBandwidthPlaneLuma[k] + locals->ReadBandwidthPlaneChroma[k] + locals->cursor_bw[k],
2376 						locals->final_flip_bw[k] + locals->RequiredPrefetchPixDataBWLuma[k] + locals->RequiredPrefetchPixDataBWChroma[k] + locals->cursor_bw_pre[k]);
2377 
2378 			}
2379 			mode_lib->vba.FractionOfUrgentBandwidthImmediateFlip = mode_lib->vba.total_dcn_read_bw_with_flip_no_urgent_burst / mode_lib->vba.ReturnBW;
2380 
2381 			mode_lib->vba.ImmediateFlipSupported = true;
2382 			if (mode_lib->vba.total_dcn_read_bw_with_flip > mode_lib->vba.ReturnBW) {
2383 				mode_lib->vba.ImmediateFlipSupported = false;
2384 			}
2385 			for (k = 0; k < mode_lib->vba.NumberOfActivePlanes; ++k) {
2386 				if (locals->ImmediateFlipSupportedForPipe[k] == false) {
2387 					mode_lib->vba.ImmediateFlipSupported = false;
2388 				}
2389 			}
2390 		} else {
2391 			mode_lib->vba.ImmediateFlipSupported = false;
2392 		}
2393 
2394 		for (k = 0; k < mode_lib->vba.NumberOfActivePlanes; ++k) {
2395 			if (mode_lib->vba.ErrorResult[k]) {
2396 				mode_lib->vba.PrefetchModeSupported = false;
2397 				dml_print(
2398 						"DML: CalculatePrefetchSchedule ***failed***. Prefetch schedule violation. Results are NOT valid\n");
2399 			}
2400 		}
2401 
2402 		mode_lib->vba.VStartupLines = mode_lib->vba.VStartupLines + 1;
2403 	} while (!((mode_lib->vba.PrefetchModeSupported
2404 			&& ((!mode_lib->vba.ImmediateFlipSupport && !mode_lib->vba.HostVMEnable)
2405 					|| mode_lib->vba.ImmediateFlipSupported))
2406 			|| locals->MaximumMaxVStartupLines < mode_lib->vba.VStartupLines));
2407 
2408 	//Watermarks and NB P-State/DRAM Clock Change Support
2409 	{
2410 		enum clock_change_support DRAMClockChangeSupport; // dummy
2411 		CalculateWatermarksAndDRAMSpeedChangeSupport(
2412 				mode_lib,
2413 				mode_lib->vba.PrefetchMode[mode_lib->vba.VoltageLevel][mode_lib->vba.maxMpcComb],
2414 				mode_lib->vba.NumberOfActivePlanes,
2415 				mode_lib->vba.MaxLineBufferLines,
2416 				mode_lib->vba.LineBufferSize,
2417 				mode_lib->vba.DPPOutputBufferPixels,
2418 				mode_lib->vba.DETBufferSizeInKByte,
2419 				mode_lib->vba.WritebackInterfaceLumaBufferSize,
2420 				mode_lib->vba.WritebackInterfaceChromaBufferSize,
2421 				mode_lib->vba.DCFCLK,
2422 				mode_lib->vba.UrgentOutOfOrderReturnPerChannel * mode_lib->vba.NumberOfChannels,
2423 				mode_lib->vba.ReturnBW,
2424 				mode_lib->vba.GPUVMEnable,
2425 				locals->dpte_group_bytes,
2426 				mode_lib->vba.MetaChunkSize,
2427 				mode_lib->vba.UrgentLatency,
2428 				mode_lib->vba.UrgentExtraLatency,
2429 				mode_lib->vba.WritebackLatency,
2430 				mode_lib->vba.WritebackChunkSize,
2431 				mode_lib->vba.SOCCLK,
2432 				mode_lib->vba.DRAMClockChangeLatency,
2433 				mode_lib->vba.SRExitTime,
2434 				mode_lib->vba.SREnterPlusExitTime,
2435 				mode_lib->vba.DCFCLKDeepSleep,
2436 				mode_lib->vba.DPPPerPlane,
2437 				mode_lib->vba.DCCEnable,
2438 				locals->DPPCLK,
2439 				locals->SwathWidthSingleDPPY,
2440 				mode_lib->vba.SwathHeightY,
2441 				locals->ReadBandwidthPlaneLuma,
2442 				mode_lib->vba.SwathHeightC,
2443 				locals->ReadBandwidthPlaneChroma,
2444 				mode_lib->vba.LBBitPerPixel,
2445 				locals->SwathWidthY,
2446 				mode_lib->vba.HRatio,
2447 				mode_lib->vba.vtaps,
2448 				mode_lib->vba.VTAPsChroma,
2449 				mode_lib->vba.VRatio,
2450 				mode_lib->vba.HTotal,
2451 				mode_lib->vba.PixelClock,
2452 				mode_lib->vba.BlendingAndTiming,
2453 				locals->BytePerPixelDETY,
2454 				locals->BytePerPixelDETC,
2455 				mode_lib->vba.WritebackEnable,
2456 				mode_lib->vba.WritebackPixelFormat,
2457 				mode_lib->vba.WritebackDestinationWidth,
2458 				mode_lib->vba.WritebackDestinationHeight,
2459 				mode_lib->vba.WritebackSourceHeight,
2460 				&DRAMClockChangeSupport,
2461 				&mode_lib->vba.UrgentWatermark,
2462 				&mode_lib->vba.WritebackUrgentWatermark,
2463 				&mode_lib->vba.DRAMClockChangeWatermark,
2464 				&mode_lib->vba.WritebackDRAMClockChangeWatermark,
2465 				&mode_lib->vba.StutterExitWatermark,
2466 				&mode_lib->vba.StutterEnterPlusExitWatermark,
2467 				&mode_lib->vba.MinActiveDRAMClockChangeLatencySupported);
2468 	}
2469 
2470 
2471 	//Display Pipeline Delivery Time in Prefetch, Groups
2472 	CalculatePixelDeliveryTimes(
2473 		mode_lib->vba.NumberOfActivePlanes,
2474 		mode_lib->vba.VRatio,
2475 		locals->VRatioPrefetchY,
2476 		locals->VRatioPrefetchC,
2477 		locals->swath_width_luma_ub,
2478 		locals->swath_width_chroma_ub,
2479 		mode_lib->vba.DPPPerPlane,
2480 		mode_lib->vba.HRatio,
2481 		mode_lib->vba.PixelClock,
2482 		locals->PSCL_THROUGHPUT_LUMA,
2483 		locals->PSCL_THROUGHPUT_CHROMA,
2484 		locals->DPPCLK,
2485 		locals->BytePerPixelDETC,
2486 		mode_lib->vba.SourceScan,
2487 		locals->BlockWidth256BytesY,
2488 		locals->BlockHeight256BytesY,
2489 		locals->BlockWidth256BytesC,
2490 		locals->BlockHeight256BytesC,
2491 		locals->DisplayPipeLineDeliveryTimeLuma,
2492 		locals->DisplayPipeLineDeliveryTimeChroma,
2493 		locals->DisplayPipeLineDeliveryTimeLumaPrefetch,
2494 		locals->DisplayPipeLineDeliveryTimeChromaPrefetch,
2495 		locals->DisplayPipeRequestDeliveryTimeLuma,
2496 		locals->DisplayPipeRequestDeliveryTimeChroma,
2497 		locals->DisplayPipeRequestDeliveryTimeLumaPrefetch,
2498 		locals->DisplayPipeRequestDeliveryTimeChromaPrefetch);
2499 
2500 	CalculateMetaAndPTETimes(
2501 		mode_lib->vba.NumberOfActivePlanes,
2502 		mode_lib->vba.GPUVMEnable,
2503 		mode_lib->vba.MetaChunkSize,
2504 		mode_lib->vba.MinMetaChunkSizeBytes,
2505 		mode_lib->vba.GPUVMMaxPageTableLevels,
2506 		mode_lib->vba.HTotal,
2507 		mode_lib->vba.VRatio,
2508 		locals->VRatioPrefetchY,
2509 		locals->VRatioPrefetchC,
2510 		locals->DestinationLinesToRequestRowInVBlank,
2511 		locals->DestinationLinesToRequestRowInImmediateFlip,
2512 		locals->DestinationLinesToRequestVMInVBlank,
2513 		locals->DestinationLinesToRequestVMInImmediateFlip,
2514 		mode_lib->vba.DCCEnable,
2515 		mode_lib->vba.PixelClock,
2516 		locals->BytePerPixelDETY,
2517 		locals->BytePerPixelDETC,
2518 		mode_lib->vba.SourceScan,
2519 		locals->dpte_row_height,
2520 		locals->dpte_row_height_chroma,
2521 		locals->meta_row_width,
2522 		locals->meta_row_height,
2523 		locals->meta_req_width,
2524 		locals->meta_req_height,
2525 		locals->dpte_group_bytes,
2526 		locals->PTERequestSizeY,
2527 		locals->PTERequestSizeC,
2528 		locals->PixelPTEReqWidthY,
2529 		locals->PixelPTEReqHeightY,
2530 		locals->PixelPTEReqWidthC,
2531 		locals->PixelPTEReqHeightC,
2532 		locals->dpte_row_width_luma_ub,
2533 		locals->dpte_row_width_chroma_ub,
2534 		locals->vm_group_bytes,
2535 		locals->dpde0_bytes_per_frame_ub_l,
2536 		locals->dpde0_bytes_per_frame_ub_c,
2537 		locals->meta_pte_bytes_per_frame_ub_l,
2538 		locals->meta_pte_bytes_per_frame_ub_c,
2539 		locals->DST_Y_PER_PTE_ROW_NOM_L,
2540 		locals->DST_Y_PER_PTE_ROW_NOM_C,
2541 		locals->DST_Y_PER_META_ROW_NOM_L,
2542 		locals->TimePerMetaChunkNominal,
2543 		locals->TimePerMetaChunkVBlank,
2544 		locals->TimePerMetaChunkFlip,
2545 		locals->time_per_pte_group_nom_luma,
2546 		locals->time_per_pte_group_vblank_luma,
2547 		locals->time_per_pte_group_flip_luma,
2548 		locals->time_per_pte_group_nom_chroma,
2549 		locals->time_per_pte_group_vblank_chroma,
2550 		locals->time_per_pte_group_flip_chroma,
2551 		locals->TimePerVMGroupVBlank,
2552 		locals->TimePerVMGroupFlip,
2553 		locals->TimePerVMRequestVBlank,
2554 		locals->TimePerVMRequestFlip);
2555 
2556 
2557 	// Min TTUVBlank
2558 	for (k = 0; k < mode_lib->vba.NumberOfActivePlanes; ++k) {
2559 		if (mode_lib->vba.PrefetchMode[mode_lib->vba.VoltageLevel][mode_lib->vba.maxMpcComb] == 0) {
2560 			locals->AllowDRAMClockChangeDuringVBlank[k] = true;
2561 			locals->AllowDRAMSelfRefreshDuringVBlank[k] = true;
2562 			locals->MinTTUVBlank[k] = dml_max(
2563 					mode_lib->vba.DRAMClockChangeWatermark,
2564 					dml_max(
2565 							mode_lib->vba.StutterEnterPlusExitWatermark,
2566 							mode_lib->vba.UrgentWatermark));
2567 		} else if (mode_lib->vba.PrefetchMode[mode_lib->vba.VoltageLevel][mode_lib->vba.maxMpcComb] == 1) {
2568 			locals->AllowDRAMClockChangeDuringVBlank[k] = false;
2569 			locals->AllowDRAMSelfRefreshDuringVBlank[k] = true;
2570 			locals->MinTTUVBlank[k] = dml_max(
2571 					mode_lib->vba.StutterEnterPlusExitWatermark,
2572 					mode_lib->vba.UrgentWatermark);
2573 		} else {
2574 			locals->AllowDRAMClockChangeDuringVBlank[k] = false;
2575 			locals->AllowDRAMSelfRefreshDuringVBlank[k] = false;
2576 			locals->MinTTUVBlank[k] = mode_lib->vba.UrgentWatermark;
2577 		}
2578 		if (!mode_lib->vba.DynamicMetadataEnable[k])
2579 			locals->MinTTUVBlank[k] = mode_lib->vba.TCalc
2580 					+ locals->MinTTUVBlank[k];
2581 	}
2582 
2583 	// DCC Configuration
2584 	mode_lib->vba.ActiveDPPs = 0;
2585 	for (k = 0; k < mode_lib->vba.NumberOfActivePlanes; ++k) {
2586 		locals->MaximumDCCCompressionYSurface[k] = CalculateDCCConfiguration(
2587 			mode_lib->vba.DCCEnable[k],
2588 			false, // We should always know the direction DCCProgrammingAssumesScanDirectionUnknown,
2589 			mode_lib->vba.ViewportWidth[k],
2590 			mode_lib->vba.ViewportHeight[k],
2591 			mode_lib->vba.DETBufferSizeInKByte * 1024,
2592 			locals->BlockHeight256BytesY[k],
2593 			mode_lib->vba.SwathHeightY[k],
2594 			mode_lib->vba.SurfaceTiling[k],
2595 			locals->BytePerPixelDETY[k],
2596 			mode_lib->vba.SourceScan[k],
2597 			&locals->DCCYMaxUncompressedBlock[k],
2598 			&locals->DCCYMaxCompressedBlock[k],
2599 			&locals->DCCYIndependent64ByteBlock[k]);
2600 	}
2601 
2602 	//XFC Parameters:
2603 	for (k = 0; k < mode_lib->vba.NumberOfActivePlanes; ++k) {
2604 		if (mode_lib->vba.XFCEnabled[k] == true) {
2605 			double TWait;
2606 
2607 			locals->XFCSlaveVUpdateOffset[k] = mode_lib->vba.XFCTSlvVupdateOffset;
2608 			locals->XFCSlaveVupdateWidth[k] = mode_lib->vba.XFCTSlvVupdateWidth;
2609 			locals->XFCSlaveVReadyOffset[k] = mode_lib->vba.XFCTSlvVreadyOffset;
2610 			TWait = CalculateTWait(
2611 					mode_lib->vba.PrefetchMode[mode_lib->vba.VoltageLevel][mode_lib->vba.maxMpcComb],
2612 					mode_lib->vba.DRAMClockChangeLatency,
2613 					mode_lib->vba.UrgentLatency,
2614 					mode_lib->vba.SREnterPlusExitTime);
2615 			mode_lib->vba.XFCRemoteSurfaceFlipDelay = CalculateRemoteSurfaceFlipDelay(
2616 					mode_lib,
2617 					mode_lib->vba.VRatio[k],
2618 					locals->SwathWidthY[k],
2619 					dml_ceil(locals->BytePerPixelDETY[k], 1),
2620 					mode_lib->vba.HTotal[k] / mode_lib->vba.PixelClock[k],
2621 					mode_lib->vba.XFCTSlvVupdateOffset,
2622 					mode_lib->vba.XFCTSlvVupdateWidth,
2623 					mode_lib->vba.XFCTSlvVreadyOffset,
2624 					mode_lib->vba.XFCXBUFLatencyTolerance,
2625 					mode_lib->vba.XFCFillBWOverhead,
2626 					mode_lib->vba.XFCSlvChunkSize,
2627 					mode_lib->vba.XFCBusTransportTime,
2628 					mode_lib->vba.TCalc,
2629 					TWait,
2630 					&mode_lib->vba.SrcActiveDrainRate,
2631 					&mode_lib->vba.TInitXFill,
2632 					&mode_lib->vba.TslvChk);
2633 					locals->XFCRemoteSurfaceFlipLatency[k] =
2634 					dml_floor(
2635 							mode_lib->vba.XFCRemoteSurfaceFlipDelay
2636 									/ (mode_lib->vba.HTotal[k]
2637 											/ mode_lib->vba.PixelClock[k]),
2638 							1);
2639 			locals->XFCTransferDelay[k] =
2640 					dml_ceil(
2641 							mode_lib->vba.XFCBusTransportTime
2642 									/ (mode_lib->vba.HTotal[k]
2643 											/ mode_lib->vba.PixelClock[k]),
2644 							1);
2645 			locals->XFCPrechargeDelay[k] =
2646 					dml_ceil(
2647 							(mode_lib->vba.XFCBusTransportTime
2648 									+ mode_lib->vba.TInitXFill
2649 									+ mode_lib->vba.TslvChk)
2650 									/ (mode_lib->vba.HTotal[k]
2651 											/ mode_lib->vba.PixelClock[k]),
2652 							1);
2653 			mode_lib->vba.InitFillLevel = mode_lib->vba.XFCXBUFLatencyTolerance
2654 					* mode_lib->vba.SrcActiveDrainRate;
2655 			mode_lib->vba.FinalFillMargin =
2656 					(locals->DestinationLinesToRequestVMInVBlank[k]
2657 							+ locals->DestinationLinesToRequestRowInVBlank[k])
2658 							* mode_lib->vba.HTotal[k]
2659 							/ mode_lib->vba.PixelClock[k]
2660 							* mode_lib->vba.SrcActiveDrainRate
2661 							+ mode_lib->vba.XFCFillConstant;
2662 			mode_lib->vba.FinalFillLevel = mode_lib->vba.XFCRemoteSurfaceFlipDelay
2663 					* mode_lib->vba.SrcActiveDrainRate
2664 					+ mode_lib->vba.FinalFillMargin;
2665 			mode_lib->vba.RemainingFillLevel = dml_max(
2666 					0.0,
2667 					mode_lib->vba.FinalFillLevel - mode_lib->vba.InitFillLevel);
2668 			mode_lib->vba.TFinalxFill = mode_lib->vba.RemainingFillLevel
2669 					/ (mode_lib->vba.SrcActiveDrainRate
2670 							* mode_lib->vba.XFCFillBWOverhead / 100);
2671 			locals->XFCPrefetchMargin[k] =
2672 					mode_lib->vba.XFCRemoteSurfaceFlipDelay
2673 							+ mode_lib->vba.TFinalxFill
2674 							+ (locals->DestinationLinesToRequestVMInVBlank[k]
2675 									+ locals->DestinationLinesToRequestRowInVBlank[k])
2676 									* mode_lib->vba.HTotal[k]
2677 									/ mode_lib->vba.PixelClock[k];
2678 		} else {
2679 			locals->XFCSlaveVUpdateOffset[k] = 0;
2680 			locals->XFCSlaveVupdateWidth[k] = 0;
2681 			locals->XFCSlaveVReadyOffset[k] = 0;
2682 			locals->XFCRemoteSurfaceFlipLatency[k] = 0;
2683 			locals->XFCPrechargeDelay[k] = 0;
2684 			locals->XFCTransferDelay[k] = 0;
2685 			locals->XFCPrefetchMargin[k] = 0;
2686 		}
2687 	}
2688 
2689 	// Stutter Efficiency
2690 	for (k = 0; k < mode_lib->vba.NumberOfActivePlanes; ++k) {
2691 		CalculateDETBufferSize(
2692 			mode_lib->vba.DETBufferSizeInKByte,
2693 			mode_lib->vba.SwathHeightY[k],
2694 			mode_lib->vba.SwathHeightC[k],
2695 			&locals->DETBufferSizeY[k],
2696 			&locals->DETBufferSizeC[k]);
2697 
2698 		locals->LinesInDETY[k] = locals->DETBufferSizeY[k]
2699 				/ locals->BytePerPixelDETY[k] / locals->SwathWidthY[k];
2700 		locals->LinesInDETYRoundedDownToSwath[k] = dml_floor(
2701 				locals->LinesInDETY[k],
2702 				mode_lib->vba.SwathHeightY[k]);
2703 		locals->FullDETBufferingTimeY[k] =
2704 				locals->LinesInDETYRoundedDownToSwath[k]
2705 						* (mode_lib->vba.HTotal[k]
2706 								/ mode_lib->vba.PixelClock[k])
2707 						/ mode_lib->vba.VRatio[k];
2708 	}
2709 
2710 	mode_lib->vba.StutterPeriod = 999999.0;
2711 	for (k = 0; k < mode_lib->vba.NumberOfActivePlanes; ++k) {
2712 		if (locals->FullDETBufferingTimeY[k] < mode_lib->vba.StutterPeriod) {
2713 			mode_lib->vba.StutterPeriod = locals->FullDETBufferingTimeY[k];
2714 			mode_lib->vba.FrameTimeForMinFullDETBufferingTime =
2715 				(double) mode_lib->vba.VTotal[k] * mode_lib->vba.HTotal[k]
2716 				/ mode_lib->vba.PixelClock[k];
2717 			locals->BytePerPixelYCriticalPlane = dml_ceil(locals->BytePerPixelDETY[k], 1);
2718 			locals->SwathWidthYCriticalPlane = locals->SwathWidthY[k];
2719 			locals->LinesToFinishSwathTransferStutterCriticalPlane =
2720 				mode_lib->vba.SwathHeightY[k] - (locals->LinesInDETY[k] - locals->LinesInDETYRoundedDownToSwath[k]);
2721 		}
2722 	}
2723 
2724 	mode_lib->vba.AverageReadBandwidth = 0.0;
2725 	mode_lib->vba.TotalRowReadBandwidth = 0.0;
2726 	for (k = 0; k < mode_lib->vba.NumberOfActivePlanes; ++k) {
2727 		unsigned int DCCRateLimit;
2728 
2729 		if (mode_lib->vba.DCCEnable[k]) {
2730 			if (locals->DCCYMaxCompressedBlock[k] == 256)
2731 				DCCRateLimit = 4;
2732 			else
2733 				DCCRateLimit = 2;
2734 
2735 			mode_lib->vba.AverageReadBandwidth =
2736 					mode_lib->vba.AverageReadBandwidth
2737 							+ (locals->ReadBandwidthPlaneLuma[k] + locals->ReadBandwidthPlaneChroma[k]) /
2738 								dml_min(mode_lib->vba.DCCRate[k], DCCRateLimit);
2739 		} else {
2740 			mode_lib->vba.AverageReadBandwidth =
2741 					mode_lib->vba.AverageReadBandwidth
2742 							+ locals->ReadBandwidthPlaneLuma[k]
2743 							+ locals->ReadBandwidthPlaneChroma[k];
2744 		}
2745 		mode_lib->vba.TotalRowReadBandwidth = mode_lib->vba.TotalRowReadBandwidth +
2746 		locals->meta_row_bw[k] + locals->dpte_row_bw[k];
2747 	}
2748 
2749 	mode_lib->vba.AverageDCCCompressionRate = mode_lib->vba.TotalDataReadBandwidth / mode_lib->vba.AverageReadBandwidth;
2750 
2751 	mode_lib->vba.PartOfBurstThatFitsInROB =
2752 			dml_min(
2753 					mode_lib->vba.StutterPeriod
2754 							* mode_lib->vba.TotalDataReadBandwidth,
2755 					mode_lib->vba.ROBBufferSizeInKByte * 1024
2756 							* mode_lib->vba.AverageDCCCompressionRate);
2757 	mode_lib->vba.StutterBurstTime = mode_lib->vba.PartOfBurstThatFitsInROB
2758 			/ mode_lib->vba.AverageDCCCompressionRate / mode_lib->vba.ReturnBW
2759 			+ (mode_lib->vba.StutterPeriod * mode_lib->vba.TotalDataReadBandwidth
2760 					- mode_lib->vba.PartOfBurstThatFitsInROB)
2761 					/ (mode_lib->vba.DCFCLK * 64)
2762 					+ mode_lib->vba.StutterPeriod * mode_lib->vba.TotalRowReadBandwidth / mode_lib->vba.ReturnBW;
2763 	mode_lib->vba.StutterBurstTime = dml_max(
2764 		mode_lib->vba.StutterBurstTime,
2765 		(locals->LinesToFinishSwathTransferStutterCriticalPlane * locals->BytePerPixelYCriticalPlane *
2766 		locals->SwathWidthYCriticalPlane / mode_lib->vba.ReturnBW)
2767 	);
2768 
2769 	mode_lib->vba.TotalActiveWriteback = 0;
2770 	for (k = 0; k < mode_lib->vba.NumberOfActivePlanes; ++k) {
2771 		if (mode_lib->vba.WritebackEnable[k] == true) {
2772 			mode_lib->vba.TotalActiveWriteback = mode_lib->vba.TotalActiveWriteback + 1;
2773 		}
2774 	}
2775 
2776 	if (mode_lib->vba.TotalActiveWriteback == 0) {
2777 		mode_lib->vba.StutterEfficiencyNotIncludingVBlank = (1
2778 				- (mode_lib->vba.SRExitTime + mode_lib->vba.StutterBurstTime)
2779 						/ mode_lib->vba.StutterPeriod) * 100;
2780 	} else {
2781 		mode_lib->vba.StutterEfficiencyNotIncludingVBlank = 0;
2782 	}
2783 
2784 	mode_lib->vba.SmallestVBlank = 999999;
2785 	for (k = 0; k < mode_lib->vba.NumberOfActivePlanes; ++k) {
2786 		if (mode_lib->vba.SynchronizedVBlank || mode_lib->vba.NumberOfActivePlanes == 1) {
2787 			mode_lib->vba.VBlankTime = (double) (mode_lib->vba.VTotal[k]
2788 					- mode_lib->vba.VActive[k]) * mode_lib->vba.HTotal[k]
2789 					/ mode_lib->vba.PixelClock[k];
2790 		} else {
2791 			mode_lib->vba.VBlankTime = 0;
2792 		}
2793 		mode_lib->vba.SmallestVBlank = dml_min(
2794 				mode_lib->vba.SmallestVBlank,
2795 				mode_lib->vba.VBlankTime);
2796 	}
2797 
2798 	mode_lib->vba.StutterEfficiency = (mode_lib->vba.StutterEfficiencyNotIncludingVBlank / 100
2799 			* (mode_lib->vba.FrameTimeForMinFullDETBufferingTime
2800 					- mode_lib->vba.SmallestVBlank)
2801 			+ mode_lib->vba.SmallestVBlank)
2802 			/ mode_lib->vba.FrameTimeForMinFullDETBufferingTime * 100;
2803 }
2804 
2805 static void DisplayPipeConfiguration(struct display_mode_lib *mode_lib)
2806 {
2807 	// Display Pipe Configuration
2808 	double BytePerPixDETY;
2809 	double BytePerPixDETC;
2810 	double Read256BytesBlockHeightY;
2811 	double Read256BytesBlockHeightC;
2812 	double Read256BytesBlockWidthY;
2813 	double Read256BytesBlockWidthC;
2814 	double MaximumSwathHeightY;
2815 	double MaximumSwathHeightC;
2816 	double MinimumSwathHeightY;
2817 	double MinimumSwathHeightC;
2818 	double SwathWidth;
2819 	double SwathWidthGranularityY;
2820 	double SwathWidthGranularityC;
2821 	double RoundedUpMaxSwathSizeBytesY;
2822 	double RoundedUpMaxSwathSizeBytesC;
2823 	unsigned int j, k;
2824 
2825 	for (k = 0; k < mode_lib->vba.NumberOfActivePlanes; ++k) {
2826 		bool MainPlaneDoesODMCombine = false;
2827 
2828 		if (mode_lib->vba.SourcePixelFormat[k] == dm_444_64) {
2829 			BytePerPixDETY = 8;
2830 			BytePerPixDETC = 0;
2831 		} else if (mode_lib->vba.SourcePixelFormat[k] == dm_444_32) {
2832 			BytePerPixDETY = 4;
2833 			BytePerPixDETC = 0;
2834 		} else if (mode_lib->vba.SourcePixelFormat[k] == dm_444_16) {
2835 			BytePerPixDETY = 2;
2836 			BytePerPixDETC = 0;
2837 		} else if (mode_lib->vba.SourcePixelFormat[k] == dm_444_8) {
2838 			BytePerPixDETY = 1;
2839 			BytePerPixDETC = 0;
2840 		} else if (mode_lib->vba.SourcePixelFormat[k] == dm_420_8) {
2841 			BytePerPixDETY = 1;
2842 			BytePerPixDETC = 2;
2843 		} else {
2844 			BytePerPixDETY = 4.0 / 3.0;
2845 			BytePerPixDETC = 8.0 / 3.0;
2846 		}
2847 
2848 		if ((mode_lib->vba.SourcePixelFormat[k] == dm_444_64
2849 				|| mode_lib->vba.SourcePixelFormat[k] == dm_444_32
2850 				|| mode_lib->vba.SourcePixelFormat[k] == dm_444_16
2851 				|| mode_lib->vba.SourcePixelFormat[k] == dm_444_8)) {
2852 			if (mode_lib->vba.SurfaceTiling[k] == dm_sw_linear) {
2853 				Read256BytesBlockHeightY = 1;
2854 			} else if (mode_lib->vba.SourcePixelFormat[k] == dm_444_64) {
2855 				Read256BytesBlockHeightY = 4;
2856 			} else if (mode_lib->vba.SourcePixelFormat[k] == dm_444_32
2857 					|| mode_lib->vba.SourcePixelFormat[k] == dm_444_16) {
2858 				Read256BytesBlockHeightY = 8;
2859 			} else {
2860 				Read256BytesBlockHeightY = 16;
2861 			}
2862 			Read256BytesBlockWidthY = 256 / dml_ceil(BytePerPixDETY, 1)
2863 					/ Read256BytesBlockHeightY;
2864 			Read256BytesBlockHeightC = 0;
2865 			Read256BytesBlockWidthC = 0;
2866 		} else {
2867 			if (mode_lib->vba.SurfaceTiling[k] == dm_sw_linear) {
2868 				Read256BytesBlockHeightY = 1;
2869 				Read256BytesBlockHeightC = 1;
2870 			} else if (mode_lib->vba.SourcePixelFormat[k] == dm_420_8) {
2871 				Read256BytesBlockHeightY = 16;
2872 				Read256BytesBlockHeightC = 8;
2873 			} else {
2874 				Read256BytesBlockHeightY = 8;
2875 				Read256BytesBlockHeightC = 8;
2876 			}
2877 			Read256BytesBlockWidthY = 256 / dml_ceil(BytePerPixDETY, 1)
2878 					/ Read256BytesBlockHeightY;
2879 			Read256BytesBlockWidthC = 256 / dml_ceil(BytePerPixDETC, 2)
2880 					/ Read256BytesBlockHeightC;
2881 		}
2882 
2883 		if (mode_lib->vba.SourceScan[k] == dm_horz) {
2884 			MaximumSwathHeightY = Read256BytesBlockHeightY;
2885 			MaximumSwathHeightC = Read256BytesBlockHeightC;
2886 		} else {
2887 			MaximumSwathHeightY = Read256BytesBlockWidthY;
2888 			MaximumSwathHeightC = Read256BytesBlockWidthC;
2889 		}
2890 
2891 		if ((mode_lib->vba.SourcePixelFormat[k] == dm_444_64
2892 				|| mode_lib->vba.SourcePixelFormat[k] == dm_444_32
2893 				|| mode_lib->vba.SourcePixelFormat[k] == dm_444_16
2894 				|| mode_lib->vba.SourcePixelFormat[k] == dm_444_8)) {
2895 			if (mode_lib->vba.SurfaceTiling[k] == dm_sw_linear
2896 					|| (mode_lib->vba.SourcePixelFormat[k] == dm_444_64
2897 							&& (mode_lib->vba.SurfaceTiling[k]
2898 									== dm_sw_4kb_s
2899 									|| mode_lib->vba.SurfaceTiling[k]
2900 											== dm_sw_4kb_s_x
2901 									|| mode_lib->vba.SurfaceTiling[k]
2902 											== dm_sw_64kb_s
2903 									|| mode_lib->vba.SurfaceTiling[k]
2904 											== dm_sw_64kb_s_t
2905 									|| mode_lib->vba.SurfaceTiling[k]
2906 											== dm_sw_64kb_s_x
2907 									|| mode_lib->vba.SurfaceTiling[k]
2908 											== dm_sw_var_s
2909 									|| mode_lib->vba.SurfaceTiling[k]
2910 											== dm_sw_var_s_x)
2911 							&& mode_lib->vba.SourceScan[k] == dm_horz)) {
2912 				MinimumSwathHeightY = MaximumSwathHeightY;
2913 			} else if (mode_lib->vba.SourcePixelFormat[k] == dm_444_8
2914 					&& mode_lib->vba.SourceScan[k] != dm_horz) {
2915 				MinimumSwathHeightY = MaximumSwathHeightY;
2916 			} else {
2917 				MinimumSwathHeightY = MaximumSwathHeightY / 2.0;
2918 			}
2919 			MinimumSwathHeightC = MaximumSwathHeightC;
2920 		} else {
2921 			if (mode_lib->vba.SurfaceTiling[k] == dm_sw_linear) {
2922 				MinimumSwathHeightY = MaximumSwathHeightY;
2923 				MinimumSwathHeightC = MaximumSwathHeightC;
2924 			} else if (mode_lib->vba.SourcePixelFormat[k] == dm_420_8
2925 					&& mode_lib->vba.SourceScan[k] == dm_horz) {
2926 				MinimumSwathHeightY = MaximumSwathHeightY / 2.0;
2927 				MinimumSwathHeightC = MaximumSwathHeightC;
2928 			} else if (mode_lib->vba.SourcePixelFormat[k] == dm_420_10
2929 					&& mode_lib->vba.SourceScan[k] == dm_horz) {
2930 				MinimumSwathHeightC = MaximumSwathHeightC / 2.0;
2931 				MinimumSwathHeightY = MaximumSwathHeightY;
2932 			} else {
2933 				MinimumSwathHeightY = MaximumSwathHeightY;
2934 				MinimumSwathHeightC = MaximumSwathHeightC;
2935 			}
2936 		}
2937 
2938 		if (mode_lib->vba.SourceScan[k] == dm_horz) {
2939 			SwathWidth = mode_lib->vba.ViewportWidth[k];
2940 		} else {
2941 			SwathWidth = mode_lib->vba.ViewportHeight[k];
2942 		}
2943 
2944 		if (mode_lib->vba.ODMCombineEnabled[k] == true) {
2945 			MainPlaneDoesODMCombine = true;
2946 		}
2947 		for (j = 0; j < mode_lib->vba.NumberOfActivePlanes; ++j) {
2948 			if (mode_lib->vba.BlendingAndTiming[k] == j
2949 					&& mode_lib->vba.ODMCombineEnabled[j] == true) {
2950 				MainPlaneDoesODMCombine = true;
2951 			}
2952 		}
2953 
2954 		if (MainPlaneDoesODMCombine == true) {
2955 			SwathWidth = dml_min(
2956 					SwathWidth,
2957 					mode_lib->vba.HActive[k] / 2.0 * mode_lib->vba.HRatio[k]);
2958 		} else {
2959 			SwathWidth = SwathWidth / mode_lib->vba.DPPPerPlane[k];
2960 		}
2961 
2962 		SwathWidthGranularityY = 256 / dml_ceil(BytePerPixDETY, 1) / MaximumSwathHeightY;
2963 		RoundedUpMaxSwathSizeBytesY = (dml_ceil(
2964 				(double) (SwathWidth - 1),
2965 				SwathWidthGranularityY) + SwathWidthGranularityY) * BytePerPixDETY
2966 				* MaximumSwathHeightY;
2967 		if (mode_lib->vba.SourcePixelFormat[k] == dm_420_10) {
2968 			RoundedUpMaxSwathSizeBytesY = dml_ceil(RoundedUpMaxSwathSizeBytesY, 256)
2969 					+ 256;
2970 		}
2971 		if (MaximumSwathHeightC > 0) {
2972 			SwathWidthGranularityC = 256.0 / dml_ceil(BytePerPixDETC, 2)
2973 					/ MaximumSwathHeightC;
2974 			RoundedUpMaxSwathSizeBytesC = (dml_ceil(
2975 					(double) (SwathWidth / 2.0 - 1),
2976 					SwathWidthGranularityC) + SwathWidthGranularityC)
2977 					* BytePerPixDETC * MaximumSwathHeightC;
2978 			if (mode_lib->vba.SourcePixelFormat[k] == dm_420_10) {
2979 				RoundedUpMaxSwathSizeBytesC = dml_ceil(
2980 						RoundedUpMaxSwathSizeBytesC,
2981 						256) + 256;
2982 			}
2983 		} else
2984 			RoundedUpMaxSwathSizeBytesC = 0.0;
2985 
2986 		if (RoundedUpMaxSwathSizeBytesY + RoundedUpMaxSwathSizeBytesC
2987 				<= mode_lib->vba.DETBufferSizeInKByte * 1024.0 / 2.0) {
2988 			mode_lib->vba.SwathHeightY[k] = MaximumSwathHeightY;
2989 			mode_lib->vba.SwathHeightC[k] = MaximumSwathHeightC;
2990 		} else {
2991 			mode_lib->vba.SwathHeightY[k] = MinimumSwathHeightY;
2992 			mode_lib->vba.SwathHeightC[k] = MinimumSwathHeightC;
2993 		}
2994 
2995 		CalculateDETBufferSize(
2996 				mode_lib->vba.DETBufferSizeInKByte,
2997 				mode_lib->vba.SwathHeightY[k],
2998 				mode_lib->vba.SwathHeightC[k],
2999 				&mode_lib->vba.DETBufferSizeY[k],
3000 				&mode_lib->vba.DETBufferSizeC[k]);
3001 	}
3002 }
3003 
3004 static double CalculateTWait(
3005 		unsigned int PrefetchMode,
3006 		double DRAMClockChangeLatency,
3007 		double UrgentLatency,
3008 		double SREnterPlusExitTime)
3009 {
3010 	if (PrefetchMode == 0) {
3011 		return dml_max(
3012 				DRAMClockChangeLatency + UrgentLatency,
3013 				dml_max(SREnterPlusExitTime, UrgentLatency));
3014 	} else if (PrefetchMode == 1) {
3015 		return dml_max(SREnterPlusExitTime, UrgentLatency);
3016 	} else {
3017 		return UrgentLatency;
3018 	}
3019 }
3020 
3021 static double CalculateRemoteSurfaceFlipDelay(
3022 		struct display_mode_lib *mode_lib,
3023 		double VRatio,
3024 		double SwathWidth,
3025 		double Bpp,
3026 		double LineTime,
3027 		double XFCTSlvVupdateOffset,
3028 		double XFCTSlvVupdateWidth,
3029 		double XFCTSlvVreadyOffset,
3030 		double XFCXBUFLatencyTolerance,
3031 		double XFCFillBWOverhead,
3032 		double XFCSlvChunkSize,
3033 		double XFCBusTransportTime,
3034 		double TCalc,
3035 		double TWait,
3036 		double *SrcActiveDrainRate,
3037 		double *TInitXFill,
3038 		double *TslvChk)
3039 {
3040 	double TSlvSetup, AvgfillRate, result;
3041 
3042 	*SrcActiveDrainRate = VRatio * SwathWidth * Bpp / LineTime;
3043 	TSlvSetup = XFCTSlvVupdateOffset + XFCTSlvVupdateWidth + XFCTSlvVreadyOffset;
3044 	*TInitXFill = XFCXBUFLatencyTolerance / (1 + XFCFillBWOverhead / 100);
3045 	AvgfillRate = *SrcActiveDrainRate * (1 + XFCFillBWOverhead / 100);
3046 	*TslvChk = XFCSlvChunkSize / AvgfillRate;
3047 	dml_print(
3048 			"DML::CalculateRemoteSurfaceFlipDelay: SrcActiveDrainRate: %f\n",
3049 			*SrcActiveDrainRate);
3050 	dml_print("DML::CalculateRemoteSurfaceFlipDelay: TSlvSetup: %f\n", TSlvSetup);
3051 	dml_print("DML::CalculateRemoteSurfaceFlipDelay: TInitXFill: %f\n", *TInitXFill);
3052 	dml_print("DML::CalculateRemoteSurfaceFlipDelay: AvgfillRate: %f\n", AvgfillRate);
3053 	dml_print("DML::CalculateRemoteSurfaceFlipDelay: TslvChk: %f\n", *TslvChk);
3054 	result = 2 * XFCBusTransportTime + TSlvSetup + TCalc + TWait + *TslvChk + *TInitXFill; // TODO: This doesn't seem to match programming guide
3055 	dml_print("DML::CalculateRemoteSurfaceFlipDelay: RemoteSurfaceFlipDelay: %f\n", result);
3056 	return result;
3057 }
3058 
3059 static double CalculateWriteBackDelay(
3060 		enum source_format_class WritebackPixelFormat,
3061 		double WritebackHRatio,
3062 		double WritebackVRatio,
3063 		unsigned int WritebackLumaHTaps,
3064 		unsigned int WritebackLumaVTaps,
3065 		unsigned int WritebackChromaHTaps,
3066 		unsigned int WritebackChromaVTaps,
3067 		unsigned int WritebackDestinationWidth)
3068 {
3069 	double CalculateWriteBackDelay =
3070 			dml_max(
3071 					dml_ceil(WritebackLumaHTaps / 4.0, 1) / WritebackHRatio,
3072 					WritebackLumaVTaps * dml_ceil(1.0 / WritebackVRatio, 1)
3073 							* dml_ceil(
3074 									WritebackDestinationWidth
3075 											/ 4.0,
3076 									1)
3077 							+ dml_ceil(1.0 / WritebackVRatio, 1)
3078 									* (dml_ceil(
3079 											WritebackLumaVTaps
3080 													/ 4.0,
3081 											1) + 4));
3082 
3083 	if (WritebackPixelFormat != dm_444_32) {
3084 		CalculateWriteBackDelay =
3085 				dml_max(
3086 						CalculateWriteBackDelay,
3087 						dml_max(
3088 								dml_ceil(
3089 										WritebackChromaHTaps
3090 												/ 2.0,
3091 										1)
3092 										/ (2
3093 												* WritebackHRatio),
3094 								WritebackChromaVTaps
3095 										* dml_ceil(
3096 												1
3097 														/ (2
3098 																* WritebackVRatio),
3099 												1)
3100 										* dml_ceil(
3101 												WritebackDestinationWidth
3102 														/ 2.0
3103 														/ 2.0,
3104 												1)
3105 										+ dml_ceil(
3106 												1
3107 														/ (2
3108 																* WritebackVRatio),
3109 												1)
3110 												* (dml_ceil(
3111 														WritebackChromaVTaps
3112 																/ 4.0,
3113 														1)
3114 														+ 4)));
3115 	}
3116 	return CalculateWriteBackDelay;
3117 }
3118 
3119 static void CalculateActiveRowBandwidth(
3120 		bool GPUVMEnable,
3121 		enum source_format_class SourcePixelFormat,
3122 		double VRatio,
3123 		bool DCCEnable,
3124 		double LineTime,
3125 		unsigned int MetaRowByteLuma,
3126 		unsigned int MetaRowByteChroma,
3127 		unsigned int meta_row_height_luma,
3128 		unsigned int meta_row_height_chroma,
3129 		unsigned int PixelPTEBytesPerRowLuma,
3130 		unsigned int PixelPTEBytesPerRowChroma,
3131 		unsigned int dpte_row_height_luma,
3132 		unsigned int dpte_row_height_chroma,
3133 		double *meta_row_bw,
3134 		double *dpte_row_bw)
3135 {
3136 	if (DCCEnable != true) {
3137 		*meta_row_bw = 0;
3138 	} else if (SourcePixelFormat == dm_420_8 || SourcePixelFormat == dm_420_10) {
3139 		*meta_row_bw = VRatio * MetaRowByteLuma / (meta_row_height_luma * LineTime)
3140 				+ VRatio / 2 * MetaRowByteChroma
3141 						/ (meta_row_height_chroma * LineTime);
3142 	} else {
3143 		*meta_row_bw = VRatio * MetaRowByteLuma / (meta_row_height_luma * LineTime);
3144 	}
3145 
3146 	if (GPUVMEnable != true) {
3147 		*dpte_row_bw = 0;
3148 	} else if (SourcePixelFormat == dm_420_8 || SourcePixelFormat == dm_420_10) {
3149 		*dpte_row_bw = VRatio * PixelPTEBytesPerRowLuma / (dpte_row_height_luma * LineTime)
3150 				+ VRatio / 2 * PixelPTEBytesPerRowChroma
3151 						/ (dpte_row_height_chroma * LineTime);
3152 	} else {
3153 		*dpte_row_bw = VRatio * PixelPTEBytesPerRowLuma / (dpte_row_height_luma * LineTime);
3154 	}
3155 }
3156 
3157 static void CalculateFlipSchedule(
3158 		struct display_mode_lib *mode_lib,
3159 		double PercentOfIdealDRAMFabricAndSDPPortBWReceivedAfterUrgLatencyPixelMixedWithVMData,
3160 		double PercentOfIdealDRAMFabricAndSDPPortBWReceivedAfterUrgLatencyVMDataOnly,
3161 		double UrgentExtraLatency,
3162 		double UrgentLatency,
3163 		unsigned int GPUVMMaxPageTableLevels,
3164 		bool HostVMEnable,
3165 		unsigned int HostVMMaxPageTableLevels,
3166 		unsigned int HostVMCachedPageTableLevels,
3167 		bool GPUVMEnable,
3168 		double PDEAndMetaPTEBytesPerFrame,
3169 		double MetaRowBytes,
3170 		double DPTEBytesPerRow,
3171 		double BandwidthAvailableForImmediateFlip,
3172 		unsigned int TotImmediateFlipBytes,
3173 		enum source_format_class SourcePixelFormat,
3174 		double LineTime,
3175 		double VRatio,
3176 		double Tno_bw,
3177 		bool DCCEnable,
3178 		unsigned int dpte_row_height,
3179 		unsigned int meta_row_height,
3180 		unsigned int dpte_row_height_chroma,
3181 		unsigned int meta_row_height_chroma,
3182 		double *DestinationLinesToRequestVMInImmediateFlip,
3183 		double *DestinationLinesToRequestRowInImmediateFlip,
3184 		double *final_flip_bw,
3185 		bool *ImmediateFlipSupportedForPipe)
3186 {
3187 	double min_row_time = 0.0;
3188 	unsigned int HostVMDynamicLevels;
3189 	double TimeForFetchingMetaPTEImmediateFlip;
3190 	double TimeForFetchingRowInVBlankImmediateFlip;
3191 	double ImmediateFlipBW;
3192 	double HostVMInefficiencyFactor;
3193 
3194 	if (GPUVMEnable == true && HostVMEnable == true) {
3195 		HostVMInefficiencyFactor =
3196 				PercentOfIdealDRAMFabricAndSDPPortBWReceivedAfterUrgLatencyPixelMixedWithVMData
3197 						/ PercentOfIdealDRAMFabricAndSDPPortBWReceivedAfterUrgLatencyVMDataOnly;
3198 		HostVMDynamicLevels = HostVMMaxPageTableLevels - HostVMCachedPageTableLevels;
3199 	} else {
3200 		HostVMInefficiencyFactor = 1;
3201 		HostVMDynamicLevels = 0;
3202 	}
3203 
3204 	ImmediateFlipBW = (PDEAndMetaPTEBytesPerFrame + MetaRowBytes + DPTEBytesPerRow)
3205 			* BandwidthAvailableForImmediateFlip / TotImmediateFlipBytes;
3206 
3207 	if (GPUVMEnable == true) {
3208 		TimeForFetchingMetaPTEImmediateFlip = dml_max3(
3209 			Tno_bw + PDEAndMetaPTEBytesPerFrame * HostVMInefficiencyFactor / ImmediateFlipBW,
3210 			UrgentExtraLatency + UrgentLatency * (GPUVMMaxPageTableLevels * (HostVMDynamicLevels + 1) - 1),
3211 			LineTime / 4.0);
3212 	} else {
3213 		TimeForFetchingMetaPTEImmediateFlip = 0;
3214 	}
3215 
3216 	*DestinationLinesToRequestVMInImmediateFlip = dml_ceil(4.0 * (TimeForFetchingMetaPTEImmediateFlip / LineTime), 1) / 4.0;
3217 	if ((GPUVMEnable == true || DCCEnable == true)) {
3218 		TimeForFetchingRowInVBlankImmediateFlip = dml_max3((MetaRowBytes + DPTEBytesPerRow) * HostVMInefficiencyFactor / ImmediateFlipBW, UrgentLatency * (HostVMDynamicLevels + 1), LineTime / 4);
3219 	} else {
3220 		TimeForFetchingRowInVBlankImmediateFlip = 0;
3221 	}
3222 
3223 	*DestinationLinesToRequestRowInImmediateFlip = dml_ceil(4.0 * (TimeForFetchingRowInVBlankImmediateFlip / LineTime), 1) / 4.0;
3224 	*final_flip_bw = dml_max(PDEAndMetaPTEBytesPerFrame * HostVMInefficiencyFactor / (*DestinationLinesToRequestVMInImmediateFlip * LineTime), (MetaRowBytes + DPTEBytesPerRow) * HostVMInefficiencyFactor / (*DestinationLinesToRequestRowInImmediateFlip * LineTime));
3225 	if (SourcePixelFormat == dm_420_8 || SourcePixelFormat == dm_420_10) {
3226 		if (GPUVMEnable == true && DCCEnable != true) {
3227 			min_row_time = dml_min(
3228 					dpte_row_height * LineTime / VRatio,
3229 					dpte_row_height_chroma * LineTime / (VRatio / 2));
3230 		} else if (GPUVMEnable != true && DCCEnable == true) {
3231 			min_row_time = dml_min(
3232 					meta_row_height * LineTime / VRatio,
3233 					meta_row_height_chroma * LineTime / (VRatio / 2));
3234 		} else {
3235 			min_row_time = dml_min4(
3236 					dpte_row_height * LineTime / VRatio,
3237 					meta_row_height * LineTime / VRatio,
3238 					dpte_row_height_chroma * LineTime / (VRatio / 2),
3239 					meta_row_height_chroma * LineTime / (VRatio / 2));
3240 		}
3241 	} else {
3242 		if (GPUVMEnable == true && DCCEnable != true) {
3243 			min_row_time = dpte_row_height * LineTime / VRatio;
3244 		} else if (GPUVMEnable != true && DCCEnable == true) {
3245 			min_row_time = meta_row_height * LineTime / VRatio;
3246 		} else {
3247 			min_row_time = dml_min(
3248 					dpte_row_height * LineTime / VRatio,
3249 					meta_row_height * LineTime / VRatio);
3250 		}
3251 	}
3252 
3253 	if (*DestinationLinesToRequestVMInImmediateFlip >= 32
3254 			|| *DestinationLinesToRequestRowInImmediateFlip >= 16
3255 			|| TimeForFetchingMetaPTEImmediateFlip + 2 * TimeForFetchingRowInVBlankImmediateFlip > min_row_time) {
3256 		*ImmediateFlipSupportedForPipe = false;
3257 	} else {
3258 		*ImmediateFlipSupportedForPipe = true;
3259 	}
3260 }
3261 
3262 static unsigned int TruncToValidBPP(
3263 		double DecimalBPP,
3264 		double DesiredBPP,
3265 		bool DSCEnabled,
3266 		enum output_encoder_class Output,
3267 		enum output_format_class Format,
3268 		unsigned int DSCInputBitPerComponent)
3269 {
3270 	if (Output == dm_hdmi) {
3271 		if (Format == dm_420) {
3272 			if (DecimalBPP >= 18 && (DesiredBPP == 0 || DesiredBPP == 18))
3273 				return 18;
3274 			else if (DecimalBPP >= 15 && (DesiredBPP == 0 || DesiredBPP == 15))
3275 				return 15;
3276 			else if (DecimalBPP >= 12 && (DesiredBPP == 0 || DesiredBPP == 12))
3277 				return 12;
3278 			else
3279 				return BPP_INVALID;
3280 		} else if (Format == dm_444) {
3281 			if (DecimalBPP >= 36 && (DesiredBPP == 0 || DesiredBPP == 36))
3282 				return 36;
3283 			else if (DecimalBPP >= 30 && (DesiredBPP == 0 || DesiredBPP == 30))
3284 				return 30;
3285 			else if (DecimalBPP >= 24 && (DesiredBPP == 0 || DesiredBPP == 24))
3286 				return 24;
3287 			else if (DecimalBPP >= 18 && (DesiredBPP == 0 || DesiredBPP == 18))
3288 				return 18;
3289 			else
3290 				return BPP_INVALID;
3291 		} else {
3292 			if (DecimalBPP / 1.5 >= 24 && (DesiredBPP == 0 || DesiredBPP == 24))
3293 				return 24;
3294 			else if (DecimalBPP / 1.5 >= 20 && (DesiredBPP == 0 || DesiredBPP == 20))
3295 				return 20;
3296 			else if (DecimalBPP / 1.5 >= 16 && (DesiredBPP == 0 || DesiredBPP == 16))
3297 				return 16;
3298 			else
3299 				return BPP_INVALID;
3300 		}
3301 	} else {
3302 		if (DSCEnabled) {
3303 			if (Format == dm_420) {
3304 				if (DesiredBPP == 0) {
3305 					if (DecimalBPP < 6)
3306 						return BPP_INVALID;
3307 					else if (DecimalBPP >= 1.5 * DSCInputBitPerComponent - 1.0 / 16.0)
3308 						return 1.5 * DSCInputBitPerComponent - 1.0 / 16.0;
3309 					else
3310 						return dml_floor(16 * DecimalBPP, 1) / 16.0;
3311 				} else {
3312 					if (DecimalBPP < 6
3313 							|| DesiredBPP < 6
3314 							|| DesiredBPP > 1.5 * DSCInputBitPerComponent - 1.0 / 16.0
3315 							|| DecimalBPP < DesiredBPP) {
3316 						return BPP_INVALID;
3317 					} else {
3318 						return DesiredBPP;
3319 					}
3320 				}
3321 			} else if (Format == dm_n422) {
3322 				if (DesiredBPP == 0) {
3323 					if (DecimalBPP < 7)
3324 						return BPP_INVALID;
3325 					else if (DecimalBPP >= 2 * DSCInputBitPerComponent - 1.0 / 16.0)
3326 						return 2 * DSCInputBitPerComponent - 1.0 / 16.0;
3327 					else
3328 						return dml_floor(16 * DecimalBPP, 1) / 16.0;
3329 				} else {
3330 					if (DecimalBPP < 7
3331 							|| DesiredBPP < 7
3332 							|| DesiredBPP > 2 * DSCInputBitPerComponent - 1.0 / 16.0
3333 							|| DecimalBPP < DesiredBPP) {
3334 						return BPP_INVALID;
3335 					} else {
3336 						return DesiredBPP;
3337 					}
3338 				}
3339 			} else {
3340 				if (DesiredBPP == 0) {
3341 					if (DecimalBPP < 8)
3342 						return BPP_INVALID;
3343 					else if (DecimalBPP >= 3 * DSCInputBitPerComponent - 1.0 / 16.0)
3344 						return 3 * DSCInputBitPerComponent - 1.0 / 16.0;
3345 					else
3346 						return dml_floor(16 * DecimalBPP, 1) / 16.0;
3347 				} else {
3348 					if (DecimalBPP < 8
3349 							|| DesiredBPP < 8
3350 							|| DesiredBPP > 3 * DSCInputBitPerComponent - 1.0 / 16.0
3351 							|| DecimalBPP < DesiredBPP) {
3352 						return BPP_INVALID;
3353 					} else {
3354 						return DesiredBPP;
3355 					}
3356 				}
3357 			}
3358 		} else if (Format == dm_420) {
3359 			if (DecimalBPP >= 18 && (DesiredBPP == 0 || DesiredBPP == 18))
3360 				return 18;
3361 			else if (DecimalBPP >= 15 && (DesiredBPP == 0 || DesiredBPP == 15))
3362 				return 15;
3363 			else if (DecimalBPP >= 12 && (DesiredBPP == 0 || DesiredBPP == 12))
3364 				return 12;
3365 			else
3366 				return BPP_INVALID;
3367 		} else if (Format == dm_s422 || Format == dm_n422) {
3368 			if (DecimalBPP >= 24 && (DesiredBPP == 0 || DesiredBPP == 24))
3369 				return 24;
3370 			else if (DecimalBPP >= 20 && (DesiredBPP == 0 || DesiredBPP == 20))
3371 				return 20;
3372 			else if (DecimalBPP >= 16 && (DesiredBPP == 0 || DesiredBPP == 16))
3373 				return 16;
3374 			else
3375 				return BPP_INVALID;
3376 		} else {
3377 			if (DecimalBPP >= 36 && (DesiredBPP == 0 || DesiredBPP == 36))
3378 				return 36;
3379 			else if (DecimalBPP >= 30 && (DesiredBPP == 0 || DesiredBPP == 30))
3380 				return 30;
3381 			else if (DecimalBPP >= 24 && (DesiredBPP == 0 || DesiredBPP == 24))
3382 				return 24;
3383 			else if (DecimalBPP >= 18 && (DesiredBPP == 0 || DesiredBPP == 18))
3384 				return 18;
3385 			else
3386 				return BPP_INVALID;
3387 		}
3388 	}
3389 }
3390 
3391 void dml21_ModeSupportAndSystemConfigurationFull(struct display_mode_lib *mode_lib)
3392 {
3393 	struct vba_vars_st *locals = &mode_lib->vba;
3394 
3395 	int i;
3396 	unsigned int j, k, m;
3397 
3398 	/*MODE SUPPORT, VOLTAGE STATE AND SOC CONFIGURATION*/
3399 
3400 	/*Scale Ratio, taps Support Check*/
3401 
3402 	mode_lib->vba.ScaleRatioAndTapsSupport = true;
3403 	for (k = 0; k <= mode_lib->vba.NumberOfActivePlanes - 1; k++) {
3404 		if (mode_lib->vba.ScalerEnabled[k] == false
3405 				&& ((mode_lib->vba.SourcePixelFormat[k] != dm_444_64
3406 						&& mode_lib->vba.SourcePixelFormat[k] != dm_444_32
3407 						&& mode_lib->vba.SourcePixelFormat[k] != dm_444_16
3408 						&& mode_lib->vba.SourcePixelFormat[k] != dm_mono_16
3409 						&& mode_lib->vba.SourcePixelFormat[k] != dm_mono_8)
3410 						|| mode_lib->vba.HRatio[k] != 1.0
3411 						|| mode_lib->vba.htaps[k] != 1.0
3412 						|| mode_lib->vba.VRatio[k] != 1.0
3413 						|| mode_lib->vba.vtaps[k] != 1.0)) {
3414 			mode_lib->vba.ScaleRatioAndTapsSupport = false;
3415 		} else if (mode_lib->vba.vtaps[k] < 1.0 || mode_lib->vba.vtaps[k] > 8.0
3416 				|| mode_lib->vba.htaps[k] < 1.0 || mode_lib->vba.htaps[k] > 8.0
3417 				|| (mode_lib->vba.htaps[k] > 1.0
3418 						&& (mode_lib->vba.htaps[k] % 2) == 1)
3419 				|| mode_lib->vba.HRatio[k] > mode_lib->vba.MaxHSCLRatio
3420 				|| mode_lib->vba.VRatio[k] > mode_lib->vba.MaxVSCLRatio
3421 				|| mode_lib->vba.HRatio[k] > mode_lib->vba.htaps[k]
3422 				|| mode_lib->vba.VRatio[k] > mode_lib->vba.vtaps[k]
3423 				|| (mode_lib->vba.SourcePixelFormat[k] != dm_444_64
3424 						&& mode_lib->vba.SourcePixelFormat[k] != dm_444_32
3425 						&& mode_lib->vba.SourcePixelFormat[k] != dm_444_16
3426 						&& mode_lib->vba.SourcePixelFormat[k] != dm_mono_16
3427 						&& mode_lib->vba.SourcePixelFormat[k] != dm_mono_8
3428 						&& (mode_lib->vba.HRatio[k] / 2.0
3429 								> mode_lib->vba.HTAPsChroma[k]
3430 								|| mode_lib->vba.VRatio[k] / 2.0
3431 										> mode_lib->vba.VTAPsChroma[k]))) {
3432 			mode_lib->vba.ScaleRatioAndTapsSupport = false;
3433 		}
3434 	}
3435 	/*Source Format, Pixel Format and Scan Support Check*/
3436 
3437 	mode_lib->vba.SourceFormatPixelAndScanSupport = true;
3438 	for (k = 0; k <= mode_lib->vba.NumberOfActivePlanes - 1; k++) {
3439 		if ((mode_lib->vba.SurfaceTiling[k] == dm_sw_linear
3440 				&& mode_lib->vba.SourceScan[k] != dm_horz)
3441 				|| ((mode_lib->vba.SurfaceTiling[k] == dm_sw_4kb_d
3442 						|| mode_lib->vba.SurfaceTiling[k] == dm_sw_4kb_d_x
3443 						|| mode_lib->vba.SurfaceTiling[k] == dm_sw_64kb_d
3444 						|| mode_lib->vba.SurfaceTiling[k] == dm_sw_64kb_d_t
3445 						|| mode_lib->vba.SurfaceTiling[k] == dm_sw_64kb_d_x
3446 						|| mode_lib->vba.SurfaceTiling[k] == dm_sw_var_d
3447 						|| mode_lib->vba.SurfaceTiling[k] == dm_sw_var_d_x)
3448 						&& mode_lib->vba.SourcePixelFormat[k] != dm_444_64)
3449 				|| (mode_lib->vba.SurfaceTiling[k] == dm_sw_64kb_r_x
3450 						&& (mode_lib->vba.SourcePixelFormat[k] == dm_mono_8
3451 								|| mode_lib->vba.SourcePixelFormat[k]
3452 										== dm_420_8
3453 								|| mode_lib->vba.SourcePixelFormat[k]
3454 										== dm_420_10))
3455 				|| (((mode_lib->vba.SurfaceTiling[k] == dm_sw_gfx7_2d_thin_gl
3456 						|| mode_lib->vba.SurfaceTiling[k]
3457 								== dm_sw_gfx7_2d_thin_lvp)
3458 						&& !((mode_lib->vba.SourcePixelFormat[k]
3459 								== dm_444_64
3460 								|| mode_lib->vba.SourcePixelFormat[k]
3461 										== dm_444_32)
3462 								&& mode_lib->vba.SourceScan[k]
3463 										== dm_horz
3464 								&& mode_lib->vba.SupportGFX7CompatibleTilingIn32bppAnd64bpp
3465 										== true
3466 								&& mode_lib->vba.DCCEnable[k]
3467 										== false))
3468 						|| (mode_lib->vba.DCCEnable[k] == true
3469 								&& (mode_lib->vba.SurfaceTiling[k]
3470 										== dm_sw_linear
3471 										|| mode_lib->vba.SourcePixelFormat[k]
3472 												== dm_420_8
3473 										|| mode_lib->vba.SourcePixelFormat[k]
3474 												== dm_420_10)))) {
3475 			mode_lib->vba.SourceFormatPixelAndScanSupport = false;
3476 		}
3477 	}
3478 	/*Bandwidth Support Check*/
3479 
3480 	for (k = 0; k <= mode_lib->vba.NumberOfActivePlanes - 1; k++) {
3481 		if (mode_lib->vba.SourcePixelFormat[k] == dm_444_64) {
3482 			locals->BytePerPixelInDETY[k] = 8.0;
3483 			locals->BytePerPixelInDETC[k] = 0.0;
3484 		} else if (mode_lib->vba.SourcePixelFormat[k] == dm_444_32) {
3485 			locals->BytePerPixelInDETY[k] = 4.0;
3486 			locals->BytePerPixelInDETC[k] = 0.0;
3487 		} else if (mode_lib->vba.SourcePixelFormat[k] == dm_444_16
3488 				|| mode_lib->vba.SourcePixelFormat[k] == dm_mono_16) {
3489 			locals->BytePerPixelInDETY[k] = 2.0;
3490 			locals->BytePerPixelInDETC[k] = 0.0;
3491 		} else if (mode_lib->vba.SourcePixelFormat[k] == dm_mono_8) {
3492 			locals->BytePerPixelInDETY[k] = 1.0;
3493 			locals->BytePerPixelInDETC[k] = 0.0;
3494 		} else if (mode_lib->vba.SourcePixelFormat[k] == dm_420_8) {
3495 			locals->BytePerPixelInDETY[k] = 1.0;
3496 			locals->BytePerPixelInDETC[k] = 2.0;
3497 		} else {
3498 			locals->BytePerPixelInDETY[k] = 4.0 / 3;
3499 			locals->BytePerPixelInDETC[k] = 8.0 / 3;
3500 		}
3501 		if (mode_lib->vba.SourceScan[k] == dm_horz) {
3502 			locals->SwathWidthYSingleDPP[k] = mode_lib->vba.ViewportWidth[k];
3503 		} else {
3504 			locals->SwathWidthYSingleDPP[k] = mode_lib->vba.ViewportHeight[k];
3505 		}
3506 	}
3507 	for (k = 0; k <= mode_lib->vba.NumberOfActivePlanes - 1; k++) {
3508 		locals->ReadBandwidthLuma[k] = locals->SwathWidthYSingleDPP[k] * dml_ceil(locals->BytePerPixelInDETY[k], 1.0)
3509 				/ (mode_lib->vba.HTotal[k] / mode_lib->vba.PixelClock[k]) * mode_lib->vba.VRatio[k];
3510 		locals->ReadBandwidthChroma[k] = locals->SwathWidthYSingleDPP[k] / 2 * dml_ceil(locals->BytePerPixelInDETC[k], 2.0)
3511 				/ (mode_lib->vba.HTotal[k] / mode_lib->vba.PixelClock[k]) * mode_lib->vba.VRatio[k] / 2.0;
3512 		locals->ReadBandwidth[k] = locals->ReadBandwidthLuma[k] + locals->ReadBandwidthChroma[k];
3513 	}
3514 	for (k = 0; k <= mode_lib->vba.NumberOfActivePlanes - 1; k++) {
3515 		if (mode_lib->vba.WritebackEnable[k] == true
3516 				&& mode_lib->vba.WritebackPixelFormat[k] == dm_444_32) {
3517 			locals->WriteBandwidth[k] = mode_lib->vba.WritebackDestinationWidth[k]
3518 					* mode_lib->vba.WritebackDestinationHeight[k]
3519 					/ (mode_lib->vba.WritebackSourceHeight[k]
3520 							* mode_lib->vba.HTotal[k]
3521 							/ mode_lib->vba.PixelClock[k]) * 4.0;
3522 		} else if (mode_lib->vba.WritebackEnable[k] == true
3523 				&& mode_lib->vba.WritebackPixelFormat[k] == dm_420_10) {
3524 			locals->WriteBandwidth[k] = mode_lib->vba.WritebackDestinationWidth[k]
3525 					* mode_lib->vba.WritebackDestinationHeight[k]
3526 					/ (mode_lib->vba.WritebackSourceHeight[k]
3527 							* mode_lib->vba.HTotal[k]
3528 							/ mode_lib->vba.PixelClock[k]) * 3.0;
3529 		} else if (mode_lib->vba.WritebackEnable[k] == true) {
3530 			locals->WriteBandwidth[k] = mode_lib->vba.WritebackDestinationWidth[k]
3531 					* mode_lib->vba.WritebackDestinationHeight[k]
3532 					/ (mode_lib->vba.WritebackSourceHeight[k]
3533 							* mode_lib->vba.HTotal[k]
3534 							/ mode_lib->vba.PixelClock[k]) * 1.5;
3535 		} else {
3536 			locals->WriteBandwidth[k] = 0.0;
3537 		}
3538 	}
3539 	mode_lib->vba.DCCEnabledInAnyPlane = false;
3540 	for (k = 0; k <= mode_lib->vba.NumberOfActivePlanes - 1; k++) {
3541 		if (mode_lib->vba.DCCEnable[k] == true) {
3542 			mode_lib->vba.DCCEnabledInAnyPlane = true;
3543 		}
3544 	}
3545 	for (i = 0; i <= mode_lib->vba.soc.num_states; i++) {
3546 		locals->IdealSDPPortBandwidthPerState[i] = dml_min3(
3547 				mode_lib->vba.ReturnBusWidth * mode_lib->vba.DCFCLKPerState[i],
3548 				mode_lib->vba.DRAMSpeedPerState[i] * mode_lib->vba.NumberOfChannels
3549 						* mode_lib->vba.DRAMChannelWidth,
3550 				mode_lib->vba.FabricClockPerState[i]
3551 						* mode_lib->vba.FabricDatapathToDCNDataReturn);
3552 		if (mode_lib->vba.HostVMEnable == false) {
3553 			locals->ReturnBWPerState[i] = locals->IdealSDPPortBandwidthPerState[i]
3554 					* mode_lib->vba.PercentOfIdealDRAMFabricAndSDPPortBWReceivedAfterUrgLatencyPixelDataOnly / 100.0;
3555 		} else {
3556 			locals->ReturnBWPerState[i] = locals->IdealSDPPortBandwidthPerState[i]
3557 					* mode_lib->vba.PercentOfIdealDRAMFabricAndSDPPortBWReceivedAfterUrgLatencyPixelMixedWithVMData / 100.0;
3558 		}
3559 	}
3560 	/*Writeback Latency support check*/
3561 
3562 	mode_lib->vba.WritebackLatencySupport = true;
3563 	for (k = 0; k <= mode_lib->vba.NumberOfActivePlanes - 1; k++) {
3564 		if (mode_lib->vba.WritebackEnable[k] == true) {
3565 			if (mode_lib->vba.WritebackPixelFormat[k] == dm_444_32) {
3566 				if (locals->WriteBandwidth[k]
3567 						> (mode_lib->vba.WritebackInterfaceLumaBufferSize
3568 								+ mode_lib->vba.WritebackInterfaceChromaBufferSize)
3569 								/ mode_lib->vba.WritebackLatency) {
3570 					mode_lib->vba.WritebackLatencySupport = false;
3571 				}
3572 			} else {
3573 				if (locals->WriteBandwidth[k]
3574 						> 1.5
3575 								* dml_min(
3576 										mode_lib->vba.WritebackInterfaceLumaBufferSize,
3577 										2.0
3578 												* mode_lib->vba.WritebackInterfaceChromaBufferSize)
3579 								/ mode_lib->vba.WritebackLatency) {
3580 					mode_lib->vba.WritebackLatencySupport = false;
3581 				}
3582 			}
3583 		}
3584 	}
3585 	/*Re-ordering Buffer Support Check*/
3586 
3587 	for (i = 0; i <= mode_lib->vba.soc.num_states; i++) {
3588 		locals->UrgentRoundTripAndOutOfOrderLatencyPerState[i] =
3589 				(mode_lib->vba.RoundTripPingLatencyCycles + 32.0) / mode_lib->vba.DCFCLKPerState[i]
3590 				+ dml_max3(mode_lib->vba.UrgentOutOfOrderReturnPerChannelPixelDataOnly,
3591 						mode_lib->vba.UrgentOutOfOrderReturnPerChannelPixelMixedWithVMData,
3592 						mode_lib->vba.UrgentOutOfOrderReturnPerChannelVMDataOnly)
3593 					* mode_lib->vba.NumberOfChannels / locals->ReturnBWPerState[i];
3594 		if ((mode_lib->vba.ROBBufferSizeInKByte - mode_lib->vba.PixelChunkSizeInKByte) * 1024.0 / locals->ReturnBWPerState[i]
3595 				> locals->UrgentRoundTripAndOutOfOrderLatencyPerState[i]) {
3596 			locals->ROBSupport[i] = true;
3597 		} else {
3598 			locals->ROBSupport[i] = false;
3599 		}
3600 	}
3601 	/*Writeback Mode Support Check*/
3602 
3603 	mode_lib->vba.TotalNumberOfActiveWriteback = 0;
3604 	for (k = 0; k <= mode_lib->vba.NumberOfActivePlanes - 1; k++) {
3605 		if (mode_lib->vba.WritebackEnable[k] == true) {
3606 			if (mode_lib->vba.ActiveWritebacksPerPlane[k] == 0)
3607 				mode_lib->vba.ActiveWritebacksPerPlane[k] = 1;
3608 			mode_lib->vba.TotalNumberOfActiveWriteback =
3609 					mode_lib->vba.TotalNumberOfActiveWriteback
3610 							+ mode_lib->vba.ActiveWritebacksPerPlane[k];
3611 		}
3612 	}
3613 	mode_lib->vba.WritebackModeSupport = true;
3614 	if (mode_lib->vba.TotalNumberOfActiveWriteback > mode_lib->vba.MaxNumWriteback) {
3615 		mode_lib->vba.WritebackModeSupport = false;
3616 	}
3617 	for (k = 0; k <= mode_lib->vba.NumberOfActivePlanes - 1; k++) {
3618 		if (mode_lib->vba.WritebackEnable[k] == true
3619 				&& mode_lib->vba.Writeback10bpc420Supported != true
3620 				&& mode_lib->vba.WritebackPixelFormat[k] == dm_420_10) {
3621 			mode_lib->vba.WritebackModeSupport = false;
3622 		}
3623 	}
3624 	/*Writeback Scale Ratio and Taps Support Check*/
3625 
3626 	mode_lib->vba.WritebackScaleRatioAndTapsSupport = true;
3627 	for (k = 0; k <= mode_lib->vba.NumberOfActivePlanes - 1; k++) {
3628 		if (mode_lib->vba.WritebackEnable[k] == true) {
3629 			if (mode_lib->vba.WritebackLumaAndChromaScalingSupported == false
3630 					&& (mode_lib->vba.WritebackHRatio[k] != 1.0
3631 							|| mode_lib->vba.WritebackVRatio[k] != 1.0)) {
3632 				mode_lib->vba.WritebackScaleRatioAndTapsSupport = false;
3633 			}
3634 			if (mode_lib->vba.WritebackHRatio[k] > mode_lib->vba.WritebackMaxHSCLRatio
3635 					|| mode_lib->vba.WritebackVRatio[k]
3636 							> mode_lib->vba.WritebackMaxVSCLRatio
3637 					|| mode_lib->vba.WritebackHRatio[k]
3638 							< mode_lib->vba.WritebackMinHSCLRatio
3639 					|| mode_lib->vba.WritebackVRatio[k]
3640 							< mode_lib->vba.WritebackMinVSCLRatio
3641 					|| mode_lib->vba.WritebackLumaHTaps[k]
3642 							> mode_lib->vba.WritebackMaxHSCLTaps
3643 					|| mode_lib->vba.WritebackLumaVTaps[k]
3644 							> mode_lib->vba.WritebackMaxVSCLTaps
3645 					|| mode_lib->vba.WritebackHRatio[k]
3646 							> mode_lib->vba.WritebackLumaHTaps[k]
3647 					|| mode_lib->vba.WritebackVRatio[k]
3648 							> mode_lib->vba.WritebackLumaVTaps[k]
3649 					|| (mode_lib->vba.WritebackLumaHTaps[k] > 2.0
3650 							&& ((mode_lib->vba.WritebackLumaHTaps[k] % 2)
3651 									== 1))
3652 					|| (mode_lib->vba.WritebackPixelFormat[k] != dm_444_32
3653 							&& (mode_lib->vba.WritebackChromaHTaps[k]
3654 									> mode_lib->vba.WritebackMaxHSCLTaps
3655 									|| mode_lib->vba.WritebackChromaVTaps[k]
3656 											> mode_lib->vba.WritebackMaxVSCLTaps
3657 									|| 2.0
3658 											* mode_lib->vba.WritebackHRatio[k]
3659 											> mode_lib->vba.WritebackChromaHTaps[k]
3660 									|| 2.0
3661 											* mode_lib->vba.WritebackVRatio[k]
3662 											> mode_lib->vba.WritebackChromaVTaps[k]
3663 									|| (mode_lib->vba.WritebackChromaHTaps[k] > 2.0
3664 										&& ((mode_lib->vba.WritebackChromaHTaps[k] % 2) == 1))))) {
3665 				mode_lib->vba.WritebackScaleRatioAndTapsSupport = false;
3666 			}
3667 			if (mode_lib->vba.WritebackVRatio[k] < 1.0) {
3668 				mode_lib->vba.WritebackLumaVExtra =
3669 						dml_max(1.0 - 2.0 / dml_ceil(1.0 / mode_lib->vba.WritebackVRatio[k], 1.0), 0.0);
3670 			} else {
3671 				mode_lib->vba.WritebackLumaVExtra = -1;
3672 			}
3673 			if ((mode_lib->vba.WritebackPixelFormat[k] == dm_444_32
3674 					&& mode_lib->vba.WritebackLumaVTaps[k]
3675 							> (mode_lib->vba.WritebackLineBufferLumaBufferSize
3676 									+ mode_lib->vba.WritebackLineBufferChromaBufferSize)
3677 									/ 3.0
3678 									/ mode_lib->vba.WritebackDestinationWidth[k]
3679 									- mode_lib->vba.WritebackLumaVExtra)
3680 					|| (mode_lib->vba.WritebackPixelFormat[k] == dm_420_8
3681 							&& mode_lib->vba.WritebackLumaVTaps[k]
3682 									> mode_lib->vba.WritebackLineBufferLumaBufferSize
3683 											* 8.0 / 10.0 / mode_lib->vba.WritebackDestinationWidth[k]
3684 											- mode_lib->vba.WritebackLumaVExtra)
3685 					|| (mode_lib->vba.WritebackPixelFormat[k] == dm_420_10
3686 							&& mode_lib->vba.WritebackLumaVTaps[k]
3687 									> mode_lib->vba.WritebackLineBufferLumaBufferSize
3688 											* 8.0 / 10.0
3689 											/ mode_lib->vba.WritebackDestinationWidth[k]
3690 											- mode_lib->vba.WritebackLumaVExtra)) {
3691 				mode_lib->vba.WritebackScaleRatioAndTapsSupport = false;
3692 			}
3693 			if (2.0 * mode_lib->vba.WritebackVRatio[k] < 1) {
3694 				mode_lib->vba.WritebackChromaVExtra = 0.0;
3695 			} else {
3696 				mode_lib->vba.WritebackChromaVExtra = -1;
3697 			}
3698 			if ((mode_lib->vba.WritebackPixelFormat[k] == dm_420_8
3699 					&& mode_lib->vba.WritebackChromaVTaps[k]
3700 							> mode_lib->vba.WritebackLineBufferChromaBufferSize
3701 									* 8.0 / 10.0 / mode_lib->vba.WritebackDestinationWidth[k]
3702 									- mode_lib->vba.WritebackChromaVExtra)
3703 					|| (mode_lib->vba.WritebackPixelFormat[k] == dm_420_10
3704 							&& mode_lib->vba.WritebackChromaVTaps[k]
3705 									> mode_lib->vba.WritebackLineBufferChromaBufferSize
3706 											* 8.0 / 10.0
3707 											/ mode_lib->vba.WritebackDestinationWidth[k]
3708 											- mode_lib->vba.WritebackChromaVExtra)) {
3709 				mode_lib->vba.WritebackScaleRatioAndTapsSupport = false;
3710 			}
3711 		}
3712 	}
3713 	/*Maximum DISPCLK/DPPCLK Support check*/
3714 
3715 	mode_lib->vba.WritebackRequiredDISPCLK = 0.0;
3716 	for (k = 0; k <= mode_lib->vba.NumberOfActivePlanes - 1; k++) {
3717 		if (mode_lib->vba.WritebackEnable[k] == true) {
3718 			mode_lib->vba.WritebackRequiredDISPCLK =
3719 					dml_max(
3720 							mode_lib->vba.WritebackRequiredDISPCLK,
3721 							CalculateWriteBackDISPCLK(
3722 									mode_lib->vba.WritebackPixelFormat[k],
3723 									mode_lib->vba.PixelClock[k],
3724 									mode_lib->vba.WritebackHRatio[k],
3725 									mode_lib->vba.WritebackVRatio[k],
3726 									mode_lib->vba.WritebackLumaHTaps[k],
3727 									mode_lib->vba.WritebackLumaVTaps[k],
3728 									mode_lib->vba.WritebackChromaHTaps[k],
3729 									mode_lib->vba.WritebackChromaVTaps[k],
3730 									mode_lib->vba.WritebackDestinationWidth[k],
3731 									mode_lib->vba.HTotal[k],
3732 									mode_lib->vba.WritebackChromaLineBufferWidth));
3733 		}
3734 	}
3735 	for (k = 0; k <= mode_lib->vba.NumberOfActivePlanes - 1; k++) {
3736 		if (mode_lib->vba.HRatio[k] > 1.0) {
3737 			locals->PSCL_FACTOR[k] = dml_min(
3738 					mode_lib->vba.MaxDCHUBToPSCLThroughput,
3739 					mode_lib->vba.MaxPSCLToLBThroughput
3740 							* mode_lib->vba.HRatio[k]
3741 							/ dml_ceil(
3742 									mode_lib->vba.htaps[k]
3743 											/ 6.0,
3744 									1.0));
3745 		} else {
3746 			locals->PSCL_FACTOR[k] = dml_min(
3747 					mode_lib->vba.MaxDCHUBToPSCLThroughput,
3748 					mode_lib->vba.MaxPSCLToLBThroughput);
3749 		}
3750 		if (locals->BytePerPixelInDETC[k] == 0.0) {
3751 			locals->PSCL_FACTOR_CHROMA[k] = 0.0;
3752 			locals->MinDPPCLKUsingSingleDPP[k] =
3753 					mode_lib->vba.PixelClock[k]
3754 							* dml_max3(
3755 									mode_lib->vba.vtaps[k] / 6.0
3756 											* dml_min(
3757 													1.0,
3758 													mode_lib->vba.HRatio[k]),
3759 									mode_lib->vba.HRatio[k]
3760 											* mode_lib->vba.VRatio[k]
3761 											/ locals->PSCL_FACTOR[k],
3762 									1.0);
3763 			if ((mode_lib->vba.htaps[k] > 6.0 || mode_lib->vba.vtaps[k] > 6.0)
3764 					&& locals->MinDPPCLKUsingSingleDPP[k]
3765 							< 2.0 * mode_lib->vba.PixelClock[k]) {
3766 				locals->MinDPPCLKUsingSingleDPP[k] = 2.0
3767 						* mode_lib->vba.PixelClock[k];
3768 			}
3769 		} else {
3770 			if (mode_lib->vba.HRatio[k] / 2.0 > 1.0) {
3771 				locals->PSCL_FACTOR_CHROMA[k] =
3772 						dml_min(
3773 								mode_lib->vba.MaxDCHUBToPSCLThroughput,
3774 								mode_lib->vba.MaxPSCLToLBThroughput
3775 										* mode_lib->vba.HRatio[k]
3776 										/ 2.0
3777 										/ dml_ceil(
3778 												mode_lib->vba.HTAPsChroma[k]
3779 														/ 6.0,
3780 												1.0));
3781 			} else {
3782 				locals->PSCL_FACTOR_CHROMA[k] = dml_min(
3783 						mode_lib->vba.MaxDCHUBToPSCLThroughput,
3784 						mode_lib->vba.MaxPSCLToLBThroughput);
3785 			}
3786 			locals->MinDPPCLKUsingSingleDPP[k] =
3787 					mode_lib->vba.PixelClock[k]
3788 							* dml_max5(
3789 									mode_lib->vba.vtaps[k] / 6.0
3790 											* dml_min(
3791 													1.0,
3792 													mode_lib->vba.HRatio[k]),
3793 									mode_lib->vba.HRatio[k]
3794 											* mode_lib->vba.VRatio[k]
3795 											/ locals->PSCL_FACTOR[k],
3796 									mode_lib->vba.VTAPsChroma[k]
3797 											/ 6.0
3798 											* dml_min(
3799 													1.0,
3800 													mode_lib->vba.HRatio[k]
3801 															/ 2.0),
3802 									mode_lib->vba.HRatio[k]
3803 											* mode_lib->vba.VRatio[k]
3804 											/ 4.0
3805 											/ locals->PSCL_FACTOR_CHROMA[k],
3806 									1.0);
3807 			if ((mode_lib->vba.htaps[k] > 6.0 || mode_lib->vba.vtaps[k] > 6.0
3808 					|| mode_lib->vba.HTAPsChroma[k] > 6.0
3809 					|| mode_lib->vba.VTAPsChroma[k] > 6.0)
3810 					&& locals->MinDPPCLKUsingSingleDPP[k]
3811 							< 2.0 * mode_lib->vba.PixelClock[k]) {
3812 				locals->MinDPPCLKUsingSingleDPP[k] = 2.0
3813 						* mode_lib->vba.PixelClock[k];
3814 			}
3815 		}
3816 	}
3817 	for (k = 0; k <= mode_lib->vba.NumberOfActivePlanes - 1; k++) {
3818 		Calculate256BBlockSizes(
3819 				mode_lib->vba.SourcePixelFormat[k],
3820 				mode_lib->vba.SurfaceTiling[k],
3821 				dml_ceil(locals->BytePerPixelInDETY[k], 1.0),
3822 				dml_ceil(locals->BytePerPixelInDETC[k], 2.0),
3823 				&locals->Read256BlockHeightY[k],
3824 				&locals->Read256BlockHeightC[k],
3825 				&locals->Read256BlockWidthY[k],
3826 				&locals->Read256BlockWidthC[k]);
3827 		if (mode_lib->vba.SourceScan[k] == dm_horz) {
3828 			locals->MaxSwathHeightY[k] = locals->Read256BlockHeightY[k];
3829 			locals->MaxSwathHeightC[k] = locals->Read256BlockHeightC[k];
3830 		} else {
3831 			locals->MaxSwathHeightY[k] = locals->Read256BlockWidthY[k];
3832 			locals->MaxSwathHeightC[k] = locals->Read256BlockWidthC[k];
3833 		}
3834 		if ((mode_lib->vba.SourcePixelFormat[k] == dm_444_64
3835 				|| mode_lib->vba.SourcePixelFormat[k] == dm_444_32
3836 				|| mode_lib->vba.SourcePixelFormat[k] == dm_444_16
3837 				|| mode_lib->vba.SourcePixelFormat[k] == dm_mono_16
3838 				|| mode_lib->vba.SourcePixelFormat[k] == dm_mono_8)) {
3839 			if (mode_lib->vba.SurfaceTiling[k] == dm_sw_linear
3840 					|| (mode_lib->vba.SourcePixelFormat[k] == dm_444_64
3841 							&& (mode_lib->vba.SurfaceTiling[k]
3842 									== dm_sw_4kb_s
3843 									|| mode_lib->vba.SurfaceTiling[k]
3844 											== dm_sw_4kb_s_x
3845 									|| mode_lib->vba.SurfaceTiling[k]
3846 											== dm_sw_64kb_s
3847 									|| mode_lib->vba.SurfaceTiling[k]
3848 											== dm_sw_64kb_s_t
3849 									|| mode_lib->vba.SurfaceTiling[k]
3850 											== dm_sw_64kb_s_x
3851 									|| mode_lib->vba.SurfaceTiling[k]
3852 											== dm_sw_var_s
3853 									|| mode_lib->vba.SurfaceTiling[k]
3854 											== dm_sw_var_s_x)
3855 							&& mode_lib->vba.SourceScan[k] == dm_horz)) {
3856 				locals->MinSwathHeightY[k] = locals->MaxSwathHeightY[k];
3857 			} else {
3858 				locals->MinSwathHeightY[k] = locals->MaxSwathHeightY[k]
3859 						/ 2.0;
3860 			}
3861 			locals->MinSwathHeightC[k] = locals->MaxSwathHeightC[k];
3862 		} else {
3863 			if (mode_lib->vba.SurfaceTiling[k] == dm_sw_linear) {
3864 				locals->MinSwathHeightY[k] = locals->MaxSwathHeightY[k];
3865 				locals->MinSwathHeightC[k] = locals->MaxSwathHeightC[k];
3866 			} else if (mode_lib->vba.SourcePixelFormat[k] == dm_420_8
3867 					&& mode_lib->vba.SourceScan[k] == dm_horz) {
3868 				locals->MinSwathHeightY[k] = locals->MaxSwathHeightY[k]
3869 						/ 2.0;
3870 				locals->MinSwathHeightC[k] = locals->MaxSwathHeightC[k];
3871 			} else if (mode_lib->vba.SourcePixelFormat[k] == dm_420_10
3872 					&& mode_lib->vba.SourceScan[k] == dm_horz) {
3873 				locals->MinSwathHeightC[k] = locals->MaxSwathHeightC[k]
3874 						/ 2.0;
3875 				locals->MinSwathHeightY[k] = locals->MaxSwathHeightY[k];
3876 			} else {
3877 				locals->MinSwathHeightY[k] = locals->MaxSwathHeightY[k];
3878 				locals->MinSwathHeightC[k] = locals->MaxSwathHeightC[k];
3879 			}
3880 		}
3881 		if (mode_lib->vba.SurfaceTiling[k] == dm_sw_linear) {
3882 			mode_lib->vba.MaximumSwathWidthSupport = 8192.0;
3883 		} else {
3884 			mode_lib->vba.MaximumSwathWidthSupport = 5120.0;
3885 		}
3886 		mode_lib->vba.MaximumSwathWidthInDETBuffer =
3887 				dml_min(
3888 						mode_lib->vba.MaximumSwathWidthSupport,
3889 						mode_lib->vba.DETBufferSizeInKByte * 1024.0 / 2.0
3890 								/ (locals->BytePerPixelInDETY[k]
3891 										* locals->MinSwathHeightY[k]
3892 										+ locals->BytePerPixelInDETC[k]
3893 												/ 2.0
3894 												* locals->MinSwathHeightC[k]));
3895 		if (locals->BytePerPixelInDETC[k] == 0.0) {
3896 			mode_lib->vba.MaximumSwathWidthInLineBuffer =
3897 					mode_lib->vba.LineBufferSize
3898 							* dml_max(mode_lib->vba.HRatio[k], 1.0)
3899 							/ mode_lib->vba.LBBitPerPixel[k]
3900 							/ (mode_lib->vba.vtaps[k]
3901 									+ dml_max(
3902 											dml_ceil(
3903 													mode_lib->vba.VRatio[k],
3904 													1.0)
3905 													- 2,
3906 											0.0));
3907 		} else {
3908 			mode_lib->vba.MaximumSwathWidthInLineBuffer =
3909 					dml_min(
3910 							mode_lib->vba.LineBufferSize
3911 									* dml_max(
3912 											mode_lib->vba.HRatio[k],
3913 											1.0)
3914 									/ mode_lib->vba.LBBitPerPixel[k]
3915 									/ (mode_lib->vba.vtaps[k]
3916 											+ dml_max(
3917 													dml_ceil(
3918 															mode_lib->vba.VRatio[k],
3919 															1.0)
3920 															- 2,
3921 													0.0)),
3922 							2.0 * mode_lib->vba.LineBufferSize
3923 									* dml_max(
3924 											mode_lib->vba.HRatio[k]
3925 													/ 2.0,
3926 											1.0)
3927 									/ mode_lib->vba.LBBitPerPixel[k]
3928 									/ (mode_lib->vba.VTAPsChroma[k]
3929 											+ dml_max(
3930 													dml_ceil(
3931 															mode_lib->vba.VRatio[k]
3932 																	/ 2.0,
3933 															1.0)
3934 															- 2,
3935 													0.0)));
3936 		}
3937 		locals->MaximumSwathWidth[k] = dml_min(
3938 				mode_lib->vba.MaximumSwathWidthInDETBuffer,
3939 				mode_lib->vba.MaximumSwathWidthInLineBuffer);
3940 	}
3941 	for (i = 0; i <= mode_lib->vba.soc.num_states; i++) {
3942 		double MaxMaxDispclkRoundedDown = RoundToDFSGranularityDown(
3943 			mode_lib->vba.MaxDispclk[mode_lib->vba.soc.num_states],
3944 			mode_lib->vba.DISPCLKDPPCLKVCOSpeed);
3945 
3946 		for (j = 0; j < 2; j++) {
3947 			mode_lib->vba.MaxDispclkRoundedDownToDFSGranularity = RoundToDFSGranularityDown(
3948 				mode_lib->vba.MaxDispclk[i],
3949 				mode_lib->vba.DISPCLKDPPCLKVCOSpeed);
3950 			mode_lib->vba.MaxDppclkRoundedDownToDFSGranularity = RoundToDFSGranularityDown(
3951 				mode_lib->vba.MaxDppclk[i],
3952 				mode_lib->vba.DISPCLKDPPCLKVCOSpeed);
3953 			locals->RequiredDISPCLK[i][j] = 0.0;
3954 			locals->DISPCLK_DPPCLK_Support[i][j] = true;
3955 			for (k = 0; k <= mode_lib->vba.NumberOfActivePlanes - 1; k++) {
3956 				mode_lib->vba.PlaneRequiredDISPCLKWithoutODMCombine =
3957 						mode_lib->vba.PixelClock[k]
3958 								* (1.0
3959 										+ mode_lib->vba.DISPCLKDPPCLKDSCCLKDownSpreading
3960 												/ 100.0)
3961 								* (1.0
3962 										+ mode_lib->vba.DISPCLKRampingMargin
3963 												/ 100.0);
3964 				if (mode_lib->vba.PlaneRequiredDISPCLKWithoutODMCombine >= mode_lib->vba.MaxDispclk[i]
3965 						&& i == mode_lib->vba.soc.num_states)
3966 					mode_lib->vba.PlaneRequiredDISPCLKWithoutODMCombine = mode_lib->vba.PixelClock[k]
3967 							* (1 + mode_lib->vba.DISPCLKDPPCLKDSCCLKDownSpreading / 100.0);
3968 
3969 				mode_lib->vba.PlaneRequiredDISPCLKWithODMCombine = mode_lib->vba.PixelClock[k] / 2
3970 					* (1 + mode_lib->vba.DISPCLKDPPCLKDSCCLKDownSpreading / 100.0) * (1 + mode_lib->vba.DISPCLKRampingMargin / 100.0);
3971 				if (mode_lib->vba.PlaneRequiredDISPCLKWithODMCombine >= mode_lib->vba.MaxDispclk[i]
3972 						&& i == mode_lib->vba.soc.num_states)
3973 					mode_lib->vba.PlaneRequiredDISPCLKWithODMCombine = mode_lib->vba.PixelClock[k] / 2
3974 							* (1 + mode_lib->vba.DISPCLKDPPCLKDSCCLKDownSpreading / 100.0);
3975 				if (mode_lib->vba.ODMCapability == false ||
3976 						(locals->PlaneRequiredDISPCLKWithoutODMCombine <= MaxMaxDispclkRoundedDown
3977 							&& (!locals->DSCEnabled[k] || locals->HActive[k] <= DCN21_MAX_DSC_IMAGE_WIDTH))) {
3978 					locals->ODMCombineEnablePerState[i][k] = false;
3979 					mode_lib->vba.PlaneRequiredDISPCLK = mode_lib->vba.PlaneRequiredDISPCLKWithoutODMCombine;
3980 				} else {
3981 					locals->ODMCombineEnablePerState[i][k] = true;
3982 					mode_lib->vba.PlaneRequiredDISPCLK = mode_lib->vba.PlaneRequiredDISPCLKWithODMCombine;
3983 				}
3984 				if (locals->MinDPPCLKUsingSingleDPP[k] * (1.0 + mode_lib->vba.DISPCLKDPPCLKDSCCLKDownSpreading / 100.0) <= mode_lib->vba.MaxDppclkRoundedDownToDFSGranularity
3985 						&& locals->SwathWidthYSingleDPP[k] <= locals->MaximumSwathWidth[k]
3986 						&& locals->ODMCombineEnablePerState[i][k] == false) {
3987 					locals->NoOfDPP[i][j][k] = 1;
3988 					locals->RequiredDPPCLK[i][j][k] =
3989 						locals->MinDPPCLKUsingSingleDPP[k] * (1.0 + mode_lib->vba.DISPCLKDPPCLKDSCCLKDownSpreading / 100.0);
3990 				} else {
3991 					locals->NoOfDPP[i][j][k] = 2;
3992 					locals->RequiredDPPCLK[i][j][k] =
3993 						locals->MinDPPCLKUsingSingleDPP[k] * (1.0 + mode_lib->vba.DISPCLKDPPCLKDSCCLKDownSpreading / 100.0) / 2.0;
3994 				}
3995 				locals->RequiredDISPCLK[i][j] = dml_max(
3996 						locals->RequiredDISPCLK[i][j],
3997 						mode_lib->vba.PlaneRequiredDISPCLK);
3998 				if ((locals->MinDPPCLKUsingSingleDPP[k] / locals->NoOfDPP[i][j][k] * (1.0 + mode_lib->vba.DISPCLKDPPCLKDSCCLKDownSpreading / 100.0)
3999 						> mode_lib->vba.MaxDppclkRoundedDownToDFSGranularity)
4000 						|| (mode_lib->vba.PlaneRequiredDISPCLK > mode_lib->vba.MaxDispclkRoundedDownToDFSGranularity)) {
4001 					locals->DISPCLK_DPPCLK_Support[i][j] = false;
4002 				}
4003 			}
4004 			locals->TotalNumberOfActiveDPP[i][j] = 0.0;
4005 			for (k = 0; k <= mode_lib->vba.NumberOfActivePlanes - 1; k++)
4006 				locals->TotalNumberOfActiveDPP[i][j] = locals->TotalNumberOfActiveDPP[i][j] + locals->NoOfDPP[i][j][k];
4007 			if (j == 1) {
4008 				while (locals->TotalNumberOfActiveDPP[i][j] < mode_lib->vba.MaxNumDPP
4009 						&& locals->TotalNumberOfActiveDPP[i][j] < 2 * mode_lib->vba.NumberOfActivePlanes) {
4010 					double BWOfNonSplitPlaneOfMaximumBandwidth;
4011 					unsigned int NumberOfNonSplitPlaneOfMaximumBandwidth;
4012 
4013 					BWOfNonSplitPlaneOfMaximumBandwidth = 0;
4014 					NumberOfNonSplitPlaneOfMaximumBandwidth = 0;
4015 					for (k = 0; k < mode_lib->vba.NumberOfActivePlanes; ++k) {
4016 						if (locals->ReadBandwidth[k] > BWOfNonSplitPlaneOfMaximumBandwidth && locals->NoOfDPP[i][j][k] == 1) {
4017 							BWOfNonSplitPlaneOfMaximumBandwidth = locals->ReadBandwidth[k];
4018 							NumberOfNonSplitPlaneOfMaximumBandwidth = k;
4019 						}
4020 					}
4021 					locals->NoOfDPP[i][j][NumberOfNonSplitPlaneOfMaximumBandwidth] = 2;
4022 					locals->RequiredDPPCLK[i][j][NumberOfNonSplitPlaneOfMaximumBandwidth] =
4023 						locals->MinDPPCLKUsingSingleDPP[NumberOfNonSplitPlaneOfMaximumBandwidth]
4024 							* (1 + mode_lib->vba.DISPCLKDPPCLKDSCCLKDownSpreading / 100) / 2;
4025 					locals->TotalNumberOfActiveDPP[i][j] = locals->TotalNumberOfActiveDPP[i][j] + 1;
4026 				}
4027 			}
4028 			if (locals->TotalNumberOfActiveDPP[i][j] > mode_lib->vba.MaxNumDPP) {
4029 				locals->RequiredDISPCLK[i][j] = 0.0;
4030 				locals->DISPCLK_DPPCLK_Support[i][j] = true;
4031 				for (k = 0; k <= mode_lib->vba.NumberOfActivePlanes - 1; k++) {
4032 					locals->ODMCombineEnablePerState[i][k] = false;
4033 					if (locals->SwathWidthYSingleDPP[k] <= locals->MaximumSwathWidth[k]) {
4034 						locals->NoOfDPP[i][j][k] = 1;
4035 						locals->RequiredDPPCLK[i][j][k] = locals->MinDPPCLKUsingSingleDPP[k]
4036 							* (1.0 + mode_lib->vba.DISPCLKDPPCLKDSCCLKDownSpreading / 100.0);
4037 					} else {
4038 						locals->NoOfDPP[i][j][k] = 2;
4039 						locals->RequiredDPPCLK[i][j][k] = locals->MinDPPCLKUsingSingleDPP[k]
4040 										* (1.0 + mode_lib->vba.DISPCLKDPPCLKDSCCLKDownSpreading / 100.0) / 2.0;
4041 					}
4042 					if (i != mode_lib->vba.soc.num_states) {
4043 						mode_lib->vba.PlaneRequiredDISPCLK =
4044 								mode_lib->vba.PixelClock[k]
4045 										* (1.0 + mode_lib->vba.DISPCLKDPPCLKDSCCLKDownSpreading / 100.0)
4046 										* (1.0 + mode_lib->vba.DISPCLKRampingMargin / 100.0);
4047 					} else {
4048 						mode_lib->vba.PlaneRequiredDISPCLK = mode_lib->vba.PixelClock[k]
4049 							* (1.0 + mode_lib->vba.DISPCLKDPPCLKDSCCLKDownSpreading / 100.0);
4050 					}
4051 					locals->RequiredDISPCLK[i][j] = dml_max(
4052 							locals->RequiredDISPCLK[i][j],
4053 							mode_lib->vba.PlaneRequiredDISPCLK);
4054 					if (locals->MinDPPCLKUsingSingleDPP[k] / locals->NoOfDPP[i][j][k] * (1.0 + mode_lib->vba.DISPCLKDPPCLKDSCCLKDownSpreading / 100.0)
4055 							> mode_lib->vba.MaxDppclkRoundedDownToDFSGranularity
4056 							|| mode_lib->vba.PlaneRequiredDISPCLK > mode_lib->vba.MaxDispclkRoundedDownToDFSGranularity)
4057 						locals->DISPCLK_DPPCLK_Support[i][j] = false;
4058 				}
4059 				locals->TotalNumberOfActiveDPP[i][j] = 0.0;
4060 				for (k = 0; k <= mode_lib->vba.NumberOfActivePlanes - 1; k++)
4061 					locals->TotalNumberOfActiveDPP[i][j] = locals->TotalNumberOfActiveDPP[i][j] + locals->NoOfDPP[i][j][k];
4062 			}
4063 			locals->RequiredDISPCLK[i][j] = dml_max(
4064 					locals->RequiredDISPCLK[i][j],
4065 					mode_lib->vba.WritebackRequiredDISPCLK);
4066 			if (mode_lib->vba.MaxDispclkRoundedDownToDFSGranularity
4067 					< mode_lib->vba.WritebackRequiredDISPCLK) {
4068 				locals->DISPCLK_DPPCLK_Support[i][j] = false;
4069 			}
4070 		}
4071 	}
4072 	/*Viewport Size Check*/
4073 
4074 	for (i = 0; i <= mode_lib->vba.soc.num_states; i++) {
4075 		locals->ViewportSizeSupport[i] = true;
4076 		for (k = 0; k <= mode_lib->vba.NumberOfActivePlanes - 1; k++) {
4077 			if (locals->ODMCombineEnablePerState[i][k] == true) {
4078 				if (dml_min(locals->SwathWidthYSingleDPP[k], dml_round(mode_lib->vba.HActive[k] / 2.0 * mode_lib->vba.HRatio[k]))
4079 						> locals->MaximumSwathWidth[k]) {
4080 					locals->ViewportSizeSupport[i] = false;
4081 				}
4082 			} else {
4083 				if (locals->SwathWidthYSingleDPP[k] / 2.0 > locals->MaximumSwathWidth[k]) {
4084 					locals->ViewportSizeSupport[i] = false;
4085 				}
4086 			}
4087 		}
4088 	}
4089 	/*Total Available Pipes Support Check*/
4090 
4091 	for (i = 0; i <= mode_lib->vba.soc.num_states; i++) {
4092 		for (j = 0; j < 2; j++) {
4093 			if (locals->TotalNumberOfActiveDPP[i][j] <= mode_lib->vba.MaxNumDPP)
4094 				locals->TotalAvailablePipesSupport[i][j] = true;
4095 			else
4096 				locals->TotalAvailablePipesSupport[i][j] = false;
4097 		}
4098 	}
4099 	/*Total Available OTG Support Check*/
4100 
4101 	mode_lib->vba.TotalNumberOfActiveOTG = 0.0;
4102 	for (k = 0; k <= mode_lib->vba.NumberOfActivePlanes - 1; k++) {
4103 		if (mode_lib->vba.BlendingAndTiming[k] == k) {
4104 			mode_lib->vba.TotalNumberOfActiveOTG = mode_lib->vba.TotalNumberOfActiveOTG
4105 					+ 1.0;
4106 		}
4107 	}
4108 	if (mode_lib->vba.TotalNumberOfActiveOTG <= mode_lib->vba.MaxNumOTG) {
4109 		mode_lib->vba.NumberOfOTGSupport = true;
4110 	} else {
4111 		mode_lib->vba.NumberOfOTGSupport = false;
4112 	}
4113 	/*Display IO and DSC Support Check*/
4114 
4115 	mode_lib->vba.NonsupportedDSCInputBPC = false;
4116 	for (k = 0; k <= mode_lib->vba.NumberOfActivePlanes - 1; k++) {
4117 		if (!(mode_lib->vba.DSCInputBitPerComponent[k] == 12.0
4118 				|| mode_lib->vba.DSCInputBitPerComponent[k] == 10.0
4119 				|| mode_lib->vba.DSCInputBitPerComponent[k] == 8.0)) {
4120 			mode_lib->vba.NonsupportedDSCInputBPC = true;
4121 		}
4122 	}
4123 	for (i = 0; i <= mode_lib->vba.soc.num_states; i++) {
4124 		for (k = 0; k <= mode_lib->vba.NumberOfActivePlanes - 1; k++) {
4125 			locals->RequiresDSC[i][k] = 0;
4126 			locals->RequiresFEC[i][k] = 0;
4127 			if (mode_lib->vba.BlendingAndTiming[k] == k) {
4128 				if (mode_lib->vba.Output[k] == dm_hdmi) {
4129 					locals->RequiresDSC[i][k] = 0;
4130 					locals->RequiresFEC[i][k] = 0;
4131 					locals->OutputBppPerState[i][k] = TruncToValidBPP(
4132 							dml_min(600.0, mode_lib->vba.PHYCLKPerState[i]) / mode_lib->vba.PixelClockBackEnd[k] * 24,
4133 							mode_lib->vba.ForcedOutputLinkBPP[k],
4134 							false,
4135 							mode_lib->vba.Output[k],
4136 							mode_lib->vba.OutputFormat[k],
4137 							mode_lib->vba.DSCInputBitPerComponent[k]);
4138 				} else if (mode_lib->vba.Output[k] == dm_dp
4139 						|| mode_lib->vba.Output[k] == dm_edp) {
4140 					if (mode_lib->vba.Output[k] == dm_edp) {
4141 						mode_lib->vba.EffectiveFECOverhead = 0.0;
4142 					} else {
4143 						mode_lib->vba.EffectiveFECOverhead =
4144 								mode_lib->vba.FECOverhead;
4145 					}
4146 					if (mode_lib->vba.PHYCLKPerState[i] >= 270.0) {
4147 						mode_lib->vba.Outbpp = TruncToValidBPP(
4148 								(1.0 - mode_lib->vba.Downspreading / 100.0) * 270.0
4149 								* mode_lib->vba.OutputLinkDPLanes[k] / mode_lib->vba.PixelClockBackEnd[k] * 8.0,
4150 								mode_lib->vba.ForcedOutputLinkBPP[k],
4151 								false,
4152 								mode_lib->vba.Output[k],
4153 								mode_lib->vba.OutputFormat[k],
4154 								mode_lib->vba.DSCInputBitPerComponent[k]);
4155 						mode_lib->vba.OutbppDSC = TruncToValidBPP(
4156 								(1.0 - mode_lib->vba.Downspreading / 100.0) * (1.0 - mode_lib->vba.EffectiveFECOverhead / 100.0) * 270.0
4157 								* mode_lib->vba.OutputLinkDPLanes[k] / mode_lib->vba.PixelClockBackEnd[k] * 8.0,
4158 								mode_lib->vba.ForcedOutputLinkBPP[k],
4159 								true,
4160 								mode_lib->vba.Output[k],
4161 								mode_lib->vba.OutputFormat[k],
4162 								mode_lib->vba.DSCInputBitPerComponent[k]);
4163 						if (mode_lib->vba.DSCEnabled[k] == true) {
4164 							locals->RequiresDSC[i][k] = true;
4165 							if (mode_lib->vba.Output[k] == dm_dp) {
4166 								locals->RequiresFEC[i][k] = true;
4167 							} else {
4168 								locals->RequiresFEC[i][k] = false;
4169 							}
4170 							mode_lib->vba.Outbpp = mode_lib->vba.OutbppDSC;
4171 						} else {
4172 							locals->RequiresDSC[i][k] = false;
4173 							locals->RequiresFEC[i][k] = false;
4174 						}
4175 						locals->OutputBppPerState[i][k] = mode_lib->vba.Outbpp;
4176 					}
4177 					if (mode_lib->vba.Outbpp == BPP_INVALID && mode_lib->vba.PHYCLKPerState[i] >= 540.0) {
4178 						mode_lib->vba.Outbpp = TruncToValidBPP(
4179 								(1.0 - mode_lib->vba.Downspreading / 100.0) * 540.0
4180 								* mode_lib->vba.OutputLinkDPLanes[k] / mode_lib->vba.PixelClockBackEnd[k] * 8.0,
4181 								mode_lib->vba.ForcedOutputLinkBPP[k],
4182 									false,
4183 									mode_lib->vba.Output[k],
4184 									mode_lib->vba.OutputFormat[k],
4185 									mode_lib->vba.DSCInputBitPerComponent[k]);
4186 						mode_lib->vba.OutbppDSC = TruncToValidBPP(
4187 								(1.0 - mode_lib->vba.Downspreading / 100.0) * (1.0 - mode_lib->vba.EffectiveFECOverhead / 100.0) * 540.0
4188 								* mode_lib->vba.OutputLinkDPLanes[k] / mode_lib->vba.PixelClockBackEnd[k] * 8.0,
4189 								mode_lib->vba.ForcedOutputLinkBPP[k],
4190 								true,
4191 								mode_lib->vba.Output[k],
4192 								mode_lib->vba.OutputFormat[k],
4193 								mode_lib->vba.DSCInputBitPerComponent[k]);
4194 						if (mode_lib->vba.DSCEnabled[k] == true) {
4195 							locals->RequiresDSC[i][k] = true;
4196 							if (mode_lib->vba.Output[k] == dm_dp) {
4197 								locals->RequiresFEC[i][k] = true;
4198 							} else {
4199 								locals->RequiresFEC[i][k] = false;
4200 							}
4201 							mode_lib->vba.Outbpp = mode_lib->vba.OutbppDSC;
4202 						} else {
4203 							locals->RequiresDSC[i][k] = false;
4204 							locals->RequiresFEC[i][k] = false;
4205 						}
4206 						locals->OutputBppPerState[i][k] = mode_lib->vba.Outbpp;
4207 					}
4208 					if (mode_lib->vba.Outbpp == BPP_INVALID
4209 							&& mode_lib->vba.PHYCLKPerState[i]
4210 									>= 810.0) {
4211 						mode_lib->vba.Outbpp = TruncToValidBPP(
4212 								(1.0 - mode_lib->vba.Downspreading / 100.0) * 810.0
4213 								* mode_lib->vba.OutputLinkDPLanes[k] / mode_lib->vba.PixelClockBackEnd[k] * 8.0,
4214 								mode_lib->vba.ForcedOutputLinkBPP[k],
4215 								false,
4216 								mode_lib->vba.Output[k],
4217 								mode_lib->vba.OutputFormat[k],
4218 								mode_lib->vba.DSCInputBitPerComponent[k]);
4219 						mode_lib->vba.OutbppDSC = TruncToValidBPP(
4220 								(1.0 - mode_lib->vba.Downspreading / 100.0) * (1.0 - mode_lib->vba.EffectiveFECOverhead / 100.0) * 810.0
4221 								* mode_lib->vba.OutputLinkDPLanes[k] / mode_lib->vba.PixelClockBackEnd[k] * 8.0,
4222 								mode_lib->vba.ForcedOutputLinkBPP[k],
4223 								true,
4224 								mode_lib->vba.Output[k],
4225 								mode_lib->vba.OutputFormat[k],
4226 								mode_lib->vba.DSCInputBitPerComponent[k]);
4227 						if (mode_lib->vba.DSCEnabled[k] == true || mode_lib->vba.Outbpp == BPP_INVALID) {
4228 							locals->RequiresDSC[i][k] = true;
4229 							if (mode_lib->vba.Output[k] == dm_dp) {
4230 								locals->RequiresFEC[i][k] = true;
4231 							} else {
4232 								locals->RequiresFEC[i][k] = false;
4233 							}
4234 							mode_lib->vba.Outbpp = mode_lib->vba.OutbppDSC;
4235 						} else {
4236 							locals->RequiresDSC[i][k] = false;
4237 							locals->RequiresFEC[i][k] = false;
4238 						}
4239 						locals->OutputBppPerState[i][k] =
4240 								mode_lib->vba.Outbpp;
4241 					}
4242 				}
4243 			} else {
4244 				locals->OutputBppPerState[i][k] = BPP_BLENDED_PIPE;
4245 			}
4246 		}
4247 	}
4248 	for (i = 0; i <= mode_lib->vba.soc.num_states; i++) {
4249 		locals->DIOSupport[i] = true;
4250 		for (k = 0; k <= mode_lib->vba.NumberOfActivePlanes - 1; k++) {
4251 			if (locals->OutputBppPerState[i][k] == BPP_INVALID
4252 					|| (mode_lib->vba.OutputFormat[k] == dm_420
4253 							&& mode_lib->vba.Interlace[k] == true
4254 							&& mode_lib->vba.ProgressiveToInterlaceUnitInOPP == true)) {
4255 				locals->DIOSupport[i] = false;
4256 			}
4257 		}
4258 	}
4259 	for (i = 0; i <= mode_lib->vba.soc.num_states; i++) {
4260 		for (k = 0; k <= mode_lib->vba.NumberOfActivePlanes - 1; k++) {
4261 			locals->DSCCLKRequiredMoreThanSupported[i] = false;
4262 			if (mode_lib->vba.BlendingAndTiming[k] == k) {
4263 				if ((mode_lib->vba.Output[k] == dm_dp
4264 						|| mode_lib->vba.Output[k] == dm_edp)) {
4265 					if (mode_lib->vba.OutputFormat[k] == dm_420
4266 							|| mode_lib->vba.OutputFormat[k]
4267 									== dm_n422) {
4268 						mode_lib->vba.DSCFormatFactor = 2;
4269 					} else {
4270 						mode_lib->vba.DSCFormatFactor = 1;
4271 					}
4272 					if (locals->RequiresDSC[i][k] == true) {
4273 						if (locals->ODMCombineEnablePerState[i][k]
4274 								== true) {
4275 							if (mode_lib->vba.PixelClockBackEnd[k] / 6.0 / mode_lib->vba.DSCFormatFactor
4276 									> (1.0 - mode_lib->vba.DISPCLKDPPCLKDSCCLKDownSpreading / 100.0) * mode_lib->vba.MaxDSCCLK[i]) {
4277 								locals->DSCCLKRequiredMoreThanSupported[i] =
4278 										true;
4279 							}
4280 						} else {
4281 							if (mode_lib->vba.PixelClockBackEnd[k] / 3.0 / mode_lib->vba.DSCFormatFactor
4282 									> (1.0 - mode_lib->vba.DISPCLKDPPCLKDSCCLKDownSpreading / 100.0) * mode_lib->vba.MaxDSCCLK[i]) {
4283 								locals->DSCCLKRequiredMoreThanSupported[i] =
4284 										true;
4285 							}
4286 						}
4287 					}
4288 				}
4289 			}
4290 		}
4291 	}
4292 	for (i = 0; i <= mode_lib->vba.soc.num_states; i++) {
4293 		locals->NotEnoughDSCUnits[i] = false;
4294 		mode_lib->vba.TotalDSCUnitsRequired = 0.0;
4295 		for (k = 0; k <= mode_lib->vba.NumberOfActivePlanes - 1; k++) {
4296 			if (locals->RequiresDSC[i][k] == true) {
4297 				if (locals->ODMCombineEnablePerState[i][k] == true) {
4298 					mode_lib->vba.TotalDSCUnitsRequired =
4299 							mode_lib->vba.TotalDSCUnitsRequired + 2.0;
4300 				} else {
4301 					mode_lib->vba.TotalDSCUnitsRequired =
4302 							mode_lib->vba.TotalDSCUnitsRequired + 1.0;
4303 				}
4304 			}
4305 		}
4306 		if (mode_lib->vba.TotalDSCUnitsRequired > mode_lib->vba.NumberOfDSC) {
4307 			locals->NotEnoughDSCUnits[i] = true;
4308 		}
4309 	}
4310 	/*DSC Delay per state*/
4311 
4312 	for (i = 0; i <= mode_lib->vba.soc.num_states; i++) {
4313 		for (k = 0; k <= mode_lib->vba.NumberOfActivePlanes - 1; k++) {
4314 			if (mode_lib->vba.BlendingAndTiming[k] != k) {
4315 				mode_lib->vba.slices = 0;
4316 			} else if (locals->RequiresDSC[i][k] == 0
4317 					|| locals->RequiresDSC[i][k] == false) {
4318 				mode_lib->vba.slices = 0;
4319 			} else if (mode_lib->vba.PixelClockBackEnd[k] > 3200.0) {
4320 				mode_lib->vba.slices = dml_ceil(
4321 						mode_lib->vba.PixelClockBackEnd[k] / 400.0,
4322 						4.0);
4323 			} else if (mode_lib->vba.PixelClockBackEnd[k] > 1360.0) {
4324 				mode_lib->vba.slices = 8.0;
4325 			} else if (mode_lib->vba.PixelClockBackEnd[k] > 680.0) {
4326 				mode_lib->vba.slices = 4.0;
4327 			} else if (mode_lib->vba.PixelClockBackEnd[k] > 340.0) {
4328 				mode_lib->vba.slices = 2.0;
4329 			} else {
4330 				mode_lib->vba.slices = 1.0;
4331 			}
4332 			if (locals->OutputBppPerState[i][k] == BPP_BLENDED_PIPE
4333 					|| locals->OutputBppPerState[i][k] == BPP_INVALID) {
4334 				mode_lib->vba.bpp = 0.0;
4335 			} else {
4336 				mode_lib->vba.bpp = locals->OutputBppPerState[i][k];
4337 			}
4338 			if (locals->RequiresDSC[i][k] == true && mode_lib->vba.bpp != 0.0) {
4339 				if (locals->ODMCombineEnablePerState[i][k] == false) {
4340 					locals->DSCDelayPerState[i][k] =
4341 							dscceComputeDelay(
4342 									mode_lib->vba.DSCInputBitPerComponent[k],
4343 									mode_lib->vba.bpp,
4344 									dml_ceil(
4345 											mode_lib->vba.HActive[k]
4346 													/ mode_lib->vba.slices,
4347 											1.0),
4348 									mode_lib->vba.slices,
4349 									mode_lib->vba.OutputFormat[k])
4350 									+ dscComputeDelay(
4351 											mode_lib->vba.OutputFormat[k]);
4352 				} else {
4353 					locals->DSCDelayPerState[i][k] =
4354 							2.0 * (dscceComputeDelay(
4355 											mode_lib->vba.DSCInputBitPerComponent[k],
4356 											mode_lib->vba.bpp,
4357 											dml_ceil(mode_lib->vba.HActive[k] / mode_lib->vba.slices, 1.0),
4358 											mode_lib->vba.slices / 2,
4359 											mode_lib->vba.OutputFormat[k])
4360 									+ dscComputeDelay(mode_lib->vba.OutputFormat[k]));
4361 				}
4362 				locals->DSCDelayPerState[i][k] =
4363 						locals->DSCDelayPerState[i][k] * mode_lib->vba.PixelClock[k] / mode_lib->vba.PixelClockBackEnd[k];
4364 			} else {
4365 				locals->DSCDelayPerState[i][k] = 0.0;
4366 			}
4367 		}
4368 		for (k = 0; k <= mode_lib->vba.NumberOfActivePlanes - 1; k++) {
4369 			for (m = 0; m <= mode_lib->vba.NumberOfActivePlanes - 1; m++) {
4370 				for (j = 0; j <= mode_lib->vba.NumberOfActivePlanes - 1; j++) {
4371 					if (mode_lib->vba.BlendingAndTiming[k] == m && locals->RequiresDSC[i][m] == true)
4372 						locals->DSCDelayPerState[i][k] = locals->DSCDelayPerState[i][m];
4373 				}
4374 			}
4375 		}
4376 	}
4377 
4378 	//Prefetch Check
4379 	for (i = 0; i <= mode_lib->vba.soc.num_states; ++i) {
4380 		for (j = 0; j <= 1; ++j) {
4381 			locals->TotalNumberOfDCCActiveDPP[i][j] = 0;
4382 			for (k = 0; k < mode_lib->vba.NumberOfActivePlanes; ++k) {
4383 				if (mode_lib->vba.DCCEnable[k] == true)
4384 					locals->TotalNumberOfDCCActiveDPP[i][j] = locals->TotalNumberOfDCCActiveDPP[i][j] + locals->NoOfDPP[i][j][k];
4385 			}
4386 		}
4387 	}
4388 
4389 	mode_lib->vba.UrgentLatency = dml_max3(
4390 			mode_lib->vba.UrgentLatencyPixelDataOnly,
4391 			mode_lib->vba.UrgentLatencyPixelMixedWithVMData,
4392 			mode_lib->vba.UrgentLatencyVMDataOnly);
4393 	mode_lib->vba.PrefetchERROR = CalculateMinAndMaxPrefetchMode(
4394 			mode_lib->vba.AllowDRAMSelfRefreshOrDRAMClockChangeInVblank,
4395 			&mode_lib->vba.MinPrefetchMode,
4396 			&mode_lib->vba.MaxPrefetchMode);
4397 
4398 	for (i = 0; i <= mode_lib->vba.soc.num_states; i++) {
4399 		for (j = 0; j < 2; j++) {
4400 			for (k = 0; k <= mode_lib->vba.NumberOfActivePlanes - 1; k++) {
4401 				locals->RequiredDPPCLKThisState[k] = locals->RequiredDPPCLK[i][j][k];
4402 				locals->NoOfDPPThisState[k]        = locals->NoOfDPP[i][j][k];
4403 				if (locals->ODMCombineEnablePerState[i][k] == true) {
4404 					locals->SwathWidthYThisState[k] =
4405 						dml_min(locals->SwathWidthYSingleDPP[k], dml_round(mode_lib->vba.HActive[k] / 2.0 * mode_lib->vba.HRatio[k]));
4406 				} else {
4407 					locals->SwathWidthYThisState[k] = locals->SwathWidthYSingleDPP[k] / locals->NoOfDPP[i][j][k];
4408 				}
4409 				mode_lib->vba.SwathWidthGranularityY = 256.0
4410 					/ dml_ceil(locals->BytePerPixelInDETY[k], 1.0)
4411 					/ locals->MaxSwathHeightY[k];
4412 				mode_lib->vba.RoundedUpMaxSwathSizeBytesY =
4413 					(dml_ceil(locals->SwathWidthYThisState[k] - 1.0, mode_lib->vba.SwathWidthGranularityY)
4414 					+ mode_lib->vba.SwathWidthGranularityY) * locals->BytePerPixelInDETY[k] * locals->MaxSwathHeightY[k];
4415 				if (mode_lib->vba.SourcePixelFormat[k] == dm_420_10) {
4416 					mode_lib->vba.RoundedUpMaxSwathSizeBytesY = dml_ceil(
4417 							mode_lib->vba.RoundedUpMaxSwathSizeBytesY,
4418 							256.0) + 256;
4419 				}
4420 				if (locals->MaxSwathHeightC[k] > 0.0) {
4421 					mode_lib->vba.SwathWidthGranularityC = 256.0 / dml_ceil(locals->BytePerPixelInDETC[k], 2.0) / locals->MaxSwathHeightC[k];
4422 					mode_lib->vba.RoundedUpMaxSwathSizeBytesC = (dml_ceil(locals->SwathWidthYThisState[k] / 2.0 - 1.0, mode_lib->vba.SwathWidthGranularityC)
4423 							+ mode_lib->vba.SwathWidthGranularityC) * locals->BytePerPixelInDETC[k] * locals->MaxSwathHeightC[k];
4424 					if (mode_lib->vba.SourcePixelFormat[k] == dm_420_10) {
4425 						mode_lib->vba.RoundedUpMaxSwathSizeBytesC = dml_ceil(mode_lib->vba.RoundedUpMaxSwathSizeBytesC, 256.0) + 256;
4426 					}
4427 				} else {
4428 					mode_lib->vba.RoundedUpMaxSwathSizeBytesC = 0.0;
4429 				}
4430 				if (mode_lib->vba.RoundedUpMaxSwathSizeBytesY + mode_lib->vba.RoundedUpMaxSwathSizeBytesC
4431 						<= mode_lib->vba.DETBufferSizeInKByte * 1024.0 / 2.0) {
4432 					locals->SwathHeightYThisState[k] = locals->MaxSwathHeightY[k];
4433 					locals->SwathHeightCThisState[k] = locals->MaxSwathHeightC[k];
4434 				} else {
4435 					locals->SwathHeightYThisState[k] =
4436 							locals->MinSwathHeightY[k];
4437 					locals->SwathHeightCThisState[k] =
4438 							locals->MinSwathHeightC[k];
4439 				}
4440 			}
4441 
4442 			CalculateDCFCLKDeepSleep(
4443 					mode_lib,
4444 					mode_lib->vba.NumberOfActivePlanes,
4445 					locals->BytePerPixelInDETY,
4446 					locals->BytePerPixelInDETC,
4447 					mode_lib->vba.VRatio,
4448 					locals->SwathWidthYThisState,
4449 					locals->NoOfDPPThisState,
4450 					mode_lib->vba.HRatio,
4451 					mode_lib->vba.PixelClock,
4452 					locals->PSCL_FACTOR,
4453 					locals->PSCL_FACTOR_CHROMA,
4454 					locals->RequiredDPPCLKThisState,
4455 					&mode_lib->vba.ProjectedDCFCLKDeepSleep);
4456 
4457 			for (k = 0; k <= mode_lib->vba.NumberOfActivePlanes - 1; k++) {
4458 				if ((mode_lib->vba.SourcePixelFormat[k] != dm_444_64
4459 						&& mode_lib->vba.SourcePixelFormat[k] != dm_444_32
4460 						&& mode_lib->vba.SourcePixelFormat[k] != dm_444_16
4461 						&& mode_lib->vba.SourcePixelFormat[k] != dm_mono_16
4462 						&& mode_lib->vba.SourcePixelFormat[k] != dm_mono_8)) {
4463 					mode_lib->vba.PDEAndMetaPTEBytesPerFrameC = CalculateVMAndRowBytes(
4464 							mode_lib,
4465 							mode_lib->vba.DCCEnable[k],
4466 							locals->Read256BlockHeightC[k],
4467 							locals->Read256BlockWidthC[k],
4468 							mode_lib->vba.SourcePixelFormat[k],
4469 							mode_lib->vba.SurfaceTiling[k],
4470 							dml_ceil(locals->BytePerPixelInDETC[k], 2.0),
4471 							mode_lib->vba.SourceScan[k],
4472 							mode_lib->vba.ViewportWidth[k] / 2.0,
4473 							mode_lib->vba.ViewportHeight[k] / 2.0,
4474 							locals->SwathWidthYThisState[k] / 2.0,
4475 							mode_lib->vba.GPUVMEnable,
4476 							mode_lib->vba.HostVMEnable,
4477 							mode_lib->vba.HostVMMaxPageTableLevels,
4478 							mode_lib->vba.HostVMCachedPageTableLevels,
4479 							mode_lib->vba.VMMPageSize,
4480 							mode_lib->vba.PTEBufferSizeInRequestsChroma,
4481 							mode_lib->vba.PitchC[k],
4482 							0.0,
4483 							&locals->MacroTileWidthC[k],
4484 							&mode_lib->vba.MetaRowBytesC,
4485 							&mode_lib->vba.DPTEBytesPerRowC,
4486 							&locals->PTEBufferSizeNotExceededC[i][j][k],
4487 							locals->dpte_row_width_chroma_ub,
4488 							&locals->dpte_row_height_chroma[k],
4489 							&locals->meta_req_width_chroma[k],
4490 							&locals->meta_req_height_chroma[k],
4491 							&locals->meta_row_width_chroma[k],
4492 							&locals->meta_row_height_chroma[k],
4493 							&locals->vm_group_bytes_chroma,
4494 							&locals->dpte_group_bytes_chroma,
4495 							locals->PixelPTEReqWidthC,
4496 							locals->PixelPTEReqHeightC,
4497 							locals->PTERequestSizeC,
4498 							locals->dpde0_bytes_per_frame_ub_c,
4499 							locals->meta_pte_bytes_per_frame_ub_c);
4500 					locals->PrefetchLinesC[k] = CalculatePrefetchSourceLines(
4501 							mode_lib,
4502 							mode_lib->vba.VRatio[k]/2,
4503 							mode_lib->vba.VTAPsChroma[k],
4504 							mode_lib->vba.Interlace[k],
4505 							mode_lib->vba.ProgressiveToInterlaceUnitInOPP,
4506 							locals->SwathHeightCThisState[k],
4507 							mode_lib->vba.ViewportYStartC[k],
4508 							&locals->PrefillC[k],
4509 							&locals->MaxNumSwC[k]);
4510 					locals->PTEBufferSizeInRequestsForLuma = mode_lib->vba.PTEBufferSizeInRequestsLuma;
4511 				} else {
4512 					mode_lib->vba.PDEAndMetaPTEBytesPerFrameC = 0.0;
4513 					mode_lib->vba.MetaRowBytesC = 0.0;
4514 					mode_lib->vba.DPTEBytesPerRowC = 0.0;
4515 					locals->PrefetchLinesC[k] = 0.0;
4516 					locals->PTEBufferSizeNotExceededC[i][j][k] = true;
4517 					locals->PTEBufferSizeInRequestsForLuma = mode_lib->vba.PTEBufferSizeInRequestsLuma + mode_lib->vba.PTEBufferSizeInRequestsChroma;
4518 				}
4519 				mode_lib->vba.PDEAndMetaPTEBytesPerFrameY = CalculateVMAndRowBytes(
4520 						mode_lib,
4521 						mode_lib->vba.DCCEnable[k],
4522 						locals->Read256BlockHeightY[k],
4523 						locals->Read256BlockWidthY[k],
4524 						mode_lib->vba.SourcePixelFormat[k],
4525 						mode_lib->vba.SurfaceTiling[k],
4526 						dml_ceil(locals->BytePerPixelInDETY[k], 1.0),
4527 						mode_lib->vba.SourceScan[k],
4528 						mode_lib->vba.ViewportWidth[k],
4529 						mode_lib->vba.ViewportHeight[k],
4530 						locals->SwathWidthYThisState[k],
4531 						mode_lib->vba.GPUVMEnable,
4532 						mode_lib->vba.HostVMEnable,
4533 						mode_lib->vba.HostVMMaxPageTableLevels,
4534 						mode_lib->vba.HostVMCachedPageTableLevels,
4535 						mode_lib->vba.VMMPageSize,
4536 						locals->PTEBufferSizeInRequestsForLuma,
4537 						mode_lib->vba.PitchY[k],
4538 						mode_lib->vba.DCCMetaPitchY[k],
4539 						&locals->MacroTileWidthY[k],
4540 						&mode_lib->vba.MetaRowBytesY,
4541 						&mode_lib->vba.DPTEBytesPerRowY,
4542 						&locals->PTEBufferSizeNotExceededY[i][j][k],
4543 						locals->dpte_row_width_luma_ub,
4544 						&locals->dpte_row_height[k],
4545 						&locals->meta_req_width[k],
4546 						&locals->meta_req_height[k],
4547 						&locals->meta_row_width[k],
4548 						&locals->meta_row_height[k],
4549 						&locals->vm_group_bytes[k],
4550 						&locals->dpte_group_bytes[k],
4551 						locals->PixelPTEReqWidthY,
4552 						locals->PixelPTEReqHeightY,
4553 						locals->PTERequestSizeY,
4554 						locals->dpde0_bytes_per_frame_ub_l,
4555 						locals->meta_pte_bytes_per_frame_ub_l);
4556 				locals->PrefetchLinesY[k] = CalculatePrefetchSourceLines(
4557 						mode_lib,
4558 						mode_lib->vba.VRatio[k],
4559 						mode_lib->vba.vtaps[k],
4560 						mode_lib->vba.Interlace[k],
4561 						mode_lib->vba.ProgressiveToInterlaceUnitInOPP,
4562 						locals->SwathHeightYThisState[k],
4563 						mode_lib->vba.ViewportYStartY[k],
4564 						&locals->PrefillY[k],
4565 						&locals->MaxNumSwY[k]);
4566 				locals->PDEAndMetaPTEBytesPerFrame[k] =
4567 						mode_lib->vba.PDEAndMetaPTEBytesPerFrameY + mode_lib->vba.PDEAndMetaPTEBytesPerFrameC;
4568 				locals->MetaRowBytes[k] = mode_lib->vba.MetaRowBytesY + mode_lib->vba.MetaRowBytesC;
4569 				locals->DPTEBytesPerRow[k] = mode_lib->vba.DPTEBytesPerRowY + mode_lib->vba.DPTEBytesPerRowC;
4570 
4571 				CalculateActiveRowBandwidth(
4572 						mode_lib->vba.GPUVMEnable,
4573 						mode_lib->vba.SourcePixelFormat[k],
4574 						mode_lib->vba.VRatio[k],
4575 						mode_lib->vba.DCCEnable[k],
4576 						mode_lib->vba.HTotal[k] /
4577 						mode_lib->vba.PixelClock[k],
4578 						mode_lib->vba.MetaRowBytesY,
4579 						mode_lib->vba.MetaRowBytesC,
4580 						locals->meta_row_height[k],
4581 						locals->meta_row_height_chroma[k],
4582 						mode_lib->vba.DPTEBytesPerRowY,
4583 						mode_lib->vba.DPTEBytesPerRowC,
4584 						locals->dpte_row_height[k],
4585 						locals->dpte_row_height_chroma[k],
4586 						&locals->meta_row_bw[k],
4587 						&locals->dpte_row_bw[k]);
4588 			}
4589 			mode_lib->vba.ExtraLatency = CalculateExtraLatency(
4590 					locals->UrgentRoundTripAndOutOfOrderLatencyPerState[i],
4591 					locals->TotalNumberOfActiveDPP[i][j],
4592 					mode_lib->vba.PixelChunkSizeInKByte,
4593 					locals->TotalNumberOfDCCActiveDPP[i][j],
4594 					mode_lib->vba.MetaChunkSize,
4595 					locals->ReturnBWPerState[i],
4596 					mode_lib->vba.GPUVMEnable,
4597 					mode_lib->vba.HostVMEnable,
4598 					mode_lib->vba.NumberOfActivePlanes,
4599 					locals->NoOfDPPThisState,
4600 					locals->dpte_group_bytes,
4601 					mode_lib->vba.PercentOfIdealDRAMFabricAndSDPPortBWReceivedAfterUrgLatencyPixelMixedWithVMData,
4602 					mode_lib->vba.PercentOfIdealDRAMFabricAndSDPPortBWReceivedAfterUrgLatencyVMDataOnly,
4603 					mode_lib->vba.HostVMMaxPageTableLevels,
4604 					mode_lib->vba.HostVMCachedPageTableLevels);
4605 
4606 			mode_lib->vba.TimeCalc = 24.0 / mode_lib->vba.ProjectedDCFCLKDeepSleep;
4607 			for (k = 0; k <= mode_lib->vba.NumberOfActivePlanes - 1; k++) {
4608 				if (mode_lib->vba.BlendingAndTiming[k] == k) {
4609 					if (mode_lib->vba.WritebackEnable[k] == true) {
4610 						locals->WritebackDelay[i][k] = mode_lib->vba.WritebackLatency
4611 								+ CalculateWriteBackDelay(
4612 										mode_lib->vba.WritebackPixelFormat[k],
4613 										mode_lib->vba.WritebackHRatio[k],
4614 										mode_lib->vba.WritebackVRatio[k],
4615 										mode_lib->vba.WritebackLumaHTaps[k],
4616 										mode_lib->vba.WritebackLumaVTaps[k],
4617 										mode_lib->vba.WritebackChromaHTaps[k],
4618 										mode_lib->vba.WritebackChromaVTaps[k],
4619 										mode_lib->vba.WritebackDestinationWidth[k]) / locals->RequiredDISPCLK[i][j];
4620 					} else {
4621 						locals->WritebackDelay[i][k] = 0.0;
4622 					}
4623 					for (m = 0; m <= mode_lib->vba.NumberOfActivePlanes - 1; m++) {
4624 						if (mode_lib->vba.BlendingAndTiming[m] == k
4625 								&& mode_lib->vba.WritebackEnable[m]
4626 										== true) {
4627 							locals->WritebackDelay[i][k] = dml_max(locals->WritebackDelay[i][k],
4628 											mode_lib->vba.WritebackLatency + CalculateWriteBackDelay(
4629 													mode_lib->vba.WritebackPixelFormat[m],
4630 													mode_lib->vba.WritebackHRatio[m],
4631 													mode_lib->vba.WritebackVRatio[m],
4632 													mode_lib->vba.WritebackLumaHTaps[m],
4633 													mode_lib->vba.WritebackLumaVTaps[m],
4634 													mode_lib->vba.WritebackChromaHTaps[m],
4635 													mode_lib->vba.WritebackChromaVTaps[m],
4636 													mode_lib->vba.WritebackDestinationWidth[m]) / locals->RequiredDISPCLK[i][j]);
4637 						}
4638 					}
4639 				}
4640 			}
4641 			for (k = 0; k <= mode_lib->vba.NumberOfActivePlanes - 1; k++) {
4642 				for (m = 0; m <= mode_lib->vba.NumberOfActivePlanes - 1; m++) {
4643 					if (mode_lib->vba.BlendingAndTiming[k] == m) {
4644 						locals->WritebackDelay[i][k] = locals->WritebackDelay[i][m];
4645 					}
4646 				}
4647 			}
4648 			mode_lib->vba.MaxMaxVStartup = 0;
4649 			for (k = 0; k <= mode_lib->vba.NumberOfActivePlanes - 1; k++) {
4650 				locals->MaximumVStartup[k] = mode_lib->vba.VTotal[k] - mode_lib->vba.VActive[k]
4651 					- dml_max(1.0, dml_ceil(locals->WritebackDelay[i][k] / (mode_lib->vba.HTotal[k] / mode_lib->vba.PixelClock[k]), 1.0));
4652 				mode_lib->vba.MaxMaxVStartup = dml_max(mode_lib->vba.MaxMaxVStartup, locals->MaximumVStartup[k]);
4653 			}
4654 
4655 			mode_lib->vba.NextPrefetchMode = mode_lib->vba.MinPrefetchMode;
4656 			mode_lib->vba.NextMaxVStartup = mode_lib->vba.MaxMaxVStartup;
4657 			do {
4658 				mode_lib->vba.PrefetchMode[i][j] = mode_lib->vba.NextPrefetchMode;
4659 				mode_lib->vba.MaxVStartup = mode_lib->vba.NextMaxVStartup;
4660 
4661 				mode_lib->vba.TWait = CalculateTWait(
4662 						mode_lib->vba.PrefetchMode[i][j],
4663 						mode_lib->vba.DRAMClockChangeLatency,
4664 						mode_lib->vba.UrgentLatency,
4665 						mode_lib->vba.SREnterPlusExitTime);
4666 				for (k = 0; k <= mode_lib->vba.NumberOfActivePlanes - 1; k++) {
4667 					Pipe myPipe;
4668 					HostVM myHostVM;
4669 
4670 					if (mode_lib->vba.XFCEnabled[k] == true) {
4671 						mode_lib->vba.XFCRemoteSurfaceFlipDelay =
4672 								CalculateRemoteSurfaceFlipDelay(
4673 										mode_lib,
4674 										mode_lib->vba.VRatio[k],
4675 										locals->SwathWidthYThisState[k],
4676 										dml_ceil(locals->BytePerPixelInDETY[k], 1.0),
4677 										mode_lib->vba.HTotal[k] / mode_lib->vba.PixelClock[k],
4678 										mode_lib->vba.XFCTSlvVupdateOffset,
4679 										mode_lib->vba.XFCTSlvVupdateWidth,
4680 										mode_lib->vba.XFCTSlvVreadyOffset,
4681 										mode_lib->vba.XFCXBUFLatencyTolerance,
4682 										mode_lib->vba.XFCFillBWOverhead,
4683 										mode_lib->vba.XFCSlvChunkSize,
4684 										mode_lib->vba.XFCBusTransportTime,
4685 										mode_lib->vba.TimeCalc,
4686 										mode_lib->vba.TWait,
4687 										&mode_lib->vba.SrcActiveDrainRate,
4688 										&mode_lib->vba.TInitXFill,
4689 										&mode_lib->vba.TslvChk);
4690 					} else {
4691 						mode_lib->vba.XFCRemoteSurfaceFlipDelay = 0.0;
4692 					}
4693 
4694 					myPipe.DPPCLK = locals->RequiredDPPCLK[i][j][k];
4695 					myPipe.DISPCLK = locals->RequiredDISPCLK[i][j];
4696 					myPipe.PixelClock = mode_lib->vba.PixelClock[k];
4697 					myPipe.DCFCLKDeepSleep = mode_lib->vba.ProjectedDCFCLKDeepSleep;
4698 					myPipe.DPPPerPlane = locals->NoOfDPP[i][j][k];
4699 					myPipe.ScalerEnabled = mode_lib->vba.ScalerEnabled[k];
4700 					myPipe.SourceScan = mode_lib->vba.SourceScan[k];
4701 					myPipe.BlockWidth256BytesY = locals->Read256BlockWidthY[k];
4702 					myPipe.BlockHeight256BytesY = locals->Read256BlockHeightY[k];
4703 					myPipe.BlockWidth256BytesC = locals->Read256BlockWidthC[k];
4704 					myPipe.BlockHeight256BytesC = locals->Read256BlockHeightC[k];
4705 					myPipe.InterlaceEnable = mode_lib->vba.Interlace[k];
4706 					myPipe.NumberOfCursors = mode_lib->vba.NumberOfCursors[k];
4707 					myPipe.VBlank = mode_lib->vba.VTotal[k] - mode_lib->vba.VActive[k];
4708 					myPipe.HTotal = mode_lib->vba.HTotal[k];
4709 
4710 
4711 					myHostVM.Enable = mode_lib->vba.HostVMEnable;
4712 					myHostVM.MaxPageTableLevels = mode_lib->vba.HostVMMaxPageTableLevels;
4713 					myHostVM.CachedPageTableLevels = mode_lib->vba.HostVMCachedPageTableLevels;
4714 
4715 
4716 					mode_lib->vba.IsErrorResult[i][j][k] = CalculatePrefetchSchedule(
4717 							mode_lib,
4718 							mode_lib->vba.PercentOfIdealDRAMFabricAndSDPPortBWReceivedAfterUrgLatencyPixelMixedWithVMData,
4719 							mode_lib->vba.PercentOfIdealDRAMFabricAndSDPPortBWReceivedAfterUrgLatencyVMDataOnly,
4720 							&myPipe,
4721 							locals->DSCDelayPerState[i][k],
4722 							mode_lib->vba.DPPCLKDelaySubtotal,
4723 							mode_lib->vba.DPPCLKDelaySCL,
4724 							mode_lib->vba.DPPCLKDelaySCLLBOnly,
4725 							mode_lib->vba.DPPCLKDelayCNVCFormater,
4726 							mode_lib->vba.DPPCLKDelayCNVCCursor,
4727 							mode_lib->vba.DISPCLKDelaySubtotal,
4728 							locals->SwathWidthYThisState[k] / mode_lib->vba.HRatio[k],
4729 							mode_lib->vba.OutputFormat[k],
4730 							mode_lib->vba.MaxInterDCNTileRepeaters,
4731 							dml_min(mode_lib->vba.MaxVStartup, locals->MaximumVStartup[k]),
4732 							locals->MaximumVStartup[k],
4733 							mode_lib->vba.GPUVMMaxPageTableLevels,
4734 							mode_lib->vba.GPUVMEnable,
4735 							&myHostVM,
4736 							mode_lib->vba.DynamicMetadataEnable[k],
4737 							mode_lib->vba.DynamicMetadataLinesBeforeActiveRequired[k],
4738 							mode_lib->vba.DynamicMetadataTransmittedBytes[k],
4739 							mode_lib->vba.DCCEnable[k],
4740 							mode_lib->vba.UrgentLatency,
4741 							mode_lib->vba.ExtraLatency,
4742 							mode_lib->vba.TimeCalc,
4743 							locals->PDEAndMetaPTEBytesPerFrame[k],
4744 							locals->MetaRowBytes[k],
4745 							locals->DPTEBytesPerRow[k],
4746 							locals->PrefetchLinesY[k],
4747 							locals->SwathWidthYThisState[k],
4748 							locals->BytePerPixelInDETY[k],
4749 							locals->PrefillY[k],
4750 							locals->MaxNumSwY[k],
4751 							locals->PrefetchLinesC[k],
4752 							locals->BytePerPixelInDETC[k],
4753 							locals->PrefillC[k],
4754 							locals->MaxNumSwC[k],
4755 							locals->SwathHeightYThisState[k],
4756 							locals->SwathHeightCThisState[k],
4757 							mode_lib->vba.TWait,
4758 							mode_lib->vba.XFCEnabled[k],
4759 							mode_lib->vba.XFCRemoteSurfaceFlipDelay,
4760 							mode_lib->vba.ProgressiveToInterlaceUnitInOPP,
4761 							&locals->dst_x_after_scaler,
4762 							&locals->dst_y_after_scaler,
4763 							&locals->LineTimesForPrefetch[k],
4764 							&locals->PrefetchBW[k],
4765 							&locals->LinesForMetaPTE[k],
4766 							&locals->LinesForMetaAndDPTERow[k],
4767 							&locals->VRatioPreY[i][j][k],
4768 							&locals->VRatioPreC[i][j][k],
4769 							&locals->RequiredPrefetchPixelDataBWLuma[i][j][k],
4770 							&locals->RequiredPrefetchPixelDataBWChroma[i][j][k],
4771 							&locals->VStartupRequiredWhenNotEnoughTimeForDynamicMetadata,
4772 							&locals->Tno_bw[k],
4773 							&locals->prefetch_vmrow_bw[k],
4774 							locals->swath_width_luma_ub,
4775 							locals->swath_width_chroma_ub,
4776 							&mode_lib->vba.VUpdateOffsetPix[k],
4777 							&mode_lib->vba.VUpdateWidthPix[k],
4778 							&mode_lib->vba.VReadyOffsetPix[k]);
4779 				}
4780 				mode_lib->vba.MaximumReadBandwidthWithoutPrefetch = 0.0;
4781 				mode_lib->vba.MaximumReadBandwidthWithPrefetch = 0.0;
4782 				for (k = 0; k <= mode_lib->vba.NumberOfActivePlanes - 1; k++) {
4783 					unsigned int m;
4784 
4785 					locals->cursor_bw[k] = 0;
4786 					locals->cursor_bw_pre[k] = 0;
4787 					for (m = 0; m < mode_lib->vba.NumberOfCursors[k]; m++) {
4788 						locals->cursor_bw[k] = mode_lib->vba.CursorWidth[k][m] * mode_lib->vba.CursorBPP[k][m]
4789 							/ 8.0 / (mode_lib->vba.HTotal[k] / mode_lib->vba.PixelClock[k]) * mode_lib->vba.VRatio[k];
4790 						locals->cursor_bw_pre[k] = mode_lib->vba.CursorWidth[k][m] * mode_lib->vba.CursorBPP[k][m]
4791 							/ 8.0 / (mode_lib->vba.HTotal[k] / mode_lib->vba.PixelClock[k]) * locals->VRatioPreY[i][j][k];
4792 					}
4793 
4794 					CalculateUrgentBurstFactor(
4795 							mode_lib->vba.DETBufferSizeInKByte,
4796 							locals->SwathHeightYThisState[k],
4797 							locals->SwathHeightCThisState[k],
4798 							locals->SwathWidthYThisState[k],
4799 							mode_lib->vba.HTotal[k] / mode_lib->vba.PixelClock[k],
4800 							mode_lib->vba.UrgentLatency,
4801 							mode_lib->vba.CursorBufferSize,
4802 							mode_lib->vba.CursorWidth[k][0] + mode_lib->vba.CursorWidth[k][1],
4803 							dml_max(mode_lib->vba.CursorBPP[k][0], mode_lib->vba.CursorBPP[k][1]),
4804 							mode_lib->vba.VRatio[k],
4805 							locals->VRatioPreY[i][j][k],
4806 							locals->VRatioPreC[i][j][k],
4807 							locals->BytePerPixelInDETY[k],
4808 							locals->BytePerPixelInDETC[k],
4809 							&locals->UrgentBurstFactorCursor[k],
4810 							&locals->UrgentBurstFactorCursorPre[k],
4811 							&locals->UrgentBurstFactorLuma[k],
4812 							&locals->UrgentBurstFactorLumaPre[k],
4813 							&locals->UrgentBurstFactorChroma[k],
4814 							&locals->UrgentBurstFactorChromaPre[k],
4815 							&locals->NotEnoughUrgentLatencyHiding,
4816 							&locals->NotEnoughUrgentLatencyHidingPre);
4817 
4818 					if (mode_lib->vba.UseUrgentBurstBandwidth == false) {
4819 						locals->UrgentBurstFactorCursor[k] = 1;
4820 						locals->UrgentBurstFactorCursorPre[k] = 1;
4821 						locals->UrgentBurstFactorLuma[k] = 1;
4822 						locals->UrgentBurstFactorLumaPre[k] = 1;
4823 						locals->UrgentBurstFactorChroma[k] = 1;
4824 						locals->UrgentBurstFactorChromaPre[k] = 1;
4825 					}
4826 
4827 					mode_lib->vba.MaximumReadBandwidthWithoutPrefetch = mode_lib->vba.MaximumReadBandwidthWithoutPrefetch
4828 						+ locals->cursor_bw[k] * locals->UrgentBurstFactorCursor[k] + locals->ReadBandwidthLuma[k]
4829 						* locals->UrgentBurstFactorLuma[k] + locals->ReadBandwidthChroma[k]
4830 						* locals->UrgentBurstFactorChroma[k] + locals->meta_row_bw[k] + locals->dpte_row_bw[k];
4831 					mode_lib->vba.MaximumReadBandwidthWithPrefetch = mode_lib->vba.MaximumReadBandwidthWithPrefetch
4832 						+ dml_max3(locals->prefetch_vmrow_bw[k],
4833 						locals->ReadBandwidthLuma[k] * locals->UrgentBurstFactorLuma[k] + locals->ReadBandwidthChroma[k]
4834 						* locals->UrgentBurstFactorChroma[k] + locals->cursor_bw[k] * locals->UrgentBurstFactorCursor[k]
4835 						+ locals->meta_row_bw[k] + locals->dpte_row_bw[k],
4836 						locals->RequiredPrefetchPixelDataBWLuma[i][j][k] * locals->UrgentBurstFactorLumaPre[k]
4837 						+ locals->RequiredPrefetchPixelDataBWChroma[i][j][k] * locals->UrgentBurstFactorChromaPre[k]
4838 						+ locals->cursor_bw_pre[k] * locals->UrgentBurstFactorCursorPre[k]);
4839 				}
4840 				locals->BandwidthWithoutPrefetchSupported[i] = true;
4841 				if (mode_lib->vba.MaximumReadBandwidthWithoutPrefetch > locals->ReturnBWPerState[i]
4842 						|| locals->NotEnoughUrgentLatencyHiding == 1) {
4843 					locals->BandwidthWithoutPrefetchSupported[i] = false;
4844 				}
4845 
4846 				locals->PrefetchSupported[i][j] = true;
4847 				if (mode_lib->vba.MaximumReadBandwidthWithPrefetch > locals->ReturnBWPerState[i]
4848 						|| locals->NotEnoughUrgentLatencyHiding == 1
4849 						|| locals->NotEnoughUrgentLatencyHidingPre == 1) {
4850 					locals->PrefetchSupported[i][j] = false;
4851 				}
4852 				for (k = 0; k <= mode_lib->vba.NumberOfActivePlanes - 1; k++) {
4853 					if (locals->LineTimesForPrefetch[k] < 2.0
4854 							|| locals->LinesForMetaPTE[k] >= 32.0
4855 							|| locals->LinesForMetaAndDPTERow[k] >= 16.0
4856 							|| mode_lib->vba.IsErrorResult[i][j][k] == true) {
4857 						locals->PrefetchSupported[i][j] = false;
4858 					}
4859 				}
4860 				locals->VRatioInPrefetchSupported[i][j] = true;
4861 				for (k = 0; k <= mode_lib->vba.NumberOfActivePlanes - 1; k++) {
4862 					if (locals->VRatioPreY[i][j][k] > 4.0
4863 							|| locals->VRatioPreC[i][j][k] > 4.0
4864 							|| mode_lib->vba.IsErrorResult[i][j][k] == true) {
4865 						locals->VRatioInPrefetchSupported[i][j] = false;
4866 					}
4867 				}
4868 				mode_lib->vba.AnyLinesForVMOrRowTooLarge = false;
4869 				for (k = 0; k < mode_lib->vba.NumberOfActivePlanes; ++k) {
4870 					if (locals->LinesForMetaAndDPTERow[k] >= 16 || locals->LinesForMetaPTE[k] >= 32) {
4871 						mode_lib->vba.AnyLinesForVMOrRowTooLarge = true;
4872 					}
4873 				}
4874 
4875 				if (mode_lib->vba.MaxVStartup <= 13 || mode_lib->vba.AnyLinesForVMOrRowTooLarge == false) {
4876 					mode_lib->vba.NextMaxVStartup = mode_lib->vba.MaxMaxVStartup;
4877 					mode_lib->vba.NextPrefetchMode = mode_lib->vba.NextPrefetchMode + 1;
4878 				} else {
4879 					mode_lib->vba.NextMaxVStartup = mode_lib->vba.NextMaxVStartup - 1;
4880 				}
4881 			} while ((locals->PrefetchSupported[i][j] != true || locals->VRatioInPrefetchSupported[i][j] != true)
4882 					&& (mode_lib->vba.NextMaxVStartup != mode_lib->vba.MaxMaxVStartup
4883 						|| mode_lib->vba.NextPrefetchMode < mode_lib->vba.MaxPrefetchMode));
4884 
4885 			if (locals->PrefetchSupported[i][j] == true && locals->VRatioInPrefetchSupported[i][j] == true) {
4886 				mode_lib->vba.BandwidthAvailableForImmediateFlip = locals->ReturnBWPerState[i];
4887 				for (k = 0; k <= mode_lib->vba.NumberOfActivePlanes - 1; k++) {
4888 					mode_lib->vba.BandwidthAvailableForImmediateFlip = mode_lib->vba.BandwidthAvailableForImmediateFlip
4889 						- dml_max(locals->ReadBandwidthLuma[k] * locals->UrgentBurstFactorLuma[k]
4890 							+ locals->ReadBandwidthChroma[k] * locals->UrgentBurstFactorChroma[k]
4891 							+ locals->cursor_bw[k] * locals->UrgentBurstFactorCursor[k],
4892 							locals->RequiredPrefetchPixelDataBWLuma[i][j][k] * locals->UrgentBurstFactorLumaPre[k]
4893 							+ locals->RequiredPrefetchPixelDataBWChroma[i][j][k] * locals->UrgentBurstFactorChromaPre[k]
4894 							+ locals->cursor_bw_pre[k] * locals->UrgentBurstFactorCursorPre[k]);
4895 				}
4896 				mode_lib->vba.TotImmediateFlipBytes = 0.0;
4897 				for (k = 0; k <= mode_lib->vba.NumberOfActivePlanes - 1; k++) {
4898 					mode_lib->vba.TotImmediateFlipBytes = mode_lib->vba.TotImmediateFlipBytes
4899 						+ locals->PDEAndMetaPTEBytesPerFrame[k] + locals->MetaRowBytes[k] + locals->DPTEBytesPerRow[k];
4900 				}
4901 
4902 				for (k = 0; k <= mode_lib->vba.NumberOfActivePlanes - 1; k++) {
4903 					CalculateFlipSchedule(
4904 							mode_lib,
4905 							mode_lib->vba.PercentOfIdealDRAMFabricAndSDPPortBWReceivedAfterUrgLatencyPixelMixedWithVMData,
4906 							mode_lib->vba.PercentOfIdealDRAMFabricAndSDPPortBWReceivedAfterUrgLatencyVMDataOnly,
4907 							mode_lib->vba.ExtraLatency,
4908 							mode_lib->vba.UrgentLatency,
4909 							mode_lib->vba.GPUVMMaxPageTableLevels,
4910 							mode_lib->vba.HostVMEnable,
4911 							mode_lib->vba.HostVMMaxPageTableLevels,
4912 							mode_lib->vba.HostVMCachedPageTableLevels,
4913 							mode_lib->vba.GPUVMEnable,
4914 							locals->PDEAndMetaPTEBytesPerFrame[k],
4915 							locals->MetaRowBytes[k],
4916 							locals->DPTEBytesPerRow[k],
4917 							mode_lib->vba.BandwidthAvailableForImmediateFlip,
4918 							mode_lib->vba.TotImmediateFlipBytes,
4919 							mode_lib->vba.SourcePixelFormat[k],
4920 							mode_lib->vba.HTotal[k] / mode_lib->vba.PixelClock[k],
4921 							mode_lib->vba.VRatio[k],
4922 							locals->Tno_bw[k],
4923 							mode_lib->vba.DCCEnable[k],
4924 							locals->dpte_row_height[k],
4925 							locals->meta_row_height[k],
4926 							locals->dpte_row_height_chroma[k],
4927 							locals->meta_row_height_chroma[k],
4928 							&locals->DestinationLinesToRequestVMInImmediateFlip[k],
4929 							&locals->DestinationLinesToRequestRowInImmediateFlip[k],
4930 							&locals->final_flip_bw[k],
4931 							&locals->ImmediateFlipSupportedForPipe[k]);
4932 				}
4933 				mode_lib->vba.total_dcn_read_bw_with_flip = 0.0;
4934 				for (k = 0; k <= mode_lib->vba.NumberOfActivePlanes - 1; k++) {
4935 					mode_lib->vba.total_dcn_read_bw_with_flip = mode_lib->vba.total_dcn_read_bw_with_flip + dml_max3(
4936 						locals->prefetch_vmrow_bw[k],
4937 						locals->final_flip_bw[k] +  locals->ReadBandwidthLuma[k] * locals->UrgentBurstFactorLuma[k]
4938 						+ locals->ReadBandwidthChroma[k] * locals->UrgentBurstFactorChroma[k]
4939 						+ locals->cursor_bw[k] * locals->UrgentBurstFactorCursor[k],
4940 						locals->final_flip_bw[k] + locals->RequiredPrefetchPixelDataBWLuma[i][j][k]
4941 						* locals->UrgentBurstFactorLumaPre[k] + locals->RequiredPrefetchPixelDataBWChroma[i][j][k]
4942 						* locals->UrgentBurstFactorChromaPre[k] + locals->cursor_bw_pre[k]
4943 						* locals->UrgentBurstFactorCursorPre[k]);
4944 				}
4945 				locals->ImmediateFlipSupportedForState[i][j] = true;
4946 				if (mode_lib->vba.total_dcn_read_bw_with_flip
4947 						> locals->ReturnBWPerState[i]) {
4948 					locals->ImmediateFlipSupportedForState[i][j] = false;
4949 				}
4950 				for (k = 0; k <= mode_lib->vba.NumberOfActivePlanes - 1; k++) {
4951 					if (locals->ImmediateFlipSupportedForPipe[k] == false) {
4952 						locals->ImmediateFlipSupportedForState[i][j] = false;
4953 					}
4954 				}
4955 			} else {
4956 				locals->ImmediateFlipSupportedForState[i][j] = false;
4957 			}
4958 			mode_lib->vba.UrgentOutOfOrderReturnPerChannel = dml_max3(
4959 					mode_lib->vba.UrgentOutOfOrderReturnPerChannelPixelDataOnly,
4960 					mode_lib->vba.UrgentOutOfOrderReturnPerChannelPixelMixedWithVMData,
4961 					mode_lib->vba.UrgentOutOfOrderReturnPerChannelVMDataOnly);
4962 			CalculateWatermarksAndDRAMSpeedChangeSupport(
4963 					mode_lib,
4964 					mode_lib->vba.PrefetchMode[i][j],
4965 					mode_lib->vba.NumberOfActivePlanes,
4966 					mode_lib->vba.MaxLineBufferLines,
4967 					mode_lib->vba.LineBufferSize,
4968 					mode_lib->vba.DPPOutputBufferPixels,
4969 					mode_lib->vba.DETBufferSizeInKByte,
4970 					mode_lib->vba.WritebackInterfaceLumaBufferSize,
4971 					mode_lib->vba.WritebackInterfaceChromaBufferSize,
4972 					mode_lib->vba.DCFCLKPerState[i],
4973 					mode_lib->vba.UrgentOutOfOrderReturnPerChannel * mode_lib->vba.NumberOfChannels,
4974 					locals->ReturnBWPerState[i],
4975 					mode_lib->vba.GPUVMEnable,
4976 					locals->dpte_group_bytes,
4977 					mode_lib->vba.MetaChunkSize,
4978 					mode_lib->vba.UrgentLatency,
4979 					mode_lib->vba.ExtraLatency,
4980 					mode_lib->vba.WritebackLatency,
4981 					mode_lib->vba.WritebackChunkSize,
4982 					mode_lib->vba.SOCCLKPerState[i],
4983 					mode_lib->vba.DRAMClockChangeLatency,
4984 					mode_lib->vba.SRExitTime,
4985 					mode_lib->vba.SREnterPlusExitTime,
4986 					mode_lib->vba.ProjectedDCFCLKDeepSleep,
4987 					locals->NoOfDPPThisState,
4988 					mode_lib->vba.DCCEnable,
4989 					locals->RequiredDPPCLKThisState,
4990 					locals->SwathWidthYSingleDPP,
4991 					locals->SwathHeightYThisState,
4992 					locals->ReadBandwidthLuma,
4993 					locals->SwathHeightCThisState,
4994 					locals->ReadBandwidthChroma,
4995 					mode_lib->vba.LBBitPerPixel,
4996 					locals->SwathWidthYThisState,
4997 					mode_lib->vba.HRatio,
4998 					mode_lib->vba.vtaps,
4999 					mode_lib->vba.VTAPsChroma,
5000 					mode_lib->vba.VRatio,
5001 					mode_lib->vba.HTotal,
5002 					mode_lib->vba.PixelClock,
5003 					mode_lib->vba.BlendingAndTiming,
5004 					locals->BytePerPixelInDETY,
5005 					locals->BytePerPixelInDETC,
5006 					mode_lib->vba.WritebackEnable,
5007 					mode_lib->vba.WritebackPixelFormat,
5008 					mode_lib->vba.WritebackDestinationWidth,
5009 					mode_lib->vba.WritebackDestinationHeight,
5010 					mode_lib->vba.WritebackSourceHeight,
5011 					&locals->DRAMClockChangeSupport[i][j],
5012 					&mode_lib->vba.UrgentWatermark,
5013 					&mode_lib->vba.WritebackUrgentWatermark,
5014 					&mode_lib->vba.DRAMClockChangeWatermark,
5015 					&mode_lib->vba.WritebackDRAMClockChangeWatermark,
5016 					&mode_lib->vba.StutterExitWatermark,
5017 					&mode_lib->vba.StutterEnterPlusExitWatermark,
5018 					&mode_lib->vba.MinActiveDRAMClockChangeLatencySupported);
5019 			}
5020 		}
5021 
5022 		/*Vertical Active BW support*/
5023 		{
5024 			double MaxTotalVActiveRDBandwidth = 0.0;
5025 			for (k = 0; k < mode_lib->vba.NumberOfActivePlanes; ++k) {
5026 				MaxTotalVActiveRDBandwidth = MaxTotalVActiveRDBandwidth + locals->ReadBandwidth[k];
5027 		}
5028 		for (i = 0; i <= mode_lib->vba.soc.num_states; ++i) {
5029 			locals->MaxTotalVerticalActiveAvailableBandwidth[i] = dml_min(
5030 				locals->IdealSDPPortBandwidthPerState[i] *
5031 				mode_lib->vba.MaxAveragePercentOfIdealSDPPortBWDisplayCanUseInNormalSystemOperation
5032 				/ 100.0, mode_lib->vba.DRAMSpeedPerState[i] *
5033 				mode_lib->vba.NumberOfChannels *
5034 				mode_lib->vba.DRAMChannelWidth *
5035 				mode_lib->vba.MaxAveragePercentOfIdealDRAMBWDisplayCanUseInNormalSystemOperation
5036 				/ 100.0);
5037 
5038 			if (MaxTotalVActiveRDBandwidth <= locals->MaxTotalVerticalActiveAvailableBandwidth[i]) {
5039 				locals->TotalVerticalActiveBandwidthSupport[i] = true;
5040 			} else {
5041 				locals->TotalVerticalActiveBandwidthSupport[i] = false;
5042 			}
5043 		}
5044 	}
5045 
5046 	/*PTE Buffer Size Check*/
5047 
5048 	for (i = 0; i <= mode_lib->vba.soc.num_states; i++) {
5049 		for (j = 0; j < 2; j++) {
5050 			locals->PTEBufferSizeNotExceeded[i][j] = true;
5051 			for (k = 0; k <= mode_lib->vba.NumberOfActivePlanes - 1; k++) {
5052 				if (locals->PTEBufferSizeNotExceededY[i][j][k] == false
5053 						|| locals->PTEBufferSizeNotExceededC[i][j][k] == false) {
5054 					locals->PTEBufferSizeNotExceeded[i][j] = false;
5055 				}
5056 			}
5057 		}
5058 	}
5059 	/*Cursor Support Check*/
5060 
5061 	mode_lib->vba.CursorSupport = true;
5062 	for (k = 0; k <= mode_lib->vba.NumberOfActivePlanes - 1; k++) {
5063 		if (mode_lib->vba.CursorWidth[k][0] > 0.0) {
5064 			for (m = 0; m < mode_lib->vba.NumberOfCursors[k]; m++) {
5065 				if (mode_lib->vba.CursorBPP[k][m] == 64 && mode_lib->vba.Cursor64BppSupport == false) {
5066 					mode_lib->vba.CursorSupport = false;
5067 				}
5068 			}
5069 		}
5070 	}
5071 	/*Valid Pitch Check*/
5072 
5073 	mode_lib->vba.PitchSupport = true;
5074 	for (k = 0; k <= mode_lib->vba.NumberOfActivePlanes - 1; k++) {
5075 		locals->AlignedYPitch[k] = dml_ceil(
5076 				dml_max(mode_lib->vba.PitchY[k], mode_lib->vba.ViewportWidth[k]),
5077 				locals->MacroTileWidthY[k]);
5078 		if (locals->AlignedYPitch[k] > mode_lib->vba.PitchY[k]) {
5079 			mode_lib->vba.PitchSupport = false;
5080 		}
5081 		if (mode_lib->vba.DCCEnable[k] == true) {
5082 			locals->AlignedDCCMetaPitch[k] = dml_ceil(
5083 					dml_max(
5084 							mode_lib->vba.DCCMetaPitchY[k],
5085 							mode_lib->vba.ViewportWidth[k]),
5086 					64.0 * locals->Read256BlockWidthY[k]);
5087 		} else {
5088 			locals->AlignedDCCMetaPitch[k] = mode_lib->vba.DCCMetaPitchY[k];
5089 		}
5090 		if (locals->AlignedDCCMetaPitch[k] > mode_lib->vba.DCCMetaPitchY[k]) {
5091 			mode_lib->vba.PitchSupport = false;
5092 		}
5093 		if (mode_lib->vba.SourcePixelFormat[k] != dm_444_64
5094 				&& mode_lib->vba.SourcePixelFormat[k] != dm_444_32
5095 				&& mode_lib->vba.SourcePixelFormat[k] != dm_444_16
5096 				&& mode_lib->vba.SourcePixelFormat[k] != dm_mono_16
5097 				&& mode_lib->vba.SourcePixelFormat[k] != dm_mono_8) {
5098 			locals->AlignedCPitch[k] = dml_ceil(
5099 					dml_max(
5100 							mode_lib->vba.PitchC[k],
5101 							mode_lib->vba.ViewportWidth[k] / 2.0),
5102 					locals->MacroTileWidthC[k]);
5103 		} else {
5104 			locals->AlignedCPitch[k] = mode_lib->vba.PitchC[k];
5105 		}
5106 		if (locals->AlignedCPitch[k] > mode_lib->vba.PitchC[k]) {
5107 			mode_lib->vba.PitchSupport = false;
5108 		}
5109 	}
5110 	/*Mode Support, Voltage State and SOC Configuration*/
5111 
5112 	for (i = mode_lib->vba.soc.num_states; i >= 0; i--) {
5113 		for (j = 0; j < 2; j++) {
5114 			enum dm_validation_status status = DML_VALIDATION_OK;
5115 
5116 			if (mode_lib->vba.ScaleRatioAndTapsSupport != true) {
5117 				status = DML_FAIL_SCALE_RATIO_TAP;
5118 			} else if (mode_lib->vba.SourceFormatPixelAndScanSupport != true) {
5119 				status = DML_FAIL_SOURCE_PIXEL_FORMAT;
5120 			} else if (locals->ViewportSizeSupport[i] != true) {
5121 				status = DML_FAIL_VIEWPORT_SIZE;
5122 			} else if (locals->DIOSupport[i] != true) {
5123 				status = DML_FAIL_DIO_SUPPORT;
5124 			} else if (locals->NotEnoughDSCUnits[i] != false) {
5125 				status = DML_FAIL_NOT_ENOUGH_DSC;
5126 			} else if (locals->DSCCLKRequiredMoreThanSupported[i] != false) {
5127 				status = DML_FAIL_DSC_CLK_REQUIRED;
5128 			} else if (locals->ROBSupport[i] != true) {
5129 				status = DML_FAIL_REORDERING_BUFFER;
5130 			} else if (locals->DISPCLK_DPPCLK_Support[i][j] != true) {
5131 				status = DML_FAIL_DISPCLK_DPPCLK;
5132 			} else if (locals->TotalAvailablePipesSupport[i][j] != true) {
5133 				status = DML_FAIL_TOTAL_AVAILABLE_PIPES;
5134 			} else if (mode_lib->vba.NumberOfOTGSupport != true) {
5135 				status = DML_FAIL_NUM_OTG;
5136 			} else if (mode_lib->vba.WritebackModeSupport != true) {
5137 				status = DML_FAIL_WRITEBACK_MODE;
5138 			} else if (mode_lib->vba.WritebackLatencySupport != true) {
5139 				status = DML_FAIL_WRITEBACK_LATENCY;
5140 			} else if (mode_lib->vba.WritebackScaleRatioAndTapsSupport != true) {
5141 				status = DML_FAIL_WRITEBACK_SCALE_RATIO_TAP;
5142 			} else if (mode_lib->vba.CursorSupport != true) {
5143 				status = DML_FAIL_CURSOR_SUPPORT;
5144 			} else if (mode_lib->vba.PitchSupport != true) {
5145 				status = DML_FAIL_PITCH_SUPPORT;
5146 			} else if (locals->TotalVerticalActiveBandwidthSupport[i] != true) {
5147 				status = DML_FAIL_TOTAL_V_ACTIVE_BW;
5148 			} else if (locals->PTEBufferSizeNotExceeded[i][j] != true) {
5149 				status = DML_FAIL_PTE_BUFFER_SIZE;
5150 			} else if (mode_lib->vba.NonsupportedDSCInputBPC != false) {
5151 				status = DML_FAIL_DSC_INPUT_BPC;
5152 			} else if ((mode_lib->vba.HostVMEnable != false
5153 					&& locals->ImmediateFlipSupportedForState[i][j] != true)) {
5154 				status = DML_FAIL_HOST_VM_IMMEDIATE_FLIP;
5155 			} else if (locals->PrefetchSupported[i][j] != true) {
5156 				status = DML_FAIL_PREFETCH_SUPPORT;
5157 			} else if (locals->VRatioInPrefetchSupported[i][j] != true) {
5158 				status = DML_FAIL_V_RATIO_PREFETCH;
5159 			}
5160 
5161 			if (status == DML_VALIDATION_OK) {
5162 				locals->ModeSupport[i][j] = true;
5163 			} else {
5164 				locals->ModeSupport[i][j] = false;
5165 			}
5166 			locals->ValidationStatus[i] = status;
5167 		}
5168 	}
5169 	{
5170 		unsigned int MaximumMPCCombine = 0;
5171 		mode_lib->vba.VoltageLevel = mode_lib->vba.soc.num_states + 1;
5172 		for (i = mode_lib->vba.VoltageOverrideLevel; i <= mode_lib->vba.soc.num_states; i++) {
5173 			if (locals->ModeSupport[i][0] == true || locals->ModeSupport[i][1] == true) {
5174 				mode_lib->vba.VoltageLevel = i;
5175 				if (locals->ModeSupport[i][1] == true && (locals->ModeSupport[i][0] == false
5176 						|| mode_lib->vba.WhenToDoMPCCombine == dm_mpc_always_when_possible
5177 						|| (mode_lib->vba.WhenToDoMPCCombine == dm_mpc_reduce_voltage_and_clocks
5178 							&& ((locals->DRAMClockChangeSupport[i][1] == dm_dram_clock_change_vactive
5179 								&& locals->DRAMClockChangeSupport[i][0] != dm_dram_clock_change_vactive)
5180 							|| (locals->DRAMClockChangeSupport[i][1] == dm_dram_clock_change_vblank
5181 								&& locals->DRAMClockChangeSupport[i][0] == dm_dram_clock_change_unsupported))))) {
5182 					MaximumMPCCombine = 1;
5183 				} else {
5184 					MaximumMPCCombine = 0;
5185 				}
5186 				break;
5187 			}
5188 		}
5189 		mode_lib->vba.ImmediateFlipSupport =
5190 			locals->ImmediateFlipSupportedForState[mode_lib->vba.VoltageLevel][MaximumMPCCombine];
5191 		for (k = 0; k <= mode_lib->vba.NumberOfActivePlanes - 1; k++) {
5192 			mode_lib->vba.DPPPerPlane[k] = locals->NoOfDPP[mode_lib->vba.VoltageLevel][MaximumMPCCombine][k];
5193 			locals->DPPCLK[k] = locals->RequiredDPPCLK[mode_lib->vba.VoltageLevel][MaximumMPCCombine][k];
5194 		}
5195 		mode_lib->vba.DISPCLK = locals->RequiredDISPCLK[mode_lib->vba.VoltageLevel][MaximumMPCCombine];
5196 		mode_lib->vba.maxMpcComb = MaximumMPCCombine;
5197 	}
5198 	mode_lib->vba.DCFCLK = mode_lib->vba.DCFCLKPerState[mode_lib->vba.VoltageLevel];
5199 	mode_lib->vba.DRAMSpeed = mode_lib->vba.DRAMSpeedPerState[mode_lib->vba.VoltageLevel];
5200 	mode_lib->vba.FabricClock = mode_lib->vba.FabricClockPerState[mode_lib->vba.VoltageLevel];
5201 	mode_lib->vba.SOCCLK = mode_lib->vba.SOCCLKPerState[mode_lib->vba.VoltageLevel];
5202 	mode_lib->vba.ReturnBW = locals->ReturnBWPerState[mode_lib->vba.VoltageLevel];
5203 	for (k = 0; k <= mode_lib->vba.NumberOfActivePlanes - 1; k++) {
5204 		if (mode_lib->vba.BlendingAndTiming[k] == k) {
5205 			mode_lib->vba.ODMCombineEnabled[k] =
5206 					locals->ODMCombineEnablePerState[mode_lib->vba.VoltageLevel][k];
5207 		} else {
5208 			mode_lib->vba.ODMCombineEnabled[k] = 0;
5209 		}
5210 		mode_lib->vba.DSCEnabled[k] =
5211 				locals->RequiresDSC[mode_lib->vba.VoltageLevel][k];
5212 		mode_lib->vba.OutputBpp[k] =
5213 				locals->OutputBppPerState[mode_lib->vba.VoltageLevel][k];
5214 	}
5215 }
5216 
5217 static void CalculateWatermarksAndDRAMSpeedChangeSupport(
5218 		struct display_mode_lib *mode_lib,
5219 		unsigned int PrefetchMode,
5220 		unsigned int NumberOfActivePlanes,
5221 		unsigned int MaxLineBufferLines,
5222 		unsigned int LineBufferSize,
5223 		unsigned int DPPOutputBufferPixels,
5224 		double DETBufferSizeInKByte,
5225 		unsigned int WritebackInterfaceLumaBufferSize,
5226 		unsigned int WritebackInterfaceChromaBufferSize,
5227 		double DCFCLK,
5228 		double UrgentOutOfOrderReturn,
5229 		double ReturnBW,
5230 		bool GPUVMEnable,
5231 		long dpte_group_bytes[],
5232 		unsigned int MetaChunkSize,
5233 		double UrgentLatency,
5234 		double ExtraLatency,
5235 		double WritebackLatency,
5236 		double WritebackChunkSize,
5237 		double SOCCLK,
5238 		double DRAMClockChangeLatency,
5239 		double SRExitTime,
5240 		double SREnterPlusExitTime,
5241 		double DCFCLKDeepSleep,
5242 		int DPPPerPlane[],
5243 		bool DCCEnable[],
5244 		double DPPCLK[],
5245 		unsigned int SwathWidthSingleDPPY[],
5246 		unsigned int SwathHeightY[],
5247 		double ReadBandwidthPlaneLuma[],
5248 		unsigned int SwathHeightC[],
5249 		double ReadBandwidthPlaneChroma[],
5250 		unsigned int LBBitPerPixel[],
5251 		unsigned int SwathWidthY[],
5252 		double HRatio[],
5253 		unsigned int vtaps[],
5254 		unsigned int VTAPsChroma[],
5255 		double VRatio[],
5256 		unsigned int HTotal[],
5257 		double PixelClock[],
5258 		unsigned int BlendingAndTiming[],
5259 		double BytePerPixelDETY[],
5260 		double BytePerPixelDETC[],
5261 		bool WritebackEnable[],
5262 		enum source_format_class WritebackPixelFormat[],
5263 		double WritebackDestinationWidth[],
5264 		double WritebackDestinationHeight[],
5265 		double WritebackSourceHeight[],
5266 		enum clock_change_support *DRAMClockChangeSupport,
5267 		double *UrgentWatermark,
5268 		double *WritebackUrgentWatermark,
5269 		double *DRAMClockChangeWatermark,
5270 		double *WritebackDRAMClockChangeWatermark,
5271 		double *StutterExitWatermark,
5272 		double *StutterEnterPlusExitWatermark,
5273 		double *MinActiveDRAMClockChangeLatencySupported)
5274 {
5275 	double EffectiveLBLatencyHidingY;
5276 	double EffectiveLBLatencyHidingC;
5277 	double DPPOutputBufferLinesY;
5278 	double DPPOutputBufferLinesC;
5279 	double DETBufferSizeY;
5280 	double DETBufferSizeC;
5281 	double LinesInDETY[DC__NUM_DPP__MAX];
5282 	double LinesInDETC;
5283 	unsigned int LinesInDETYRoundedDownToSwath[DC__NUM_DPP__MAX];
5284 	unsigned int LinesInDETCRoundedDownToSwath;
5285 	double FullDETBufferingTimeY[DC__NUM_DPP__MAX];
5286 	double FullDETBufferingTimeC;
5287 	double ActiveDRAMClockChangeLatencyMarginY;
5288 	double ActiveDRAMClockChangeLatencyMarginC;
5289 	double WritebackDRAMClockChangeLatencyMargin;
5290 	double PlaneWithMinActiveDRAMClockChangeMargin;
5291 	double SecondMinActiveDRAMClockChangeMarginOneDisplayInVBLank;
5292 	double FullDETBufferingTimeYStutterCriticalPlane = 0;
5293 	double TimeToFinishSwathTransferStutterCriticalPlane = 0;
5294 	unsigned int k, j;
5295 
5296 	mode_lib->vba.TotalActiveDPP = 0;
5297 	mode_lib->vba.TotalDCCActiveDPP = 0;
5298 	for (k = 0; k < NumberOfActivePlanes; ++k) {
5299 		mode_lib->vba.TotalActiveDPP = mode_lib->vba.TotalActiveDPP + DPPPerPlane[k];
5300 		if (DCCEnable[k] == true) {
5301 			mode_lib->vba.TotalDCCActiveDPP = mode_lib->vba.TotalDCCActiveDPP + DPPPerPlane[k];
5302 		}
5303 	}
5304 
5305 	mode_lib->vba.TotalDataReadBandwidth = 0;
5306 	for (k = 0; k < NumberOfActivePlanes; ++k) {
5307 		mode_lib->vba.TotalDataReadBandwidth = mode_lib->vba.TotalDataReadBandwidth
5308 				+ ReadBandwidthPlaneLuma[k] + ReadBandwidthPlaneChroma[k];
5309 	}
5310 
5311 	*UrgentWatermark = UrgentLatency + ExtraLatency;
5312 
5313 	*DRAMClockChangeWatermark = DRAMClockChangeLatency + *UrgentWatermark;
5314 
5315 	mode_lib->vba.TotalActiveWriteback = 0;
5316 	for (k = 0; k < NumberOfActivePlanes; ++k) {
5317 		if (WritebackEnable[k] == true) {
5318 			mode_lib->vba.TotalActiveWriteback = mode_lib->vba.TotalActiveWriteback + 1;
5319 		}
5320 	}
5321 
5322 	if (mode_lib->vba.TotalActiveWriteback <= 1) {
5323 		*WritebackUrgentWatermark = WritebackLatency;
5324 	} else {
5325 		*WritebackUrgentWatermark = WritebackLatency
5326 				+ WritebackChunkSize * 1024.0 / 32.0 / SOCCLK;
5327 	}
5328 
5329 	if (mode_lib->vba.TotalActiveWriteback <= 1) {
5330 		*WritebackDRAMClockChangeWatermark = DRAMClockChangeLatency + WritebackLatency;
5331 	} else {
5332 		*WritebackDRAMClockChangeWatermark = DRAMClockChangeLatency + WritebackLatency
5333 				+ WritebackChunkSize * 1024.0 / 32.0 / SOCCLK;
5334 	}
5335 
5336 	for (k = 0; k < NumberOfActivePlanes; ++k) {
5337 
5338 		mode_lib->vba.LBLatencyHidingSourceLinesY = dml_min((double) MaxLineBufferLines,
5339 			dml_floor(LineBufferSize / LBBitPerPixel[k] / (SwathWidthY[k] / dml_max(HRatio[k], 1.0)), 1))
5340 				- (vtaps[k] - 1);
5341 
5342 		mode_lib->vba.LBLatencyHidingSourceLinesC = dml_min((double) MaxLineBufferLines,
5343 			dml_floor(LineBufferSize / LBBitPerPixel[k] / (SwathWidthY[k] / 2 / dml_max(HRatio[k] / 2, 1.0)), 1))
5344 				- (VTAPsChroma[k] - 1);
5345 
5346 		EffectiveLBLatencyHidingY = mode_lib->vba.LBLatencyHidingSourceLinesY / VRatio[k]
5347 				* (HTotal[k] / PixelClock[k]);
5348 
5349 		EffectiveLBLatencyHidingC = mode_lib->vba.LBLatencyHidingSourceLinesC
5350 				/ (VRatio[k] / 2) * (HTotal[k] / PixelClock[k]);
5351 
5352 		if (SwathWidthY[k] > 2 * DPPOutputBufferPixels) {
5353 			DPPOutputBufferLinesY = (double) DPPOutputBufferPixels / SwathWidthY[k];
5354 		} else if (SwathWidthY[k] > DPPOutputBufferPixels) {
5355 			DPPOutputBufferLinesY = 0.5;
5356 		} else {
5357 			DPPOutputBufferLinesY = 1;
5358 		}
5359 
5360 		if (SwathWidthY[k] / 2.0 > 2 * DPPOutputBufferPixels) {
5361 			DPPOutputBufferLinesC = (double) DPPOutputBufferPixels
5362 					/ (SwathWidthY[k] / 2.0);
5363 		} else if (SwathWidthY[k] / 2.0 > DPPOutputBufferPixels) {
5364 			DPPOutputBufferLinesC = 0.5;
5365 		} else {
5366 			DPPOutputBufferLinesC = 1;
5367 		}
5368 
5369 		CalculateDETBufferSize(
5370 				DETBufferSizeInKByte,
5371 				SwathHeightY[k],
5372 				SwathHeightC[k],
5373 				&DETBufferSizeY,
5374 				&DETBufferSizeC);
5375 
5376 		LinesInDETY[k] = DETBufferSizeY / BytePerPixelDETY[k] / SwathWidthY[k];
5377 		LinesInDETYRoundedDownToSwath[k] = dml_floor(LinesInDETY[k], SwathHeightY[k]);
5378 		FullDETBufferingTimeY[k] = LinesInDETYRoundedDownToSwath[k]
5379 				* (HTotal[k] / PixelClock[k]) / VRatio[k];
5380 		if (BytePerPixelDETC[k] > 0) {
5381 			LinesInDETC = DETBufferSizeC / BytePerPixelDETC[k] / (SwathWidthY[k] / 2.0);
5382 			LinesInDETCRoundedDownToSwath = dml_floor(LinesInDETC, SwathHeightC[k]);
5383 			FullDETBufferingTimeC = LinesInDETCRoundedDownToSwath
5384 					* (HTotal[k] / PixelClock[k]) / (VRatio[k] / 2);
5385 		} else {
5386 			LinesInDETC = 0;
5387 			FullDETBufferingTimeC = 999999;
5388 		}
5389 
5390 		ActiveDRAMClockChangeLatencyMarginY = HTotal[k] / PixelClock[k]
5391 				* DPPOutputBufferLinesY + EffectiveLBLatencyHidingY
5392 				+ FullDETBufferingTimeY[k] - *DRAMClockChangeWatermark;
5393 
5394 		if (NumberOfActivePlanes > 1) {
5395 			ActiveDRAMClockChangeLatencyMarginY = ActiveDRAMClockChangeLatencyMarginY
5396 				- (1 - 1.0 / NumberOfActivePlanes) * SwathHeightY[k] * HTotal[k] / PixelClock[k] / VRatio[k];
5397 		}
5398 
5399 		if (BytePerPixelDETC[k] > 0) {
5400 			ActiveDRAMClockChangeLatencyMarginC = HTotal[k] / PixelClock[k]
5401 					* DPPOutputBufferLinesC + EffectiveLBLatencyHidingC
5402 					+ FullDETBufferingTimeC - *DRAMClockChangeWatermark;
5403 			if (NumberOfActivePlanes > 1) {
5404 				ActiveDRAMClockChangeLatencyMarginC = ActiveDRAMClockChangeLatencyMarginC
5405 					- (1 - 1.0 / NumberOfActivePlanes) * SwathHeightC[k] * HTotal[k] / PixelClock[k] / (VRatio[k] / 2);
5406 			}
5407 			mode_lib->vba.ActiveDRAMClockChangeLatencyMargin[k] = dml_min(
5408 					ActiveDRAMClockChangeLatencyMarginY,
5409 					ActiveDRAMClockChangeLatencyMarginC);
5410 		} else {
5411 			mode_lib->vba.ActiveDRAMClockChangeLatencyMargin[k] = ActiveDRAMClockChangeLatencyMarginY;
5412 		}
5413 
5414 		if (WritebackEnable[k] == true) {
5415 			if (WritebackPixelFormat[k] == dm_444_32) {
5416 				WritebackDRAMClockChangeLatencyMargin = (WritebackInterfaceLumaBufferSize
5417 					+ WritebackInterfaceChromaBufferSize) / (WritebackDestinationWidth[k]
5418 					* WritebackDestinationHeight[k] / (WritebackSourceHeight[k] * HTotal[k]
5419 					/ PixelClock[k]) * 4) - *WritebackDRAMClockChangeWatermark;
5420 			} else {
5421 				WritebackDRAMClockChangeLatencyMargin = dml_min(
5422 						WritebackInterfaceLumaBufferSize * 8.0 / 10,
5423 						2 * WritebackInterfaceChromaBufferSize * 8.0 / 10) / (WritebackDestinationWidth[k]
5424 							* WritebackDestinationHeight[k] / (WritebackSourceHeight[k] * HTotal[k] / PixelClock[k]))
5425 						- *WritebackDRAMClockChangeWatermark;
5426 			}
5427 			mode_lib->vba.ActiveDRAMClockChangeLatencyMargin[k] = dml_min(
5428 					mode_lib->vba.ActiveDRAMClockChangeLatencyMargin[k],
5429 					WritebackDRAMClockChangeLatencyMargin);
5430 		}
5431 	}
5432 
5433 	mode_lib->vba.MinActiveDRAMClockChangeMargin = 999999;
5434 	PlaneWithMinActiveDRAMClockChangeMargin = 0;
5435 	for (k = 0; k < NumberOfActivePlanes; ++k) {
5436 		if (mode_lib->vba.ActiveDRAMClockChangeLatencyMargin[k]
5437 				< mode_lib->vba.MinActiveDRAMClockChangeMargin) {
5438 			mode_lib->vba.MinActiveDRAMClockChangeMargin =
5439 					mode_lib->vba.ActiveDRAMClockChangeLatencyMargin[k];
5440 			if (BlendingAndTiming[k] == k) {
5441 				PlaneWithMinActiveDRAMClockChangeMargin = k;
5442 			} else {
5443 				for (j = 0; j < NumberOfActivePlanes; ++j) {
5444 					if (BlendingAndTiming[k] == j) {
5445 						PlaneWithMinActiveDRAMClockChangeMargin = j;
5446 					}
5447 				}
5448 			}
5449 		}
5450 	}
5451 
5452 	*MinActiveDRAMClockChangeLatencySupported = mode_lib->vba.MinActiveDRAMClockChangeMargin + DRAMClockChangeLatency;
5453 
5454 	SecondMinActiveDRAMClockChangeMarginOneDisplayInVBLank = 999999;
5455 	for (k = 0; k < NumberOfActivePlanes; ++k) {
5456 		if (!((k == PlaneWithMinActiveDRAMClockChangeMargin) && (BlendingAndTiming[k] == k))
5457 				&& !(BlendingAndTiming[k] == PlaneWithMinActiveDRAMClockChangeMargin)
5458 				&& mode_lib->vba.ActiveDRAMClockChangeLatencyMargin[k]
5459 						< SecondMinActiveDRAMClockChangeMarginOneDisplayInVBLank) {
5460 			SecondMinActiveDRAMClockChangeMarginOneDisplayInVBLank =
5461 					mode_lib->vba.ActiveDRAMClockChangeLatencyMargin[k];
5462 		}
5463 	}
5464 
5465 	mode_lib->vba.TotalNumberOfActiveOTG = 0;
5466 	for (k = 0; k < NumberOfActivePlanes; ++k) {
5467 		if (BlendingAndTiming[k] == k) {
5468 			mode_lib->vba.TotalNumberOfActiveOTG = mode_lib->vba.TotalNumberOfActiveOTG + 1;
5469 		}
5470 	}
5471 
5472 	if (mode_lib->vba.MinActiveDRAMClockChangeMargin > 0) {
5473 		*DRAMClockChangeSupport = dm_dram_clock_change_vactive;
5474 	} else if (((mode_lib->vba.SynchronizedVBlank == true
5475 			|| mode_lib->vba.TotalNumberOfActiveOTG == 1
5476 			|| SecondMinActiveDRAMClockChangeMarginOneDisplayInVBLank > 0)
5477 			&& PrefetchMode == 0)) {
5478 		*DRAMClockChangeSupport = dm_dram_clock_change_vblank;
5479 	} else {
5480 		*DRAMClockChangeSupport = dm_dram_clock_change_unsupported;
5481 	}
5482 
5483 	FullDETBufferingTimeYStutterCriticalPlane = FullDETBufferingTimeY[0];
5484 	for (k = 0; k < NumberOfActivePlanes; ++k) {
5485 		if (FullDETBufferingTimeY[k] <= FullDETBufferingTimeYStutterCriticalPlane) {
5486 			TimeToFinishSwathTransferStutterCriticalPlane = (SwathHeightY[k]
5487 					- (LinesInDETY[k] - LinesInDETYRoundedDownToSwath[k]))
5488 					* (HTotal[k] / PixelClock[k]) / VRatio[k];
5489 		}
5490 	}
5491 
5492 	*StutterExitWatermark = SRExitTime + mode_lib->vba.LastPixelOfLineExtraWatermark
5493 			+ ExtraLatency + 10 / DCFCLKDeepSleep;
5494 	*StutterEnterPlusExitWatermark = dml_max(
5495 			SREnterPlusExitTime + mode_lib->vba.LastPixelOfLineExtraWatermark
5496 					+ ExtraLatency + 10 / DCFCLKDeepSleep,
5497 			TimeToFinishSwathTransferStutterCriticalPlane);
5498 
5499 }
5500 
5501 static void CalculateDCFCLKDeepSleep(
5502 		struct display_mode_lib *mode_lib,
5503 		unsigned int NumberOfActivePlanes,
5504 		double BytePerPixelDETY[],
5505 		double BytePerPixelDETC[],
5506 		double VRatio[],
5507 		unsigned int SwathWidthY[],
5508 		int DPPPerPlane[],
5509 		double HRatio[],
5510 		double PixelClock[],
5511 		double PSCL_THROUGHPUT[],
5512 		double PSCL_THROUGHPUT_CHROMA[],
5513 		double DPPCLK[],
5514 		double *DCFCLKDeepSleep)
5515 {
5516 	unsigned int k;
5517 	double DisplayPipeLineDeliveryTimeLuma;
5518 	double DisplayPipeLineDeliveryTimeChroma;
5519 	//double   DCFCLKDeepSleepPerPlane[DC__NUM_DPP__MAX];
5520 
5521 	for (k = 0; k < NumberOfActivePlanes; ++k) {
5522 		if (VRatio[k] <= 1) {
5523 			DisplayPipeLineDeliveryTimeLuma = SwathWidthY[k] * DPPPerPlane[k]
5524 					/ HRatio[k] / PixelClock[k];
5525 		} else {
5526 			DisplayPipeLineDeliveryTimeLuma = SwathWidthY[k] / PSCL_THROUGHPUT[k]
5527 					/ DPPCLK[k];
5528 		}
5529 		if (BytePerPixelDETC[k] == 0) {
5530 			DisplayPipeLineDeliveryTimeChroma = 0;
5531 		} else {
5532 			if (VRatio[k] / 2 <= 1) {
5533 				DisplayPipeLineDeliveryTimeChroma = SwathWidthY[k] / 2.0
5534 						* DPPPerPlane[k] / (HRatio[k] / 2) / PixelClock[k];
5535 			} else {
5536 				DisplayPipeLineDeliveryTimeChroma = SwathWidthY[k] / 2.0
5537 						/ PSCL_THROUGHPUT_CHROMA[k] / DPPCLK[k];
5538 			}
5539 		}
5540 
5541 		if (BytePerPixelDETC[k] > 0) {
5542 			mode_lib->vba.DCFCLKDeepSleepPerPlane[k] = dml_max(
5543 					1.1 * SwathWidthY[k] * dml_ceil(BytePerPixelDETY[k], 1)
5544 							/ 32.0 / DisplayPipeLineDeliveryTimeLuma,
5545 					1.1 * SwathWidthY[k] / 2.0
5546 							* dml_ceil(BytePerPixelDETC[k], 2) / 32.0
5547 							/ DisplayPipeLineDeliveryTimeChroma);
5548 		} else {
5549 			mode_lib->vba.DCFCLKDeepSleepPerPlane[k] = 1.1 * SwathWidthY[k]
5550 					* dml_ceil(BytePerPixelDETY[k], 1) / 64.0
5551 					/ DisplayPipeLineDeliveryTimeLuma;
5552 		}
5553 		mode_lib->vba.DCFCLKDeepSleepPerPlane[k] = dml_max(
5554 				mode_lib->vba.DCFCLKDeepSleepPerPlane[k],
5555 				PixelClock[k] / 16);
5556 
5557 	}
5558 
5559 	*DCFCLKDeepSleep = 8;
5560 	for (k = 0; k < NumberOfActivePlanes; ++k) {
5561 		*DCFCLKDeepSleep = dml_max(
5562 				*DCFCLKDeepSleep,
5563 				mode_lib->vba.DCFCLKDeepSleepPerPlane[k]);
5564 	}
5565 }
5566 
5567 static void CalculateDETBufferSize(
5568 		double DETBufferSizeInKByte,
5569 		unsigned int SwathHeightY,
5570 		unsigned int SwathHeightC,
5571 		double *DETBufferSizeY,
5572 		double *DETBufferSizeC)
5573 {
5574 	if (SwathHeightC == 0) {
5575 		*DETBufferSizeY = DETBufferSizeInKByte * 1024;
5576 		*DETBufferSizeC = 0;
5577 	} else if (SwathHeightY <= SwathHeightC) {
5578 		*DETBufferSizeY = DETBufferSizeInKByte * 1024 / 2;
5579 		*DETBufferSizeC = DETBufferSizeInKByte * 1024 / 2;
5580 	} else {
5581 		*DETBufferSizeY = DETBufferSizeInKByte * 1024 * 2 / 3;
5582 		*DETBufferSizeC = DETBufferSizeInKByte * 1024 / 3;
5583 	}
5584 }
5585 
5586 static void CalculateUrgentBurstFactor(
5587 		unsigned int DETBufferSizeInKByte,
5588 		unsigned int SwathHeightY,
5589 		unsigned int SwathHeightC,
5590 		unsigned int SwathWidthY,
5591 		double LineTime,
5592 		double UrgentLatency,
5593 		double CursorBufferSize,
5594 		unsigned int CursorWidth,
5595 		unsigned int CursorBPP,
5596 		double VRatio,
5597 		double VRatioPreY,
5598 		double VRatioPreC,
5599 		double BytePerPixelInDETY,
5600 		double BytePerPixelInDETC,
5601 		double *UrgentBurstFactorCursor,
5602 		double *UrgentBurstFactorCursorPre,
5603 		double *UrgentBurstFactorLuma,
5604 		double *UrgentBurstFactorLumaPre,
5605 		double *UrgentBurstFactorChroma,
5606 		double *UrgentBurstFactorChromaPre,
5607 		unsigned int *NotEnoughUrgentLatencyHiding,
5608 		unsigned int *NotEnoughUrgentLatencyHidingPre)
5609 {
5610 	double LinesInDETLuma;
5611 	double LinesInDETChroma;
5612 	unsigned int LinesInCursorBuffer;
5613 	double CursorBufferSizeInTime;
5614 	double CursorBufferSizeInTimePre;
5615 	double DETBufferSizeInTimeLuma;
5616 	double DETBufferSizeInTimeLumaPre;
5617 	double DETBufferSizeInTimeChroma;
5618 	double DETBufferSizeInTimeChromaPre;
5619 	double DETBufferSizeY;
5620 	double DETBufferSizeC;
5621 
5622 	*NotEnoughUrgentLatencyHiding = 0;
5623 	*NotEnoughUrgentLatencyHidingPre = 0;
5624 
5625 	if (CursorWidth > 0) {
5626 		LinesInCursorBuffer = 1 << (unsigned int) dml_floor(
5627 			dml_log2(CursorBufferSize * 1024.0 / (CursorWidth * CursorBPP / 8.0)), 1.0);
5628 		CursorBufferSizeInTime = LinesInCursorBuffer * LineTime / VRatio;
5629 		if (CursorBufferSizeInTime - UrgentLatency <= 0) {
5630 			*NotEnoughUrgentLatencyHiding = 1;
5631 			*UrgentBurstFactorCursor = 0;
5632 		} else {
5633 			*UrgentBurstFactorCursor = CursorBufferSizeInTime
5634 					/ (CursorBufferSizeInTime - UrgentLatency);
5635 		}
5636 		if (VRatioPreY > 0) {
5637 			CursorBufferSizeInTimePre = LinesInCursorBuffer * LineTime / VRatioPreY;
5638 			if (CursorBufferSizeInTimePre - UrgentLatency <= 0) {
5639 				*NotEnoughUrgentLatencyHidingPre = 1;
5640 				*UrgentBurstFactorCursorPre = 0;
5641 			} else {
5642 				*UrgentBurstFactorCursorPre = CursorBufferSizeInTimePre
5643 						/ (CursorBufferSizeInTimePre - UrgentLatency);
5644 			}
5645 		} else {
5646 			*UrgentBurstFactorCursorPre = 1;
5647 		}
5648 	}
5649 
5650 	CalculateDETBufferSize(
5651 			DETBufferSizeInKByte,
5652 			SwathHeightY,
5653 			SwathHeightC,
5654 			&DETBufferSizeY,
5655 			&DETBufferSizeC);
5656 
5657 	LinesInDETLuma = DETBufferSizeY / BytePerPixelInDETY / SwathWidthY;
5658 	DETBufferSizeInTimeLuma = dml_floor(LinesInDETLuma, SwathHeightY) * LineTime / VRatio;
5659 	if (DETBufferSizeInTimeLuma - UrgentLatency <= 0) {
5660 		*NotEnoughUrgentLatencyHiding = 1;
5661 		*UrgentBurstFactorLuma = 0;
5662 	} else {
5663 		*UrgentBurstFactorLuma = DETBufferSizeInTimeLuma
5664 				/ (DETBufferSizeInTimeLuma - UrgentLatency);
5665 	}
5666 	if (VRatioPreY > 0) {
5667 		DETBufferSizeInTimeLumaPre = dml_floor(LinesInDETLuma, SwathHeightY) * LineTime
5668 				/ VRatioPreY;
5669 		if (DETBufferSizeInTimeLumaPre - UrgentLatency <= 0) {
5670 			*NotEnoughUrgentLatencyHidingPre = 1;
5671 			*UrgentBurstFactorLumaPre = 0;
5672 		} else {
5673 			*UrgentBurstFactorLumaPre = DETBufferSizeInTimeLumaPre
5674 					/ (DETBufferSizeInTimeLumaPre - UrgentLatency);
5675 		}
5676 	} else {
5677 		*UrgentBurstFactorLumaPre = 1;
5678 	}
5679 
5680 	if (BytePerPixelInDETC > 0) {
5681 		LinesInDETChroma = DETBufferSizeC / BytePerPixelInDETC / (SwathWidthY / 2);
5682 		DETBufferSizeInTimeChroma = dml_floor(LinesInDETChroma, SwathHeightC) * LineTime
5683 				/ (VRatio / 2);
5684 		if (DETBufferSizeInTimeChroma - UrgentLatency <= 0) {
5685 			*NotEnoughUrgentLatencyHiding = 1;
5686 			*UrgentBurstFactorChroma = 0;
5687 		} else {
5688 			*UrgentBurstFactorChroma = DETBufferSizeInTimeChroma
5689 					/ (DETBufferSizeInTimeChroma - UrgentLatency);
5690 		}
5691 		if (VRatioPreC > 0) {
5692 			DETBufferSizeInTimeChromaPre = dml_floor(LinesInDETChroma, SwathHeightC)
5693 					* LineTime / VRatioPreC;
5694 			if (DETBufferSizeInTimeChromaPre - UrgentLatency <= 0) {
5695 				*NotEnoughUrgentLatencyHidingPre = 1;
5696 				*UrgentBurstFactorChromaPre = 0;
5697 			} else {
5698 				*UrgentBurstFactorChromaPre = DETBufferSizeInTimeChromaPre
5699 						/ (DETBufferSizeInTimeChromaPre - UrgentLatency);
5700 			}
5701 		} else {
5702 			*UrgentBurstFactorChromaPre = 1;
5703 		}
5704 	}
5705 }
5706 
5707 static void CalculatePixelDeliveryTimes(
5708 		unsigned int NumberOfActivePlanes,
5709 		double VRatio[],
5710 		double VRatioPrefetchY[],
5711 		double VRatioPrefetchC[],
5712 		unsigned int swath_width_luma_ub[],
5713 		unsigned int swath_width_chroma_ub[],
5714 		int DPPPerPlane[],
5715 		double HRatio[],
5716 		double PixelClock[],
5717 		double PSCL_THROUGHPUT[],
5718 		double PSCL_THROUGHPUT_CHROMA[],
5719 		double DPPCLK[],
5720 		double BytePerPixelDETC[],
5721 		enum scan_direction_class SourceScan[],
5722 		unsigned int BlockWidth256BytesY[],
5723 		unsigned int BlockHeight256BytesY[],
5724 		unsigned int BlockWidth256BytesC[],
5725 		unsigned int BlockHeight256BytesC[],
5726 		double DisplayPipeLineDeliveryTimeLuma[],
5727 		double DisplayPipeLineDeliveryTimeChroma[],
5728 		double DisplayPipeLineDeliveryTimeLumaPrefetch[],
5729 		double DisplayPipeLineDeliveryTimeChromaPrefetch[],
5730 		double DisplayPipeRequestDeliveryTimeLuma[],
5731 		double DisplayPipeRequestDeliveryTimeChroma[],
5732 		double DisplayPipeRequestDeliveryTimeLumaPrefetch[],
5733 		double DisplayPipeRequestDeliveryTimeChromaPrefetch[])
5734 {
5735 	double req_per_swath_ub;
5736 	unsigned int k;
5737 
5738 	for (k = 0; k < NumberOfActivePlanes; ++k) {
5739 		if (VRatio[k] <= 1) {
5740 			DisplayPipeLineDeliveryTimeLuma[k] = swath_width_luma_ub[k] * DPPPerPlane[k]
5741 					/ HRatio[k] / PixelClock[k];
5742 		} else {
5743 			DisplayPipeLineDeliveryTimeLuma[k] = swath_width_luma_ub[k]
5744 					/ PSCL_THROUGHPUT[k] / DPPCLK[k];
5745 		}
5746 
5747 		if (BytePerPixelDETC[k] == 0) {
5748 			DisplayPipeLineDeliveryTimeChroma[k] = 0;
5749 		} else {
5750 			if (VRatio[k] / 2 <= 1) {
5751 				DisplayPipeLineDeliveryTimeChroma[k] = swath_width_chroma_ub[k]
5752 						* DPPPerPlane[k] / (HRatio[k] / 2) / PixelClock[k];
5753 			} else {
5754 				DisplayPipeLineDeliveryTimeChroma[k] = swath_width_chroma_ub[k]
5755 						/ PSCL_THROUGHPUT_CHROMA[k] / DPPCLK[k];
5756 			}
5757 		}
5758 
5759 		if (VRatioPrefetchY[k] <= 1) {
5760 			DisplayPipeLineDeliveryTimeLumaPrefetch[k] = swath_width_luma_ub[k]
5761 					* DPPPerPlane[k] / HRatio[k] / PixelClock[k];
5762 		} else {
5763 			DisplayPipeLineDeliveryTimeLumaPrefetch[k] = swath_width_luma_ub[k]
5764 					/ PSCL_THROUGHPUT[k] / DPPCLK[k];
5765 		}
5766 
5767 		if (BytePerPixelDETC[k] == 0) {
5768 			DisplayPipeLineDeliveryTimeChromaPrefetch[k] = 0;
5769 		} else {
5770 			if (VRatioPrefetchC[k] <= 1) {
5771 				DisplayPipeLineDeliveryTimeChromaPrefetch[k] =
5772 						swath_width_chroma_ub[k] * DPPPerPlane[k]
5773 								/ (HRatio[k] / 2) / PixelClock[k];
5774 			} else {
5775 				DisplayPipeLineDeliveryTimeChromaPrefetch[k] =
5776 						swath_width_chroma_ub[k] / PSCL_THROUGHPUT_CHROMA[k] / DPPCLK[k];
5777 			}
5778 		}
5779 	}
5780 
5781 	for (k = 0; k < NumberOfActivePlanes; ++k) {
5782 		if (SourceScan[k] == dm_horz) {
5783 			req_per_swath_ub = swath_width_luma_ub[k] / BlockWidth256BytesY[k];
5784 		} else {
5785 			req_per_swath_ub = swath_width_luma_ub[k] / BlockHeight256BytesY[k];
5786 		}
5787 		DisplayPipeRequestDeliveryTimeLuma[k] = DisplayPipeLineDeliveryTimeLuma[k]
5788 				/ req_per_swath_ub;
5789 		DisplayPipeRequestDeliveryTimeLumaPrefetch[k] =
5790 				DisplayPipeLineDeliveryTimeLumaPrefetch[k] / req_per_swath_ub;
5791 		if (BytePerPixelDETC[k] == 0) {
5792 			DisplayPipeRequestDeliveryTimeChroma[k] = 0;
5793 			DisplayPipeRequestDeliveryTimeChromaPrefetch[k] = 0;
5794 		} else {
5795 			if (SourceScan[k] == dm_horz) {
5796 				req_per_swath_ub = swath_width_chroma_ub[k]
5797 						/ BlockWidth256BytesC[k];
5798 			} else {
5799 				req_per_swath_ub = swath_width_chroma_ub[k]
5800 						/ BlockHeight256BytesC[k];
5801 			}
5802 			DisplayPipeRequestDeliveryTimeChroma[k] =
5803 					DisplayPipeLineDeliveryTimeChroma[k] / req_per_swath_ub;
5804 			DisplayPipeRequestDeliveryTimeChromaPrefetch[k] =
5805 					DisplayPipeLineDeliveryTimeChromaPrefetch[k] / req_per_swath_ub;
5806 		}
5807 	}
5808 }
5809 
5810 static void CalculateMetaAndPTETimes(
5811 		unsigned int NumberOfActivePlanes,
5812 		bool GPUVMEnable,
5813 		unsigned int MetaChunkSize,
5814 		unsigned int MinMetaChunkSizeBytes,
5815 		unsigned int GPUVMMaxPageTableLevels,
5816 		unsigned int HTotal[],
5817 		double VRatio[],
5818 		double VRatioPrefetchY[],
5819 		double VRatioPrefetchC[],
5820 		double DestinationLinesToRequestRowInVBlank[],
5821 		double DestinationLinesToRequestRowInImmediateFlip[],
5822 		double DestinationLinesToRequestVMInVBlank[],
5823 		double DestinationLinesToRequestVMInImmediateFlip[],
5824 		bool DCCEnable[],
5825 		double PixelClock[],
5826 		double BytePerPixelDETY[],
5827 		double BytePerPixelDETC[],
5828 		enum scan_direction_class SourceScan[],
5829 		unsigned int dpte_row_height[],
5830 		unsigned int dpte_row_height_chroma[],
5831 		unsigned int meta_row_width[],
5832 		unsigned int meta_row_height[],
5833 		unsigned int meta_req_width[],
5834 		unsigned int meta_req_height[],
5835 		long dpte_group_bytes[],
5836 		unsigned int PTERequestSizeY[],
5837 		unsigned int PTERequestSizeC[],
5838 		unsigned int PixelPTEReqWidthY[],
5839 		unsigned int PixelPTEReqHeightY[],
5840 		unsigned int PixelPTEReqWidthC[],
5841 		unsigned int PixelPTEReqHeightC[],
5842 		unsigned int dpte_row_width_luma_ub[],
5843 		unsigned int dpte_row_width_chroma_ub[],
5844 		unsigned int vm_group_bytes[],
5845 		unsigned int dpde0_bytes_per_frame_ub_l[],
5846 		unsigned int dpde0_bytes_per_frame_ub_c[],
5847 		unsigned int meta_pte_bytes_per_frame_ub_l[],
5848 		unsigned int meta_pte_bytes_per_frame_ub_c[],
5849 		double DST_Y_PER_PTE_ROW_NOM_L[],
5850 		double DST_Y_PER_PTE_ROW_NOM_C[],
5851 		double DST_Y_PER_META_ROW_NOM_L[],
5852 		double TimePerMetaChunkNominal[],
5853 		double TimePerMetaChunkVBlank[],
5854 		double TimePerMetaChunkFlip[],
5855 		double time_per_pte_group_nom_luma[],
5856 		double time_per_pte_group_vblank_luma[],
5857 		double time_per_pte_group_flip_luma[],
5858 		double time_per_pte_group_nom_chroma[],
5859 		double time_per_pte_group_vblank_chroma[],
5860 		double time_per_pte_group_flip_chroma[],
5861 		double TimePerVMGroupVBlank[],
5862 		double TimePerVMGroupFlip[],
5863 		double TimePerVMRequestVBlank[],
5864 		double TimePerVMRequestFlip[])
5865 {
5866 	unsigned int meta_chunk_width;
5867 	unsigned int min_meta_chunk_width;
5868 	unsigned int meta_chunk_per_row_int;
5869 	unsigned int meta_row_remainder;
5870 	unsigned int meta_chunk_threshold;
5871 	unsigned int meta_chunks_per_row_ub;
5872 	unsigned int dpte_group_width_luma;
5873 	unsigned int dpte_group_width_chroma;
5874 	unsigned int dpte_groups_per_row_luma_ub;
5875 	unsigned int dpte_groups_per_row_chroma_ub;
5876 	unsigned int num_group_per_lower_vm_stage;
5877 	unsigned int num_req_per_lower_vm_stage;
5878 	unsigned int k;
5879 
5880 	for (k = 0; k < NumberOfActivePlanes; ++k) {
5881 		if (GPUVMEnable == true) {
5882 			DST_Y_PER_PTE_ROW_NOM_L[k] = dpte_row_height[k] / VRatio[k];
5883 			if (BytePerPixelDETC[k] == 0) {
5884 				DST_Y_PER_PTE_ROW_NOM_C[k] = 0;
5885 			} else {
5886 				DST_Y_PER_PTE_ROW_NOM_C[k] = dpte_row_height_chroma[k] / (VRatio[k] / 2);
5887 			}
5888 		} else {
5889 			DST_Y_PER_PTE_ROW_NOM_L[k] = 0;
5890 			DST_Y_PER_PTE_ROW_NOM_C[k] = 0;
5891 		}
5892 		if (DCCEnable[k] == true) {
5893 			DST_Y_PER_META_ROW_NOM_L[k] = meta_row_height[k] / VRatio[k];
5894 		} else {
5895 			DST_Y_PER_META_ROW_NOM_L[k] = 0;
5896 		}
5897 	}
5898 
5899 	for (k = 0; k < NumberOfActivePlanes; ++k) {
5900 		if (DCCEnable[k] == true) {
5901 			meta_chunk_width = MetaChunkSize * 1024 * 256
5902 					/ dml_ceil(BytePerPixelDETY[k], 1) / meta_row_height[k];
5903 			min_meta_chunk_width = MinMetaChunkSizeBytes * 256
5904 					/ dml_ceil(BytePerPixelDETY[k], 1) / meta_row_height[k];
5905 			meta_chunk_per_row_int = meta_row_width[k] / meta_chunk_width;
5906 			meta_row_remainder = meta_row_width[k] % meta_chunk_width;
5907 			if (SourceScan[k] == dm_horz) {
5908 				meta_chunk_threshold = 2 * min_meta_chunk_width - meta_req_width[k];
5909 			} else {
5910 				meta_chunk_threshold = 2 * min_meta_chunk_width
5911 						- meta_req_height[k];
5912 			}
5913 			if (meta_row_remainder <= meta_chunk_threshold) {
5914 				meta_chunks_per_row_ub = meta_chunk_per_row_int + 1;
5915 			} else {
5916 				meta_chunks_per_row_ub = meta_chunk_per_row_int + 2;
5917 			}
5918 			TimePerMetaChunkNominal[k] = meta_row_height[k] / VRatio[k] * HTotal[k]
5919 					/ PixelClock[k] / meta_chunks_per_row_ub;
5920 			TimePerMetaChunkVBlank[k] = DestinationLinesToRequestRowInVBlank[k]
5921 					* HTotal[k] / PixelClock[k] / meta_chunks_per_row_ub;
5922 			TimePerMetaChunkFlip[k] = DestinationLinesToRequestRowInImmediateFlip[k]
5923 					* HTotal[k] / PixelClock[k] / meta_chunks_per_row_ub;
5924 		} else {
5925 			TimePerMetaChunkNominal[k] = 0;
5926 			TimePerMetaChunkVBlank[k] = 0;
5927 			TimePerMetaChunkFlip[k] = 0;
5928 		}
5929 	}
5930 
5931 	for (k = 0; k < NumberOfActivePlanes; ++k) {
5932 		if (GPUVMEnable == true) {
5933 			if (SourceScan[k] == dm_horz) {
5934 				dpte_group_width_luma = dpte_group_bytes[k] / PTERequestSizeY[k]
5935 						* PixelPTEReqWidthY[k];
5936 			} else {
5937 				dpte_group_width_luma = dpte_group_bytes[k] / PTERequestSizeY[k]
5938 						* PixelPTEReqHeightY[k];
5939 			}
5940 			dpte_groups_per_row_luma_ub = dml_ceil(
5941 					dpte_row_width_luma_ub[k] / dpte_group_width_luma,
5942 					1);
5943 			time_per_pte_group_nom_luma[k] = DST_Y_PER_PTE_ROW_NOM_L[k] * HTotal[k]
5944 					/ PixelClock[k] / dpte_groups_per_row_luma_ub;
5945 			time_per_pte_group_vblank_luma[k] = DestinationLinesToRequestRowInVBlank[k]
5946 					* HTotal[k] / PixelClock[k] / dpte_groups_per_row_luma_ub;
5947 			time_per_pte_group_flip_luma[k] =
5948 					DestinationLinesToRequestRowInImmediateFlip[k] * HTotal[k]
5949 							/ PixelClock[k]
5950 							/ dpte_groups_per_row_luma_ub;
5951 			if (BytePerPixelDETC[k] == 0) {
5952 				time_per_pte_group_nom_chroma[k] = 0;
5953 				time_per_pte_group_vblank_chroma[k] = 0;
5954 				time_per_pte_group_flip_chroma[k] = 0;
5955 			} else {
5956 				if (SourceScan[k] == dm_horz) {
5957 					dpte_group_width_chroma = dpte_group_bytes[k]
5958 							/ PTERequestSizeC[k] * PixelPTEReqWidthC[k];
5959 				} else {
5960 					dpte_group_width_chroma = dpte_group_bytes[k]
5961 							/ PTERequestSizeC[k]
5962 							* PixelPTEReqHeightC[k];
5963 				}
5964 				dpte_groups_per_row_chroma_ub = dml_ceil(
5965 						dpte_row_width_chroma_ub[k]
5966 								/ dpte_group_width_chroma,
5967 						1);
5968 				time_per_pte_group_nom_chroma[k] = DST_Y_PER_PTE_ROW_NOM_C[k]
5969 						* HTotal[k] / PixelClock[k]
5970 						/ dpte_groups_per_row_chroma_ub;
5971 				time_per_pte_group_vblank_chroma[k] =
5972 						DestinationLinesToRequestRowInVBlank[k] * HTotal[k]
5973 								/ PixelClock[k]
5974 								/ dpte_groups_per_row_chroma_ub;
5975 				time_per_pte_group_flip_chroma[k] =
5976 						DestinationLinesToRequestRowInImmediateFlip[k]
5977 								* HTotal[k] / PixelClock[k]
5978 								/ dpte_groups_per_row_chroma_ub;
5979 			}
5980 		} else {
5981 			time_per_pte_group_nom_luma[k] = 0;
5982 			time_per_pte_group_vblank_luma[k] = 0;
5983 			time_per_pte_group_flip_luma[k] = 0;
5984 			time_per_pte_group_nom_chroma[k] = 0;
5985 			time_per_pte_group_vblank_chroma[k] = 0;
5986 			time_per_pte_group_flip_chroma[k] = 0;
5987 		}
5988 	}
5989 
5990 	for (k = 0; k < NumberOfActivePlanes; ++k) {
5991 		if (GPUVMEnable == true && (DCCEnable[k] == true || GPUVMMaxPageTableLevels > 1)) {
5992 			if (DCCEnable[k] == false) {
5993 				if (BytePerPixelDETC[k] > 0) {
5994 					num_group_per_lower_vm_stage =
5995 						dml_ceil((double) (dpde0_bytes_per_frame_ub_l[k]) / (double) (vm_group_bytes[k]), 1)
5996 						+ dml_ceil((double) (dpde0_bytes_per_frame_ub_c[k]) / (double) (vm_group_bytes[k]), 1);
5997 				} else {
5998 					num_group_per_lower_vm_stage =
5999 							dml_ceil((double) (dpde0_bytes_per_frame_ub_l[k]) / (double) (vm_group_bytes[k]), 1);
6000 				}
6001 			} else {
6002 				if (GPUVMMaxPageTableLevels == 1) {
6003 					if (BytePerPixelDETC[k] > 0) {
6004 						num_group_per_lower_vm_stage =
6005 							dml_ceil((double) (meta_pte_bytes_per_frame_ub_l[k]) / (double) (vm_group_bytes[k]), 1)
6006 							+ dml_ceil((double) (meta_pte_bytes_per_frame_ub_c[k]) / (double) (vm_group_bytes[k]), 1);
6007 					} else {
6008 						num_group_per_lower_vm_stage =
6009 							dml_ceil((double) (meta_pte_bytes_per_frame_ub_l[k]) / (double) (vm_group_bytes[k]), 1);
6010 					}
6011 				} else {
6012 					if (BytePerPixelDETC[k] > 0) {
6013 						num_group_per_lower_vm_stage =
6014 							dml_ceil((double) (dpde0_bytes_per_frame_ub_l[k]) / (double) (vm_group_bytes[k]), 1)
6015 							+ dml_ceil((double) (dpde0_bytes_per_frame_ub_c[k]) / (double) (vm_group_bytes[k]), 1)
6016 							+ dml_ceil((double) (meta_pte_bytes_per_frame_ub_l[k]) / (double) (vm_group_bytes[k]), 1)
6017 							+ dml_ceil((double) (meta_pte_bytes_per_frame_ub_c[k]) / (double) (vm_group_bytes[k]), 1);
6018 					} else {
6019 						num_group_per_lower_vm_stage =
6020 							dml_ceil((double) (dpde0_bytes_per_frame_ub_l[k]) / (double) (vm_group_bytes[k]), 1)
6021 							+ dml_ceil((double) (meta_pte_bytes_per_frame_ub_l[k]) / (double) (vm_group_bytes[k]), 1);
6022 					}
6023 				}
6024 			}
6025 
6026 			if (DCCEnable[k] == false) {
6027 				if (BytePerPixelDETC[k] > 0) {
6028 					num_req_per_lower_vm_stage = dpde0_bytes_per_frame_ub_l[k]
6029 							/ 64 + dpde0_bytes_per_frame_ub_c[k] / 64;
6030 				} else {
6031 					num_req_per_lower_vm_stage = dpde0_bytes_per_frame_ub_l[k]
6032 							/ 64;
6033 				}
6034 			} else {
6035 				if (GPUVMMaxPageTableLevels == 1) {
6036 					if (BytePerPixelDETC[k] > 0) {
6037 						num_req_per_lower_vm_stage = meta_pte_bytes_per_frame_ub_l[k] / 64
6038 							+ meta_pte_bytes_per_frame_ub_c[k] / 64;
6039 					} else {
6040 						num_req_per_lower_vm_stage = meta_pte_bytes_per_frame_ub_l[k] / 64;
6041 					}
6042 				} else {
6043 					if (BytePerPixelDETC[k] > 0) {
6044 						num_req_per_lower_vm_stage = dpde0_bytes_per_frame_ub_l[k] / 64
6045 							+ dpde0_bytes_per_frame_ub_c[k] / 64
6046 							+ meta_pte_bytes_per_frame_ub_l[k] / 64
6047 							+ meta_pte_bytes_per_frame_ub_c[k] / 64;
6048 					} else {
6049 						num_req_per_lower_vm_stage = dpde0_bytes_per_frame_ub_l[k] / 64
6050 							+ meta_pte_bytes_per_frame_ub_l[k] / 64;
6051 					}
6052 				}
6053 			}
6054 
6055 			TimePerVMGroupVBlank[k] = DestinationLinesToRequestVMInVBlank[k] * HTotal[k]
6056 					/ PixelClock[k] / num_group_per_lower_vm_stage;
6057 			TimePerVMGroupFlip[k] = DestinationLinesToRequestVMInImmediateFlip[k]
6058 					* HTotal[k] / PixelClock[k] / num_group_per_lower_vm_stage;
6059 			TimePerVMRequestVBlank[k] = DestinationLinesToRequestVMInVBlank[k]
6060 					* HTotal[k] / PixelClock[k] / num_req_per_lower_vm_stage;
6061 			TimePerVMRequestFlip[k] = DestinationLinesToRequestVMInImmediateFlip[k]
6062 					* HTotal[k] / PixelClock[k] / num_req_per_lower_vm_stage;
6063 
6064 			if (GPUVMMaxPageTableLevels > 2) {
6065 				TimePerVMGroupVBlank[k] = TimePerVMGroupVBlank[k] / 2;
6066 				TimePerVMGroupFlip[k] = TimePerVMGroupFlip[k] / 2;
6067 				TimePerVMRequestVBlank[k] = TimePerVMRequestVBlank[k] / 2;
6068 				TimePerVMRequestFlip[k] = TimePerVMRequestFlip[k] / 2;
6069 			}
6070 
6071 		} else {
6072 			TimePerVMGroupVBlank[k] = 0;
6073 			TimePerVMGroupFlip[k] = 0;
6074 			TimePerVMRequestVBlank[k] = 0;
6075 			TimePerVMRequestFlip[k] = 0;
6076 		}
6077 	}
6078 }
6079 
6080 static double CalculateExtraLatency(
6081 		double UrgentRoundTripAndOutOfOrderLatency,
6082 		int TotalNumberOfActiveDPP,
6083 		int PixelChunkSizeInKByte,
6084 		int TotalNumberOfDCCActiveDPP,
6085 		int MetaChunkSize,
6086 		double ReturnBW,
6087 		bool GPUVMEnable,
6088 		bool HostVMEnable,
6089 		int NumberOfActivePlanes,
6090 		int NumberOfDPP[],
6091 		long dpte_group_bytes[],
6092 		double PercentOfIdealDRAMFabricAndSDPPortBWReceivedAfterUrgLatencyPixelMixedWithVMData,
6093 		double PercentOfIdealDRAMFabricAndSDPPortBWReceivedAfterUrgLatencyVMDataOnly,
6094 		int HostVMMaxPageTableLevels,
6095 		int HostVMCachedPageTableLevels)
6096 {
6097 	double CalculateExtraLatency;
6098 	double HostVMInefficiencyFactor;
6099 	int HostVMDynamicLevels;
6100 
6101 	if (GPUVMEnable && HostVMEnable) {
6102 		HostVMInefficiencyFactor =
6103 				PercentOfIdealDRAMFabricAndSDPPortBWReceivedAfterUrgLatencyPixelMixedWithVMData
6104 						/ PercentOfIdealDRAMFabricAndSDPPortBWReceivedAfterUrgLatencyVMDataOnly;
6105 		HostVMDynamicLevels = HostVMMaxPageTableLevels - HostVMCachedPageTableLevels;
6106 	} else {
6107 		HostVMInefficiencyFactor = 1;
6108 		HostVMDynamicLevels = 0;
6109 	}
6110 
6111 	CalculateExtraLatency = UrgentRoundTripAndOutOfOrderLatency
6112 			+ (TotalNumberOfActiveDPP * PixelChunkSizeInKByte
6113 					+ TotalNumberOfDCCActiveDPP * MetaChunkSize) * 1024.0
6114 					/ ReturnBW;
6115 
6116 	if (GPUVMEnable) {
6117 		int k;
6118 
6119 		for (k = 0; k < NumberOfActivePlanes; k++) {
6120 			CalculateExtraLatency = CalculateExtraLatency
6121 					+ NumberOfDPP[k] * dpte_group_bytes[k]
6122 							* (1 + 8 * HostVMDynamicLevels)
6123 							* HostVMInefficiencyFactor / ReturnBW;
6124 		}
6125 	}
6126 	return CalculateExtraLatency;
6127 }
6128 
6129 #endif
6130