xref: /openbmc/linux/drivers/gpu/drm/vc4/vc4_kms.c (revision d9fd5a71)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2015 Broadcom
4  */
5 
6 /**
7  * DOC: VC4 KMS
8  *
9  * This is the general code for implementing KMS mode setting that
10  * doesn't clearly associate with any of the other objects (plane,
11  * crtc, HDMI encoder).
12  */
13 
14 #include <linux/clk.h>
15 
16 #include <drm/drm_atomic.h>
17 #include <drm/drm_atomic_helper.h>
18 #include <drm/drm_crtc.h>
19 #include <drm/drm_gem_framebuffer_helper.h>
20 #include <drm/drm_plane_helper.h>
21 #include <drm/drm_probe_helper.h>
22 #include <drm/drm_vblank.h>
23 
24 #include "vc4_drv.h"
25 #include "vc4_regs.h"
26 
27 #define HVS_NUM_CHANNELS 3
28 
29 struct vc4_ctm_state {
30 	struct drm_private_state base;
31 	struct drm_color_ctm *ctm;
32 	int fifo;
33 };
34 
35 static struct vc4_ctm_state *to_vc4_ctm_state(struct drm_private_state *priv)
36 {
37 	return container_of(priv, struct vc4_ctm_state, base);
38 }
39 
40 struct vc4_hvs_state {
41 	struct drm_private_state base;
42 
43 	struct {
44 		unsigned in_use: 1;
45 		struct drm_crtc_commit *pending_commit;
46 	} fifo_state[HVS_NUM_CHANNELS];
47 };
48 
49 static struct vc4_hvs_state *
50 to_vc4_hvs_state(struct drm_private_state *priv)
51 {
52 	return container_of(priv, struct vc4_hvs_state, base);
53 }
54 
55 struct vc4_load_tracker_state {
56 	struct drm_private_state base;
57 	u64 hvs_load;
58 	u64 membus_load;
59 };
60 
61 static struct vc4_load_tracker_state *
62 to_vc4_load_tracker_state(struct drm_private_state *priv)
63 {
64 	return container_of(priv, struct vc4_load_tracker_state, base);
65 }
66 
67 static struct vc4_ctm_state *vc4_get_ctm_state(struct drm_atomic_state *state,
68 					       struct drm_private_obj *manager)
69 {
70 	struct drm_device *dev = state->dev;
71 	struct vc4_dev *vc4 = to_vc4_dev(dev);
72 	struct drm_private_state *priv_state;
73 	int ret;
74 
75 	ret = drm_modeset_lock(&vc4->ctm_state_lock, state->acquire_ctx);
76 	if (ret)
77 		return ERR_PTR(ret);
78 
79 	priv_state = drm_atomic_get_private_obj_state(state, manager);
80 	if (IS_ERR(priv_state))
81 		return ERR_CAST(priv_state);
82 
83 	return to_vc4_ctm_state(priv_state);
84 }
85 
86 static struct drm_private_state *
87 vc4_ctm_duplicate_state(struct drm_private_obj *obj)
88 {
89 	struct vc4_ctm_state *state;
90 
91 	state = kmemdup(obj->state, sizeof(*state), GFP_KERNEL);
92 	if (!state)
93 		return NULL;
94 
95 	__drm_atomic_helper_private_obj_duplicate_state(obj, &state->base);
96 
97 	return &state->base;
98 }
99 
100 static void vc4_ctm_destroy_state(struct drm_private_obj *obj,
101 				  struct drm_private_state *state)
102 {
103 	struct vc4_ctm_state *ctm_state = to_vc4_ctm_state(state);
104 
105 	kfree(ctm_state);
106 }
107 
108 static const struct drm_private_state_funcs vc4_ctm_state_funcs = {
109 	.atomic_duplicate_state = vc4_ctm_duplicate_state,
110 	.atomic_destroy_state = vc4_ctm_destroy_state,
111 };
112 
113 static void vc4_ctm_obj_fini(struct drm_device *dev, void *unused)
114 {
115 	struct vc4_dev *vc4 = to_vc4_dev(dev);
116 
117 	drm_atomic_private_obj_fini(&vc4->ctm_manager);
118 }
119 
120 static int vc4_ctm_obj_init(struct vc4_dev *vc4)
121 {
122 	struct vc4_ctm_state *ctm_state;
123 
124 	drm_modeset_lock_init(&vc4->ctm_state_lock);
125 
126 	ctm_state = kzalloc(sizeof(*ctm_state), GFP_KERNEL);
127 	if (!ctm_state)
128 		return -ENOMEM;
129 
130 	drm_atomic_private_obj_init(&vc4->base, &vc4->ctm_manager, &ctm_state->base,
131 				    &vc4_ctm_state_funcs);
132 
133 	return drmm_add_action_or_reset(&vc4->base, vc4_ctm_obj_fini, NULL);
134 }
135 
136 /* Converts a DRM S31.32 value to the HW S0.9 format. */
137 static u16 vc4_ctm_s31_32_to_s0_9(u64 in)
138 {
139 	u16 r;
140 
141 	/* Sign bit. */
142 	r = in & BIT_ULL(63) ? BIT(9) : 0;
143 
144 	if ((in & GENMASK_ULL(62, 32)) > 0) {
145 		/* We have zero integer bits so we can only saturate here. */
146 		r |= GENMASK(8, 0);
147 	} else {
148 		/* Otherwise take the 9 most important fractional bits. */
149 		r |= (in >> 23) & GENMASK(8, 0);
150 	}
151 
152 	return r;
153 }
154 
155 static void
156 vc4_ctm_commit(struct vc4_dev *vc4, struct drm_atomic_state *state)
157 {
158 	struct vc4_ctm_state *ctm_state = to_vc4_ctm_state(vc4->ctm_manager.state);
159 	struct drm_color_ctm *ctm = ctm_state->ctm;
160 
161 	if (ctm_state->fifo) {
162 		HVS_WRITE(SCALER_OLEDCOEF2,
163 			  VC4_SET_FIELD(vc4_ctm_s31_32_to_s0_9(ctm->matrix[0]),
164 					SCALER_OLEDCOEF2_R_TO_R) |
165 			  VC4_SET_FIELD(vc4_ctm_s31_32_to_s0_9(ctm->matrix[3]),
166 					SCALER_OLEDCOEF2_R_TO_G) |
167 			  VC4_SET_FIELD(vc4_ctm_s31_32_to_s0_9(ctm->matrix[6]),
168 					SCALER_OLEDCOEF2_R_TO_B));
169 		HVS_WRITE(SCALER_OLEDCOEF1,
170 			  VC4_SET_FIELD(vc4_ctm_s31_32_to_s0_9(ctm->matrix[1]),
171 					SCALER_OLEDCOEF1_G_TO_R) |
172 			  VC4_SET_FIELD(vc4_ctm_s31_32_to_s0_9(ctm->matrix[4]),
173 					SCALER_OLEDCOEF1_G_TO_G) |
174 			  VC4_SET_FIELD(vc4_ctm_s31_32_to_s0_9(ctm->matrix[7]),
175 					SCALER_OLEDCOEF1_G_TO_B));
176 		HVS_WRITE(SCALER_OLEDCOEF0,
177 			  VC4_SET_FIELD(vc4_ctm_s31_32_to_s0_9(ctm->matrix[2]),
178 					SCALER_OLEDCOEF0_B_TO_R) |
179 			  VC4_SET_FIELD(vc4_ctm_s31_32_to_s0_9(ctm->matrix[5]),
180 					SCALER_OLEDCOEF0_B_TO_G) |
181 			  VC4_SET_FIELD(vc4_ctm_s31_32_to_s0_9(ctm->matrix[8]),
182 					SCALER_OLEDCOEF0_B_TO_B));
183 	}
184 
185 	HVS_WRITE(SCALER_OLEDOFFS,
186 		  VC4_SET_FIELD(ctm_state->fifo, SCALER_OLEDOFFS_DISPFIFO));
187 }
188 
189 static struct vc4_hvs_state *
190 vc4_hvs_get_new_global_state(struct drm_atomic_state *state)
191 {
192 	struct vc4_dev *vc4 = to_vc4_dev(state->dev);
193 	struct drm_private_state *priv_state;
194 
195 	priv_state = drm_atomic_get_new_private_obj_state(state, &vc4->hvs_channels);
196 	if (IS_ERR(priv_state))
197 		return ERR_CAST(priv_state);
198 
199 	return to_vc4_hvs_state(priv_state);
200 }
201 
202 static struct vc4_hvs_state *
203 vc4_hvs_get_old_global_state(struct drm_atomic_state *state)
204 {
205 	struct vc4_dev *vc4 = to_vc4_dev(state->dev);
206 	struct drm_private_state *priv_state;
207 
208 	priv_state = drm_atomic_get_old_private_obj_state(state, &vc4->hvs_channels);
209 	if (IS_ERR(priv_state))
210 		return ERR_CAST(priv_state);
211 
212 	return to_vc4_hvs_state(priv_state);
213 }
214 
215 static struct vc4_hvs_state *
216 vc4_hvs_get_global_state(struct drm_atomic_state *state)
217 {
218 	struct vc4_dev *vc4 = to_vc4_dev(state->dev);
219 	struct drm_private_state *priv_state;
220 
221 	priv_state = drm_atomic_get_private_obj_state(state, &vc4->hvs_channels);
222 	if (IS_ERR(priv_state))
223 		return ERR_CAST(priv_state);
224 
225 	return to_vc4_hvs_state(priv_state);
226 }
227 
228 static void vc4_hvs_pv_muxing_commit(struct vc4_dev *vc4,
229 				     struct drm_atomic_state *state)
230 {
231 	struct drm_crtc_state *crtc_state;
232 	struct drm_crtc *crtc;
233 	unsigned int i;
234 
235 	for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
236 		struct vc4_crtc_state *vc4_state = to_vc4_crtc_state(crtc_state);
237 		u32 dispctrl;
238 		u32 dsp3_mux;
239 
240 		if (!crtc_state->active)
241 			continue;
242 
243 		if (vc4_state->assigned_channel != 2)
244 			continue;
245 
246 		/*
247 		 * SCALER_DISPCTRL_DSP3 = X, where X < 2 means 'connect DSP3 to
248 		 * FIFO X'.
249 		 * SCALER_DISPCTRL_DSP3 = 3 means 'disable DSP 3'.
250 		 *
251 		 * DSP3 is connected to FIFO2 unless the transposer is
252 		 * enabled. In this case, FIFO 2 is directly accessed by the
253 		 * TXP IP, and we need to disable the FIFO2 -> pixelvalve1
254 		 * route.
255 		 */
256 		if (vc4_state->feed_txp)
257 			dsp3_mux = VC4_SET_FIELD(3, SCALER_DISPCTRL_DSP3_MUX);
258 		else
259 			dsp3_mux = VC4_SET_FIELD(2, SCALER_DISPCTRL_DSP3_MUX);
260 
261 		dispctrl = HVS_READ(SCALER_DISPCTRL) &
262 			   ~SCALER_DISPCTRL_DSP3_MUX_MASK;
263 		HVS_WRITE(SCALER_DISPCTRL, dispctrl | dsp3_mux);
264 	}
265 }
266 
267 static void vc5_hvs_pv_muxing_commit(struct vc4_dev *vc4,
268 				     struct drm_atomic_state *state)
269 {
270 	struct drm_crtc_state *crtc_state;
271 	struct drm_crtc *crtc;
272 	unsigned char mux;
273 	unsigned int i;
274 	u32 reg;
275 
276 	for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
277 		struct vc4_crtc_state *vc4_state = to_vc4_crtc_state(crtc_state);
278 		struct vc4_crtc *vc4_crtc = to_vc4_crtc(crtc);
279 
280 		if (!vc4_state->update_muxing)
281 			continue;
282 
283 		switch (vc4_crtc->data->hvs_output) {
284 		case 2:
285 			mux = (vc4_state->assigned_channel == 2) ? 0 : 1;
286 			reg = HVS_READ(SCALER_DISPECTRL);
287 			HVS_WRITE(SCALER_DISPECTRL,
288 				  (reg & ~SCALER_DISPECTRL_DSP2_MUX_MASK) |
289 				  VC4_SET_FIELD(mux, SCALER_DISPECTRL_DSP2_MUX));
290 			break;
291 
292 		case 3:
293 			if (vc4_state->assigned_channel == VC4_HVS_CHANNEL_DISABLED)
294 				mux = 3;
295 			else
296 				mux = vc4_state->assigned_channel;
297 
298 			reg = HVS_READ(SCALER_DISPCTRL);
299 			HVS_WRITE(SCALER_DISPCTRL,
300 				  (reg & ~SCALER_DISPCTRL_DSP3_MUX_MASK) |
301 				  VC4_SET_FIELD(mux, SCALER_DISPCTRL_DSP3_MUX));
302 			break;
303 
304 		case 4:
305 			if (vc4_state->assigned_channel == VC4_HVS_CHANNEL_DISABLED)
306 				mux = 3;
307 			else
308 				mux = vc4_state->assigned_channel;
309 
310 			reg = HVS_READ(SCALER_DISPEOLN);
311 			HVS_WRITE(SCALER_DISPEOLN,
312 				  (reg & ~SCALER_DISPEOLN_DSP4_MUX_MASK) |
313 				  VC4_SET_FIELD(mux, SCALER_DISPEOLN_DSP4_MUX));
314 
315 			break;
316 
317 		case 5:
318 			if (vc4_state->assigned_channel == VC4_HVS_CHANNEL_DISABLED)
319 				mux = 3;
320 			else
321 				mux = vc4_state->assigned_channel;
322 
323 			reg = HVS_READ(SCALER_DISPDITHER);
324 			HVS_WRITE(SCALER_DISPDITHER,
325 				  (reg & ~SCALER_DISPDITHER_DSP5_MUX_MASK) |
326 				  VC4_SET_FIELD(mux, SCALER_DISPDITHER_DSP5_MUX));
327 			break;
328 
329 		default:
330 			break;
331 		}
332 	}
333 }
334 
335 static void vc4_atomic_commit_tail(struct drm_atomic_state *state)
336 {
337 	struct drm_device *dev = state->dev;
338 	struct vc4_dev *vc4 = to_vc4_dev(dev);
339 	struct vc4_hvs *hvs = vc4->hvs;
340 	struct drm_crtc_state *old_crtc_state;
341 	struct drm_crtc_state *new_crtc_state;
342 	struct drm_crtc *crtc;
343 	struct vc4_hvs_state *old_hvs_state;
344 	int i;
345 
346 	for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
347 		struct vc4_crtc_state *vc4_crtc_state;
348 
349 		if (!new_crtc_state->commit)
350 			continue;
351 
352 		vc4_crtc_state = to_vc4_crtc_state(new_crtc_state);
353 		vc4_hvs_mask_underrun(dev, vc4_crtc_state->assigned_channel);
354 	}
355 
356 	if (vc4->hvs->hvs5)
357 		clk_set_min_rate(hvs->core_clk, 500000000);
358 
359 	old_hvs_state = vc4_hvs_get_old_global_state(state);
360 	if (!old_hvs_state)
361 		return;
362 
363 	for_each_old_crtc_in_state(state, crtc, old_crtc_state, i) {
364 		struct vc4_crtc_state *vc4_crtc_state =
365 			to_vc4_crtc_state(old_crtc_state);
366 		struct drm_crtc_commit *commit;
367 		unsigned int channel = vc4_crtc_state->assigned_channel;
368 		unsigned long done;
369 
370 		if (channel == VC4_HVS_CHANNEL_DISABLED)
371 			continue;
372 
373 		if (!old_hvs_state->fifo_state[channel].in_use)
374 			continue;
375 
376 		commit = old_hvs_state->fifo_state[i].pending_commit;
377 		if (!commit)
378 			continue;
379 
380 		done = wait_for_completion_timeout(&commit->hw_done, 10 * HZ);
381 		if (!done)
382 			drm_err(dev, "Timed out waiting for hw_done\n");
383 
384 		done = wait_for_completion_timeout(&commit->flip_done, 10 * HZ);
385 		if (!done)
386 			drm_err(dev, "Timed out waiting for flip_done\n");
387 	}
388 
389 	drm_atomic_helper_commit_modeset_disables(dev, state);
390 
391 	vc4_ctm_commit(vc4, state);
392 
393 	if (vc4->hvs->hvs5)
394 		vc5_hvs_pv_muxing_commit(vc4, state);
395 	else
396 		vc4_hvs_pv_muxing_commit(vc4, state);
397 
398 	drm_atomic_helper_commit_planes(dev, state, 0);
399 
400 	drm_atomic_helper_commit_modeset_enables(dev, state);
401 
402 	drm_atomic_helper_fake_vblank(state);
403 
404 	drm_atomic_helper_commit_hw_done(state);
405 
406 	drm_atomic_helper_wait_for_flip_done(dev, state);
407 
408 	drm_atomic_helper_cleanup_planes(dev, state);
409 
410 	if (vc4->hvs->hvs5)
411 		clk_set_min_rate(hvs->core_clk, 0);
412 }
413 
414 static int vc4_atomic_commit_setup(struct drm_atomic_state *state)
415 {
416 	struct drm_crtc_state *crtc_state;
417 	struct vc4_hvs_state *hvs_state;
418 	struct drm_crtc *crtc;
419 	unsigned int i;
420 
421 	hvs_state = vc4_hvs_get_new_global_state(state);
422 	if (!hvs_state)
423 		return -EINVAL;
424 
425 	for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
426 		struct vc4_crtc_state *vc4_crtc_state =
427 			to_vc4_crtc_state(crtc_state);
428 		unsigned int channel =
429 			vc4_crtc_state->assigned_channel;
430 
431 		if (channel == VC4_HVS_CHANNEL_DISABLED)
432 			continue;
433 
434 		if (!hvs_state->fifo_state[channel].in_use)
435 			continue;
436 
437 		hvs_state->fifo_state[channel].pending_commit =
438 			drm_crtc_commit_get(crtc_state->commit);
439 	}
440 
441 	return 0;
442 }
443 
444 static struct drm_framebuffer *vc4_fb_create(struct drm_device *dev,
445 					     struct drm_file *file_priv,
446 					     const struct drm_mode_fb_cmd2 *mode_cmd)
447 {
448 	struct drm_mode_fb_cmd2 mode_cmd_local;
449 
450 	/* If the user didn't specify a modifier, use the
451 	 * vc4_set_tiling_ioctl() state for the BO.
452 	 */
453 	if (!(mode_cmd->flags & DRM_MODE_FB_MODIFIERS)) {
454 		struct drm_gem_object *gem_obj;
455 		struct vc4_bo *bo;
456 
457 		gem_obj = drm_gem_object_lookup(file_priv,
458 						mode_cmd->handles[0]);
459 		if (!gem_obj) {
460 			DRM_DEBUG("Failed to look up GEM BO %d\n",
461 				  mode_cmd->handles[0]);
462 			return ERR_PTR(-ENOENT);
463 		}
464 		bo = to_vc4_bo(gem_obj);
465 
466 		mode_cmd_local = *mode_cmd;
467 
468 		if (bo->t_format) {
469 			mode_cmd_local.modifier[0] =
470 				DRM_FORMAT_MOD_BROADCOM_VC4_T_TILED;
471 		} else {
472 			mode_cmd_local.modifier[0] = DRM_FORMAT_MOD_NONE;
473 		}
474 
475 		drm_gem_object_put(gem_obj);
476 
477 		mode_cmd = &mode_cmd_local;
478 	}
479 
480 	return drm_gem_fb_create(dev, file_priv, mode_cmd);
481 }
482 
483 /* Our CTM has some peculiar limitations: we can only enable it for one CRTC
484  * at a time and the HW only supports S0.9 scalars. To account for the latter,
485  * we don't allow userland to set a CTM that we have no hope of approximating.
486  */
487 static int
488 vc4_ctm_atomic_check(struct drm_device *dev, struct drm_atomic_state *state)
489 {
490 	struct vc4_dev *vc4 = to_vc4_dev(dev);
491 	struct vc4_ctm_state *ctm_state = NULL;
492 	struct drm_crtc *crtc;
493 	struct drm_crtc_state *old_crtc_state, *new_crtc_state;
494 	struct drm_color_ctm *ctm;
495 	int i;
496 
497 	for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
498 		/* CTM is being disabled. */
499 		if (!new_crtc_state->ctm && old_crtc_state->ctm) {
500 			ctm_state = vc4_get_ctm_state(state, &vc4->ctm_manager);
501 			if (IS_ERR(ctm_state))
502 				return PTR_ERR(ctm_state);
503 			ctm_state->fifo = 0;
504 		}
505 	}
506 
507 	for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
508 		if (new_crtc_state->ctm == old_crtc_state->ctm)
509 			continue;
510 
511 		if (!ctm_state) {
512 			ctm_state = vc4_get_ctm_state(state, &vc4->ctm_manager);
513 			if (IS_ERR(ctm_state))
514 				return PTR_ERR(ctm_state);
515 		}
516 
517 		/* CTM is being enabled or the matrix changed. */
518 		if (new_crtc_state->ctm) {
519 			struct vc4_crtc_state *vc4_crtc_state =
520 				to_vc4_crtc_state(new_crtc_state);
521 
522 			/* fifo is 1-based since 0 disables CTM. */
523 			int fifo = vc4_crtc_state->assigned_channel + 1;
524 
525 			/* Check userland isn't trying to turn on CTM for more
526 			 * than one CRTC at a time.
527 			 */
528 			if (ctm_state->fifo && ctm_state->fifo != fifo) {
529 				DRM_DEBUG_DRIVER("Too many CTM configured\n");
530 				return -EINVAL;
531 			}
532 
533 			/* Check we can approximate the specified CTM.
534 			 * We disallow scalars |c| > 1.0 since the HW has
535 			 * no integer bits.
536 			 */
537 			ctm = new_crtc_state->ctm->data;
538 			for (i = 0; i < ARRAY_SIZE(ctm->matrix); i++) {
539 				u64 val = ctm->matrix[i];
540 
541 				val &= ~BIT_ULL(63);
542 				if (val > BIT_ULL(32))
543 					return -EINVAL;
544 			}
545 
546 			ctm_state->fifo = fifo;
547 			ctm_state->ctm = ctm;
548 		}
549 	}
550 
551 	return 0;
552 }
553 
554 static int vc4_load_tracker_atomic_check(struct drm_atomic_state *state)
555 {
556 	struct drm_plane_state *old_plane_state, *new_plane_state;
557 	struct vc4_dev *vc4 = to_vc4_dev(state->dev);
558 	struct vc4_load_tracker_state *load_state;
559 	struct drm_private_state *priv_state;
560 	struct drm_plane *plane;
561 	int i;
562 
563 	if (!vc4->load_tracker_available)
564 		return 0;
565 
566 	priv_state = drm_atomic_get_private_obj_state(state,
567 						      &vc4->load_tracker);
568 	if (IS_ERR(priv_state))
569 		return PTR_ERR(priv_state);
570 
571 	load_state = to_vc4_load_tracker_state(priv_state);
572 	for_each_oldnew_plane_in_state(state, plane, old_plane_state,
573 				       new_plane_state, i) {
574 		struct vc4_plane_state *vc4_plane_state;
575 
576 		if (old_plane_state->fb && old_plane_state->crtc) {
577 			vc4_plane_state = to_vc4_plane_state(old_plane_state);
578 			load_state->membus_load -= vc4_plane_state->membus_load;
579 			load_state->hvs_load -= vc4_plane_state->hvs_load;
580 		}
581 
582 		if (new_plane_state->fb && new_plane_state->crtc) {
583 			vc4_plane_state = to_vc4_plane_state(new_plane_state);
584 			load_state->membus_load += vc4_plane_state->membus_load;
585 			load_state->hvs_load += vc4_plane_state->hvs_load;
586 		}
587 	}
588 
589 	/* Don't check the load when the tracker is disabled. */
590 	if (!vc4->load_tracker_enabled)
591 		return 0;
592 
593 	/* The absolute limit is 2Gbyte/sec, but let's take a margin to let
594 	 * the system work when other blocks are accessing the memory.
595 	 */
596 	if (load_state->membus_load > SZ_1G + SZ_512M)
597 		return -ENOSPC;
598 
599 	/* HVS clock is supposed to run @ 250Mhz, let's take a margin and
600 	 * consider the maximum number of cycles is 240M.
601 	 */
602 	if (load_state->hvs_load > 240000000ULL)
603 		return -ENOSPC;
604 
605 	return 0;
606 }
607 
608 static struct drm_private_state *
609 vc4_load_tracker_duplicate_state(struct drm_private_obj *obj)
610 {
611 	struct vc4_load_tracker_state *state;
612 
613 	state = kmemdup(obj->state, sizeof(*state), GFP_KERNEL);
614 	if (!state)
615 		return NULL;
616 
617 	__drm_atomic_helper_private_obj_duplicate_state(obj, &state->base);
618 
619 	return &state->base;
620 }
621 
622 static void vc4_load_tracker_destroy_state(struct drm_private_obj *obj,
623 					   struct drm_private_state *state)
624 {
625 	struct vc4_load_tracker_state *load_state;
626 
627 	load_state = to_vc4_load_tracker_state(state);
628 	kfree(load_state);
629 }
630 
631 static const struct drm_private_state_funcs vc4_load_tracker_state_funcs = {
632 	.atomic_duplicate_state = vc4_load_tracker_duplicate_state,
633 	.atomic_destroy_state = vc4_load_tracker_destroy_state,
634 };
635 
636 static void vc4_load_tracker_obj_fini(struct drm_device *dev, void *unused)
637 {
638 	struct vc4_dev *vc4 = to_vc4_dev(dev);
639 
640 	if (!vc4->load_tracker_available)
641 		return;
642 
643 	drm_atomic_private_obj_fini(&vc4->load_tracker);
644 }
645 
646 static int vc4_load_tracker_obj_init(struct vc4_dev *vc4)
647 {
648 	struct vc4_load_tracker_state *load_state;
649 
650 	if (!vc4->load_tracker_available)
651 		return 0;
652 
653 	load_state = kzalloc(sizeof(*load_state), GFP_KERNEL);
654 	if (!load_state)
655 		return -ENOMEM;
656 
657 	drm_atomic_private_obj_init(&vc4->base, &vc4->load_tracker,
658 				    &load_state->base,
659 				    &vc4_load_tracker_state_funcs);
660 
661 	return drmm_add_action_or_reset(&vc4->base, vc4_load_tracker_obj_fini, NULL);
662 }
663 
664 static struct drm_private_state *
665 vc4_hvs_channels_duplicate_state(struct drm_private_obj *obj)
666 {
667 	struct vc4_hvs_state *old_state = to_vc4_hvs_state(obj->state);
668 	struct vc4_hvs_state *state;
669 	unsigned int i;
670 
671 	state = kzalloc(sizeof(*state), GFP_KERNEL);
672 	if (!state)
673 		return NULL;
674 
675 	__drm_atomic_helper_private_obj_duplicate_state(obj, &state->base);
676 
677 
678 	for (i = 0; i < HVS_NUM_CHANNELS; i++) {
679 		state->fifo_state[i].in_use = old_state->fifo_state[i].in_use;
680 
681 		if (!old_state->fifo_state[i].pending_commit)
682 			continue;
683 
684 		state->fifo_state[i].pending_commit =
685 			drm_crtc_commit_get(old_state->fifo_state[i].pending_commit);
686 	}
687 
688 	return &state->base;
689 }
690 
691 static void vc4_hvs_channels_destroy_state(struct drm_private_obj *obj,
692 					   struct drm_private_state *state)
693 {
694 	struct vc4_hvs_state *hvs_state = to_vc4_hvs_state(state);
695 	unsigned int i;
696 
697 	for (i = 0; i < HVS_NUM_CHANNELS; i++) {
698 		if (!hvs_state->fifo_state[i].pending_commit)
699 			continue;
700 
701 		drm_crtc_commit_put(hvs_state->fifo_state[i].pending_commit);
702 	}
703 
704 	kfree(hvs_state);
705 }
706 
707 static const struct drm_private_state_funcs vc4_hvs_state_funcs = {
708 	.atomic_duplicate_state = vc4_hvs_channels_duplicate_state,
709 	.atomic_destroy_state = vc4_hvs_channels_destroy_state,
710 };
711 
712 static void vc4_hvs_channels_obj_fini(struct drm_device *dev, void *unused)
713 {
714 	struct vc4_dev *vc4 = to_vc4_dev(dev);
715 
716 	drm_atomic_private_obj_fini(&vc4->hvs_channels);
717 }
718 
719 static int vc4_hvs_channels_obj_init(struct vc4_dev *vc4)
720 {
721 	struct vc4_hvs_state *state;
722 
723 	state = kzalloc(sizeof(*state), GFP_KERNEL);
724 	if (!state)
725 		return -ENOMEM;
726 
727 	drm_atomic_private_obj_init(&vc4->base, &vc4->hvs_channels,
728 				    &state->base,
729 				    &vc4_hvs_state_funcs);
730 
731 	return drmm_add_action_or_reset(&vc4->base, vc4_hvs_channels_obj_fini, NULL);
732 }
733 
734 /*
735  * The BCM2711 HVS has up to 7 outputs connected to the pixelvalves and
736  * the TXP (and therefore all the CRTCs found on that platform).
737  *
738  * The naive (and our initial) implementation would just iterate over
739  * all the active CRTCs, try to find a suitable FIFO, and then remove it
740  * from the pool of available FIFOs. However, there are a few corner
741  * cases that need to be considered:
742  *
743  * - When running in a dual-display setup (so with two CRTCs involved),
744  *   we can update the state of a single CRTC (for example by changing
745  *   its mode using xrandr under X11) without affecting the other. In
746  *   this case, the other CRTC wouldn't be in the state at all, so we
747  *   need to consider all the running CRTCs in the DRM device to assign
748  *   a FIFO, not just the one in the state.
749  *
750  * - To fix the above, we can't use drm_atomic_get_crtc_state on all
751  *   enabled CRTCs to pull their CRTC state into the global state, since
752  *   a page flip would start considering their vblank to complete. Since
753  *   we don't have a guarantee that they are actually active, that
754  *   vblank might never happen, and shouldn't even be considered if we
755  *   want to do a page flip on a single CRTC. That can be tested by
756  *   doing a modetest -v first on HDMI1 and then on HDMI0.
757  *
758  * - Since we need the pixelvalve to be disabled and enabled back when
759  *   the FIFO is changed, we should keep the FIFO assigned for as long
760  *   as the CRTC is enabled, only considering it free again once that
761  *   CRTC has been disabled. This can be tested by booting X11 on a
762  *   single display, and changing the resolution down and then back up.
763  */
764 static int vc4_pv_muxing_atomic_check(struct drm_device *dev,
765 				      struct drm_atomic_state *state)
766 {
767 	struct vc4_hvs_state *hvs_new_state;
768 	struct drm_crtc_state *old_crtc_state, *new_crtc_state;
769 	struct drm_crtc *crtc;
770 	unsigned int unassigned_channels = 0;
771 	unsigned int i;
772 
773 	hvs_new_state = vc4_hvs_get_global_state(state);
774 	if (!hvs_new_state)
775 		return -EINVAL;
776 
777 	for (i = 0; i < ARRAY_SIZE(hvs_new_state->fifo_state); i++)
778 		if (!hvs_new_state->fifo_state[i].in_use)
779 			unassigned_channels |= BIT(i);
780 
781 	for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
782 		struct vc4_crtc_state *old_vc4_crtc_state =
783 			to_vc4_crtc_state(old_crtc_state);
784 		struct vc4_crtc_state *new_vc4_crtc_state =
785 			to_vc4_crtc_state(new_crtc_state);
786 		struct vc4_crtc *vc4_crtc = to_vc4_crtc(crtc);
787 		unsigned int matching_channels;
788 		unsigned int channel;
789 
790 		/* Nothing to do here, let's skip it */
791 		if (old_crtc_state->enable == new_crtc_state->enable)
792 			continue;
793 
794 		/* Muxing will need to be modified, mark it as such */
795 		new_vc4_crtc_state->update_muxing = true;
796 
797 		/* If we're disabling our CRTC, we put back our channel */
798 		if (!new_crtc_state->enable) {
799 			channel = old_vc4_crtc_state->assigned_channel;
800 			hvs_new_state->fifo_state[channel].in_use = false;
801 			new_vc4_crtc_state->assigned_channel = VC4_HVS_CHANNEL_DISABLED;
802 			continue;
803 		}
804 
805 		/*
806 		 * The problem we have to solve here is that we have
807 		 * up to 7 encoders, connected to up to 6 CRTCs.
808 		 *
809 		 * Those CRTCs, depending on the instance, can be
810 		 * routed to 1, 2 or 3 HVS FIFOs, and we need to set
811 		 * the change the muxing between FIFOs and outputs in
812 		 * the HVS accordingly.
813 		 *
814 		 * It would be pretty hard to come up with an
815 		 * algorithm that would generically solve
816 		 * this. However, the current routing trees we support
817 		 * allow us to simplify a bit the problem.
818 		 *
819 		 * Indeed, with the current supported layouts, if we
820 		 * try to assign in the ascending crtc index order the
821 		 * FIFOs, we can't fall into the situation where an
822 		 * earlier CRTC that had multiple routes is assigned
823 		 * one that was the only option for a later CRTC.
824 		 *
825 		 * If the layout changes and doesn't give us that in
826 		 * the future, we will need to have something smarter,
827 		 * but it works so far.
828 		 */
829 		matching_channels = unassigned_channels & vc4_crtc->data->hvs_available_channels;
830 		if (!matching_channels)
831 			return -EINVAL;
832 
833 		channel = ffs(matching_channels) - 1;
834 		new_vc4_crtc_state->assigned_channel = channel;
835 		unassigned_channels &= ~BIT(channel);
836 		hvs_new_state->fifo_state[channel].in_use = true;
837 	}
838 
839 	return 0;
840 }
841 
842 static int
843 vc4_atomic_check(struct drm_device *dev, struct drm_atomic_state *state)
844 {
845 	int ret;
846 
847 	ret = vc4_pv_muxing_atomic_check(dev, state);
848 	if (ret)
849 		return ret;
850 
851 	ret = vc4_ctm_atomic_check(dev, state);
852 	if (ret < 0)
853 		return ret;
854 
855 	ret = drm_atomic_helper_check(dev, state);
856 	if (ret)
857 		return ret;
858 
859 	return vc4_load_tracker_atomic_check(state);
860 }
861 
862 static struct drm_mode_config_helper_funcs vc4_mode_config_helpers = {
863 	.atomic_commit_setup	= vc4_atomic_commit_setup,
864 	.atomic_commit_tail	= vc4_atomic_commit_tail,
865 };
866 
867 static const struct drm_mode_config_funcs vc4_mode_funcs = {
868 	.atomic_check = vc4_atomic_check,
869 	.atomic_commit = drm_atomic_helper_commit,
870 	.fb_create = vc4_fb_create,
871 };
872 
873 int vc4_kms_load(struct drm_device *dev)
874 {
875 	struct vc4_dev *vc4 = to_vc4_dev(dev);
876 	bool is_vc5 = of_device_is_compatible(dev->dev->of_node,
877 					      "brcm,bcm2711-vc5");
878 	int ret;
879 
880 	if (!is_vc5) {
881 		vc4->load_tracker_available = true;
882 
883 		/* Start with the load tracker enabled. Can be
884 		 * disabled through the debugfs load_tracker file.
885 		 */
886 		vc4->load_tracker_enabled = true;
887 	}
888 
889 	/* Set support for vblank irq fast disable, before drm_vblank_init() */
890 	dev->vblank_disable_immediate = true;
891 
892 	dev->irq_enabled = true;
893 	ret = drm_vblank_init(dev, dev->mode_config.num_crtc);
894 	if (ret < 0) {
895 		dev_err(dev->dev, "failed to initialize vblank\n");
896 		return ret;
897 	}
898 
899 	if (is_vc5) {
900 		dev->mode_config.max_width = 7680;
901 		dev->mode_config.max_height = 7680;
902 	} else {
903 		dev->mode_config.max_width = 2048;
904 		dev->mode_config.max_height = 2048;
905 	}
906 
907 	dev->mode_config.funcs = &vc4_mode_funcs;
908 	dev->mode_config.helper_private = &vc4_mode_config_helpers;
909 	dev->mode_config.preferred_depth = 24;
910 	dev->mode_config.async_page_flip = true;
911 	dev->mode_config.allow_fb_modifiers = true;
912 
913 	ret = vc4_ctm_obj_init(vc4);
914 	if (ret)
915 		return ret;
916 
917 	ret = vc4_load_tracker_obj_init(vc4);
918 	if (ret)
919 		return ret;
920 
921 	ret = vc4_hvs_channels_obj_init(vc4);
922 	if (ret)
923 		return ret;
924 
925 	drm_mode_config_reset(dev);
926 
927 	drm_kms_helper_poll_init(dev);
928 
929 	return 0;
930 }
931