1 // SPDX-License-Identifier: MIT
2 /*
3  * Copyright © 2022 Intel Corporation
4  */
5 
6 #include <drm/drm_blend.h>
7 
8 #include "intel_atomic.h"
9 #include "intel_atomic_plane.h"
10 #include "intel_bw.h"
11 #include "intel_de.h"
12 #include "intel_display.h"
13 #include "intel_display_power.h"
14 #include "intel_display_types.h"
15 #include "intel_fb.h"
16 #include "skl_watermark.h"
17 
18 #include "i915_drv.h"
19 #include "i915_fixed.h"
20 #include "i915_reg.h"
21 #include "intel_pcode.h"
22 #include "intel_pm.h"
23 
24 static void skl_sagv_disable(struct drm_i915_private *i915);
25 
26 /* Stores plane specific WM parameters */
27 struct skl_wm_params {
28 	bool x_tiled, y_tiled;
29 	bool rc_surface;
30 	bool is_planar;
31 	u32 width;
32 	u8 cpp;
33 	u32 plane_pixel_rate;
34 	u32 y_min_scanlines;
35 	u32 plane_bytes_per_line;
36 	uint_fixed_16_16_t plane_blocks_per_line;
37 	uint_fixed_16_16_t y_tile_minimum;
38 	u32 linetime_us;
39 	u32 dbuf_block_size;
40 };
41 
42 u8 intel_enabled_dbuf_slices_mask(struct drm_i915_private *i915)
43 {
44 	u8 enabled_slices = 0;
45 	enum dbuf_slice slice;
46 
47 	for_each_dbuf_slice(i915, slice) {
48 		if (intel_uncore_read(&i915->uncore,
49 				      DBUF_CTL_S(slice)) & DBUF_POWER_STATE)
50 			enabled_slices |= BIT(slice);
51 	}
52 
53 	return enabled_slices;
54 }
55 
56 /*
57  * FIXME: We still don't have the proper code detect if we need to apply the WA,
58  * so assume we'll always need it in order to avoid underruns.
59  */
60 static bool skl_needs_memory_bw_wa(struct drm_i915_private *i915)
61 {
62 	return DISPLAY_VER(i915) == 9;
63 }
64 
65 static bool
66 intel_has_sagv(struct drm_i915_private *i915)
67 {
68 	return DISPLAY_VER(i915) >= 9 && !IS_LP(i915) &&
69 		i915->display.sagv.status != I915_SAGV_NOT_CONTROLLED;
70 }
71 
72 static u32
73 intel_sagv_block_time(struct drm_i915_private *i915)
74 {
75 	if (DISPLAY_VER(i915) >= 14) {
76 		u32 val;
77 
78 		val = intel_uncore_read(&i915->uncore, MTL_LATENCY_SAGV);
79 
80 		return REG_FIELD_GET(MTL_LATENCY_QCLK_SAGV, val);
81 	} else if (DISPLAY_VER(i915) >= 12) {
82 		u32 val = 0;
83 		int ret;
84 
85 		ret = snb_pcode_read(&i915->uncore,
86 				     GEN12_PCODE_READ_SAGV_BLOCK_TIME_US,
87 				     &val, NULL);
88 		if (ret) {
89 			drm_dbg_kms(&i915->drm, "Couldn't read SAGV block time!\n");
90 			return 0;
91 		}
92 
93 		return val;
94 	} else if (DISPLAY_VER(i915) == 11) {
95 		return 10;
96 	} else if (DISPLAY_VER(i915) == 9 && !IS_LP(i915)) {
97 		return 30;
98 	} else {
99 		return 0;
100 	}
101 }
102 
103 static void intel_sagv_init(struct drm_i915_private *i915)
104 {
105 	if (!intel_has_sagv(i915))
106 		i915->display.sagv.status = I915_SAGV_NOT_CONTROLLED;
107 
108 	/*
109 	 * Probe to see if we have working SAGV control.
110 	 * For icl+ this was already determined by intel_bw_init_hw().
111 	 */
112 	if (DISPLAY_VER(i915) < 11)
113 		skl_sagv_disable(i915);
114 
115 	drm_WARN_ON(&i915->drm, i915->display.sagv.status == I915_SAGV_UNKNOWN);
116 
117 	i915->display.sagv.block_time_us = intel_sagv_block_time(i915);
118 
119 	drm_dbg_kms(&i915->drm, "SAGV supported: %s, original SAGV block time: %u us\n",
120 		    str_yes_no(intel_has_sagv(i915)), i915->display.sagv.block_time_us);
121 
122 	/* avoid overflow when adding with wm0 latency/etc. */
123 	if (drm_WARN(&i915->drm, i915->display.sagv.block_time_us > U16_MAX,
124 		     "Excessive SAGV block time %u, ignoring\n",
125 		     i915->display.sagv.block_time_us))
126 		i915->display.sagv.block_time_us = 0;
127 
128 	if (!intel_has_sagv(i915))
129 		i915->display.sagv.block_time_us = 0;
130 }
131 
132 /*
133  * SAGV dynamically adjusts the system agent voltage and clock frequencies
134  * depending on power and performance requirements. The display engine access
135  * to system memory is blocked during the adjustment time. Because of the
136  * blocking time, having this enabled can cause full system hangs and/or pipe
137  * underruns if we don't meet all of the following requirements:
138  *
139  *  - <= 1 pipe enabled
140  *  - All planes can enable watermarks for latencies >= SAGV engine block time
141  *  - We're not using an interlaced display configuration
142  */
143 static void skl_sagv_enable(struct drm_i915_private *i915)
144 {
145 	int ret;
146 
147 	if (!intel_has_sagv(i915))
148 		return;
149 
150 	if (i915->display.sagv.status == I915_SAGV_ENABLED)
151 		return;
152 
153 	drm_dbg_kms(&i915->drm, "Enabling SAGV\n");
154 	ret = snb_pcode_write(&i915->uncore, GEN9_PCODE_SAGV_CONTROL,
155 			      GEN9_SAGV_ENABLE);
156 
157 	/* We don't need to wait for SAGV when enabling */
158 
159 	/*
160 	 * Some skl systems, pre-release machines in particular,
161 	 * don't actually have SAGV.
162 	 */
163 	if (IS_SKYLAKE(i915) && ret == -ENXIO) {
164 		drm_dbg(&i915->drm, "No SAGV found on system, ignoring\n");
165 		i915->display.sagv.status = I915_SAGV_NOT_CONTROLLED;
166 		return;
167 	} else if (ret < 0) {
168 		drm_err(&i915->drm, "Failed to enable SAGV\n");
169 		return;
170 	}
171 
172 	i915->display.sagv.status = I915_SAGV_ENABLED;
173 }
174 
175 static void skl_sagv_disable(struct drm_i915_private *i915)
176 {
177 	int ret;
178 
179 	if (!intel_has_sagv(i915))
180 		return;
181 
182 	if (i915->display.sagv.status == I915_SAGV_DISABLED)
183 		return;
184 
185 	drm_dbg_kms(&i915->drm, "Disabling SAGV\n");
186 	/* bspec says to keep retrying for at least 1 ms */
187 	ret = skl_pcode_request(&i915->uncore, GEN9_PCODE_SAGV_CONTROL,
188 				GEN9_SAGV_DISABLE,
189 				GEN9_SAGV_IS_DISABLED, GEN9_SAGV_IS_DISABLED,
190 				1);
191 	/*
192 	 * Some skl systems, pre-release machines in particular,
193 	 * don't actually have SAGV.
194 	 */
195 	if (IS_SKYLAKE(i915) && ret == -ENXIO) {
196 		drm_dbg(&i915->drm, "No SAGV found on system, ignoring\n");
197 		i915->display.sagv.status = I915_SAGV_NOT_CONTROLLED;
198 		return;
199 	} else if (ret < 0) {
200 		drm_err(&i915->drm, "Failed to disable SAGV (%d)\n", ret);
201 		return;
202 	}
203 
204 	i915->display.sagv.status = I915_SAGV_DISABLED;
205 }
206 
207 static void skl_sagv_pre_plane_update(struct intel_atomic_state *state)
208 {
209 	struct drm_i915_private *i915 = to_i915(state->base.dev);
210 	const struct intel_bw_state *new_bw_state =
211 		intel_atomic_get_new_bw_state(state);
212 
213 	if (!new_bw_state)
214 		return;
215 
216 	if (!intel_can_enable_sagv(i915, new_bw_state))
217 		skl_sagv_disable(i915);
218 }
219 
220 static void skl_sagv_post_plane_update(struct intel_atomic_state *state)
221 {
222 	struct drm_i915_private *i915 = to_i915(state->base.dev);
223 	const struct intel_bw_state *new_bw_state =
224 		intel_atomic_get_new_bw_state(state);
225 
226 	if (!new_bw_state)
227 		return;
228 
229 	if (intel_can_enable_sagv(i915, new_bw_state))
230 		skl_sagv_enable(i915);
231 }
232 
233 static void icl_sagv_pre_plane_update(struct intel_atomic_state *state)
234 {
235 	struct drm_i915_private *i915 = to_i915(state->base.dev);
236 	const struct intel_bw_state *old_bw_state =
237 		intel_atomic_get_old_bw_state(state);
238 	const struct intel_bw_state *new_bw_state =
239 		intel_atomic_get_new_bw_state(state);
240 	u16 old_mask, new_mask;
241 
242 	if (!new_bw_state)
243 		return;
244 
245 	old_mask = old_bw_state->qgv_points_mask;
246 	new_mask = old_bw_state->qgv_points_mask | new_bw_state->qgv_points_mask;
247 
248 	if (old_mask == new_mask)
249 		return;
250 
251 	WARN_ON(!new_bw_state->base.changed);
252 
253 	drm_dbg_kms(&i915->drm, "Restricting QGV points: 0x%x -> 0x%x\n",
254 		    old_mask, new_mask);
255 
256 	/*
257 	 * Restrict required qgv points before updating the configuration.
258 	 * According to BSpec we can't mask and unmask qgv points at the same
259 	 * time. Also masking should be done before updating the configuration
260 	 * and unmasking afterwards.
261 	 */
262 	icl_pcode_restrict_qgv_points(i915, new_mask);
263 }
264 
265 static void icl_sagv_post_plane_update(struct intel_atomic_state *state)
266 {
267 	struct drm_i915_private *i915 = to_i915(state->base.dev);
268 	const struct intel_bw_state *old_bw_state =
269 		intel_atomic_get_old_bw_state(state);
270 	const struct intel_bw_state *new_bw_state =
271 		intel_atomic_get_new_bw_state(state);
272 	u16 old_mask, new_mask;
273 
274 	if (!new_bw_state)
275 		return;
276 
277 	old_mask = old_bw_state->qgv_points_mask | new_bw_state->qgv_points_mask;
278 	new_mask = new_bw_state->qgv_points_mask;
279 
280 	if (old_mask == new_mask)
281 		return;
282 
283 	WARN_ON(!new_bw_state->base.changed);
284 
285 	drm_dbg_kms(&i915->drm, "Relaxing QGV points: 0x%x -> 0x%x\n",
286 		    old_mask, new_mask);
287 
288 	/*
289 	 * Allow required qgv points after updating the configuration.
290 	 * According to BSpec we can't mask and unmask qgv points at the same
291 	 * time. Also masking should be done before updating the configuration
292 	 * and unmasking afterwards.
293 	 */
294 	icl_pcode_restrict_qgv_points(i915, new_mask);
295 }
296 
297 void intel_sagv_pre_plane_update(struct intel_atomic_state *state)
298 {
299 	struct drm_i915_private *i915 = to_i915(state->base.dev);
300 
301 	/*
302 	 * Just return if we can't control SAGV or don't have it.
303 	 * This is different from situation when we have SAGV but just can't
304 	 * afford it due to DBuf limitation - in case if SAGV is completely
305 	 * disabled in a BIOS, we are not even allowed to send a PCode request,
306 	 * as it will throw an error. So have to check it here.
307 	 */
308 	if (!intel_has_sagv(i915))
309 		return;
310 
311 	if (DISPLAY_VER(i915) >= 11)
312 		icl_sagv_pre_plane_update(state);
313 	else
314 		skl_sagv_pre_plane_update(state);
315 }
316 
317 void intel_sagv_post_plane_update(struct intel_atomic_state *state)
318 {
319 	struct drm_i915_private *i915 = to_i915(state->base.dev);
320 
321 	/*
322 	 * Just return if we can't control SAGV or don't have it.
323 	 * This is different from situation when we have SAGV but just can't
324 	 * afford it due to DBuf limitation - in case if SAGV is completely
325 	 * disabled in a BIOS, we are not even allowed to send a PCode request,
326 	 * as it will throw an error. So have to check it here.
327 	 */
328 	if (!intel_has_sagv(i915))
329 		return;
330 
331 	if (DISPLAY_VER(i915) >= 11)
332 		icl_sagv_post_plane_update(state);
333 	else
334 		skl_sagv_post_plane_update(state);
335 }
336 
337 static bool skl_crtc_can_enable_sagv(const struct intel_crtc_state *crtc_state)
338 {
339 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
340 	struct drm_i915_private *i915 = to_i915(crtc->base.dev);
341 	enum plane_id plane_id;
342 	int max_level = INT_MAX;
343 
344 	if (!intel_has_sagv(i915))
345 		return false;
346 
347 	if (!crtc_state->hw.active)
348 		return true;
349 
350 	if (crtc_state->hw.pipe_mode.flags & DRM_MODE_FLAG_INTERLACE)
351 		return false;
352 
353 	for_each_plane_id_on_crtc(crtc, plane_id) {
354 		const struct skl_plane_wm *wm =
355 			&crtc_state->wm.skl.optimal.planes[plane_id];
356 		int level;
357 
358 		/* Skip this plane if it's not enabled */
359 		if (!wm->wm[0].enable)
360 			continue;
361 
362 		/* Find the highest enabled wm level for this plane */
363 		for (level = ilk_wm_max_level(i915);
364 		     !wm->wm[level].enable; --level)
365 		     { }
366 
367 		/* Highest common enabled wm level for all planes */
368 		max_level = min(level, max_level);
369 	}
370 
371 	/* No enabled planes? */
372 	if (max_level == INT_MAX)
373 		return true;
374 
375 	for_each_plane_id_on_crtc(crtc, plane_id) {
376 		const struct skl_plane_wm *wm =
377 			&crtc_state->wm.skl.optimal.planes[plane_id];
378 
379 		/*
380 		 * All enabled planes must have enabled a common wm level that
381 		 * can tolerate memory latencies higher than sagv_block_time_us
382 		 */
383 		if (wm->wm[0].enable && !wm->wm[max_level].can_sagv)
384 			return false;
385 	}
386 
387 	return true;
388 }
389 
390 static bool tgl_crtc_can_enable_sagv(const struct intel_crtc_state *crtc_state)
391 {
392 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
393 	enum plane_id plane_id;
394 
395 	if (!crtc_state->hw.active)
396 		return true;
397 
398 	for_each_plane_id_on_crtc(crtc, plane_id) {
399 		const struct skl_plane_wm *wm =
400 			&crtc_state->wm.skl.optimal.planes[plane_id];
401 
402 		if (wm->wm[0].enable && !wm->sagv.wm0.enable)
403 			return false;
404 	}
405 
406 	return true;
407 }
408 
409 static bool intel_crtc_can_enable_sagv(const struct intel_crtc_state *crtc_state)
410 {
411 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
412 	struct drm_i915_private *i915 = to_i915(crtc->base.dev);
413 
414 	if (DISPLAY_VER(i915) >= 12)
415 		return tgl_crtc_can_enable_sagv(crtc_state);
416 	else
417 		return skl_crtc_can_enable_sagv(crtc_state);
418 }
419 
420 bool intel_can_enable_sagv(struct drm_i915_private *i915,
421 			   const struct intel_bw_state *bw_state)
422 {
423 	if (DISPLAY_VER(i915) < 11 &&
424 	    bw_state->active_pipes && !is_power_of_2(bw_state->active_pipes))
425 		return false;
426 
427 	return bw_state->pipe_sagv_reject == 0;
428 }
429 
430 static int intel_compute_sagv_mask(struct intel_atomic_state *state)
431 {
432 	struct drm_i915_private *i915 = to_i915(state->base.dev);
433 	int ret;
434 	struct intel_crtc *crtc;
435 	struct intel_crtc_state *new_crtc_state;
436 	struct intel_bw_state *new_bw_state = NULL;
437 	const struct intel_bw_state *old_bw_state = NULL;
438 	int i;
439 
440 	for_each_new_intel_crtc_in_state(state, crtc,
441 					 new_crtc_state, i) {
442 		new_bw_state = intel_atomic_get_bw_state(state);
443 		if (IS_ERR(new_bw_state))
444 			return PTR_ERR(new_bw_state);
445 
446 		old_bw_state = intel_atomic_get_old_bw_state(state);
447 
448 		if (intel_crtc_can_enable_sagv(new_crtc_state))
449 			new_bw_state->pipe_sagv_reject &= ~BIT(crtc->pipe);
450 		else
451 			new_bw_state->pipe_sagv_reject |= BIT(crtc->pipe);
452 	}
453 
454 	if (!new_bw_state)
455 		return 0;
456 
457 	new_bw_state->active_pipes =
458 		intel_calc_active_pipes(state, old_bw_state->active_pipes);
459 
460 	if (new_bw_state->active_pipes != old_bw_state->active_pipes) {
461 		ret = intel_atomic_lock_global_state(&new_bw_state->base);
462 		if (ret)
463 			return ret;
464 	}
465 
466 	if (intel_can_enable_sagv(i915, new_bw_state) !=
467 	    intel_can_enable_sagv(i915, old_bw_state)) {
468 		ret = intel_atomic_serialize_global_state(&new_bw_state->base);
469 		if (ret)
470 			return ret;
471 	} else if (new_bw_state->pipe_sagv_reject != old_bw_state->pipe_sagv_reject) {
472 		ret = intel_atomic_lock_global_state(&new_bw_state->base);
473 		if (ret)
474 			return ret;
475 	}
476 
477 	for_each_new_intel_crtc_in_state(state, crtc,
478 					 new_crtc_state, i) {
479 		struct skl_pipe_wm *pipe_wm = &new_crtc_state->wm.skl.optimal;
480 
481 		/*
482 		 * We store use_sagv_wm in the crtc state rather than relying on
483 		 * that bw state since we have no convenient way to get at the
484 		 * latter from the plane commit hooks (especially in the legacy
485 		 * cursor case)
486 		 */
487 		pipe_wm->use_sagv_wm = !HAS_HW_SAGV_WM(i915) &&
488 			DISPLAY_VER(i915) >= 12 &&
489 			intel_can_enable_sagv(i915, new_bw_state);
490 	}
491 
492 	return 0;
493 }
494 
495 static u16 skl_ddb_entry_init(struct skl_ddb_entry *entry,
496 			      u16 start, u16 end)
497 {
498 	entry->start = start;
499 	entry->end = end;
500 
501 	return end;
502 }
503 
504 static int intel_dbuf_slice_size(struct drm_i915_private *i915)
505 {
506 	return INTEL_INFO(i915)->display.dbuf.size /
507 		hweight8(INTEL_INFO(i915)->display.dbuf.slice_mask);
508 }
509 
510 static void
511 skl_ddb_entry_for_slices(struct drm_i915_private *i915, u8 slice_mask,
512 			 struct skl_ddb_entry *ddb)
513 {
514 	int slice_size = intel_dbuf_slice_size(i915);
515 
516 	if (!slice_mask) {
517 		ddb->start = 0;
518 		ddb->end = 0;
519 		return;
520 	}
521 
522 	ddb->start = (ffs(slice_mask) - 1) * slice_size;
523 	ddb->end = fls(slice_mask) * slice_size;
524 
525 	WARN_ON(ddb->start >= ddb->end);
526 	WARN_ON(ddb->end > INTEL_INFO(i915)->display.dbuf.size);
527 }
528 
529 static unsigned int mbus_ddb_offset(struct drm_i915_private *i915, u8 slice_mask)
530 {
531 	struct skl_ddb_entry ddb;
532 
533 	if (slice_mask & (BIT(DBUF_S1) | BIT(DBUF_S2)))
534 		slice_mask = BIT(DBUF_S1);
535 	else if (slice_mask & (BIT(DBUF_S3) | BIT(DBUF_S4)))
536 		slice_mask = BIT(DBUF_S3);
537 
538 	skl_ddb_entry_for_slices(i915, slice_mask, &ddb);
539 
540 	return ddb.start;
541 }
542 
543 u32 skl_ddb_dbuf_slice_mask(struct drm_i915_private *i915,
544 			    const struct skl_ddb_entry *entry)
545 {
546 	int slice_size = intel_dbuf_slice_size(i915);
547 	enum dbuf_slice start_slice, end_slice;
548 	u8 slice_mask = 0;
549 
550 	if (!skl_ddb_entry_size(entry))
551 		return 0;
552 
553 	start_slice = entry->start / slice_size;
554 	end_slice = (entry->end - 1) / slice_size;
555 
556 	/*
557 	 * Per plane DDB entry can in a really worst case be on multiple slices
558 	 * but single entry is anyway contigious.
559 	 */
560 	while (start_slice <= end_slice) {
561 		slice_mask |= BIT(start_slice);
562 		start_slice++;
563 	}
564 
565 	return slice_mask;
566 }
567 
568 static unsigned int intel_crtc_ddb_weight(const struct intel_crtc_state *crtc_state)
569 {
570 	const struct drm_display_mode *pipe_mode = &crtc_state->hw.pipe_mode;
571 	int hdisplay, vdisplay;
572 
573 	if (!crtc_state->hw.active)
574 		return 0;
575 
576 	/*
577 	 * Watermark/ddb requirement highly depends upon width of the
578 	 * framebuffer, So instead of allocating DDB equally among pipes
579 	 * distribute DDB based on resolution/width of the display.
580 	 */
581 	drm_mode_get_hv_timing(pipe_mode, &hdisplay, &vdisplay);
582 
583 	return hdisplay;
584 }
585 
586 static void intel_crtc_dbuf_weights(const struct intel_dbuf_state *dbuf_state,
587 				    enum pipe for_pipe,
588 				    unsigned int *weight_start,
589 				    unsigned int *weight_end,
590 				    unsigned int *weight_total)
591 {
592 	struct drm_i915_private *i915 =
593 		to_i915(dbuf_state->base.state->base.dev);
594 	enum pipe pipe;
595 
596 	*weight_start = 0;
597 	*weight_end = 0;
598 	*weight_total = 0;
599 
600 	for_each_pipe(i915, pipe) {
601 		int weight = dbuf_state->weight[pipe];
602 
603 		/*
604 		 * Do not account pipes using other slice sets
605 		 * luckily as of current BSpec slice sets do not partially
606 		 * intersect(pipes share either same one slice or same slice set
607 		 * i.e no partial intersection), so it is enough to check for
608 		 * equality for now.
609 		 */
610 		if (dbuf_state->slices[pipe] != dbuf_state->slices[for_pipe])
611 			continue;
612 
613 		*weight_total += weight;
614 		if (pipe < for_pipe) {
615 			*weight_start += weight;
616 			*weight_end += weight;
617 		} else if (pipe == for_pipe) {
618 			*weight_end += weight;
619 		}
620 	}
621 }
622 
623 static int
624 skl_crtc_allocate_ddb(struct intel_atomic_state *state, struct intel_crtc *crtc)
625 {
626 	struct drm_i915_private *i915 = to_i915(crtc->base.dev);
627 	unsigned int weight_total, weight_start, weight_end;
628 	const struct intel_dbuf_state *old_dbuf_state =
629 		intel_atomic_get_old_dbuf_state(state);
630 	struct intel_dbuf_state *new_dbuf_state =
631 		intel_atomic_get_new_dbuf_state(state);
632 	struct intel_crtc_state *crtc_state;
633 	struct skl_ddb_entry ddb_slices;
634 	enum pipe pipe = crtc->pipe;
635 	unsigned int mbus_offset = 0;
636 	u32 ddb_range_size;
637 	u32 dbuf_slice_mask;
638 	u32 start, end;
639 	int ret;
640 
641 	if (new_dbuf_state->weight[pipe] == 0) {
642 		skl_ddb_entry_init(&new_dbuf_state->ddb[pipe], 0, 0);
643 		goto out;
644 	}
645 
646 	dbuf_slice_mask = new_dbuf_state->slices[pipe];
647 
648 	skl_ddb_entry_for_slices(i915, dbuf_slice_mask, &ddb_slices);
649 	mbus_offset = mbus_ddb_offset(i915, dbuf_slice_mask);
650 	ddb_range_size = skl_ddb_entry_size(&ddb_slices);
651 
652 	intel_crtc_dbuf_weights(new_dbuf_state, pipe,
653 				&weight_start, &weight_end, &weight_total);
654 
655 	start = ddb_range_size * weight_start / weight_total;
656 	end = ddb_range_size * weight_end / weight_total;
657 
658 	skl_ddb_entry_init(&new_dbuf_state->ddb[pipe],
659 			   ddb_slices.start - mbus_offset + start,
660 			   ddb_slices.start - mbus_offset + end);
661 
662 out:
663 	if (old_dbuf_state->slices[pipe] == new_dbuf_state->slices[pipe] &&
664 	    skl_ddb_entry_equal(&old_dbuf_state->ddb[pipe],
665 				&new_dbuf_state->ddb[pipe]))
666 		return 0;
667 
668 	ret = intel_atomic_lock_global_state(&new_dbuf_state->base);
669 	if (ret)
670 		return ret;
671 
672 	crtc_state = intel_atomic_get_crtc_state(&state->base, crtc);
673 	if (IS_ERR(crtc_state))
674 		return PTR_ERR(crtc_state);
675 
676 	/*
677 	 * Used for checking overlaps, so we need absolute
678 	 * offsets instead of MBUS relative offsets.
679 	 */
680 	crtc_state->wm.skl.ddb.start = mbus_offset + new_dbuf_state->ddb[pipe].start;
681 	crtc_state->wm.skl.ddb.end = mbus_offset + new_dbuf_state->ddb[pipe].end;
682 
683 	drm_dbg_kms(&i915->drm,
684 		    "[CRTC:%d:%s] dbuf slices 0x%x -> 0x%x, ddb (%d - %d) -> (%d - %d), active pipes 0x%x -> 0x%x\n",
685 		    crtc->base.base.id, crtc->base.name,
686 		    old_dbuf_state->slices[pipe], new_dbuf_state->slices[pipe],
687 		    old_dbuf_state->ddb[pipe].start, old_dbuf_state->ddb[pipe].end,
688 		    new_dbuf_state->ddb[pipe].start, new_dbuf_state->ddb[pipe].end,
689 		    old_dbuf_state->active_pipes, new_dbuf_state->active_pipes);
690 
691 	return 0;
692 }
693 
694 static int skl_compute_wm_params(const struct intel_crtc_state *crtc_state,
695 				 int width, const struct drm_format_info *format,
696 				 u64 modifier, unsigned int rotation,
697 				 u32 plane_pixel_rate, struct skl_wm_params *wp,
698 				 int color_plane);
699 
700 static void skl_compute_plane_wm(const struct intel_crtc_state *crtc_state,
701 				 struct intel_plane *plane,
702 				 int level,
703 				 unsigned int latency,
704 				 const struct skl_wm_params *wp,
705 				 const struct skl_wm_level *result_prev,
706 				 struct skl_wm_level *result /* out */);
707 
708 static unsigned int
709 skl_cursor_allocation(const struct intel_crtc_state *crtc_state,
710 		      int num_active)
711 {
712 	struct intel_plane *plane = to_intel_plane(crtc_state->uapi.crtc->cursor);
713 	struct drm_i915_private *i915 = to_i915(crtc_state->uapi.crtc->dev);
714 	int level, max_level = ilk_wm_max_level(i915);
715 	struct skl_wm_level wm = {};
716 	int ret, min_ddb_alloc = 0;
717 	struct skl_wm_params wp;
718 
719 	ret = skl_compute_wm_params(crtc_state, 256,
720 				    drm_format_info(DRM_FORMAT_ARGB8888),
721 				    DRM_FORMAT_MOD_LINEAR,
722 				    DRM_MODE_ROTATE_0,
723 				    crtc_state->pixel_rate, &wp, 0);
724 	drm_WARN_ON(&i915->drm, ret);
725 
726 	for (level = 0; level <= max_level; level++) {
727 		unsigned int latency = i915->display.wm.skl_latency[level];
728 
729 		skl_compute_plane_wm(crtc_state, plane, level, latency, &wp, &wm, &wm);
730 		if (wm.min_ddb_alloc == U16_MAX)
731 			break;
732 
733 		min_ddb_alloc = wm.min_ddb_alloc;
734 	}
735 
736 	return max(num_active == 1 ? 32 : 8, min_ddb_alloc);
737 }
738 
739 static void skl_ddb_entry_init_from_hw(struct skl_ddb_entry *entry, u32 reg)
740 {
741 	skl_ddb_entry_init(entry,
742 			   REG_FIELD_GET(PLANE_BUF_START_MASK, reg),
743 			   REG_FIELD_GET(PLANE_BUF_END_MASK, reg));
744 	if (entry->end)
745 		entry->end++;
746 }
747 
748 static void
749 skl_ddb_get_hw_plane_state(struct drm_i915_private *i915,
750 			   const enum pipe pipe,
751 			   const enum plane_id plane_id,
752 			   struct skl_ddb_entry *ddb,
753 			   struct skl_ddb_entry *ddb_y)
754 {
755 	u32 val;
756 
757 	/* Cursor doesn't support NV12/planar, so no extra calculation needed */
758 	if (plane_id == PLANE_CURSOR) {
759 		val = intel_uncore_read(&i915->uncore, CUR_BUF_CFG(pipe));
760 		skl_ddb_entry_init_from_hw(ddb, val);
761 		return;
762 	}
763 
764 	val = intel_uncore_read(&i915->uncore, PLANE_BUF_CFG(pipe, plane_id));
765 	skl_ddb_entry_init_from_hw(ddb, val);
766 
767 	if (DISPLAY_VER(i915) >= 11)
768 		return;
769 
770 	val = intel_uncore_read(&i915->uncore, PLANE_NV12_BUF_CFG(pipe, plane_id));
771 	skl_ddb_entry_init_from_hw(ddb_y, val);
772 }
773 
774 static void skl_pipe_ddb_get_hw_state(struct intel_crtc *crtc,
775 				      struct skl_ddb_entry *ddb,
776 				      struct skl_ddb_entry *ddb_y)
777 {
778 	struct drm_i915_private *i915 = to_i915(crtc->base.dev);
779 	enum intel_display_power_domain power_domain;
780 	enum pipe pipe = crtc->pipe;
781 	intel_wakeref_t wakeref;
782 	enum plane_id plane_id;
783 
784 	power_domain = POWER_DOMAIN_PIPE(pipe);
785 	wakeref = intel_display_power_get_if_enabled(i915, power_domain);
786 	if (!wakeref)
787 		return;
788 
789 	for_each_plane_id_on_crtc(crtc, plane_id)
790 		skl_ddb_get_hw_plane_state(i915, pipe,
791 					   plane_id,
792 					   &ddb[plane_id],
793 					   &ddb_y[plane_id]);
794 
795 	intel_display_power_put(i915, power_domain, wakeref);
796 }
797 
798 struct dbuf_slice_conf_entry {
799 	u8 active_pipes;
800 	u8 dbuf_mask[I915_MAX_PIPES];
801 	bool join_mbus;
802 };
803 
804 /*
805  * Table taken from Bspec 12716
806  * Pipes do have some preferred DBuf slice affinity,
807  * plus there are some hardcoded requirements on how
808  * those should be distributed for multipipe scenarios.
809  * For more DBuf slices algorithm can get even more messy
810  * and less readable, so decided to use a table almost
811  * as is from BSpec itself - that way it is at least easier
812  * to compare, change and check.
813  */
814 static const struct dbuf_slice_conf_entry icl_allowed_dbufs[] =
815 /* Autogenerated with igt/tools/intel_dbuf_map tool: */
816 {
817 	{
818 		.active_pipes = BIT(PIPE_A),
819 		.dbuf_mask = {
820 			[PIPE_A] = BIT(DBUF_S1),
821 		},
822 	},
823 	{
824 		.active_pipes = BIT(PIPE_B),
825 		.dbuf_mask = {
826 			[PIPE_B] = BIT(DBUF_S1),
827 		},
828 	},
829 	{
830 		.active_pipes = BIT(PIPE_A) | BIT(PIPE_B),
831 		.dbuf_mask = {
832 			[PIPE_A] = BIT(DBUF_S1),
833 			[PIPE_B] = BIT(DBUF_S2),
834 		},
835 	},
836 	{
837 		.active_pipes = BIT(PIPE_C),
838 		.dbuf_mask = {
839 			[PIPE_C] = BIT(DBUF_S2),
840 		},
841 	},
842 	{
843 		.active_pipes = BIT(PIPE_A) | BIT(PIPE_C),
844 		.dbuf_mask = {
845 			[PIPE_A] = BIT(DBUF_S1),
846 			[PIPE_C] = BIT(DBUF_S2),
847 		},
848 	},
849 	{
850 		.active_pipes = BIT(PIPE_B) | BIT(PIPE_C),
851 		.dbuf_mask = {
852 			[PIPE_B] = BIT(DBUF_S1),
853 			[PIPE_C] = BIT(DBUF_S2),
854 		},
855 	},
856 	{
857 		.active_pipes = BIT(PIPE_A) | BIT(PIPE_B) | BIT(PIPE_C),
858 		.dbuf_mask = {
859 			[PIPE_A] = BIT(DBUF_S1),
860 			[PIPE_B] = BIT(DBUF_S1),
861 			[PIPE_C] = BIT(DBUF_S2),
862 		},
863 	},
864 	{}
865 };
866 
867 /*
868  * Table taken from Bspec 49255
869  * Pipes do have some preferred DBuf slice affinity,
870  * plus there are some hardcoded requirements on how
871  * those should be distributed for multipipe scenarios.
872  * For more DBuf slices algorithm can get even more messy
873  * and less readable, so decided to use a table almost
874  * as is from BSpec itself - that way it is at least easier
875  * to compare, change and check.
876  */
877 static const struct dbuf_slice_conf_entry tgl_allowed_dbufs[] =
878 /* Autogenerated with igt/tools/intel_dbuf_map tool: */
879 {
880 	{
881 		.active_pipes = BIT(PIPE_A),
882 		.dbuf_mask = {
883 			[PIPE_A] = BIT(DBUF_S1) | BIT(DBUF_S2),
884 		},
885 	},
886 	{
887 		.active_pipes = BIT(PIPE_B),
888 		.dbuf_mask = {
889 			[PIPE_B] = BIT(DBUF_S1) | BIT(DBUF_S2),
890 		},
891 	},
892 	{
893 		.active_pipes = BIT(PIPE_A) | BIT(PIPE_B),
894 		.dbuf_mask = {
895 			[PIPE_A] = BIT(DBUF_S2),
896 			[PIPE_B] = BIT(DBUF_S1),
897 		},
898 	},
899 	{
900 		.active_pipes = BIT(PIPE_C),
901 		.dbuf_mask = {
902 			[PIPE_C] = BIT(DBUF_S2) | BIT(DBUF_S1),
903 		},
904 	},
905 	{
906 		.active_pipes = BIT(PIPE_A) | BIT(PIPE_C),
907 		.dbuf_mask = {
908 			[PIPE_A] = BIT(DBUF_S1),
909 			[PIPE_C] = BIT(DBUF_S2),
910 		},
911 	},
912 	{
913 		.active_pipes = BIT(PIPE_B) | BIT(PIPE_C),
914 		.dbuf_mask = {
915 			[PIPE_B] = BIT(DBUF_S1),
916 			[PIPE_C] = BIT(DBUF_S2),
917 		},
918 	},
919 	{
920 		.active_pipes = BIT(PIPE_A) | BIT(PIPE_B) | BIT(PIPE_C),
921 		.dbuf_mask = {
922 			[PIPE_A] = BIT(DBUF_S1),
923 			[PIPE_B] = BIT(DBUF_S1),
924 			[PIPE_C] = BIT(DBUF_S2),
925 		},
926 	},
927 	{
928 		.active_pipes = BIT(PIPE_D),
929 		.dbuf_mask = {
930 			[PIPE_D] = BIT(DBUF_S2) | BIT(DBUF_S1),
931 		},
932 	},
933 	{
934 		.active_pipes = BIT(PIPE_A) | BIT(PIPE_D),
935 		.dbuf_mask = {
936 			[PIPE_A] = BIT(DBUF_S1),
937 			[PIPE_D] = BIT(DBUF_S2),
938 		},
939 	},
940 	{
941 		.active_pipes = BIT(PIPE_B) | BIT(PIPE_D),
942 		.dbuf_mask = {
943 			[PIPE_B] = BIT(DBUF_S1),
944 			[PIPE_D] = BIT(DBUF_S2),
945 		},
946 	},
947 	{
948 		.active_pipes = BIT(PIPE_A) | BIT(PIPE_B) | BIT(PIPE_D),
949 		.dbuf_mask = {
950 			[PIPE_A] = BIT(DBUF_S1),
951 			[PIPE_B] = BIT(DBUF_S1),
952 			[PIPE_D] = BIT(DBUF_S2),
953 		},
954 	},
955 	{
956 		.active_pipes = BIT(PIPE_C) | BIT(PIPE_D),
957 		.dbuf_mask = {
958 			[PIPE_C] = BIT(DBUF_S1),
959 			[PIPE_D] = BIT(DBUF_S2),
960 		},
961 	},
962 	{
963 		.active_pipes = BIT(PIPE_A) | BIT(PIPE_C) | BIT(PIPE_D),
964 		.dbuf_mask = {
965 			[PIPE_A] = BIT(DBUF_S1),
966 			[PIPE_C] = BIT(DBUF_S2),
967 			[PIPE_D] = BIT(DBUF_S2),
968 		},
969 	},
970 	{
971 		.active_pipes = BIT(PIPE_B) | BIT(PIPE_C) | BIT(PIPE_D),
972 		.dbuf_mask = {
973 			[PIPE_B] = BIT(DBUF_S1),
974 			[PIPE_C] = BIT(DBUF_S2),
975 			[PIPE_D] = BIT(DBUF_S2),
976 		},
977 	},
978 	{
979 		.active_pipes = BIT(PIPE_A) | BIT(PIPE_B) | BIT(PIPE_C) | BIT(PIPE_D),
980 		.dbuf_mask = {
981 			[PIPE_A] = BIT(DBUF_S1),
982 			[PIPE_B] = BIT(DBUF_S1),
983 			[PIPE_C] = BIT(DBUF_S2),
984 			[PIPE_D] = BIT(DBUF_S2),
985 		},
986 	},
987 	{}
988 };
989 
990 static const struct dbuf_slice_conf_entry dg2_allowed_dbufs[] = {
991 	{
992 		.active_pipes = BIT(PIPE_A),
993 		.dbuf_mask = {
994 			[PIPE_A] = BIT(DBUF_S1) | BIT(DBUF_S2),
995 		},
996 	},
997 	{
998 		.active_pipes = BIT(PIPE_B),
999 		.dbuf_mask = {
1000 			[PIPE_B] = BIT(DBUF_S1) | BIT(DBUF_S2),
1001 		},
1002 	},
1003 	{
1004 		.active_pipes = BIT(PIPE_A) | BIT(PIPE_B),
1005 		.dbuf_mask = {
1006 			[PIPE_A] = BIT(DBUF_S1),
1007 			[PIPE_B] = BIT(DBUF_S2),
1008 		},
1009 	},
1010 	{
1011 		.active_pipes = BIT(PIPE_C),
1012 		.dbuf_mask = {
1013 			[PIPE_C] = BIT(DBUF_S3) | BIT(DBUF_S4),
1014 		},
1015 	},
1016 	{
1017 		.active_pipes = BIT(PIPE_A) | BIT(PIPE_C),
1018 		.dbuf_mask = {
1019 			[PIPE_A] = BIT(DBUF_S1) | BIT(DBUF_S2),
1020 			[PIPE_C] = BIT(DBUF_S3) | BIT(DBUF_S4),
1021 		},
1022 	},
1023 	{
1024 		.active_pipes = BIT(PIPE_B) | BIT(PIPE_C),
1025 		.dbuf_mask = {
1026 			[PIPE_B] = BIT(DBUF_S1) | BIT(DBUF_S2),
1027 			[PIPE_C] = BIT(DBUF_S3) | BIT(DBUF_S4),
1028 		},
1029 	},
1030 	{
1031 		.active_pipes = BIT(PIPE_A) | BIT(PIPE_B) | BIT(PIPE_C),
1032 		.dbuf_mask = {
1033 			[PIPE_A] = BIT(DBUF_S1),
1034 			[PIPE_B] = BIT(DBUF_S2),
1035 			[PIPE_C] = BIT(DBUF_S3) | BIT(DBUF_S4),
1036 		},
1037 	},
1038 	{
1039 		.active_pipes = BIT(PIPE_D),
1040 		.dbuf_mask = {
1041 			[PIPE_D] = BIT(DBUF_S3) | BIT(DBUF_S4),
1042 		},
1043 	},
1044 	{
1045 		.active_pipes = BIT(PIPE_A) | BIT(PIPE_D),
1046 		.dbuf_mask = {
1047 			[PIPE_A] = BIT(DBUF_S1) | BIT(DBUF_S2),
1048 			[PIPE_D] = BIT(DBUF_S3) | BIT(DBUF_S4),
1049 		},
1050 	},
1051 	{
1052 		.active_pipes = BIT(PIPE_B) | BIT(PIPE_D),
1053 		.dbuf_mask = {
1054 			[PIPE_B] = BIT(DBUF_S1) | BIT(DBUF_S2),
1055 			[PIPE_D] = BIT(DBUF_S3) | BIT(DBUF_S4),
1056 		},
1057 	},
1058 	{
1059 		.active_pipes = BIT(PIPE_A) | BIT(PIPE_B) | BIT(PIPE_D),
1060 		.dbuf_mask = {
1061 			[PIPE_A] = BIT(DBUF_S1),
1062 			[PIPE_B] = BIT(DBUF_S2),
1063 			[PIPE_D] = BIT(DBUF_S3) | BIT(DBUF_S4),
1064 		},
1065 	},
1066 	{
1067 		.active_pipes = BIT(PIPE_C) | BIT(PIPE_D),
1068 		.dbuf_mask = {
1069 			[PIPE_C] = BIT(DBUF_S3),
1070 			[PIPE_D] = BIT(DBUF_S4),
1071 		},
1072 	},
1073 	{
1074 		.active_pipes = BIT(PIPE_A) | BIT(PIPE_C) | BIT(PIPE_D),
1075 		.dbuf_mask = {
1076 			[PIPE_A] = BIT(DBUF_S1) | BIT(DBUF_S2),
1077 			[PIPE_C] = BIT(DBUF_S3),
1078 			[PIPE_D] = BIT(DBUF_S4),
1079 		},
1080 	},
1081 	{
1082 		.active_pipes = BIT(PIPE_B) | BIT(PIPE_C) | BIT(PIPE_D),
1083 		.dbuf_mask = {
1084 			[PIPE_B] = BIT(DBUF_S1) | BIT(DBUF_S2),
1085 			[PIPE_C] = BIT(DBUF_S3),
1086 			[PIPE_D] = BIT(DBUF_S4),
1087 		},
1088 	},
1089 	{
1090 		.active_pipes = BIT(PIPE_A) | BIT(PIPE_B) | BIT(PIPE_C) | BIT(PIPE_D),
1091 		.dbuf_mask = {
1092 			[PIPE_A] = BIT(DBUF_S1),
1093 			[PIPE_B] = BIT(DBUF_S2),
1094 			[PIPE_C] = BIT(DBUF_S3),
1095 			[PIPE_D] = BIT(DBUF_S4),
1096 		},
1097 	},
1098 	{}
1099 };
1100 
1101 static const struct dbuf_slice_conf_entry adlp_allowed_dbufs[] = {
1102 	/*
1103 	 * Keep the join_mbus cases first so check_mbus_joined()
1104 	 * will prefer them over the !join_mbus cases.
1105 	 */
1106 	{
1107 		.active_pipes = BIT(PIPE_A),
1108 		.dbuf_mask = {
1109 			[PIPE_A] = BIT(DBUF_S1) | BIT(DBUF_S2) | BIT(DBUF_S3) | BIT(DBUF_S4),
1110 		},
1111 		.join_mbus = true,
1112 	},
1113 	{
1114 		.active_pipes = BIT(PIPE_B),
1115 		.dbuf_mask = {
1116 			[PIPE_B] = BIT(DBUF_S1) | BIT(DBUF_S2) | BIT(DBUF_S3) | BIT(DBUF_S4),
1117 		},
1118 		.join_mbus = true,
1119 	},
1120 	{
1121 		.active_pipes = BIT(PIPE_A),
1122 		.dbuf_mask = {
1123 			[PIPE_A] = BIT(DBUF_S1) | BIT(DBUF_S2),
1124 		},
1125 		.join_mbus = false,
1126 	},
1127 	{
1128 		.active_pipes = BIT(PIPE_B),
1129 		.dbuf_mask = {
1130 			[PIPE_B] = BIT(DBUF_S3) | BIT(DBUF_S4),
1131 		},
1132 		.join_mbus = false,
1133 	},
1134 	{
1135 		.active_pipes = BIT(PIPE_A) | BIT(PIPE_B),
1136 		.dbuf_mask = {
1137 			[PIPE_A] = BIT(DBUF_S1) | BIT(DBUF_S2),
1138 			[PIPE_B] = BIT(DBUF_S3) | BIT(DBUF_S4),
1139 		},
1140 	},
1141 	{
1142 		.active_pipes = BIT(PIPE_C),
1143 		.dbuf_mask = {
1144 			[PIPE_C] = BIT(DBUF_S3) | BIT(DBUF_S4),
1145 		},
1146 	},
1147 	{
1148 		.active_pipes = BIT(PIPE_A) | BIT(PIPE_C),
1149 		.dbuf_mask = {
1150 			[PIPE_A] = BIT(DBUF_S1) | BIT(DBUF_S2),
1151 			[PIPE_C] = BIT(DBUF_S3) | BIT(DBUF_S4),
1152 		},
1153 	},
1154 	{
1155 		.active_pipes = BIT(PIPE_B) | BIT(PIPE_C),
1156 		.dbuf_mask = {
1157 			[PIPE_B] = BIT(DBUF_S3) | BIT(DBUF_S4),
1158 			[PIPE_C] = BIT(DBUF_S3) | BIT(DBUF_S4),
1159 		},
1160 	},
1161 	{
1162 		.active_pipes = BIT(PIPE_A) | BIT(PIPE_B) | BIT(PIPE_C),
1163 		.dbuf_mask = {
1164 			[PIPE_A] = BIT(DBUF_S1) | BIT(DBUF_S2),
1165 			[PIPE_B] = BIT(DBUF_S3) | BIT(DBUF_S4),
1166 			[PIPE_C] = BIT(DBUF_S3) | BIT(DBUF_S4),
1167 		},
1168 	},
1169 	{
1170 		.active_pipes = BIT(PIPE_D),
1171 		.dbuf_mask = {
1172 			[PIPE_D] = BIT(DBUF_S1) | BIT(DBUF_S2),
1173 		},
1174 	},
1175 	{
1176 		.active_pipes = BIT(PIPE_A) | BIT(PIPE_D),
1177 		.dbuf_mask = {
1178 			[PIPE_A] = BIT(DBUF_S1) | BIT(DBUF_S2),
1179 			[PIPE_D] = BIT(DBUF_S1) | BIT(DBUF_S2),
1180 		},
1181 	},
1182 	{
1183 		.active_pipes = BIT(PIPE_B) | BIT(PIPE_D),
1184 		.dbuf_mask = {
1185 			[PIPE_B] = BIT(DBUF_S3) | BIT(DBUF_S4),
1186 			[PIPE_D] = BIT(DBUF_S1) | BIT(DBUF_S2),
1187 		},
1188 	},
1189 	{
1190 		.active_pipes = BIT(PIPE_A) | BIT(PIPE_B) | BIT(PIPE_D),
1191 		.dbuf_mask = {
1192 			[PIPE_A] = BIT(DBUF_S1) | BIT(DBUF_S2),
1193 			[PIPE_B] = BIT(DBUF_S3) | BIT(DBUF_S4),
1194 			[PIPE_D] = BIT(DBUF_S1) | BIT(DBUF_S2),
1195 		},
1196 	},
1197 	{
1198 		.active_pipes = BIT(PIPE_C) | BIT(PIPE_D),
1199 		.dbuf_mask = {
1200 			[PIPE_C] = BIT(DBUF_S3) | BIT(DBUF_S4),
1201 			[PIPE_D] = BIT(DBUF_S1) | BIT(DBUF_S2),
1202 		},
1203 	},
1204 	{
1205 		.active_pipes = BIT(PIPE_A) | BIT(PIPE_C) | BIT(PIPE_D),
1206 		.dbuf_mask = {
1207 			[PIPE_A] = BIT(DBUF_S1) | BIT(DBUF_S2),
1208 			[PIPE_C] = BIT(DBUF_S3) | BIT(DBUF_S4),
1209 			[PIPE_D] = BIT(DBUF_S1) | BIT(DBUF_S2),
1210 		},
1211 	},
1212 	{
1213 		.active_pipes = BIT(PIPE_B) | BIT(PIPE_C) | BIT(PIPE_D),
1214 		.dbuf_mask = {
1215 			[PIPE_B] = BIT(DBUF_S3) | BIT(DBUF_S4),
1216 			[PIPE_C] = BIT(DBUF_S3) | BIT(DBUF_S4),
1217 			[PIPE_D] = BIT(DBUF_S1) | BIT(DBUF_S2),
1218 		},
1219 	},
1220 	{
1221 		.active_pipes = BIT(PIPE_A) | BIT(PIPE_B) | BIT(PIPE_C) | BIT(PIPE_D),
1222 		.dbuf_mask = {
1223 			[PIPE_A] = BIT(DBUF_S1) | BIT(DBUF_S2),
1224 			[PIPE_B] = BIT(DBUF_S3) | BIT(DBUF_S4),
1225 			[PIPE_C] = BIT(DBUF_S3) | BIT(DBUF_S4),
1226 			[PIPE_D] = BIT(DBUF_S1) | BIT(DBUF_S2),
1227 		},
1228 	},
1229 	{}
1230 
1231 };
1232 
1233 static bool check_mbus_joined(u8 active_pipes,
1234 			      const struct dbuf_slice_conf_entry *dbuf_slices)
1235 {
1236 	int i;
1237 
1238 	for (i = 0; dbuf_slices[i].active_pipes != 0; i++) {
1239 		if (dbuf_slices[i].active_pipes == active_pipes)
1240 			return dbuf_slices[i].join_mbus;
1241 	}
1242 	return false;
1243 }
1244 
1245 static bool adlp_check_mbus_joined(u8 active_pipes)
1246 {
1247 	return check_mbus_joined(active_pipes, adlp_allowed_dbufs);
1248 }
1249 
1250 static u8 compute_dbuf_slices(enum pipe pipe, u8 active_pipes, bool join_mbus,
1251 			      const struct dbuf_slice_conf_entry *dbuf_slices)
1252 {
1253 	int i;
1254 
1255 	for (i = 0; dbuf_slices[i].active_pipes != 0; i++) {
1256 		if (dbuf_slices[i].active_pipes == active_pipes &&
1257 		    dbuf_slices[i].join_mbus == join_mbus)
1258 			return dbuf_slices[i].dbuf_mask[pipe];
1259 	}
1260 	return 0;
1261 }
1262 
1263 /*
1264  * This function finds an entry with same enabled pipe configuration and
1265  * returns correspondent DBuf slice mask as stated in BSpec for particular
1266  * platform.
1267  */
1268 static u8 icl_compute_dbuf_slices(enum pipe pipe, u8 active_pipes, bool join_mbus)
1269 {
1270 	/*
1271 	 * FIXME: For ICL this is still a bit unclear as prev BSpec revision
1272 	 * required calculating "pipe ratio" in order to determine
1273 	 * if one or two slices can be used for single pipe configurations
1274 	 * as additional constraint to the existing table.
1275 	 * However based on recent info, it should be not "pipe ratio"
1276 	 * but rather ratio between pixel_rate and cdclk with additional
1277 	 * constants, so for now we are using only table until this is
1278 	 * clarified. Also this is the reason why crtc_state param is
1279 	 * still here - we will need it once those additional constraints
1280 	 * pop up.
1281 	 */
1282 	return compute_dbuf_slices(pipe, active_pipes, join_mbus,
1283 				   icl_allowed_dbufs);
1284 }
1285 
1286 static u8 tgl_compute_dbuf_slices(enum pipe pipe, u8 active_pipes, bool join_mbus)
1287 {
1288 	return compute_dbuf_slices(pipe, active_pipes, join_mbus,
1289 				   tgl_allowed_dbufs);
1290 }
1291 
1292 static u8 adlp_compute_dbuf_slices(enum pipe pipe, u8 active_pipes, bool join_mbus)
1293 {
1294 	return compute_dbuf_slices(pipe, active_pipes, join_mbus,
1295 				   adlp_allowed_dbufs);
1296 }
1297 
1298 static u8 dg2_compute_dbuf_slices(enum pipe pipe, u8 active_pipes, bool join_mbus)
1299 {
1300 	return compute_dbuf_slices(pipe, active_pipes, join_mbus,
1301 				   dg2_allowed_dbufs);
1302 }
1303 
1304 static u8 skl_compute_dbuf_slices(struct intel_crtc *crtc, u8 active_pipes, bool join_mbus)
1305 {
1306 	struct drm_i915_private *i915 = to_i915(crtc->base.dev);
1307 	enum pipe pipe = crtc->pipe;
1308 
1309 	if (IS_DG2(i915))
1310 		return dg2_compute_dbuf_slices(pipe, active_pipes, join_mbus);
1311 	else if (DISPLAY_VER(i915) >= 13)
1312 		return adlp_compute_dbuf_slices(pipe, active_pipes, join_mbus);
1313 	else if (DISPLAY_VER(i915) == 12)
1314 		return tgl_compute_dbuf_slices(pipe, active_pipes, join_mbus);
1315 	else if (DISPLAY_VER(i915) == 11)
1316 		return icl_compute_dbuf_slices(pipe, active_pipes, join_mbus);
1317 	/*
1318 	 * For anything else just return one slice yet.
1319 	 * Should be extended for other platforms.
1320 	 */
1321 	return active_pipes & BIT(pipe) ? BIT(DBUF_S1) : 0;
1322 }
1323 
1324 static bool
1325 use_minimal_wm0_only(const struct intel_crtc_state *crtc_state,
1326 		     struct intel_plane *plane)
1327 {
1328 	struct drm_i915_private *i915 = to_i915(plane->base.dev);
1329 
1330 	return DISPLAY_VER(i915) >= 13 &&
1331 	       crtc_state->uapi.async_flip &&
1332 	       plane->async_flip;
1333 }
1334 
1335 static u64
1336 skl_total_relative_data_rate(const struct intel_crtc_state *crtc_state)
1337 {
1338 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
1339 	struct drm_i915_private *i915 = to_i915(crtc->base.dev);
1340 	enum plane_id plane_id;
1341 	u64 data_rate = 0;
1342 
1343 	for_each_plane_id_on_crtc(crtc, plane_id) {
1344 		if (plane_id == PLANE_CURSOR)
1345 			continue;
1346 
1347 		data_rate += crtc_state->rel_data_rate[plane_id];
1348 
1349 		if (DISPLAY_VER(i915) < 11)
1350 			data_rate += crtc_state->rel_data_rate_y[plane_id];
1351 	}
1352 
1353 	return data_rate;
1354 }
1355 
1356 static const struct skl_wm_level *
1357 skl_plane_wm_level(const struct skl_pipe_wm *pipe_wm,
1358 		   enum plane_id plane_id,
1359 		   int level)
1360 {
1361 	const struct skl_plane_wm *wm = &pipe_wm->planes[plane_id];
1362 
1363 	if (level == 0 && pipe_wm->use_sagv_wm)
1364 		return &wm->sagv.wm0;
1365 
1366 	return &wm->wm[level];
1367 }
1368 
1369 static const struct skl_wm_level *
1370 skl_plane_trans_wm(const struct skl_pipe_wm *pipe_wm,
1371 		   enum plane_id plane_id)
1372 {
1373 	const struct skl_plane_wm *wm = &pipe_wm->planes[plane_id];
1374 
1375 	if (pipe_wm->use_sagv_wm)
1376 		return &wm->sagv.trans_wm;
1377 
1378 	return &wm->trans_wm;
1379 }
1380 
1381 /*
1382  * We only disable the watermarks for each plane if
1383  * they exceed the ddb allocation of said plane. This
1384  * is done so that we don't end up touching cursor
1385  * watermarks needlessly when some other plane reduces
1386  * our max possible watermark level.
1387  *
1388  * Bspec has this to say about the PLANE_WM enable bit:
1389  * "All the watermarks at this level for all enabled
1390  *  planes must be enabled before the level will be used."
1391  * So this is actually safe to do.
1392  */
1393 static void
1394 skl_check_wm_level(struct skl_wm_level *wm, const struct skl_ddb_entry *ddb)
1395 {
1396 	if (wm->min_ddb_alloc > skl_ddb_entry_size(ddb))
1397 		memset(wm, 0, sizeof(*wm));
1398 }
1399 
1400 static void
1401 skl_check_nv12_wm_level(struct skl_wm_level *wm, struct skl_wm_level *uv_wm,
1402 			const struct skl_ddb_entry *ddb_y, const struct skl_ddb_entry *ddb)
1403 {
1404 	if (wm->min_ddb_alloc > skl_ddb_entry_size(ddb_y) ||
1405 	    uv_wm->min_ddb_alloc > skl_ddb_entry_size(ddb)) {
1406 		memset(wm, 0, sizeof(*wm));
1407 		memset(uv_wm, 0, sizeof(*uv_wm));
1408 	}
1409 }
1410 
1411 static bool icl_need_wm1_wa(struct drm_i915_private *i915,
1412 			    enum plane_id plane_id)
1413 {
1414 	/*
1415 	 * Wa_1408961008:icl, ehl
1416 	 * Wa_14012656716:tgl, adl
1417 	 * Underruns with WM1+ disabled
1418 	 */
1419 	return DISPLAY_VER(i915) == 11 ||
1420 	       (IS_DISPLAY_VER(i915, 12, 13) && plane_id == PLANE_CURSOR);
1421 }
1422 
1423 struct skl_plane_ddb_iter {
1424 	u64 data_rate;
1425 	u16 start, size;
1426 };
1427 
1428 static void
1429 skl_allocate_plane_ddb(struct skl_plane_ddb_iter *iter,
1430 		       struct skl_ddb_entry *ddb,
1431 		       const struct skl_wm_level *wm,
1432 		       u64 data_rate)
1433 {
1434 	u16 size, extra = 0;
1435 
1436 	if (data_rate) {
1437 		extra = min_t(u16, iter->size,
1438 			      DIV64_U64_ROUND_UP(iter->size * data_rate,
1439 						 iter->data_rate));
1440 		iter->size -= extra;
1441 		iter->data_rate -= data_rate;
1442 	}
1443 
1444 	/*
1445 	 * Keep ddb entry of all disabled planes explicitly zeroed
1446 	 * to avoid skl_ddb_add_affected_planes() adding them to
1447 	 * the state when other planes change their allocations.
1448 	 */
1449 	size = wm->min_ddb_alloc + extra;
1450 	if (size)
1451 		iter->start = skl_ddb_entry_init(ddb, iter->start,
1452 						 iter->start + size);
1453 }
1454 
1455 static int
1456 skl_crtc_allocate_plane_ddb(struct intel_atomic_state *state,
1457 			    struct intel_crtc *crtc)
1458 {
1459 	struct drm_i915_private *i915 = to_i915(crtc->base.dev);
1460 	struct intel_crtc_state *crtc_state =
1461 		intel_atomic_get_new_crtc_state(state, crtc);
1462 	const struct intel_dbuf_state *dbuf_state =
1463 		intel_atomic_get_new_dbuf_state(state);
1464 	const struct skl_ddb_entry *alloc = &dbuf_state->ddb[crtc->pipe];
1465 	int num_active = hweight8(dbuf_state->active_pipes);
1466 	struct skl_plane_ddb_iter iter;
1467 	enum plane_id plane_id;
1468 	u16 cursor_size;
1469 	u32 blocks;
1470 	int level;
1471 
1472 	/* Clear the partitioning for disabled planes. */
1473 	memset(crtc_state->wm.skl.plane_ddb, 0, sizeof(crtc_state->wm.skl.plane_ddb));
1474 	memset(crtc_state->wm.skl.plane_ddb_y, 0, sizeof(crtc_state->wm.skl.plane_ddb_y));
1475 
1476 	if (!crtc_state->hw.active)
1477 		return 0;
1478 
1479 	iter.start = alloc->start;
1480 	iter.size = skl_ddb_entry_size(alloc);
1481 	if (iter.size == 0)
1482 		return 0;
1483 
1484 	/* Allocate fixed number of blocks for cursor. */
1485 	cursor_size = skl_cursor_allocation(crtc_state, num_active);
1486 	iter.size -= cursor_size;
1487 	skl_ddb_entry_init(&crtc_state->wm.skl.plane_ddb[PLANE_CURSOR],
1488 			   alloc->end - cursor_size, alloc->end);
1489 
1490 	iter.data_rate = skl_total_relative_data_rate(crtc_state);
1491 
1492 	/*
1493 	 * Find the highest watermark level for which we can satisfy the block
1494 	 * requirement of active planes.
1495 	 */
1496 	for (level = ilk_wm_max_level(i915); level >= 0; level--) {
1497 		blocks = 0;
1498 		for_each_plane_id_on_crtc(crtc, plane_id) {
1499 			const struct skl_plane_wm *wm =
1500 				&crtc_state->wm.skl.optimal.planes[plane_id];
1501 
1502 			if (plane_id == PLANE_CURSOR) {
1503 				const struct skl_ddb_entry *ddb =
1504 					&crtc_state->wm.skl.plane_ddb[plane_id];
1505 
1506 				if (wm->wm[level].min_ddb_alloc > skl_ddb_entry_size(ddb)) {
1507 					drm_WARN_ON(&i915->drm,
1508 						    wm->wm[level].min_ddb_alloc != U16_MAX);
1509 					blocks = U32_MAX;
1510 					break;
1511 				}
1512 				continue;
1513 			}
1514 
1515 			blocks += wm->wm[level].min_ddb_alloc;
1516 			blocks += wm->uv_wm[level].min_ddb_alloc;
1517 		}
1518 
1519 		if (blocks <= iter.size) {
1520 			iter.size -= blocks;
1521 			break;
1522 		}
1523 	}
1524 
1525 	if (level < 0) {
1526 		drm_dbg_kms(&i915->drm,
1527 			    "Requested display configuration exceeds system DDB limitations");
1528 		drm_dbg_kms(&i915->drm, "minimum required %d/%d\n",
1529 			    blocks, iter.size);
1530 		return -EINVAL;
1531 	}
1532 
1533 	/* avoid the WARN later when we don't allocate any extra DDB */
1534 	if (iter.data_rate == 0)
1535 		iter.size = 0;
1536 
1537 	/*
1538 	 * Grant each plane the blocks it requires at the highest achievable
1539 	 * watermark level, plus an extra share of the leftover blocks
1540 	 * proportional to its relative data rate.
1541 	 */
1542 	for_each_plane_id_on_crtc(crtc, plane_id) {
1543 		struct skl_ddb_entry *ddb =
1544 			&crtc_state->wm.skl.plane_ddb[plane_id];
1545 		struct skl_ddb_entry *ddb_y =
1546 			&crtc_state->wm.skl.plane_ddb_y[plane_id];
1547 		const struct skl_plane_wm *wm =
1548 			&crtc_state->wm.skl.optimal.planes[plane_id];
1549 
1550 		if (plane_id == PLANE_CURSOR)
1551 			continue;
1552 
1553 		if (DISPLAY_VER(i915) < 11 &&
1554 		    crtc_state->nv12_planes & BIT(plane_id)) {
1555 			skl_allocate_plane_ddb(&iter, ddb_y, &wm->wm[level],
1556 					       crtc_state->rel_data_rate_y[plane_id]);
1557 			skl_allocate_plane_ddb(&iter, ddb, &wm->uv_wm[level],
1558 					       crtc_state->rel_data_rate[plane_id]);
1559 		} else {
1560 			skl_allocate_plane_ddb(&iter, ddb, &wm->wm[level],
1561 					       crtc_state->rel_data_rate[plane_id]);
1562 		}
1563 	}
1564 	drm_WARN_ON(&i915->drm, iter.size != 0 || iter.data_rate != 0);
1565 
1566 	/*
1567 	 * When we calculated watermark values we didn't know how high
1568 	 * of a level we'd actually be able to hit, so we just marked
1569 	 * all levels as "enabled."  Go back now and disable the ones
1570 	 * that aren't actually possible.
1571 	 */
1572 	for (level++; level <= ilk_wm_max_level(i915); level++) {
1573 		for_each_plane_id_on_crtc(crtc, plane_id) {
1574 			const struct skl_ddb_entry *ddb =
1575 				&crtc_state->wm.skl.plane_ddb[plane_id];
1576 			const struct skl_ddb_entry *ddb_y =
1577 				&crtc_state->wm.skl.plane_ddb_y[plane_id];
1578 			struct skl_plane_wm *wm =
1579 				&crtc_state->wm.skl.optimal.planes[plane_id];
1580 
1581 			if (DISPLAY_VER(i915) < 11 &&
1582 			    crtc_state->nv12_planes & BIT(plane_id))
1583 				skl_check_nv12_wm_level(&wm->wm[level],
1584 							&wm->uv_wm[level],
1585 							ddb_y, ddb);
1586 			else
1587 				skl_check_wm_level(&wm->wm[level], ddb);
1588 
1589 			if (icl_need_wm1_wa(i915, plane_id) &&
1590 			    level == 1 && wm->wm[0].enable) {
1591 				wm->wm[level].blocks = wm->wm[0].blocks;
1592 				wm->wm[level].lines = wm->wm[0].lines;
1593 				wm->wm[level].ignore_lines = wm->wm[0].ignore_lines;
1594 			}
1595 		}
1596 	}
1597 
1598 	/*
1599 	 * Go back and disable the transition and SAGV watermarks
1600 	 * if it turns out we don't have enough DDB blocks for them.
1601 	 */
1602 	for_each_plane_id_on_crtc(crtc, plane_id) {
1603 		const struct skl_ddb_entry *ddb =
1604 			&crtc_state->wm.skl.plane_ddb[plane_id];
1605 		const struct skl_ddb_entry *ddb_y =
1606 			&crtc_state->wm.skl.plane_ddb_y[plane_id];
1607 		struct skl_plane_wm *wm =
1608 			&crtc_state->wm.skl.optimal.planes[plane_id];
1609 
1610 		if (DISPLAY_VER(i915) < 11 &&
1611 		    crtc_state->nv12_planes & BIT(plane_id)) {
1612 			skl_check_wm_level(&wm->trans_wm, ddb_y);
1613 		} else {
1614 			WARN_ON(skl_ddb_entry_size(ddb_y));
1615 
1616 			skl_check_wm_level(&wm->trans_wm, ddb);
1617 		}
1618 
1619 		skl_check_wm_level(&wm->sagv.wm0, ddb);
1620 		skl_check_wm_level(&wm->sagv.trans_wm, ddb);
1621 	}
1622 
1623 	return 0;
1624 }
1625 
1626 /*
1627  * The max latency should be 257 (max the punit can code is 255 and we add 2us
1628  * for the read latency) and cpp should always be <= 8, so that
1629  * should allow pixel_rate up to ~2 GHz which seems sufficient since max
1630  * 2xcdclk is 1350 MHz and the pixel rate should never exceed that.
1631  */
1632 static uint_fixed_16_16_t
1633 skl_wm_method1(const struct drm_i915_private *i915, u32 pixel_rate,
1634 	       u8 cpp, u32 latency, u32 dbuf_block_size)
1635 {
1636 	u32 wm_intermediate_val;
1637 	uint_fixed_16_16_t ret;
1638 
1639 	if (latency == 0)
1640 		return FP_16_16_MAX;
1641 
1642 	wm_intermediate_val = latency * pixel_rate * cpp;
1643 	ret = div_fixed16(wm_intermediate_val, 1000 * dbuf_block_size);
1644 
1645 	if (DISPLAY_VER(i915) >= 10)
1646 		ret = add_fixed16_u32(ret, 1);
1647 
1648 	return ret;
1649 }
1650 
1651 static uint_fixed_16_16_t
1652 skl_wm_method2(u32 pixel_rate, u32 pipe_htotal, u32 latency,
1653 	       uint_fixed_16_16_t plane_blocks_per_line)
1654 {
1655 	u32 wm_intermediate_val;
1656 	uint_fixed_16_16_t ret;
1657 
1658 	if (latency == 0)
1659 		return FP_16_16_MAX;
1660 
1661 	wm_intermediate_val = latency * pixel_rate;
1662 	wm_intermediate_val = DIV_ROUND_UP(wm_intermediate_val,
1663 					   pipe_htotal * 1000);
1664 	ret = mul_u32_fixed16(wm_intermediate_val, plane_blocks_per_line);
1665 	return ret;
1666 }
1667 
1668 static uint_fixed_16_16_t
1669 intel_get_linetime_us(const struct intel_crtc_state *crtc_state)
1670 {
1671 	struct drm_i915_private *i915 = to_i915(crtc_state->uapi.crtc->dev);
1672 	u32 pixel_rate;
1673 	u32 crtc_htotal;
1674 	uint_fixed_16_16_t linetime_us;
1675 
1676 	if (!crtc_state->hw.active)
1677 		return u32_to_fixed16(0);
1678 
1679 	pixel_rate = crtc_state->pixel_rate;
1680 
1681 	if (drm_WARN_ON(&i915->drm, pixel_rate == 0))
1682 		return u32_to_fixed16(0);
1683 
1684 	crtc_htotal = crtc_state->hw.pipe_mode.crtc_htotal;
1685 	linetime_us = div_fixed16(crtc_htotal * 1000, pixel_rate);
1686 
1687 	return linetime_us;
1688 }
1689 
1690 static int
1691 skl_compute_wm_params(const struct intel_crtc_state *crtc_state,
1692 		      int width, const struct drm_format_info *format,
1693 		      u64 modifier, unsigned int rotation,
1694 		      u32 plane_pixel_rate, struct skl_wm_params *wp,
1695 		      int color_plane)
1696 {
1697 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
1698 	struct drm_i915_private *i915 = to_i915(crtc->base.dev);
1699 	u32 interm_pbpl;
1700 
1701 	/* only planar format has two planes */
1702 	if (color_plane == 1 &&
1703 	    !intel_format_info_is_yuv_semiplanar(format, modifier)) {
1704 		drm_dbg_kms(&i915->drm,
1705 			    "Non planar format have single plane\n");
1706 		return -EINVAL;
1707 	}
1708 
1709 	wp->x_tiled = modifier == I915_FORMAT_MOD_X_TILED;
1710 	wp->y_tiled = modifier != I915_FORMAT_MOD_X_TILED &&
1711 		intel_fb_is_tiled_modifier(modifier);
1712 	wp->rc_surface = intel_fb_is_ccs_modifier(modifier);
1713 	wp->is_planar = intel_format_info_is_yuv_semiplanar(format, modifier);
1714 
1715 	wp->width = width;
1716 	if (color_plane == 1 && wp->is_planar)
1717 		wp->width /= 2;
1718 
1719 	wp->cpp = format->cpp[color_plane];
1720 	wp->plane_pixel_rate = plane_pixel_rate;
1721 
1722 	if (DISPLAY_VER(i915) >= 11 &&
1723 	    modifier == I915_FORMAT_MOD_Yf_TILED  && wp->cpp == 1)
1724 		wp->dbuf_block_size = 256;
1725 	else
1726 		wp->dbuf_block_size = 512;
1727 
1728 	if (drm_rotation_90_or_270(rotation)) {
1729 		switch (wp->cpp) {
1730 		case 1:
1731 			wp->y_min_scanlines = 16;
1732 			break;
1733 		case 2:
1734 			wp->y_min_scanlines = 8;
1735 			break;
1736 		case 4:
1737 			wp->y_min_scanlines = 4;
1738 			break;
1739 		default:
1740 			MISSING_CASE(wp->cpp);
1741 			return -EINVAL;
1742 		}
1743 	} else {
1744 		wp->y_min_scanlines = 4;
1745 	}
1746 
1747 	if (skl_needs_memory_bw_wa(i915))
1748 		wp->y_min_scanlines *= 2;
1749 
1750 	wp->plane_bytes_per_line = wp->width * wp->cpp;
1751 	if (wp->y_tiled) {
1752 		interm_pbpl = DIV_ROUND_UP(wp->plane_bytes_per_line *
1753 					   wp->y_min_scanlines,
1754 					   wp->dbuf_block_size);
1755 
1756 		if (DISPLAY_VER(i915) >= 10)
1757 			interm_pbpl++;
1758 
1759 		wp->plane_blocks_per_line = div_fixed16(interm_pbpl,
1760 							wp->y_min_scanlines);
1761 	} else {
1762 		interm_pbpl = DIV_ROUND_UP(wp->plane_bytes_per_line,
1763 					   wp->dbuf_block_size);
1764 
1765 		if (!wp->x_tiled || DISPLAY_VER(i915) >= 10)
1766 			interm_pbpl++;
1767 
1768 		wp->plane_blocks_per_line = u32_to_fixed16(interm_pbpl);
1769 	}
1770 
1771 	wp->y_tile_minimum = mul_u32_fixed16(wp->y_min_scanlines,
1772 					     wp->plane_blocks_per_line);
1773 
1774 	wp->linetime_us = fixed16_to_u32_round_up(intel_get_linetime_us(crtc_state));
1775 
1776 	return 0;
1777 }
1778 
1779 static int
1780 skl_compute_plane_wm_params(const struct intel_crtc_state *crtc_state,
1781 			    const struct intel_plane_state *plane_state,
1782 			    struct skl_wm_params *wp, int color_plane)
1783 {
1784 	const struct drm_framebuffer *fb = plane_state->hw.fb;
1785 	int width;
1786 
1787 	/*
1788 	 * Src coordinates are already rotated by 270 degrees for
1789 	 * the 90/270 degree plane rotation cases (to match the
1790 	 * GTT mapping), hence no need to account for rotation here.
1791 	 */
1792 	width = drm_rect_width(&plane_state->uapi.src) >> 16;
1793 
1794 	return skl_compute_wm_params(crtc_state, width,
1795 				     fb->format, fb->modifier,
1796 				     plane_state->hw.rotation,
1797 				     intel_plane_pixel_rate(crtc_state, plane_state),
1798 				     wp, color_plane);
1799 }
1800 
1801 static bool skl_wm_has_lines(struct drm_i915_private *i915, int level)
1802 {
1803 	if (DISPLAY_VER(i915) >= 10)
1804 		return true;
1805 
1806 	/* The number of lines are ignored for the level 0 watermark. */
1807 	return level > 0;
1808 }
1809 
1810 static int skl_wm_max_lines(struct drm_i915_private *i915)
1811 {
1812 	if (DISPLAY_VER(i915) >= 13)
1813 		return 255;
1814 	else
1815 		return 31;
1816 }
1817 
1818 static void skl_compute_plane_wm(const struct intel_crtc_state *crtc_state,
1819 				 struct intel_plane *plane,
1820 				 int level,
1821 				 unsigned int latency,
1822 				 const struct skl_wm_params *wp,
1823 				 const struct skl_wm_level *result_prev,
1824 				 struct skl_wm_level *result /* out */)
1825 {
1826 	struct drm_i915_private *i915 = to_i915(crtc_state->uapi.crtc->dev);
1827 	uint_fixed_16_16_t method1, method2;
1828 	uint_fixed_16_16_t selected_result;
1829 	u32 blocks, lines, min_ddb_alloc = 0;
1830 
1831 	if (latency == 0 ||
1832 	    (use_minimal_wm0_only(crtc_state, plane) && level > 0)) {
1833 		/* reject it */
1834 		result->min_ddb_alloc = U16_MAX;
1835 		return;
1836 	}
1837 
1838 	/*
1839 	 * WaIncreaseLatencyIPCEnabled: kbl,cfl
1840 	 * Display WA #1141: kbl,cfl
1841 	 */
1842 	if ((IS_KABYLAKE(i915) || IS_COFFEELAKE(i915) || IS_COMETLAKE(i915)) &&
1843 	    skl_watermark_ipc_enabled(i915))
1844 		latency += 4;
1845 
1846 	if (skl_needs_memory_bw_wa(i915) && wp->x_tiled)
1847 		latency += 15;
1848 
1849 	method1 = skl_wm_method1(i915, wp->plane_pixel_rate,
1850 				 wp->cpp, latency, wp->dbuf_block_size);
1851 	method2 = skl_wm_method2(wp->plane_pixel_rate,
1852 				 crtc_state->hw.pipe_mode.crtc_htotal,
1853 				 latency,
1854 				 wp->plane_blocks_per_line);
1855 
1856 	if (wp->y_tiled) {
1857 		selected_result = max_fixed16(method2, wp->y_tile_minimum);
1858 	} else {
1859 		if ((wp->cpp * crtc_state->hw.pipe_mode.crtc_htotal /
1860 		     wp->dbuf_block_size < 1) &&
1861 		     (wp->plane_bytes_per_line / wp->dbuf_block_size < 1)) {
1862 			selected_result = method2;
1863 		} else if (latency >= wp->linetime_us) {
1864 			if (DISPLAY_VER(i915) == 9)
1865 				selected_result = min_fixed16(method1, method2);
1866 			else
1867 				selected_result = method2;
1868 		} else {
1869 			selected_result = method1;
1870 		}
1871 	}
1872 
1873 	blocks = fixed16_to_u32_round_up(selected_result) + 1;
1874 	/*
1875 	 * Lets have blocks at minimum equivalent to plane_blocks_per_line
1876 	 * as there will be at minimum one line for lines configuration. This
1877 	 * is a work around for FIFO underruns observed with resolutions like
1878 	 * 4k 60 Hz in single channel DRAM configurations.
1879 	 *
1880 	 * As per the Bspec 49325, if the ddb allocation can hold at least
1881 	 * one plane_blocks_per_line, we should have selected method2 in
1882 	 * the above logic. Assuming that modern versions have enough dbuf
1883 	 * and method2 guarantees blocks equivalent to at least 1 line,
1884 	 * select the blocks as plane_blocks_per_line.
1885 	 *
1886 	 * TODO: Revisit the logic when we have better understanding on DRAM
1887 	 * channels' impact on the level 0 memory latency and the relevant
1888 	 * wm calculations.
1889 	 */
1890 	if (skl_wm_has_lines(i915, level))
1891 		blocks = max(blocks,
1892 			     fixed16_to_u32_round_up(wp->plane_blocks_per_line));
1893 	lines = div_round_up_fixed16(selected_result,
1894 				     wp->plane_blocks_per_line);
1895 
1896 	if (DISPLAY_VER(i915) == 9) {
1897 		/* Display WA #1125: skl,bxt,kbl */
1898 		if (level == 0 && wp->rc_surface)
1899 			blocks += fixed16_to_u32_round_up(wp->y_tile_minimum);
1900 
1901 		/* Display WA #1126: skl,bxt,kbl */
1902 		if (level >= 1 && level <= 7) {
1903 			if (wp->y_tiled) {
1904 				blocks += fixed16_to_u32_round_up(wp->y_tile_minimum);
1905 				lines += wp->y_min_scanlines;
1906 			} else {
1907 				blocks++;
1908 			}
1909 
1910 			/*
1911 			 * Make sure result blocks for higher latency levels are
1912 			 * at least as high as level below the current level.
1913 			 * Assumption in DDB algorithm optimization for special
1914 			 * cases. Also covers Display WA #1125 for RC.
1915 			 */
1916 			if (result_prev->blocks > blocks)
1917 				blocks = result_prev->blocks;
1918 		}
1919 	}
1920 
1921 	if (DISPLAY_VER(i915) >= 11) {
1922 		if (wp->y_tiled) {
1923 			int extra_lines;
1924 
1925 			if (lines % wp->y_min_scanlines == 0)
1926 				extra_lines = wp->y_min_scanlines;
1927 			else
1928 				extra_lines = wp->y_min_scanlines * 2 -
1929 					lines % wp->y_min_scanlines;
1930 
1931 			min_ddb_alloc = mul_round_up_u32_fixed16(lines + extra_lines,
1932 								 wp->plane_blocks_per_line);
1933 		} else {
1934 			min_ddb_alloc = blocks + DIV_ROUND_UP(blocks, 10);
1935 		}
1936 	}
1937 
1938 	if (!skl_wm_has_lines(i915, level))
1939 		lines = 0;
1940 
1941 	if (lines > skl_wm_max_lines(i915)) {
1942 		/* reject it */
1943 		result->min_ddb_alloc = U16_MAX;
1944 		return;
1945 	}
1946 
1947 	/*
1948 	 * If lines is valid, assume we can use this watermark level
1949 	 * for now.  We'll come back and disable it after we calculate the
1950 	 * DDB allocation if it turns out we don't actually have enough
1951 	 * blocks to satisfy it.
1952 	 */
1953 	result->blocks = blocks;
1954 	result->lines = lines;
1955 	/* Bspec says: value >= plane ddb allocation -> invalid, hence the +1 here */
1956 	result->min_ddb_alloc = max(min_ddb_alloc, blocks) + 1;
1957 	result->enable = true;
1958 
1959 	if (DISPLAY_VER(i915) < 12 && i915->display.sagv.block_time_us)
1960 		result->can_sagv = latency >= i915->display.sagv.block_time_us;
1961 }
1962 
1963 static void
1964 skl_compute_wm_levels(const struct intel_crtc_state *crtc_state,
1965 		      struct intel_plane *plane,
1966 		      const struct skl_wm_params *wm_params,
1967 		      struct skl_wm_level *levels)
1968 {
1969 	struct drm_i915_private *i915 = to_i915(crtc_state->uapi.crtc->dev);
1970 	int level, max_level = ilk_wm_max_level(i915);
1971 	struct skl_wm_level *result_prev = &levels[0];
1972 
1973 	for (level = 0; level <= max_level; level++) {
1974 		struct skl_wm_level *result = &levels[level];
1975 		unsigned int latency = i915->display.wm.skl_latency[level];
1976 
1977 		skl_compute_plane_wm(crtc_state, plane, level, latency,
1978 				     wm_params, result_prev, result);
1979 
1980 		result_prev = result;
1981 	}
1982 }
1983 
1984 static void tgl_compute_sagv_wm(const struct intel_crtc_state *crtc_state,
1985 				struct intel_plane *plane,
1986 				const struct skl_wm_params *wm_params,
1987 				struct skl_plane_wm *plane_wm)
1988 {
1989 	struct drm_i915_private *i915 = to_i915(crtc_state->uapi.crtc->dev);
1990 	struct skl_wm_level *sagv_wm = &plane_wm->sagv.wm0;
1991 	struct skl_wm_level *levels = plane_wm->wm;
1992 	unsigned int latency = 0;
1993 
1994 	if (i915->display.sagv.block_time_us)
1995 		latency = i915->display.sagv.block_time_us + i915->display.wm.skl_latency[0];
1996 
1997 	skl_compute_plane_wm(crtc_state, plane, 0, latency,
1998 			     wm_params, &levels[0],
1999 			     sagv_wm);
2000 }
2001 
2002 static void skl_compute_transition_wm(struct drm_i915_private *i915,
2003 				      struct skl_wm_level *trans_wm,
2004 				      const struct skl_wm_level *wm0,
2005 				      const struct skl_wm_params *wp)
2006 {
2007 	u16 trans_min, trans_amount, trans_y_tile_min;
2008 	u16 wm0_blocks, trans_offset, blocks;
2009 
2010 	/* Transition WM don't make any sense if ipc is disabled */
2011 	if (!skl_watermark_ipc_enabled(i915))
2012 		return;
2013 
2014 	/*
2015 	 * WaDisableTWM:skl,kbl,cfl,bxt
2016 	 * Transition WM are not recommended by HW team for GEN9
2017 	 */
2018 	if (DISPLAY_VER(i915) == 9)
2019 		return;
2020 
2021 	if (DISPLAY_VER(i915) >= 11)
2022 		trans_min = 4;
2023 	else
2024 		trans_min = 14;
2025 
2026 	/* Display WA #1140: glk,cnl */
2027 	if (DISPLAY_VER(i915) == 10)
2028 		trans_amount = 0;
2029 	else
2030 		trans_amount = 10; /* This is configurable amount */
2031 
2032 	trans_offset = trans_min + trans_amount;
2033 
2034 	/*
2035 	 * The spec asks for Selected Result Blocks for wm0 (the real value),
2036 	 * not Result Blocks (the integer value). Pay attention to the capital
2037 	 * letters. The value wm_l0->blocks is actually Result Blocks, but
2038 	 * since Result Blocks is the ceiling of Selected Result Blocks plus 1,
2039 	 * and since we later will have to get the ceiling of the sum in the
2040 	 * transition watermarks calculation, we can just pretend Selected
2041 	 * Result Blocks is Result Blocks minus 1 and it should work for the
2042 	 * current platforms.
2043 	 */
2044 	wm0_blocks = wm0->blocks - 1;
2045 
2046 	if (wp->y_tiled) {
2047 		trans_y_tile_min =
2048 			(u16)mul_round_up_u32_fixed16(2, wp->y_tile_minimum);
2049 		blocks = max(wm0_blocks, trans_y_tile_min) + trans_offset;
2050 	} else {
2051 		blocks = wm0_blocks + trans_offset;
2052 	}
2053 	blocks++;
2054 
2055 	/*
2056 	 * Just assume we can enable the transition watermark.  After
2057 	 * computing the DDB we'll come back and disable it if that
2058 	 * assumption turns out to be false.
2059 	 */
2060 	trans_wm->blocks = blocks;
2061 	trans_wm->min_ddb_alloc = max_t(u16, wm0->min_ddb_alloc, blocks + 1);
2062 	trans_wm->enable = true;
2063 }
2064 
2065 static int skl_build_plane_wm_single(struct intel_crtc_state *crtc_state,
2066 				     const struct intel_plane_state *plane_state,
2067 				     struct intel_plane *plane, int color_plane)
2068 {
2069 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
2070 	struct drm_i915_private *i915 = to_i915(crtc->base.dev);
2071 	struct skl_plane_wm *wm = &crtc_state->wm.skl.raw.planes[plane->id];
2072 	struct skl_wm_params wm_params;
2073 	int ret;
2074 
2075 	ret = skl_compute_plane_wm_params(crtc_state, plane_state,
2076 					  &wm_params, color_plane);
2077 	if (ret)
2078 		return ret;
2079 
2080 	skl_compute_wm_levels(crtc_state, plane, &wm_params, wm->wm);
2081 
2082 	skl_compute_transition_wm(i915, &wm->trans_wm,
2083 				  &wm->wm[0], &wm_params);
2084 
2085 	if (DISPLAY_VER(i915) >= 12) {
2086 		tgl_compute_sagv_wm(crtc_state, plane, &wm_params, wm);
2087 
2088 		skl_compute_transition_wm(i915, &wm->sagv.trans_wm,
2089 					  &wm->sagv.wm0, &wm_params);
2090 	}
2091 
2092 	return 0;
2093 }
2094 
2095 static int skl_build_plane_wm_uv(struct intel_crtc_state *crtc_state,
2096 				 const struct intel_plane_state *plane_state,
2097 				 struct intel_plane *plane)
2098 {
2099 	struct skl_plane_wm *wm = &crtc_state->wm.skl.raw.planes[plane->id];
2100 	struct skl_wm_params wm_params;
2101 	int ret;
2102 
2103 	wm->is_planar = true;
2104 
2105 	/* uv plane watermarks must also be validated for NV12/Planar */
2106 	ret = skl_compute_plane_wm_params(crtc_state, plane_state,
2107 					  &wm_params, 1);
2108 	if (ret)
2109 		return ret;
2110 
2111 	skl_compute_wm_levels(crtc_state, plane, &wm_params, wm->uv_wm);
2112 
2113 	return 0;
2114 }
2115 
2116 static int skl_build_plane_wm(struct intel_crtc_state *crtc_state,
2117 			      const struct intel_plane_state *plane_state)
2118 {
2119 	struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
2120 	enum plane_id plane_id = plane->id;
2121 	struct skl_plane_wm *wm = &crtc_state->wm.skl.raw.planes[plane_id];
2122 	const struct drm_framebuffer *fb = plane_state->hw.fb;
2123 	int ret;
2124 
2125 	memset(wm, 0, sizeof(*wm));
2126 
2127 	if (!intel_wm_plane_visible(crtc_state, plane_state))
2128 		return 0;
2129 
2130 	ret = skl_build_plane_wm_single(crtc_state, plane_state,
2131 					plane, 0);
2132 	if (ret)
2133 		return ret;
2134 
2135 	if (fb->format->is_yuv && fb->format->num_planes > 1) {
2136 		ret = skl_build_plane_wm_uv(crtc_state, plane_state,
2137 					    plane);
2138 		if (ret)
2139 			return ret;
2140 	}
2141 
2142 	return 0;
2143 }
2144 
2145 static int icl_build_plane_wm(struct intel_crtc_state *crtc_state,
2146 			      const struct intel_plane_state *plane_state)
2147 {
2148 	struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
2149 	struct drm_i915_private *i915 = to_i915(plane->base.dev);
2150 	enum plane_id plane_id = plane->id;
2151 	struct skl_plane_wm *wm = &crtc_state->wm.skl.raw.planes[plane_id];
2152 	int ret;
2153 
2154 	/* Watermarks calculated in master */
2155 	if (plane_state->planar_slave)
2156 		return 0;
2157 
2158 	memset(wm, 0, sizeof(*wm));
2159 
2160 	if (plane_state->planar_linked_plane) {
2161 		const struct drm_framebuffer *fb = plane_state->hw.fb;
2162 
2163 		drm_WARN_ON(&i915->drm,
2164 			    !intel_wm_plane_visible(crtc_state, plane_state));
2165 		drm_WARN_ON(&i915->drm, !fb->format->is_yuv ||
2166 			    fb->format->num_planes == 1);
2167 
2168 		ret = skl_build_plane_wm_single(crtc_state, plane_state,
2169 						plane_state->planar_linked_plane, 0);
2170 		if (ret)
2171 			return ret;
2172 
2173 		ret = skl_build_plane_wm_single(crtc_state, plane_state,
2174 						plane, 1);
2175 		if (ret)
2176 			return ret;
2177 	} else if (intel_wm_plane_visible(crtc_state, plane_state)) {
2178 		ret = skl_build_plane_wm_single(crtc_state, plane_state,
2179 						plane, 0);
2180 		if (ret)
2181 			return ret;
2182 	}
2183 
2184 	return 0;
2185 }
2186 
2187 static int skl_build_pipe_wm(struct intel_atomic_state *state,
2188 			     struct intel_crtc *crtc)
2189 {
2190 	struct drm_i915_private *i915 = to_i915(crtc->base.dev);
2191 	struct intel_crtc_state *crtc_state =
2192 		intel_atomic_get_new_crtc_state(state, crtc);
2193 	const struct intel_plane_state *plane_state;
2194 	struct intel_plane *plane;
2195 	int ret, i;
2196 
2197 	for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
2198 		/*
2199 		 * FIXME should perhaps check {old,new}_plane_crtc->hw.crtc
2200 		 * instead but we don't populate that correctly for NV12 Y
2201 		 * planes so for now hack this.
2202 		 */
2203 		if (plane->pipe != crtc->pipe)
2204 			continue;
2205 
2206 		if (DISPLAY_VER(i915) >= 11)
2207 			ret = icl_build_plane_wm(crtc_state, plane_state);
2208 		else
2209 			ret = skl_build_plane_wm(crtc_state, plane_state);
2210 		if (ret)
2211 			return ret;
2212 	}
2213 
2214 	crtc_state->wm.skl.optimal = crtc_state->wm.skl.raw;
2215 
2216 	return 0;
2217 }
2218 
2219 static void skl_ddb_entry_write(struct drm_i915_private *i915,
2220 				i915_reg_t reg,
2221 				const struct skl_ddb_entry *entry)
2222 {
2223 	if (entry->end)
2224 		intel_de_write_fw(i915, reg,
2225 				  PLANE_BUF_END(entry->end - 1) |
2226 				  PLANE_BUF_START(entry->start));
2227 	else
2228 		intel_de_write_fw(i915, reg, 0);
2229 }
2230 
2231 static void skl_write_wm_level(struct drm_i915_private *i915,
2232 			       i915_reg_t reg,
2233 			       const struct skl_wm_level *level)
2234 {
2235 	u32 val = 0;
2236 
2237 	if (level->enable)
2238 		val |= PLANE_WM_EN;
2239 	if (level->ignore_lines)
2240 		val |= PLANE_WM_IGNORE_LINES;
2241 	val |= REG_FIELD_PREP(PLANE_WM_BLOCKS_MASK, level->blocks);
2242 	val |= REG_FIELD_PREP(PLANE_WM_LINES_MASK, level->lines);
2243 
2244 	intel_de_write_fw(i915, reg, val);
2245 }
2246 
2247 void skl_write_plane_wm(struct intel_plane *plane,
2248 			const struct intel_crtc_state *crtc_state)
2249 {
2250 	struct drm_i915_private *i915 = to_i915(plane->base.dev);
2251 	int level, max_level = ilk_wm_max_level(i915);
2252 	enum plane_id plane_id = plane->id;
2253 	enum pipe pipe = plane->pipe;
2254 	const struct skl_pipe_wm *pipe_wm = &crtc_state->wm.skl.optimal;
2255 	const struct skl_ddb_entry *ddb =
2256 		&crtc_state->wm.skl.plane_ddb[plane_id];
2257 	const struct skl_ddb_entry *ddb_y =
2258 		&crtc_state->wm.skl.plane_ddb_y[plane_id];
2259 
2260 	for (level = 0; level <= max_level; level++)
2261 		skl_write_wm_level(i915, PLANE_WM(pipe, plane_id, level),
2262 				   skl_plane_wm_level(pipe_wm, plane_id, level));
2263 
2264 	skl_write_wm_level(i915, PLANE_WM_TRANS(pipe, plane_id),
2265 			   skl_plane_trans_wm(pipe_wm, plane_id));
2266 
2267 	if (HAS_HW_SAGV_WM(i915)) {
2268 		const struct skl_plane_wm *wm = &pipe_wm->planes[plane_id];
2269 
2270 		skl_write_wm_level(i915, PLANE_WM_SAGV(pipe, plane_id),
2271 				   &wm->sagv.wm0);
2272 		skl_write_wm_level(i915, PLANE_WM_SAGV_TRANS(pipe, plane_id),
2273 				   &wm->sagv.trans_wm);
2274 	}
2275 
2276 	skl_ddb_entry_write(i915,
2277 			    PLANE_BUF_CFG(pipe, plane_id), ddb);
2278 
2279 	if (DISPLAY_VER(i915) < 11)
2280 		skl_ddb_entry_write(i915,
2281 				    PLANE_NV12_BUF_CFG(pipe, plane_id), ddb_y);
2282 }
2283 
2284 void skl_write_cursor_wm(struct intel_plane *plane,
2285 			 const struct intel_crtc_state *crtc_state)
2286 {
2287 	struct drm_i915_private *i915 = to_i915(plane->base.dev);
2288 	int level, max_level = ilk_wm_max_level(i915);
2289 	enum plane_id plane_id = plane->id;
2290 	enum pipe pipe = plane->pipe;
2291 	const struct skl_pipe_wm *pipe_wm = &crtc_state->wm.skl.optimal;
2292 	const struct skl_ddb_entry *ddb =
2293 		&crtc_state->wm.skl.plane_ddb[plane_id];
2294 
2295 	for (level = 0; level <= max_level; level++)
2296 		skl_write_wm_level(i915, CUR_WM(pipe, level),
2297 				   skl_plane_wm_level(pipe_wm, plane_id, level));
2298 
2299 	skl_write_wm_level(i915, CUR_WM_TRANS(pipe),
2300 			   skl_plane_trans_wm(pipe_wm, plane_id));
2301 
2302 	if (HAS_HW_SAGV_WM(i915)) {
2303 		const struct skl_plane_wm *wm = &pipe_wm->planes[plane_id];
2304 
2305 		skl_write_wm_level(i915, CUR_WM_SAGV(pipe),
2306 				   &wm->sagv.wm0);
2307 		skl_write_wm_level(i915, CUR_WM_SAGV_TRANS(pipe),
2308 				   &wm->sagv.trans_wm);
2309 	}
2310 
2311 	skl_ddb_entry_write(i915, CUR_BUF_CFG(pipe), ddb);
2312 }
2313 
2314 static bool skl_wm_level_equals(const struct skl_wm_level *l1,
2315 				const struct skl_wm_level *l2)
2316 {
2317 	return l1->enable == l2->enable &&
2318 		l1->ignore_lines == l2->ignore_lines &&
2319 		l1->lines == l2->lines &&
2320 		l1->blocks == l2->blocks;
2321 }
2322 
2323 static bool skl_plane_wm_equals(struct drm_i915_private *i915,
2324 				const struct skl_plane_wm *wm1,
2325 				const struct skl_plane_wm *wm2)
2326 {
2327 	int level, max_level = ilk_wm_max_level(i915);
2328 
2329 	for (level = 0; level <= max_level; level++) {
2330 		/*
2331 		 * We don't check uv_wm as the hardware doesn't actually
2332 		 * use it. It only gets used for calculating the required
2333 		 * ddb allocation.
2334 		 */
2335 		if (!skl_wm_level_equals(&wm1->wm[level], &wm2->wm[level]))
2336 			return false;
2337 	}
2338 
2339 	return skl_wm_level_equals(&wm1->trans_wm, &wm2->trans_wm) &&
2340 		skl_wm_level_equals(&wm1->sagv.wm0, &wm2->sagv.wm0) &&
2341 		skl_wm_level_equals(&wm1->sagv.trans_wm, &wm2->sagv.trans_wm);
2342 }
2343 
2344 static bool skl_ddb_entries_overlap(const struct skl_ddb_entry *a,
2345 				    const struct skl_ddb_entry *b)
2346 {
2347 	return a->start < b->end && b->start < a->end;
2348 }
2349 
2350 static void skl_ddb_entry_union(struct skl_ddb_entry *a,
2351 				const struct skl_ddb_entry *b)
2352 {
2353 	if (a->end && b->end) {
2354 		a->start = min(a->start, b->start);
2355 		a->end = max(a->end, b->end);
2356 	} else if (b->end) {
2357 		a->start = b->start;
2358 		a->end = b->end;
2359 	}
2360 }
2361 
2362 bool skl_ddb_allocation_overlaps(const struct skl_ddb_entry *ddb,
2363 				 const struct skl_ddb_entry *entries,
2364 				 int num_entries, int ignore_idx)
2365 {
2366 	int i;
2367 
2368 	for (i = 0; i < num_entries; i++) {
2369 		if (i != ignore_idx &&
2370 		    skl_ddb_entries_overlap(ddb, &entries[i]))
2371 			return true;
2372 	}
2373 
2374 	return false;
2375 }
2376 
2377 static int
2378 skl_ddb_add_affected_planes(const struct intel_crtc_state *old_crtc_state,
2379 			    struct intel_crtc_state *new_crtc_state)
2380 {
2381 	struct intel_atomic_state *state = to_intel_atomic_state(new_crtc_state->uapi.state);
2382 	struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->uapi.crtc);
2383 	struct drm_i915_private *i915 = to_i915(crtc->base.dev);
2384 	struct intel_plane *plane;
2385 
2386 	for_each_intel_plane_on_crtc(&i915->drm, crtc, plane) {
2387 		struct intel_plane_state *plane_state;
2388 		enum plane_id plane_id = plane->id;
2389 
2390 		if (skl_ddb_entry_equal(&old_crtc_state->wm.skl.plane_ddb[plane_id],
2391 					&new_crtc_state->wm.skl.plane_ddb[plane_id]) &&
2392 		    skl_ddb_entry_equal(&old_crtc_state->wm.skl.plane_ddb_y[plane_id],
2393 					&new_crtc_state->wm.skl.plane_ddb_y[plane_id]))
2394 			continue;
2395 
2396 		plane_state = intel_atomic_get_plane_state(state, plane);
2397 		if (IS_ERR(plane_state))
2398 			return PTR_ERR(plane_state);
2399 
2400 		new_crtc_state->update_planes |= BIT(plane_id);
2401 	}
2402 
2403 	return 0;
2404 }
2405 
2406 static u8 intel_dbuf_enabled_slices(const struct intel_dbuf_state *dbuf_state)
2407 {
2408 	struct drm_i915_private *i915 = to_i915(dbuf_state->base.state->base.dev);
2409 	u8 enabled_slices;
2410 	enum pipe pipe;
2411 
2412 	/*
2413 	 * FIXME: For now we always enable slice S1 as per
2414 	 * the Bspec display initialization sequence.
2415 	 */
2416 	enabled_slices = BIT(DBUF_S1);
2417 
2418 	for_each_pipe(i915, pipe)
2419 		enabled_slices |= dbuf_state->slices[pipe];
2420 
2421 	return enabled_slices;
2422 }
2423 
2424 static int
2425 skl_compute_ddb(struct intel_atomic_state *state)
2426 {
2427 	struct drm_i915_private *i915 = to_i915(state->base.dev);
2428 	const struct intel_dbuf_state *old_dbuf_state;
2429 	struct intel_dbuf_state *new_dbuf_state = NULL;
2430 	const struct intel_crtc_state *old_crtc_state;
2431 	struct intel_crtc_state *new_crtc_state;
2432 	struct intel_crtc *crtc;
2433 	int ret, i;
2434 
2435 	for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
2436 		new_dbuf_state = intel_atomic_get_dbuf_state(state);
2437 		if (IS_ERR(new_dbuf_state))
2438 			return PTR_ERR(new_dbuf_state);
2439 
2440 		old_dbuf_state = intel_atomic_get_old_dbuf_state(state);
2441 		break;
2442 	}
2443 
2444 	if (!new_dbuf_state)
2445 		return 0;
2446 
2447 	new_dbuf_state->active_pipes =
2448 		intel_calc_active_pipes(state, old_dbuf_state->active_pipes);
2449 
2450 	if (old_dbuf_state->active_pipes != new_dbuf_state->active_pipes) {
2451 		ret = intel_atomic_lock_global_state(&new_dbuf_state->base);
2452 		if (ret)
2453 			return ret;
2454 	}
2455 
2456 	if (HAS_MBUS_JOINING(i915))
2457 		new_dbuf_state->joined_mbus =
2458 			adlp_check_mbus_joined(new_dbuf_state->active_pipes);
2459 
2460 	for_each_intel_crtc(&i915->drm, crtc) {
2461 		enum pipe pipe = crtc->pipe;
2462 
2463 		new_dbuf_state->slices[pipe] =
2464 			skl_compute_dbuf_slices(crtc, new_dbuf_state->active_pipes,
2465 						new_dbuf_state->joined_mbus);
2466 
2467 		if (old_dbuf_state->slices[pipe] == new_dbuf_state->slices[pipe])
2468 			continue;
2469 
2470 		ret = intel_atomic_lock_global_state(&new_dbuf_state->base);
2471 		if (ret)
2472 			return ret;
2473 	}
2474 
2475 	new_dbuf_state->enabled_slices = intel_dbuf_enabled_slices(new_dbuf_state);
2476 
2477 	if (old_dbuf_state->enabled_slices != new_dbuf_state->enabled_slices ||
2478 	    old_dbuf_state->joined_mbus != new_dbuf_state->joined_mbus) {
2479 		ret = intel_atomic_serialize_global_state(&new_dbuf_state->base);
2480 		if (ret)
2481 			return ret;
2482 
2483 		if (old_dbuf_state->joined_mbus != new_dbuf_state->joined_mbus) {
2484 			/* TODO: Implement vblank synchronized MBUS joining changes */
2485 			ret = intel_modeset_all_pipes(state, "MBUS joining change");
2486 			if (ret)
2487 				return ret;
2488 		}
2489 
2490 		drm_dbg_kms(&i915->drm,
2491 			    "Enabled dbuf slices 0x%x -> 0x%x (total dbuf slices 0x%x), mbus joined? %s->%s\n",
2492 			    old_dbuf_state->enabled_slices,
2493 			    new_dbuf_state->enabled_slices,
2494 			    INTEL_INFO(i915)->display.dbuf.slice_mask,
2495 			    str_yes_no(old_dbuf_state->joined_mbus),
2496 			    str_yes_no(new_dbuf_state->joined_mbus));
2497 	}
2498 
2499 	for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
2500 		enum pipe pipe = crtc->pipe;
2501 
2502 		new_dbuf_state->weight[pipe] = intel_crtc_ddb_weight(new_crtc_state);
2503 
2504 		if (old_dbuf_state->weight[pipe] == new_dbuf_state->weight[pipe])
2505 			continue;
2506 
2507 		ret = intel_atomic_lock_global_state(&new_dbuf_state->base);
2508 		if (ret)
2509 			return ret;
2510 	}
2511 
2512 	for_each_intel_crtc(&i915->drm, crtc) {
2513 		ret = skl_crtc_allocate_ddb(state, crtc);
2514 		if (ret)
2515 			return ret;
2516 	}
2517 
2518 	for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
2519 					    new_crtc_state, i) {
2520 		ret = skl_crtc_allocate_plane_ddb(state, crtc);
2521 		if (ret)
2522 			return ret;
2523 
2524 		ret = skl_ddb_add_affected_planes(old_crtc_state,
2525 						  new_crtc_state);
2526 		if (ret)
2527 			return ret;
2528 	}
2529 
2530 	return 0;
2531 }
2532 
2533 static char enast(bool enable)
2534 {
2535 	return enable ? '*' : ' ';
2536 }
2537 
2538 static void
2539 skl_print_wm_changes(struct intel_atomic_state *state)
2540 {
2541 	struct drm_i915_private *i915 = to_i915(state->base.dev);
2542 	const struct intel_crtc_state *old_crtc_state;
2543 	const struct intel_crtc_state *new_crtc_state;
2544 	struct intel_plane *plane;
2545 	struct intel_crtc *crtc;
2546 	int i;
2547 
2548 	if (!drm_debug_enabled(DRM_UT_KMS))
2549 		return;
2550 
2551 	for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
2552 					    new_crtc_state, i) {
2553 		const struct skl_pipe_wm *old_pipe_wm, *new_pipe_wm;
2554 
2555 		old_pipe_wm = &old_crtc_state->wm.skl.optimal;
2556 		new_pipe_wm = &new_crtc_state->wm.skl.optimal;
2557 
2558 		for_each_intel_plane_on_crtc(&i915->drm, crtc, plane) {
2559 			enum plane_id plane_id = plane->id;
2560 			const struct skl_ddb_entry *old, *new;
2561 
2562 			old = &old_crtc_state->wm.skl.plane_ddb[plane_id];
2563 			new = &new_crtc_state->wm.skl.plane_ddb[plane_id];
2564 
2565 			if (skl_ddb_entry_equal(old, new))
2566 				continue;
2567 
2568 			drm_dbg_kms(&i915->drm,
2569 				    "[PLANE:%d:%s] ddb (%4d - %4d) -> (%4d - %4d), size %4d -> %4d\n",
2570 				    plane->base.base.id, plane->base.name,
2571 				    old->start, old->end, new->start, new->end,
2572 				    skl_ddb_entry_size(old), skl_ddb_entry_size(new));
2573 		}
2574 
2575 		for_each_intel_plane_on_crtc(&i915->drm, crtc, plane) {
2576 			enum plane_id plane_id = plane->id;
2577 			const struct skl_plane_wm *old_wm, *new_wm;
2578 
2579 			old_wm = &old_pipe_wm->planes[plane_id];
2580 			new_wm = &new_pipe_wm->planes[plane_id];
2581 
2582 			if (skl_plane_wm_equals(i915, old_wm, new_wm))
2583 				continue;
2584 
2585 			drm_dbg_kms(&i915->drm,
2586 				    "[PLANE:%d:%s]   level %cwm0,%cwm1,%cwm2,%cwm3,%cwm4,%cwm5,%cwm6,%cwm7,%ctwm,%cswm,%cstwm"
2587 				    " -> %cwm0,%cwm1,%cwm2,%cwm3,%cwm4,%cwm5,%cwm6,%cwm7,%ctwm,%cswm,%cstwm\n",
2588 				    plane->base.base.id, plane->base.name,
2589 				    enast(old_wm->wm[0].enable), enast(old_wm->wm[1].enable),
2590 				    enast(old_wm->wm[2].enable), enast(old_wm->wm[3].enable),
2591 				    enast(old_wm->wm[4].enable), enast(old_wm->wm[5].enable),
2592 				    enast(old_wm->wm[6].enable), enast(old_wm->wm[7].enable),
2593 				    enast(old_wm->trans_wm.enable),
2594 				    enast(old_wm->sagv.wm0.enable),
2595 				    enast(old_wm->sagv.trans_wm.enable),
2596 				    enast(new_wm->wm[0].enable), enast(new_wm->wm[1].enable),
2597 				    enast(new_wm->wm[2].enable), enast(new_wm->wm[3].enable),
2598 				    enast(new_wm->wm[4].enable), enast(new_wm->wm[5].enable),
2599 				    enast(new_wm->wm[6].enable), enast(new_wm->wm[7].enable),
2600 				    enast(new_wm->trans_wm.enable),
2601 				    enast(new_wm->sagv.wm0.enable),
2602 				    enast(new_wm->sagv.trans_wm.enable));
2603 
2604 			drm_dbg_kms(&i915->drm,
2605 				    "[PLANE:%d:%s]   lines %c%3d,%c%3d,%c%3d,%c%3d,%c%3d,%c%3d,%c%3d,%c%3d,%c%3d,%c%3d,%c%4d"
2606 				      " -> %c%3d,%c%3d,%c%3d,%c%3d,%c%3d,%c%3d,%c%3d,%c%3d,%c%3d,%c%3d,%c%4d\n",
2607 				    plane->base.base.id, plane->base.name,
2608 				    enast(old_wm->wm[0].ignore_lines), old_wm->wm[0].lines,
2609 				    enast(old_wm->wm[1].ignore_lines), old_wm->wm[1].lines,
2610 				    enast(old_wm->wm[2].ignore_lines), old_wm->wm[2].lines,
2611 				    enast(old_wm->wm[3].ignore_lines), old_wm->wm[3].lines,
2612 				    enast(old_wm->wm[4].ignore_lines), old_wm->wm[4].lines,
2613 				    enast(old_wm->wm[5].ignore_lines), old_wm->wm[5].lines,
2614 				    enast(old_wm->wm[6].ignore_lines), old_wm->wm[6].lines,
2615 				    enast(old_wm->wm[7].ignore_lines), old_wm->wm[7].lines,
2616 				    enast(old_wm->trans_wm.ignore_lines), old_wm->trans_wm.lines,
2617 				    enast(old_wm->sagv.wm0.ignore_lines), old_wm->sagv.wm0.lines,
2618 				    enast(old_wm->sagv.trans_wm.ignore_lines), old_wm->sagv.trans_wm.lines,
2619 				    enast(new_wm->wm[0].ignore_lines), new_wm->wm[0].lines,
2620 				    enast(new_wm->wm[1].ignore_lines), new_wm->wm[1].lines,
2621 				    enast(new_wm->wm[2].ignore_lines), new_wm->wm[2].lines,
2622 				    enast(new_wm->wm[3].ignore_lines), new_wm->wm[3].lines,
2623 				    enast(new_wm->wm[4].ignore_lines), new_wm->wm[4].lines,
2624 				    enast(new_wm->wm[5].ignore_lines), new_wm->wm[5].lines,
2625 				    enast(new_wm->wm[6].ignore_lines), new_wm->wm[6].lines,
2626 				    enast(new_wm->wm[7].ignore_lines), new_wm->wm[7].lines,
2627 				    enast(new_wm->trans_wm.ignore_lines), new_wm->trans_wm.lines,
2628 				    enast(new_wm->sagv.wm0.ignore_lines), new_wm->sagv.wm0.lines,
2629 				    enast(new_wm->sagv.trans_wm.ignore_lines), new_wm->sagv.trans_wm.lines);
2630 
2631 			drm_dbg_kms(&i915->drm,
2632 				    "[PLANE:%d:%s]  blocks %4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d,%5d"
2633 				    " -> %4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d,%5d\n",
2634 				    plane->base.base.id, plane->base.name,
2635 				    old_wm->wm[0].blocks, old_wm->wm[1].blocks,
2636 				    old_wm->wm[2].blocks, old_wm->wm[3].blocks,
2637 				    old_wm->wm[4].blocks, old_wm->wm[5].blocks,
2638 				    old_wm->wm[6].blocks, old_wm->wm[7].blocks,
2639 				    old_wm->trans_wm.blocks,
2640 				    old_wm->sagv.wm0.blocks,
2641 				    old_wm->sagv.trans_wm.blocks,
2642 				    new_wm->wm[0].blocks, new_wm->wm[1].blocks,
2643 				    new_wm->wm[2].blocks, new_wm->wm[3].blocks,
2644 				    new_wm->wm[4].blocks, new_wm->wm[5].blocks,
2645 				    new_wm->wm[6].blocks, new_wm->wm[7].blocks,
2646 				    new_wm->trans_wm.blocks,
2647 				    new_wm->sagv.wm0.blocks,
2648 				    new_wm->sagv.trans_wm.blocks);
2649 
2650 			drm_dbg_kms(&i915->drm,
2651 				    "[PLANE:%d:%s] min_ddb %4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d,%5d"
2652 				    " -> %4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d,%5d\n",
2653 				    plane->base.base.id, plane->base.name,
2654 				    old_wm->wm[0].min_ddb_alloc, old_wm->wm[1].min_ddb_alloc,
2655 				    old_wm->wm[2].min_ddb_alloc, old_wm->wm[3].min_ddb_alloc,
2656 				    old_wm->wm[4].min_ddb_alloc, old_wm->wm[5].min_ddb_alloc,
2657 				    old_wm->wm[6].min_ddb_alloc, old_wm->wm[7].min_ddb_alloc,
2658 				    old_wm->trans_wm.min_ddb_alloc,
2659 				    old_wm->sagv.wm0.min_ddb_alloc,
2660 				    old_wm->sagv.trans_wm.min_ddb_alloc,
2661 				    new_wm->wm[0].min_ddb_alloc, new_wm->wm[1].min_ddb_alloc,
2662 				    new_wm->wm[2].min_ddb_alloc, new_wm->wm[3].min_ddb_alloc,
2663 				    new_wm->wm[4].min_ddb_alloc, new_wm->wm[5].min_ddb_alloc,
2664 				    new_wm->wm[6].min_ddb_alloc, new_wm->wm[7].min_ddb_alloc,
2665 				    new_wm->trans_wm.min_ddb_alloc,
2666 				    new_wm->sagv.wm0.min_ddb_alloc,
2667 				    new_wm->sagv.trans_wm.min_ddb_alloc);
2668 		}
2669 	}
2670 }
2671 
2672 static bool skl_plane_selected_wm_equals(struct intel_plane *plane,
2673 					 const struct skl_pipe_wm *old_pipe_wm,
2674 					 const struct skl_pipe_wm *new_pipe_wm)
2675 {
2676 	struct drm_i915_private *i915 = to_i915(plane->base.dev);
2677 	int level, max_level = ilk_wm_max_level(i915);
2678 
2679 	for (level = 0; level <= max_level; level++) {
2680 		/*
2681 		 * We don't check uv_wm as the hardware doesn't actually
2682 		 * use it. It only gets used for calculating the required
2683 		 * ddb allocation.
2684 		 */
2685 		if (!skl_wm_level_equals(skl_plane_wm_level(old_pipe_wm, plane->id, level),
2686 					 skl_plane_wm_level(new_pipe_wm, plane->id, level)))
2687 			return false;
2688 	}
2689 
2690 	if (HAS_HW_SAGV_WM(i915)) {
2691 		const struct skl_plane_wm *old_wm = &old_pipe_wm->planes[plane->id];
2692 		const struct skl_plane_wm *new_wm = &new_pipe_wm->planes[plane->id];
2693 
2694 		if (!skl_wm_level_equals(&old_wm->sagv.wm0, &new_wm->sagv.wm0) ||
2695 		    !skl_wm_level_equals(&old_wm->sagv.trans_wm, &new_wm->sagv.trans_wm))
2696 			return false;
2697 	}
2698 
2699 	return skl_wm_level_equals(skl_plane_trans_wm(old_pipe_wm, plane->id),
2700 				   skl_plane_trans_wm(new_pipe_wm, plane->id));
2701 }
2702 
2703 /*
2704  * To make sure the cursor watermark registers are always consistent
2705  * with our computed state the following scenario needs special
2706  * treatment:
2707  *
2708  * 1. enable cursor
2709  * 2. move cursor entirely offscreen
2710  * 3. disable cursor
2711  *
2712  * Step 2. does call .disable_plane() but does not zero the watermarks
2713  * (since we consider an offscreen cursor still active for the purposes
2714  * of watermarks). Step 3. would not normally call .disable_plane()
2715  * because the actual plane visibility isn't changing, and we don't
2716  * deallocate the cursor ddb until the pipe gets disabled. So we must
2717  * force step 3. to call .disable_plane() to update the watermark
2718  * registers properly.
2719  *
2720  * Other planes do not suffer from this issues as their watermarks are
2721  * calculated based on the actual plane visibility. The only time this
2722  * can trigger for the other planes is during the initial readout as the
2723  * default value of the watermarks registers is not zero.
2724  */
2725 static int skl_wm_add_affected_planes(struct intel_atomic_state *state,
2726 				      struct intel_crtc *crtc)
2727 {
2728 	struct drm_i915_private *i915 = to_i915(crtc->base.dev);
2729 	const struct intel_crtc_state *old_crtc_state =
2730 		intel_atomic_get_old_crtc_state(state, crtc);
2731 	struct intel_crtc_state *new_crtc_state =
2732 		intel_atomic_get_new_crtc_state(state, crtc);
2733 	struct intel_plane *plane;
2734 
2735 	for_each_intel_plane_on_crtc(&i915->drm, crtc, plane) {
2736 		struct intel_plane_state *plane_state;
2737 		enum plane_id plane_id = plane->id;
2738 
2739 		/*
2740 		 * Force a full wm update for every plane on modeset.
2741 		 * Required because the reset value of the wm registers
2742 		 * is non-zero, whereas we want all disabled planes to
2743 		 * have zero watermarks. So if we turn off the relevant
2744 		 * power well the hardware state will go out of sync
2745 		 * with the software state.
2746 		 */
2747 		if (!intel_crtc_needs_modeset(new_crtc_state) &&
2748 		    skl_plane_selected_wm_equals(plane,
2749 						 &old_crtc_state->wm.skl.optimal,
2750 						 &new_crtc_state->wm.skl.optimal))
2751 			continue;
2752 
2753 		plane_state = intel_atomic_get_plane_state(state, plane);
2754 		if (IS_ERR(plane_state))
2755 			return PTR_ERR(plane_state);
2756 
2757 		new_crtc_state->update_planes |= BIT(plane_id);
2758 	}
2759 
2760 	return 0;
2761 }
2762 
2763 static int
2764 skl_compute_wm(struct intel_atomic_state *state)
2765 {
2766 	struct intel_crtc *crtc;
2767 	struct intel_crtc_state *new_crtc_state;
2768 	int ret, i;
2769 
2770 	for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
2771 		ret = skl_build_pipe_wm(state, crtc);
2772 		if (ret)
2773 			return ret;
2774 	}
2775 
2776 	ret = skl_compute_ddb(state);
2777 	if (ret)
2778 		return ret;
2779 
2780 	ret = intel_compute_sagv_mask(state);
2781 	if (ret)
2782 		return ret;
2783 
2784 	/*
2785 	 * skl_compute_ddb() will have adjusted the final watermarks
2786 	 * based on how much ddb is available. Now we can actually
2787 	 * check if the final watermarks changed.
2788 	 */
2789 	for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
2790 		ret = skl_wm_add_affected_planes(state, crtc);
2791 		if (ret)
2792 			return ret;
2793 	}
2794 
2795 	skl_print_wm_changes(state);
2796 
2797 	return 0;
2798 }
2799 
2800 static void skl_wm_level_from_reg_val(u32 val, struct skl_wm_level *level)
2801 {
2802 	level->enable = val & PLANE_WM_EN;
2803 	level->ignore_lines = val & PLANE_WM_IGNORE_LINES;
2804 	level->blocks = REG_FIELD_GET(PLANE_WM_BLOCKS_MASK, val);
2805 	level->lines = REG_FIELD_GET(PLANE_WM_LINES_MASK, val);
2806 }
2807 
2808 static void skl_pipe_wm_get_hw_state(struct intel_crtc *crtc,
2809 				     struct skl_pipe_wm *out)
2810 {
2811 	struct drm_i915_private *i915 = to_i915(crtc->base.dev);
2812 	enum pipe pipe = crtc->pipe;
2813 	int level, max_level;
2814 	enum plane_id plane_id;
2815 	u32 val;
2816 
2817 	max_level = ilk_wm_max_level(i915);
2818 
2819 	for_each_plane_id_on_crtc(crtc, plane_id) {
2820 		struct skl_plane_wm *wm = &out->planes[plane_id];
2821 
2822 		for (level = 0; level <= max_level; level++) {
2823 			if (plane_id != PLANE_CURSOR)
2824 				val = intel_uncore_read(&i915->uncore, PLANE_WM(pipe, plane_id, level));
2825 			else
2826 				val = intel_uncore_read(&i915->uncore, CUR_WM(pipe, level));
2827 
2828 			skl_wm_level_from_reg_val(val, &wm->wm[level]);
2829 		}
2830 
2831 		if (plane_id != PLANE_CURSOR)
2832 			val = intel_uncore_read(&i915->uncore, PLANE_WM_TRANS(pipe, plane_id));
2833 		else
2834 			val = intel_uncore_read(&i915->uncore, CUR_WM_TRANS(pipe));
2835 
2836 		skl_wm_level_from_reg_val(val, &wm->trans_wm);
2837 
2838 		if (HAS_HW_SAGV_WM(i915)) {
2839 			if (plane_id != PLANE_CURSOR)
2840 				val = intel_uncore_read(&i915->uncore,
2841 							PLANE_WM_SAGV(pipe, plane_id));
2842 			else
2843 				val = intel_uncore_read(&i915->uncore,
2844 							CUR_WM_SAGV(pipe));
2845 
2846 			skl_wm_level_from_reg_val(val, &wm->sagv.wm0);
2847 
2848 			if (plane_id != PLANE_CURSOR)
2849 				val = intel_uncore_read(&i915->uncore,
2850 							PLANE_WM_SAGV_TRANS(pipe, plane_id));
2851 			else
2852 				val = intel_uncore_read(&i915->uncore,
2853 							CUR_WM_SAGV_TRANS(pipe));
2854 
2855 			skl_wm_level_from_reg_val(val, &wm->sagv.trans_wm);
2856 		} else if (DISPLAY_VER(i915) >= 12) {
2857 			wm->sagv.wm0 = wm->wm[0];
2858 			wm->sagv.trans_wm = wm->trans_wm;
2859 		}
2860 	}
2861 }
2862 
2863 void skl_wm_get_hw_state(struct drm_i915_private *i915)
2864 {
2865 	struct intel_dbuf_state *dbuf_state =
2866 		to_intel_dbuf_state(i915->display.dbuf.obj.state);
2867 	struct intel_crtc *crtc;
2868 
2869 	if (HAS_MBUS_JOINING(i915))
2870 		dbuf_state->joined_mbus = intel_de_read(i915, MBUS_CTL) & MBUS_JOIN;
2871 
2872 	for_each_intel_crtc(&i915->drm, crtc) {
2873 		struct intel_crtc_state *crtc_state =
2874 			to_intel_crtc_state(crtc->base.state);
2875 		enum pipe pipe = crtc->pipe;
2876 		unsigned int mbus_offset;
2877 		enum plane_id plane_id;
2878 		u8 slices;
2879 
2880 		memset(&crtc_state->wm.skl.optimal, 0,
2881 		       sizeof(crtc_state->wm.skl.optimal));
2882 		if (crtc_state->hw.active)
2883 			skl_pipe_wm_get_hw_state(crtc, &crtc_state->wm.skl.optimal);
2884 		crtc_state->wm.skl.raw = crtc_state->wm.skl.optimal;
2885 
2886 		memset(&dbuf_state->ddb[pipe], 0, sizeof(dbuf_state->ddb[pipe]));
2887 
2888 		for_each_plane_id_on_crtc(crtc, plane_id) {
2889 			struct skl_ddb_entry *ddb =
2890 				&crtc_state->wm.skl.plane_ddb[plane_id];
2891 			struct skl_ddb_entry *ddb_y =
2892 				&crtc_state->wm.skl.plane_ddb_y[plane_id];
2893 
2894 			if (!crtc_state->hw.active)
2895 				continue;
2896 
2897 			skl_ddb_get_hw_plane_state(i915, crtc->pipe,
2898 						   plane_id, ddb, ddb_y);
2899 
2900 			skl_ddb_entry_union(&dbuf_state->ddb[pipe], ddb);
2901 			skl_ddb_entry_union(&dbuf_state->ddb[pipe], ddb_y);
2902 		}
2903 
2904 		dbuf_state->weight[pipe] = intel_crtc_ddb_weight(crtc_state);
2905 
2906 		/*
2907 		 * Used for checking overlaps, so we need absolute
2908 		 * offsets instead of MBUS relative offsets.
2909 		 */
2910 		slices = skl_compute_dbuf_slices(crtc, dbuf_state->active_pipes,
2911 						 dbuf_state->joined_mbus);
2912 		mbus_offset = mbus_ddb_offset(i915, slices);
2913 		crtc_state->wm.skl.ddb.start = mbus_offset + dbuf_state->ddb[pipe].start;
2914 		crtc_state->wm.skl.ddb.end = mbus_offset + dbuf_state->ddb[pipe].end;
2915 
2916 		/* The slices actually used by the planes on the pipe */
2917 		dbuf_state->slices[pipe] =
2918 			skl_ddb_dbuf_slice_mask(i915, &crtc_state->wm.skl.ddb);
2919 
2920 		drm_dbg_kms(&i915->drm,
2921 			    "[CRTC:%d:%s] dbuf slices 0x%x, ddb (%d - %d), active pipes 0x%x, mbus joined: %s\n",
2922 			    crtc->base.base.id, crtc->base.name,
2923 			    dbuf_state->slices[pipe], dbuf_state->ddb[pipe].start,
2924 			    dbuf_state->ddb[pipe].end, dbuf_state->active_pipes,
2925 			    str_yes_no(dbuf_state->joined_mbus));
2926 	}
2927 
2928 	dbuf_state->enabled_slices = i915->display.dbuf.enabled_slices;
2929 }
2930 
2931 static bool skl_dbuf_is_misconfigured(struct drm_i915_private *i915)
2932 {
2933 	const struct intel_dbuf_state *dbuf_state =
2934 		to_intel_dbuf_state(i915->display.dbuf.obj.state);
2935 	struct skl_ddb_entry entries[I915_MAX_PIPES] = {};
2936 	struct intel_crtc *crtc;
2937 
2938 	for_each_intel_crtc(&i915->drm, crtc) {
2939 		const struct intel_crtc_state *crtc_state =
2940 			to_intel_crtc_state(crtc->base.state);
2941 
2942 		entries[crtc->pipe] = crtc_state->wm.skl.ddb;
2943 	}
2944 
2945 	for_each_intel_crtc(&i915->drm, crtc) {
2946 		const struct intel_crtc_state *crtc_state =
2947 			to_intel_crtc_state(crtc->base.state);
2948 		u8 slices;
2949 
2950 		slices = skl_compute_dbuf_slices(crtc, dbuf_state->active_pipes,
2951 						 dbuf_state->joined_mbus);
2952 		if (dbuf_state->slices[crtc->pipe] & ~slices)
2953 			return true;
2954 
2955 		if (skl_ddb_allocation_overlaps(&crtc_state->wm.skl.ddb, entries,
2956 						I915_MAX_PIPES, crtc->pipe))
2957 			return true;
2958 	}
2959 
2960 	return false;
2961 }
2962 
2963 void skl_wm_sanitize(struct drm_i915_private *i915)
2964 {
2965 	struct intel_crtc *crtc;
2966 
2967 	/*
2968 	 * On TGL/RKL (at least) the BIOS likes to assign the planes
2969 	 * to the wrong DBUF slices. This will cause an infinite loop
2970 	 * in skl_commit_modeset_enables() as it can't find a way to
2971 	 * transition between the old bogus DBUF layout to the new
2972 	 * proper DBUF layout without DBUF allocation overlaps between
2973 	 * the planes (which cannot be allowed or else the hardware
2974 	 * may hang). If we detect a bogus DBUF layout just turn off
2975 	 * all the planes so that skl_commit_modeset_enables() can
2976 	 * simply ignore them.
2977 	 */
2978 	if (!skl_dbuf_is_misconfigured(i915))
2979 		return;
2980 
2981 	drm_dbg_kms(&i915->drm, "BIOS has misprogrammed the DBUF, disabling all planes\n");
2982 
2983 	for_each_intel_crtc(&i915->drm, crtc) {
2984 		struct intel_plane *plane = to_intel_plane(crtc->base.primary);
2985 		const struct intel_plane_state *plane_state =
2986 			to_intel_plane_state(plane->base.state);
2987 		struct intel_crtc_state *crtc_state =
2988 			to_intel_crtc_state(crtc->base.state);
2989 
2990 		if (plane_state->uapi.visible)
2991 			intel_plane_disable_noatomic(crtc, plane);
2992 
2993 		drm_WARN_ON(&i915->drm, crtc_state->active_planes != 0);
2994 
2995 		memset(&crtc_state->wm.skl.ddb, 0, sizeof(crtc_state->wm.skl.ddb));
2996 	}
2997 }
2998 
2999 void intel_wm_state_verify(struct intel_crtc *crtc,
3000 			   struct intel_crtc_state *new_crtc_state)
3001 {
3002 	struct drm_i915_private *i915 = to_i915(crtc->base.dev);
3003 	struct skl_hw_state {
3004 		struct skl_ddb_entry ddb[I915_MAX_PLANES];
3005 		struct skl_ddb_entry ddb_y[I915_MAX_PLANES];
3006 		struct skl_pipe_wm wm;
3007 	} *hw;
3008 	const struct skl_pipe_wm *sw_wm = &new_crtc_state->wm.skl.optimal;
3009 	int level, max_level = ilk_wm_max_level(i915);
3010 	struct intel_plane *plane;
3011 	u8 hw_enabled_slices;
3012 
3013 	if (DISPLAY_VER(i915) < 9 || !new_crtc_state->hw.active)
3014 		return;
3015 
3016 	hw = kzalloc(sizeof(*hw), GFP_KERNEL);
3017 	if (!hw)
3018 		return;
3019 
3020 	skl_pipe_wm_get_hw_state(crtc, &hw->wm);
3021 
3022 	skl_pipe_ddb_get_hw_state(crtc, hw->ddb, hw->ddb_y);
3023 
3024 	hw_enabled_slices = intel_enabled_dbuf_slices_mask(i915);
3025 
3026 	if (DISPLAY_VER(i915) >= 11 &&
3027 	    hw_enabled_slices != i915->display.dbuf.enabled_slices)
3028 		drm_err(&i915->drm,
3029 			"mismatch in DBUF Slices (expected 0x%x, got 0x%x)\n",
3030 			i915->display.dbuf.enabled_slices,
3031 			hw_enabled_slices);
3032 
3033 	for_each_intel_plane_on_crtc(&i915->drm, crtc, plane) {
3034 		const struct skl_ddb_entry *hw_ddb_entry, *sw_ddb_entry;
3035 		const struct skl_wm_level *hw_wm_level, *sw_wm_level;
3036 
3037 		/* Watermarks */
3038 		for (level = 0; level <= max_level; level++) {
3039 			hw_wm_level = &hw->wm.planes[plane->id].wm[level];
3040 			sw_wm_level = skl_plane_wm_level(sw_wm, plane->id, level);
3041 
3042 			if (skl_wm_level_equals(hw_wm_level, sw_wm_level))
3043 				continue;
3044 
3045 			drm_err(&i915->drm,
3046 				"[PLANE:%d:%s] mismatch in WM%d (expected e=%d b=%u l=%u, got e=%d b=%u l=%u)\n",
3047 				plane->base.base.id, plane->base.name, level,
3048 				sw_wm_level->enable,
3049 				sw_wm_level->blocks,
3050 				sw_wm_level->lines,
3051 				hw_wm_level->enable,
3052 				hw_wm_level->blocks,
3053 				hw_wm_level->lines);
3054 		}
3055 
3056 		hw_wm_level = &hw->wm.planes[plane->id].trans_wm;
3057 		sw_wm_level = skl_plane_trans_wm(sw_wm, plane->id);
3058 
3059 		if (!skl_wm_level_equals(hw_wm_level, sw_wm_level)) {
3060 			drm_err(&i915->drm,
3061 				"[PLANE:%d:%s] mismatch in trans WM (expected e=%d b=%u l=%u, got e=%d b=%u l=%u)\n",
3062 				plane->base.base.id, plane->base.name,
3063 				sw_wm_level->enable,
3064 				sw_wm_level->blocks,
3065 				sw_wm_level->lines,
3066 				hw_wm_level->enable,
3067 				hw_wm_level->blocks,
3068 				hw_wm_level->lines);
3069 		}
3070 
3071 		hw_wm_level = &hw->wm.planes[plane->id].sagv.wm0;
3072 		sw_wm_level = &sw_wm->planes[plane->id].sagv.wm0;
3073 
3074 		if (HAS_HW_SAGV_WM(i915) &&
3075 		    !skl_wm_level_equals(hw_wm_level, sw_wm_level)) {
3076 			drm_err(&i915->drm,
3077 				"[PLANE:%d:%s] mismatch in SAGV WM (expected e=%d b=%u l=%u, got e=%d b=%u l=%u)\n",
3078 				plane->base.base.id, plane->base.name,
3079 				sw_wm_level->enable,
3080 				sw_wm_level->blocks,
3081 				sw_wm_level->lines,
3082 				hw_wm_level->enable,
3083 				hw_wm_level->blocks,
3084 				hw_wm_level->lines);
3085 		}
3086 
3087 		hw_wm_level = &hw->wm.planes[plane->id].sagv.trans_wm;
3088 		sw_wm_level = &sw_wm->planes[plane->id].sagv.trans_wm;
3089 
3090 		if (HAS_HW_SAGV_WM(i915) &&
3091 		    !skl_wm_level_equals(hw_wm_level, sw_wm_level)) {
3092 			drm_err(&i915->drm,
3093 				"[PLANE:%d:%s] mismatch in SAGV trans WM (expected e=%d b=%u l=%u, got e=%d b=%u l=%u)\n",
3094 				plane->base.base.id, plane->base.name,
3095 				sw_wm_level->enable,
3096 				sw_wm_level->blocks,
3097 				sw_wm_level->lines,
3098 				hw_wm_level->enable,
3099 				hw_wm_level->blocks,
3100 				hw_wm_level->lines);
3101 		}
3102 
3103 		/* DDB */
3104 		hw_ddb_entry = &hw->ddb[PLANE_CURSOR];
3105 		sw_ddb_entry = &new_crtc_state->wm.skl.plane_ddb[PLANE_CURSOR];
3106 
3107 		if (!skl_ddb_entry_equal(hw_ddb_entry, sw_ddb_entry)) {
3108 			drm_err(&i915->drm,
3109 				"[PLANE:%d:%s] mismatch in DDB (expected (%u,%u), found (%u,%u))\n",
3110 				plane->base.base.id, plane->base.name,
3111 				sw_ddb_entry->start, sw_ddb_entry->end,
3112 				hw_ddb_entry->start, hw_ddb_entry->end);
3113 		}
3114 	}
3115 
3116 	kfree(hw);
3117 }
3118 
3119 bool skl_watermark_ipc_enabled(struct drm_i915_private *i915)
3120 {
3121 	return i915->display.wm.ipc_enabled;
3122 }
3123 
3124 void skl_watermark_ipc_update(struct drm_i915_private *i915)
3125 {
3126 	if (!HAS_IPC(i915))
3127 		return;
3128 
3129 	intel_uncore_rmw(&i915->uncore, DISP_ARB_CTL2, DISP_IPC_ENABLE,
3130 			 skl_watermark_ipc_enabled(i915) ? DISP_IPC_ENABLE : 0);
3131 }
3132 
3133 static bool skl_watermark_ipc_can_enable(struct drm_i915_private *i915)
3134 {
3135 	/* Display WA #0477 WaDisableIPC: skl */
3136 	if (IS_SKYLAKE(i915))
3137 		return false;
3138 
3139 	/* Display WA #1141: SKL:all KBL:all CFL */
3140 	if (IS_KABYLAKE(i915) ||
3141 	    IS_COFFEELAKE(i915) ||
3142 	    IS_COMETLAKE(i915))
3143 		return i915->dram_info.symmetric_memory;
3144 
3145 	return true;
3146 }
3147 
3148 void skl_watermark_ipc_init(struct drm_i915_private *i915)
3149 {
3150 	if (!HAS_IPC(i915))
3151 		return;
3152 
3153 	i915->display.wm.ipc_enabled = skl_watermark_ipc_can_enable(i915);
3154 
3155 	skl_watermark_ipc_update(i915);
3156 }
3157 
3158 static void
3159 adjust_wm_latency(struct drm_i915_private *i915,
3160 		  u16 wm[], int max_level, int read_latency)
3161 {
3162 	bool wm_lv_0_adjust_needed = i915->dram_info.wm_lv_0_adjust_needed;
3163 	int i, level;
3164 
3165 	/*
3166 	 * If a level n (n > 1) has a 0us latency, all levels m (m >= n)
3167 	 * need to be disabled. We make sure to sanitize the values out
3168 	 * of the punit to satisfy this requirement.
3169 	 */
3170 	for (level = 1; level <= max_level; level++) {
3171 		if (wm[level] == 0) {
3172 			for (i = level + 1; i <= max_level; i++)
3173 				wm[i] = 0;
3174 
3175 			max_level = level - 1;
3176 			break;
3177 		}
3178 	}
3179 
3180 	/*
3181 	 * WaWmMemoryReadLatency
3182 	 *
3183 	 * punit doesn't take into account the read latency so we need
3184 	 * to add proper adjustement to each valid level we retrieve
3185 	 * from the punit when level 0 response data is 0us.
3186 	 */
3187 	if (wm[0] == 0) {
3188 		for (level = 0; level <= max_level; level++)
3189 			wm[level] += read_latency;
3190 	}
3191 
3192 	/*
3193 	 * WA Level-0 adjustment for 16GB DIMMs: SKL+
3194 	 * If we could not get dimm info enable this WA to prevent from
3195 	 * any underrun. If not able to get Dimm info assume 16GB dimm
3196 	 * to avoid any underrun.
3197 	 */
3198 	if (wm_lv_0_adjust_needed)
3199 		wm[0] += 1;
3200 }
3201 
3202 static void mtl_read_wm_latency(struct drm_i915_private *i915, u16 wm[])
3203 {
3204 	struct intel_uncore *uncore = &i915->uncore;
3205 	int max_level = ilk_wm_max_level(i915);
3206 	u32 val;
3207 
3208 	val = intel_uncore_read(uncore, MTL_LATENCY_LP0_LP1);
3209 	wm[0] = REG_FIELD_GET(MTL_LATENCY_LEVEL_EVEN_MASK, val);
3210 	wm[1] = REG_FIELD_GET(MTL_LATENCY_LEVEL_ODD_MASK, val);
3211 
3212 	val = intel_uncore_read(uncore, MTL_LATENCY_LP2_LP3);
3213 	wm[2] = REG_FIELD_GET(MTL_LATENCY_LEVEL_EVEN_MASK, val);
3214 	wm[3] = REG_FIELD_GET(MTL_LATENCY_LEVEL_ODD_MASK, val);
3215 
3216 	val = intel_uncore_read(uncore, MTL_LATENCY_LP4_LP5);
3217 	wm[4] = REG_FIELD_GET(MTL_LATENCY_LEVEL_EVEN_MASK, val);
3218 	wm[5] = REG_FIELD_GET(MTL_LATENCY_LEVEL_ODD_MASK, val);
3219 
3220 	adjust_wm_latency(i915, wm, max_level, 6);
3221 }
3222 
3223 static void skl_read_wm_latency(struct drm_i915_private *i915, u16 wm[])
3224 {
3225 	int max_level = ilk_wm_max_level(i915);
3226 	int read_latency = DISPLAY_VER(i915) >= 12 ? 3 : 2;
3227 	int mult = IS_DG2(i915) ? 2 : 1;
3228 	u32 val;
3229 	int ret;
3230 
3231 	/* read the first set of memory latencies[0:3] */
3232 	val = 0; /* data0 to be programmed to 0 for first set */
3233 	ret = snb_pcode_read(&i915->uncore, GEN9_PCODE_READ_MEM_LATENCY, &val, NULL);
3234 	if (ret) {
3235 		drm_err(&i915->drm, "SKL Mailbox read error = %d\n", ret);
3236 		return;
3237 	}
3238 
3239 	wm[0] = REG_FIELD_GET(GEN9_MEM_LATENCY_LEVEL_0_4_MASK, val) * mult;
3240 	wm[1] = REG_FIELD_GET(GEN9_MEM_LATENCY_LEVEL_1_5_MASK, val) * mult;
3241 	wm[2] = REG_FIELD_GET(GEN9_MEM_LATENCY_LEVEL_2_6_MASK, val) * mult;
3242 	wm[3] = REG_FIELD_GET(GEN9_MEM_LATENCY_LEVEL_3_7_MASK, val) * mult;
3243 
3244 	/* read the second set of memory latencies[4:7] */
3245 	val = 1; /* data0 to be programmed to 1 for second set */
3246 	ret = snb_pcode_read(&i915->uncore, GEN9_PCODE_READ_MEM_LATENCY, &val, NULL);
3247 	if (ret) {
3248 		drm_err(&i915->drm, "SKL Mailbox read error = %d\n", ret);
3249 		return;
3250 	}
3251 
3252 	wm[4] = REG_FIELD_GET(GEN9_MEM_LATENCY_LEVEL_0_4_MASK, val) * mult;
3253 	wm[5] = REG_FIELD_GET(GEN9_MEM_LATENCY_LEVEL_1_5_MASK, val) * mult;
3254 	wm[6] = REG_FIELD_GET(GEN9_MEM_LATENCY_LEVEL_2_6_MASK, val) * mult;
3255 	wm[7] = REG_FIELD_GET(GEN9_MEM_LATENCY_LEVEL_3_7_MASK, val) * mult;
3256 
3257 	adjust_wm_latency(i915, wm, max_level, read_latency);
3258 }
3259 
3260 static void skl_setup_wm_latency(struct drm_i915_private *i915)
3261 {
3262 	if (DISPLAY_VER(i915) >= 14)
3263 		mtl_read_wm_latency(i915, i915->display.wm.skl_latency);
3264 	else
3265 		skl_read_wm_latency(i915, i915->display.wm.skl_latency);
3266 
3267 	intel_print_wm_latency(i915, "Gen9 Plane", i915->display.wm.skl_latency);
3268 }
3269 
3270 static const struct intel_wm_funcs skl_wm_funcs = {
3271 	.compute_global_watermarks = skl_compute_wm,
3272 };
3273 
3274 void skl_wm_init(struct drm_i915_private *i915)
3275 {
3276 	intel_sagv_init(i915);
3277 
3278 	skl_setup_wm_latency(i915);
3279 
3280 	i915->display.funcs.wm = &skl_wm_funcs;
3281 }
3282 
3283 static struct intel_global_state *intel_dbuf_duplicate_state(struct intel_global_obj *obj)
3284 {
3285 	struct intel_dbuf_state *dbuf_state;
3286 
3287 	dbuf_state = kmemdup(obj->state, sizeof(*dbuf_state), GFP_KERNEL);
3288 	if (!dbuf_state)
3289 		return NULL;
3290 
3291 	return &dbuf_state->base;
3292 }
3293 
3294 static void intel_dbuf_destroy_state(struct intel_global_obj *obj,
3295 				     struct intel_global_state *state)
3296 {
3297 	kfree(state);
3298 }
3299 
3300 static const struct intel_global_state_funcs intel_dbuf_funcs = {
3301 	.atomic_duplicate_state = intel_dbuf_duplicate_state,
3302 	.atomic_destroy_state = intel_dbuf_destroy_state,
3303 };
3304 
3305 struct intel_dbuf_state *
3306 intel_atomic_get_dbuf_state(struct intel_atomic_state *state)
3307 {
3308 	struct drm_i915_private *i915 = to_i915(state->base.dev);
3309 	struct intel_global_state *dbuf_state;
3310 
3311 	dbuf_state = intel_atomic_get_global_obj_state(state, &i915->display.dbuf.obj);
3312 	if (IS_ERR(dbuf_state))
3313 		return ERR_CAST(dbuf_state);
3314 
3315 	return to_intel_dbuf_state(dbuf_state);
3316 }
3317 
3318 int intel_dbuf_init(struct drm_i915_private *i915)
3319 {
3320 	struct intel_dbuf_state *dbuf_state;
3321 
3322 	dbuf_state = kzalloc(sizeof(*dbuf_state), GFP_KERNEL);
3323 	if (!dbuf_state)
3324 		return -ENOMEM;
3325 
3326 	intel_atomic_global_obj_init(i915, &i915->display.dbuf.obj,
3327 				     &dbuf_state->base, &intel_dbuf_funcs);
3328 
3329 	return 0;
3330 }
3331 
3332 /*
3333  * Configure MBUS_CTL and all DBUF_CTL_S of each slice to join_mbus state before
3334  * update the request state of all DBUS slices.
3335  */
3336 static void update_mbus_pre_enable(struct intel_atomic_state *state)
3337 {
3338 	struct drm_i915_private *i915 = to_i915(state->base.dev);
3339 	u32 mbus_ctl, dbuf_min_tracker_val;
3340 	enum dbuf_slice slice;
3341 	const struct intel_dbuf_state *dbuf_state =
3342 		intel_atomic_get_new_dbuf_state(state);
3343 
3344 	if (!HAS_MBUS_JOINING(i915))
3345 		return;
3346 
3347 	/*
3348 	 * TODO: Implement vblank synchronized MBUS joining changes.
3349 	 * Must be properly coordinated with dbuf reprogramming.
3350 	 */
3351 	if (dbuf_state->joined_mbus) {
3352 		mbus_ctl = MBUS_HASHING_MODE_1x4 | MBUS_JOIN |
3353 			MBUS_JOIN_PIPE_SELECT_NONE;
3354 		dbuf_min_tracker_val = DBUF_MIN_TRACKER_STATE_SERVICE(3);
3355 	} else {
3356 		mbus_ctl = MBUS_HASHING_MODE_2x2 |
3357 			MBUS_JOIN_PIPE_SELECT_NONE;
3358 		dbuf_min_tracker_val = DBUF_MIN_TRACKER_STATE_SERVICE(1);
3359 	}
3360 
3361 	intel_de_rmw(i915, MBUS_CTL,
3362 		     MBUS_HASHING_MODE_MASK | MBUS_JOIN |
3363 		     MBUS_JOIN_PIPE_SELECT_MASK, mbus_ctl);
3364 
3365 	for_each_dbuf_slice(i915, slice)
3366 		intel_de_rmw(i915, DBUF_CTL_S(slice),
3367 			     DBUF_MIN_TRACKER_STATE_SERVICE_MASK,
3368 			     dbuf_min_tracker_val);
3369 }
3370 
3371 void intel_dbuf_pre_plane_update(struct intel_atomic_state *state)
3372 {
3373 	struct drm_i915_private *i915 = to_i915(state->base.dev);
3374 	const struct intel_dbuf_state *new_dbuf_state =
3375 		intel_atomic_get_new_dbuf_state(state);
3376 	const struct intel_dbuf_state *old_dbuf_state =
3377 		intel_atomic_get_old_dbuf_state(state);
3378 
3379 	if (!new_dbuf_state ||
3380 	    (new_dbuf_state->enabled_slices == old_dbuf_state->enabled_slices &&
3381 	     new_dbuf_state->joined_mbus == old_dbuf_state->joined_mbus))
3382 		return;
3383 
3384 	WARN_ON(!new_dbuf_state->base.changed);
3385 
3386 	update_mbus_pre_enable(state);
3387 	gen9_dbuf_slices_update(i915,
3388 				old_dbuf_state->enabled_slices |
3389 				new_dbuf_state->enabled_slices);
3390 }
3391 
3392 void intel_dbuf_post_plane_update(struct intel_atomic_state *state)
3393 {
3394 	struct drm_i915_private *i915 = to_i915(state->base.dev);
3395 	const struct intel_dbuf_state *new_dbuf_state =
3396 		intel_atomic_get_new_dbuf_state(state);
3397 	const struct intel_dbuf_state *old_dbuf_state =
3398 		intel_atomic_get_old_dbuf_state(state);
3399 
3400 	if (!new_dbuf_state ||
3401 	    (new_dbuf_state->enabled_slices == old_dbuf_state->enabled_slices &&
3402 	     new_dbuf_state->joined_mbus == old_dbuf_state->joined_mbus))
3403 		return;
3404 
3405 	WARN_ON(!new_dbuf_state->base.changed);
3406 
3407 	gen9_dbuf_slices_update(i915,
3408 				new_dbuf_state->enabled_slices);
3409 }
3410 
3411 static bool xelpdp_is_only_pipe_per_dbuf_bank(enum pipe pipe, u8 active_pipes)
3412 {
3413 	switch (pipe) {
3414 	case PIPE_A:
3415 		return !(active_pipes & BIT(PIPE_D));
3416 	case PIPE_D:
3417 		return !(active_pipes & BIT(PIPE_A));
3418 	case PIPE_B:
3419 		return !(active_pipes & BIT(PIPE_C));
3420 	case PIPE_C:
3421 		return !(active_pipes & BIT(PIPE_B));
3422 	default: /* to suppress compiler warning */
3423 		MISSING_CASE(pipe);
3424 		break;
3425 	}
3426 
3427 	return false;
3428 }
3429 
3430 void intel_mbus_dbox_update(struct intel_atomic_state *state)
3431 {
3432 	struct drm_i915_private *i915 = to_i915(state->base.dev);
3433 	const struct intel_dbuf_state *new_dbuf_state, *old_dbuf_state;
3434 	const struct intel_crtc_state *new_crtc_state;
3435 	const struct intel_crtc *crtc;
3436 	u32 val = 0;
3437 	int i;
3438 
3439 	if (DISPLAY_VER(i915) < 11)
3440 		return;
3441 
3442 	new_dbuf_state = intel_atomic_get_new_dbuf_state(state);
3443 	old_dbuf_state = intel_atomic_get_old_dbuf_state(state);
3444 	if (!new_dbuf_state ||
3445 	    (new_dbuf_state->joined_mbus == old_dbuf_state->joined_mbus &&
3446 	     new_dbuf_state->active_pipes == old_dbuf_state->active_pipes))
3447 		return;
3448 
3449 	if (DISPLAY_VER(i915) >= 14)
3450 		val |= MBUS_DBOX_I_CREDIT(2);
3451 
3452 	if (DISPLAY_VER(i915) >= 12) {
3453 		val |= MBUS_DBOX_B2B_TRANSACTIONS_MAX(16);
3454 		val |= MBUS_DBOX_B2B_TRANSACTIONS_DELAY(1);
3455 		val |= MBUS_DBOX_REGULATE_B2B_TRANSACTIONS_EN;
3456 	}
3457 
3458 	if (DISPLAY_VER(i915) >= 14)
3459 		val |= new_dbuf_state->joined_mbus ? MBUS_DBOX_A_CREDIT(12) :
3460 						     MBUS_DBOX_A_CREDIT(8);
3461 	else if (IS_ALDERLAKE_P(i915))
3462 		/* Wa_22010947358:adl-p */
3463 		val |= new_dbuf_state->joined_mbus ? MBUS_DBOX_A_CREDIT(6) :
3464 						     MBUS_DBOX_A_CREDIT(4);
3465 	else
3466 		val |= MBUS_DBOX_A_CREDIT(2);
3467 
3468 	if (DISPLAY_VER(i915) >= 14) {
3469 		val |= MBUS_DBOX_B_CREDIT(0xA);
3470 	} else if (IS_ALDERLAKE_P(i915)) {
3471 		val |= MBUS_DBOX_BW_CREDIT(2);
3472 		val |= MBUS_DBOX_B_CREDIT(8);
3473 	} else if (DISPLAY_VER(i915) >= 12) {
3474 		val |= MBUS_DBOX_BW_CREDIT(2);
3475 		val |= MBUS_DBOX_B_CREDIT(12);
3476 	} else {
3477 		val |= MBUS_DBOX_BW_CREDIT(1);
3478 		val |= MBUS_DBOX_B_CREDIT(8);
3479 	}
3480 
3481 	for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
3482 		u32 pipe_val = val;
3483 
3484 		if (!new_crtc_state->hw.active)
3485 			continue;
3486 
3487 		if (DISPLAY_VER(i915) >= 14) {
3488 			if (xelpdp_is_only_pipe_per_dbuf_bank(crtc->pipe,
3489 							      new_dbuf_state->active_pipes))
3490 				pipe_val |= MBUS_DBOX_BW_8CREDITS_MTL;
3491 			else
3492 				pipe_val |= MBUS_DBOX_BW_4CREDITS_MTL;
3493 		}
3494 
3495 		intel_de_write(i915, PIPE_MBUS_DBOX_CTL(crtc->pipe), pipe_val);
3496 	}
3497 }
3498 
3499 static int skl_watermark_ipc_status_show(struct seq_file *m, void *data)
3500 {
3501 	struct drm_i915_private *i915 = m->private;
3502 
3503 	seq_printf(m, "Isochronous Priority Control: %s\n",
3504 		   str_yes_no(skl_watermark_ipc_enabled(i915)));
3505 	return 0;
3506 }
3507 
3508 static int skl_watermark_ipc_status_open(struct inode *inode, struct file *file)
3509 {
3510 	struct drm_i915_private *i915 = inode->i_private;
3511 
3512 	return single_open(file, skl_watermark_ipc_status_show, i915);
3513 }
3514 
3515 static ssize_t skl_watermark_ipc_status_write(struct file *file,
3516 					      const char __user *ubuf,
3517 					      size_t len, loff_t *offp)
3518 {
3519 	struct seq_file *m = file->private_data;
3520 	struct drm_i915_private *i915 = m->private;
3521 	intel_wakeref_t wakeref;
3522 	bool enable;
3523 	int ret;
3524 
3525 	ret = kstrtobool_from_user(ubuf, len, &enable);
3526 	if (ret < 0)
3527 		return ret;
3528 
3529 	with_intel_runtime_pm(&i915->runtime_pm, wakeref) {
3530 		if (!skl_watermark_ipc_enabled(i915) && enable)
3531 			drm_info(&i915->drm,
3532 				 "Enabling IPC: WM will be proper only after next commit\n");
3533 		i915->display.wm.ipc_enabled = enable;
3534 		skl_watermark_ipc_update(i915);
3535 	}
3536 
3537 	return len;
3538 }
3539 
3540 static const struct file_operations skl_watermark_ipc_status_fops = {
3541 	.owner = THIS_MODULE,
3542 	.open = skl_watermark_ipc_status_open,
3543 	.read = seq_read,
3544 	.llseek = seq_lseek,
3545 	.release = single_release,
3546 	.write = skl_watermark_ipc_status_write
3547 };
3548 
3549 void skl_watermark_ipc_debugfs_register(struct drm_i915_private *i915)
3550 {
3551 	struct drm_minor *minor = i915->drm.primary;
3552 
3553 	if (!HAS_IPC(i915))
3554 		return;
3555 
3556 	debugfs_create_file("i915_ipc_status", 0644, minor->debugfs_root, i915,
3557 			    &skl_watermark_ipc_status_fops);
3558 }
3559