xref: /linux/drivers/gpu/drm/vc4/vc4_kms.c (revision 5ea5b6ff0d63aef1dc3fb25445acea183f61a934)
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 #include <linux/sort.h>
16 
17 #include <drm/drm_atomic.h>
18 #include <drm/drm_atomic_helper.h>
19 #include <drm/drm_crtc.h>
20 #include <drm/drm_fourcc.h>
21 #include <drm/drm_gem_framebuffer_helper.h>
22 #include <drm/drm_print.h>
23 #include <drm/drm_probe_helper.h>
24 #include <drm/drm_vblank.h>
25 
26 #include "vc4_drv.h"
27 #include "vc4_regs.h"
28 
29 struct vc4_ctm_state {
30 	struct drm_private_state base;
31 	struct drm_color_ctm *ctm;
32 	int fifo;
33 };
34 
35 #define to_vc4_ctm_state(_state)				\
36 	container_of_const(_state, struct vc4_ctm_state, base)
37 
38 struct vc4_load_tracker_state {
39 	struct drm_private_state base;
40 	u64 hvs_load;
41 	u64 membus_load;
42 };
43 
44 #define to_vc4_load_tracker_state(_state)				\
45 	container_of_const(_state, struct vc4_load_tracker_state, base)
46 
47 static struct vc4_ctm_state *vc4_get_ctm_state(struct drm_atomic_state *state,
48 					       struct drm_private_obj *manager)
49 {
50 	struct drm_device *dev = state->dev;
51 	struct vc4_dev *vc4 = to_vc4_dev(dev);
52 	struct drm_private_state *priv_state;
53 	int ret;
54 
55 	ret = drm_modeset_lock(&vc4->ctm_state_lock, state->acquire_ctx);
56 	if (ret)
57 		return ERR_PTR(ret);
58 
59 	priv_state = drm_atomic_get_private_obj_state(state, manager);
60 	if (IS_ERR(priv_state))
61 		return ERR_CAST(priv_state);
62 
63 	return to_vc4_ctm_state(priv_state);
64 }
65 
66 static struct drm_private_state *
67 vc4_ctm_duplicate_state(struct drm_private_obj *obj)
68 {
69 	struct vc4_ctm_state *state;
70 
71 	state = kmemdup(obj->state, sizeof(*state), GFP_KERNEL);
72 	if (!state)
73 		return NULL;
74 
75 	__drm_atomic_helper_private_obj_duplicate_state(obj, &state->base);
76 
77 	return &state->base;
78 }
79 
80 static void vc4_ctm_destroy_state(struct drm_private_obj *obj,
81 				  struct drm_private_state *state)
82 {
83 	struct vc4_ctm_state *ctm_state = to_vc4_ctm_state(state);
84 
85 	kfree(ctm_state);
86 }
87 
88 static struct drm_private_state *
89 vc4_ctm_create_state(struct drm_private_obj *obj)
90 {
91 	struct vc4_ctm_state *ctm_state;
92 
93 	ctm_state = kzalloc_obj(*ctm_state);
94 	if (!ctm_state)
95 		return ERR_PTR(-ENOMEM);
96 
97 	__drm_atomic_helper_private_obj_create_state(obj, &ctm_state->base);
98 
99 	return &ctm_state->base;
100 }
101 
102 static const struct drm_private_state_funcs vc4_ctm_state_funcs = {
103 	.atomic_create_state = vc4_ctm_create_state,
104 	.atomic_duplicate_state = vc4_ctm_duplicate_state,
105 	.atomic_destroy_state = vc4_ctm_destroy_state,
106 };
107 
108 static void vc4_ctm_obj_fini(struct drm_device *dev, void *unused)
109 {
110 	struct vc4_dev *vc4 = to_vc4_dev(dev);
111 
112 	drm_atomic_private_obj_fini(&vc4->ctm_manager);
113 }
114 
115 static int vc4_ctm_obj_init(struct vc4_dev *vc4)
116 {
117 	drm_modeset_lock_init(&vc4->ctm_state_lock);
118 
119 	drm_atomic_private_obj_init(&vc4->base, &vc4->ctm_manager, NULL,
120 				    &vc4_ctm_state_funcs);
121 
122 	return drmm_add_action_or_reset(&vc4->base, vc4_ctm_obj_fini, NULL);
123 }
124 
125 /* Converts a DRM S31.32 value to the HW S0.9 format. */
126 static u16 vc4_ctm_s31_32_to_s0_9(u64 in)
127 {
128 	u16 r;
129 
130 	/* Sign bit. */
131 	r = in & BIT_ULL(63) ? BIT(9) : 0;
132 
133 	if ((in & GENMASK_ULL(62, 32)) > 0) {
134 		/* We have zero integer bits so we can only saturate here. */
135 		r |= GENMASK(8, 0);
136 	} else {
137 		/* Otherwise take the 9 most important fractional bits. */
138 		r |= (in >> 23) & GENMASK(8, 0);
139 	}
140 
141 	return r;
142 }
143 
144 static void
145 vc4_ctm_commit(struct vc4_dev *vc4, struct drm_atomic_state *state)
146 {
147 	struct vc4_hvs *hvs = vc4->hvs;
148 	struct vc4_ctm_state *ctm_state = to_vc4_ctm_state(vc4->ctm_manager.state);
149 	struct drm_color_ctm *ctm = ctm_state->ctm;
150 
151 	WARN_ON_ONCE(vc4->gen > VC4_GEN_5);
152 
153 	if (ctm_state->fifo) {
154 		HVS_WRITE(SCALER_OLEDCOEF2,
155 			  VC4_SET_FIELD(vc4_ctm_s31_32_to_s0_9(ctm->matrix[0]),
156 					SCALER_OLEDCOEF2_R_TO_R) |
157 			  VC4_SET_FIELD(vc4_ctm_s31_32_to_s0_9(ctm->matrix[3]),
158 					SCALER_OLEDCOEF2_R_TO_G) |
159 			  VC4_SET_FIELD(vc4_ctm_s31_32_to_s0_9(ctm->matrix[6]),
160 					SCALER_OLEDCOEF2_R_TO_B));
161 		HVS_WRITE(SCALER_OLEDCOEF1,
162 			  VC4_SET_FIELD(vc4_ctm_s31_32_to_s0_9(ctm->matrix[1]),
163 					SCALER_OLEDCOEF1_G_TO_R) |
164 			  VC4_SET_FIELD(vc4_ctm_s31_32_to_s0_9(ctm->matrix[4]),
165 					SCALER_OLEDCOEF1_G_TO_G) |
166 			  VC4_SET_FIELD(vc4_ctm_s31_32_to_s0_9(ctm->matrix[7]),
167 					SCALER_OLEDCOEF1_G_TO_B));
168 		HVS_WRITE(SCALER_OLEDCOEF0,
169 			  VC4_SET_FIELD(vc4_ctm_s31_32_to_s0_9(ctm->matrix[2]),
170 					SCALER_OLEDCOEF0_B_TO_R) |
171 			  VC4_SET_FIELD(vc4_ctm_s31_32_to_s0_9(ctm->matrix[5]),
172 					SCALER_OLEDCOEF0_B_TO_G) |
173 			  VC4_SET_FIELD(vc4_ctm_s31_32_to_s0_9(ctm->matrix[8]),
174 					SCALER_OLEDCOEF0_B_TO_B));
175 	}
176 
177 	HVS_WRITE(SCALER_OLEDOFFS,
178 		  VC4_SET_FIELD(ctm_state->fifo, SCALER_OLEDOFFS_DISPFIFO));
179 }
180 
181 struct vc4_hvs_state *
182 vc4_hvs_get_new_global_state(const struct drm_atomic_state *state)
183 {
184 	struct vc4_dev *vc4 = to_vc4_dev(state->dev);
185 	struct drm_private_state *priv_state;
186 
187 	priv_state = drm_atomic_get_new_private_obj_state(state, &vc4->hvs_channels);
188 	if (!priv_state)
189 		return ERR_PTR(-EINVAL);
190 
191 	return to_vc4_hvs_state(priv_state);
192 }
193 
194 struct vc4_hvs_state *
195 vc4_hvs_get_old_global_state(const struct drm_atomic_state *state)
196 {
197 	struct vc4_dev *vc4 = to_vc4_dev(state->dev);
198 	struct drm_private_state *priv_state;
199 
200 	priv_state = drm_atomic_get_old_private_obj_state(state, &vc4->hvs_channels);
201 	if (!priv_state)
202 		return ERR_PTR(-EINVAL);
203 
204 	return to_vc4_hvs_state(priv_state);
205 }
206 
207 struct vc4_hvs_state *
208 vc4_hvs_get_global_state(struct drm_atomic_state *state)
209 {
210 	struct vc4_dev *vc4 = to_vc4_dev(state->dev);
211 	struct drm_private_state *priv_state;
212 
213 	priv_state = drm_atomic_get_private_obj_state(state, &vc4->hvs_channels);
214 	if (IS_ERR(priv_state))
215 		return ERR_CAST(priv_state);
216 
217 	return to_vc4_hvs_state(priv_state);
218 }
219 
220 static void vc4_hvs_pv_muxing_commit(struct vc4_dev *vc4,
221 				     struct drm_atomic_state *state)
222 {
223 	struct vc4_hvs *hvs = vc4->hvs;
224 	struct drm_crtc_state *crtc_state;
225 	struct drm_crtc *crtc;
226 	unsigned int i;
227 
228 	WARN_ON_ONCE(vc4->gen != VC4_GEN_4);
229 
230 	for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
231 		struct vc4_crtc *vc4_crtc = to_vc4_crtc(crtc);
232 		struct vc4_crtc_state *vc4_state = to_vc4_crtc_state(crtc_state);
233 		u32 dispctrl;
234 		u32 dsp3_mux;
235 
236 		if (!crtc_state->active)
237 			continue;
238 
239 		if (vc4_state->assigned_channel != 2)
240 			continue;
241 
242 		/*
243 		 * SCALER_DISPCTRL_DSP3 = X, where X < 2 means 'connect DSP3 to
244 		 * FIFO X'.
245 		 * SCALER_DISPCTRL_DSP3 = 3 means 'disable DSP 3'.
246 		 *
247 		 * DSP3 is connected to FIFO2 unless the transposer is
248 		 * enabled. In this case, FIFO 2 is directly accessed by the
249 		 * TXP IP, and we need to disable the FIFO2 -> pixelvalve1
250 		 * route.
251 		 */
252 		if (vc4_crtc->feeds_txp)
253 			dsp3_mux = VC4_SET_FIELD(3, SCALER_DISPCTRL_DSP3_MUX);
254 		else
255 			dsp3_mux = VC4_SET_FIELD(2, SCALER_DISPCTRL_DSP3_MUX);
256 
257 		dispctrl = HVS_READ(SCALER_DISPCTRL) &
258 			   ~SCALER_DISPCTRL_DSP3_MUX_MASK;
259 		HVS_WRITE(SCALER_DISPCTRL, dispctrl | dsp3_mux);
260 	}
261 }
262 
263 static void vc5_hvs_pv_muxing_commit(struct vc4_dev *vc4,
264 				     struct drm_atomic_state *state)
265 {
266 	struct vc4_hvs *hvs = vc4->hvs;
267 	struct drm_crtc_state *crtc_state;
268 	struct drm_crtc *crtc;
269 	unsigned char mux;
270 	unsigned int i;
271 	u32 reg;
272 
273 	WARN_ON_ONCE(vc4->gen != VC4_GEN_5);
274 
275 	for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
276 		struct vc4_crtc_state *vc4_state = to_vc4_crtc_state(crtc_state);
277 		struct vc4_crtc *vc4_crtc = to_vc4_crtc(crtc);
278 		unsigned int channel = vc4_state->assigned_channel;
279 
280 		if (!vc4_state->update_muxing)
281 			continue;
282 
283 		switch (vc4_crtc->data->hvs_output) {
284 		case 2:
285 			drm_WARN_ON(&vc4->base,
286 				    VC4_GET_FIELD(HVS_READ(SCALER_DISPCTRL),
287 						  SCALER_DISPCTRL_DSP3_MUX) == channel);
288 
289 			mux = (channel == 2) ? 0 : 1;
290 			reg = HVS_READ(SCALER_DISPECTRL);
291 			HVS_WRITE(SCALER_DISPECTRL,
292 				  (reg & ~SCALER_DISPECTRL_DSP2_MUX_MASK) |
293 				  VC4_SET_FIELD(mux, SCALER_DISPECTRL_DSP2_MUX));
294 			break;
295 
296 		case 3:
297 			if (channel == VC4_HVS_CHANNEL_DISABLED)
298 				mux = 3;
299 			else
300 				mux = channel;
301 
302 			reg = HVS_READ(SCALER_DISPCTRL);
303 			HVS_WRITE(SCALER_DISPCTRL,
304 				  (reg & ~SCALER_DISPCTRL_DSP3_MUX_MASK) |
305 				  VC4_SET_FIELD(mux, SCALER_DISPCTRL_DSP3_MUX));
306 			break;
307 
308 		case 4:
309 			if (channel == VC4_HVS_CHANNEL_DISABLED)
310 				mux = 3;
311 			else
312 				mux = channel;
313 
314 			reg = HVS_READ(SCALER_DISPEOLN);
315 			HVS_WRITE(SCALER_DISPEOLN,
316 				  (reg & ~SCALER_DISPEOLN_DSP4_MUX_MASK) |
317 				  VC4_SET_FIELD(mux, SCALER_DISPEOLN_DSP4_MUX));
318 
319 			break;
320 
321 		case 5:
322 			if (channel == VC4_HVS_CHANNEL_DISABLED)
323 				mux = 3;
324 			else
325 				mux = channel;
326 
327 			reg = HVS_READ(SCALER_DISPDITHER);
328 			HVS_WRITE(SCALER_DISPDITHER,
329 				  (reg & ~SCALER_DISPDITHER_DSP5_MUX_MASK) |
330 				  VC4_SET_FIELD(mux, SCALER_DISPDITHER_DSP5_MUX));
331 			break;
332 
333 		default:
334 			break;
335 		}
336 	}
337 }
338 
339 static void vc6_hvs_pv_muxing_commit(struct vc4_dev *vc4,
340 				     struct drm_atomic_state *state)
341 {
342 	struct vc4_hvs *hvs = vc4->hvs;
343 	struct drm_crtc_state *crtc_state;
344 	struct drm_crtc *crtc;
345 	unsigned int i;
346 
347 	WARN_ON_ONCE(vc4->gen != VC4_GEN_6_C && vc4->gen != VC4_GEN_6_D);
348 
349 	for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
350 		struct vc4_crtc_state *vc4_state = to_vc4_crtc_state(crtc_state);
351 		struct vc4_encoder *vc4_encoder;
352 		struct drm_encoder *encoder;
353 		unsigned char mux;
354 		u32 reg;
355 
356 		if (!vc4_state->update_muxing)
357 			continue;
358 
359 		if (vc4_state->assigned_channel != 1)
360 			continue;
361 
362 		encoder = vc4_get_crtc_encoder(crtc, crtc_state);
363 		vc4_encoder = to_vc4_encoder(encoder);
364 		switch (vc4_encoder->type) {
365 		case VC4_ENCODER_TYPE_HDMI1:
366 			mux = 0;
367 			break;
368 
369 		case VC4_ENCODER_TYPE_TXP1:
370 			mux = 2;
371 			break;
372 
373 		default:
374 			drm_err(&vc4->base, "Unhandled encoder type for PV muxing %d",
375 				vc4_encoder->type);
376 			mux = 0;
377 			break;
378 		}
379 
380 		reg = HVS_READ(SCALER6_CONTROL);
381 		HVS_WRITE(SCALER6_CONTROL,
382 			  (reg & ~SCALER6_CONTROL_DSP1_TARGET_MASK) |
383 			  VC4_SET_FIELD(mux, SCALER6_CONTROL_DSP1_TARGET));
384 	}
385 }
386 
387 static void vc4_atomic_commit_tail(struct drm_atomic_state *state)
388 {
389 	struct drm_device *dev = state->dev;
390 	struct vc4_dev *vc4 = to_vc4_dev(dev);
391 	struct vc4_hvs *hvs = vc4->hvs;
392 	struct vc4_hvs_state *new_hvs_state;
393 	struct vc4_hvs_state *old_hvs_state;
394 	unsigned int channel;
395 
396 	old_hvs_state = vc4_hvs_get_old_global_state(state);
397 	if (WARN_ON(IS_ERR(old_hvs_state)))
398 		return;
399 
400 	new_hvs_state = vc4_hvs_get_new_global_state(state);
401 	if (WARN_ON(IS_ERR(new_hvs_state)))
402 		return;
403 
404 	if (vc4->gen < VC4_GEN_6_C) {
405 		struct drm_crtc_state *new_crtc_state;
406 		struct drm_crtc *crtc;
407 		int i;
408 
409 		for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
410 			struct vc4_crtc_state *vc4_crtc_state;
411 
412 			if (!new_crtc_state->commit)
413 				continue;
414 
415 			vc4_crtc_state = to_vc4_crtc_state(new_crtc_state);
416 			vc4_hvs_mask_underrun(hvs, vc4_crtc_state->assigned_channel);
417 		}
418 	}
419 
420 	for (channel = 0; channel < HVS_NUM_CHANNELS; channel++) {
421 		struct drm_crtc_commit *commit;
422 		int ret;
423 
424 		if (!old_hvs_state->fifo_state[channel].in_use)
425 			continue;
426 
427 		commit = old_hvs_state->fifo_state[channel].pending_commit;
428 		if (!commit)
429 			continue;
430 
431 		ret = drm_crtc_commit_wait(commit);
432 		if (ret)
433 			drm_err(dev, "Timed out waiting for commit\n");
434 
435 		drm_crtc_commit_put(commit);
436 		old_hvs_state->fifo_state[channel].pending_commit = NULL;
437 	}
438 
439 	if (vc4->gen == VC4_GEN_5) {
440 		unsigned long state_rate = max(old_hvs_state->core_clock_rate,
441 					       new_hvs_state->core_clock_rate);
442 		unsigned long core_rate = clamp_t(unsigned long, state_rate,
443 						  500000000, hvs->max_core_rate);
444 
445 		drm_dbg(dev, "Raising the core clock at %lu Hz\n", core_rate);
446 
447 		/*
448 		 * Do a temporary request on the core clock during the
449 		 * modeset.
450 		 */
451 		WARN_ON(clk_set_min_rate(hvs->core_clk, core_rate));
452 		WARN_ON(clk_set_min_rate(hvs->disp_clk, core_rate));
453 	}
454 
455 	drm_atomic_helper_commit_modeset_disables(dev, state);
456 
457 	if (vc4->gen <= VC4_GEN_5)
458 		vc4_ctm_commit(vc4, state);
459 
460 	switch (vc4->gen) {
461 	case VC4_GEN_4:
462 		vc4_hvs_pv_muxing_commit(vc4, state);
463 		break;
464 
465 	case VC4_GEN_5:
466 		vc5_hvs_pv_muxing_commit(vc4, state);
467 		break;
468 
469 	case VC4_GEN_6_C:
470 	case VC4_GEN_6_D:
471 		vc6_hvs_pv_muxing_commit(vc4, state);
472 		break;
473 
474 	default:
475 		drm_err(dev, "Unknown VC4 generation: %d", vc4->gen);
476 		break;
477 	}
478 
479 	drm_atomic_helper_commit_planes(dev, state,
480 					DRM_PLANE_COMMIT_ACTIVE_ONLY);
481 
482 	drm_atomic_helper_commit_modeset_enables(dev, state);
483 
484 	drm_atomic_helper_fake_vblank(state);
485 
486 	drm_atomic_helper_commit_hw_done(state);
487 
488 	drm_atomic_helper_wait_for_flip_done(dev, state);
489 
490 	drm_atomic_helper_cleanup_planes(dev, state);
491 
492 	if (vc4->gen == VC4_GEN_5) {
493 		unsigned long core_rate = min_t(unsigned long,
494 						hvs->max_core_rate,
495 						new_hvs_state->core_clock_rate);
496 
497 		drm_dbg(dev, "Running the core clock at %lu Hz\n", core_rate);
498 
499 		/*
500 		 * Request a clock rate based on the current HVS
501 		 * requirements.
502 		 */
503 		WARN_ON(clk_set_min_rate(hvs->core_clk, core_rate));
504 		WARN_ON(clk_set_min_rate(hvs->disp_clk, core_rate));
505 
506 		drm_dbg(dev, "Core clock actual rate: %lu Hz\n",
507 			clk_get_rate(hvs->core_clk));
508 	}
509 }
510 
511 static int vc4_atomic_commit_setup(struct drm_atomic_state *state)
512 {
513 	struct drm_crtc_state *crtc_state;
514 	struct vc4_hvs_state *hvs_state;
515 	struct drm_crtc *crtc;
516 	unsigned int i;
517 
518 	hvs_state = vc4_hvs_get_new_global_state(state);
519 	if (WARN_ON(IS_ERR(hvs_state)))
520 		return PTR_ERR(hvs_state);
521 
522 	for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
523 		struct vc4_crtc_state *vc4_crtc_state =
524 			to_vc4_crtc_state(crtc_state);
525 		unsigned int channel =
526 			vc4_crtc_state->assigned_channel;
527 
528 		if (channel == VC4_HVS_CHANNEL_DISABLED)
529 			continue;
530 
531 		if (!hvs_state->fifo_state[channel].in_use)
532 			continue;
533 
534 		hvs_state->fifo_state[channel].pending_commit =
535 			drm_crtc_commit_get(crtc_state->commit);
536 	}
537 
538 	return 0;
539 }
540 
541 static struct drm_framebuffer *vc4_fb_create(struct drm_device *dev,
542 					     struct drm_file *file_priv,
543 					     const struct drm_format_info *info,
544 					     const struct drm_mode_fb_cmd2 *mode_cmd)
545 {
546 	struct vc4_dev *vc4 = to_vc4_dev(dev);
547 	struct drm_mode_fb_cmd2 mode_cmd_local;
548 
549 	if (WARN_ON_ONCE(vc4->gen > VC4_GEN_4))
550 		return ERR_PTR(-ENODEV);
551 
552 	/* If the user didn't specify a modifier, use the
553 	 * vc4_set_tiling_ioctl() state for the BO.
554 	 */
555 	if (!(mode_cmd->flags & DRM_MODE_FB_MODIFIERS)) {
556 		struct drm_gem_object *gem_obj;
557 		struct vc4_bo *bo;
558 
559 		gem_obj = drm_gem_object_lookup(file_priv,
560 						mode_cmd->handles[0]);
561 		if (!gem_obj) {
562 			DRM_DEBUG("Failed to look up GEM BO %d\n",
563 				  mode_cmd->handles[0]);
564 			return ERR_PTR(-ENOENT);
565 		}
566 		bo = to_vc4_bo(gem_obj);
567 
568 		mode_cmd_local = *mode_cmd;
569 
570 		if (bo->t_format) {
571 			mode_cmd_local.modifier[0] =
572 				DRM_FORMAT_MOD_BROADCOM_VC4_T_TILED;
573 		} else {
574 			mode_cmd_local.modifier[0] = DRM_FORMAT_MOD_NONE;
575 		}
576 
577 		drm_gem_object_put(gem_obj);
578 
579 		mode_cmd = &mode_cmd_local;
580 	}
581 
582 	return drm_gem_fb_create(dev, file_priv, info, mode_cmd);
583 }
584 
585 /* Our CTM has some peculiar limitations: we can only enable it for one CRTC
586  * at a time and the HW only supports S0.9 scalars. To account for the latter,
587  * we don't allow userland to set a CTM that we have no hope of approximating.
588  */
589 static int
590 vc4_ctm_atomic_check(struct drm_device *dev, struct drm_atomic_state *state)
591 {
592 	struct vc4_dev *vc4 = to_vc4_dev(dev);
593 	struct vc4_ctm_state *ctm_state = NULL;
594 	struct drm_crtc *crtc;
595 	struct drm_crtc_state *old_crtc_state, *new_crtc_state;
596 	struct drm_color_ctm *ctm;
597 	int i;
598 
599 	for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
600 		/* CTM is being disabled. */
601 		if (!new_crtc_state->ctm && old_crtc_state->ctm) {
602 			ctm_state = vc4_get_ctm_state(state, &vc4->ctm_manager);
603 			if (IS_ERR(ctm_state))
604 				return PTR_ERR(ctm_state);
605 			ctm_state->fifo = 0;
606 		}
607 	}
608 
609 	for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
610 		if (new_crtc_state->ctm == old_crtc_state->ctm)
611 			continue;
612 
613 		if (!ctm_state) {
614 			ctm_state = vc4_get_ctm_state(state, &vc4->ctm_manager);
615 			if (IS_ERR(ctm_state))
616 				return PTR_ERR(ctm_state);
617 		}
618 
619 		/* CTM is being enabled or the matrix changed. */
620 		if (new_crtc_state->ctm) {
621 			struct vc4_crtc_state *vc4_crtc_state =
622 				to_vc4_crtc_state(new_crtc_state);
623 
624 			/* fifo is 1-based since 0 disables CTM. */
625 			int fifo = vc4_crtc_state->assigned_channel + 1;
626 
627 			/* Check userland isn't trying to turn on CTM for more
628 			 * than one CRTC at a time.
629 			 */
630 			if (ctm_state->fifo && ctm_state->fifo != fifo) {
631 				DRM_DEBUG_DRIVER("Too many CTM configured\n");
632 				return -EINVAL;
633 			}
634 
635 			/* Check we can approximate the specified CTM.
636 			 * We disallow scalars |c| > 1.0 since the HW has
637 			 * no integer bits.
638 			 */
639 			ctm = new_crtc_state->ctm->data;
640 			for (i = 0; i < ARRAY_SIZE(ctm->matrix); i++) {
641 				u64 val = ctm->matrix[i];
642 
643 				val &= ~BIT_ULL(63);
644 				if (val > BIT_ULL(32))
645 					return -EINVAL;
646 			}
647 
648 			ctm_state->fifo = fifo;
649 			ctm_state->ctm = ctm;
650 		}
651 	}
652 
653 	return 0;
654 }
655 
656 static int vc4_load_tracker_atomic_check(struct drm_atomic_state *state)
657 {
658 	struct drm_plane_state *old_plane_state, *new_plane_state;
659 	struct vc4_dev *vc4 = to_vc4_dev(state->dev);
660 	struct vc4_load_tracker_state *load_state;
661 	struct drm_private_state *priv_state;
662 	struct drm_plane *plane;
663 	int i;
664 
665 	priv_state = drm_atomic_get_private_obj_state(state,
666 						      &vc4->load_tracker);
667 	if (IS_ERR(priv_state))
668 		return PTR_ERR(priv_state);
669 
670 	load_state = to_vc4_load_tracker_state(priv_state);
671 	for_each_oldnew_plane_in_state(state, plane, old_plane_state,
672 				       new_plane_state, i) {
673 		struct vc4_plane_state *vc4_plane_state;
674 
675 		if (old_plane_state->fb && old_plane_state->crtc) {
676 			vc4_plane_state = to_vc4_plane_state(old_plane_state);
677 			load_state->membus_load -= vc4_plane_state->membus_load;
678 			load_state->hvs_load -= vc4_plane_state->hvs_load;
679 		}
680 
681 		if (new_plane_state->fb && new_plane_state->crtc) {
682 			vc4_plane_state = to_vc4_plane_state(new_plane_state);
683 			load_state->membus_load += vc4_plane_state->membus_load;
684 			load_state->hvs_load += vc4_plane_state->hvs_load;
685 		}
686 	}
687 
688 	/* Don't check the load when the tracker is disabled. */
689 	if (!vc4->load_tracker_enabled)
690 		return 0;
691 
692 	/* The absolute limit is 2Gbyte/sec, but let's take a margin to let
693 	 * the system work when other blocks are accessing the memory.
694 	 */
695 	if (load_state->membus_load > SZ_1G + SZ_512M)
696 		return -ENOSPC;
697 
698 	/* HVS clock is supposed to run @ 250Mhz, let's take a margin and
699 	 * consider the maximum number of cycles is 240M.
700 	 */
701 	if (load_state->hvs_load > 240000000ULL)
702 		return -ENOSPC;
703 
704 	return 0;
705 }
706 
707 static struct drm_private_state *
708 vc4_load_tracker_duplicate_state(struct drm_private_obj *obj)
709 {
710 	struct vc4_load_tracker_state *state;
711 
712 	state = kmemdup(obj->state, sizeof(*state), GFP_KERNEL);
713 	if (!state)
714 		return NULL;
715 
716 	__drm_atomic_helper_private_obj_duplicate_state(obj, &state->base);
717 
718 	return &state->base;
719 }
720 
721 static void vc4_load_tracker_destroy_state(struct drm_private_obj *obj,
722 					   struct drm_private_state *state)
723 {
724 	struct vc4_load_tracker_state *load_state;
725 
726 	load_state = to_vc4_load_tracker_state(state);
727 	kfree(load_state);
728 }
729 
730 static struct drm_private_state *
731 vc4_load_tracker_create_state(struct drm_private_obj *obj)
732 {
733 	struct vc4_load_tracker_state *load_state;
734 
735 	load_state = kzalloc_obj(*load_state);
736 	if (!load_state)
737 		return ERR_PTR(-ENOMEM);
738 
739 	__drm_atomic_helper_private_obj_create_state(obj, &load_state->base);
740 
741 	return &load_state->base;
742 }
743 
744 static const struct drm_private_state_funcs vc4_load_tracker_state_funcs = {
745 	.atomic_create_state = vc4_load_tracker_create_state,
746 	.atomic_duplicate_state = vc4_load_tracker_duplicate_state,
747 	.atomic_destroy_state = vc4_load_tracker_destroy_state,
748 };
749 
750 static void vc4_load_tracker_obj_fini(struct drm_device *dev, void *unused)
751 {
752 	struct vc4_dev *vc4 = to_vc4_dev(dev);
753 
754 	drm_atomic_private_obj_fini(&vc4->load_tracker);
755 }
756 
757 static int vc4_load_tracker_obj_init(struct vc4_dev *vc4)
758 {
759 	drm_atomic_private_obj_init(&vc4->base, &vc4->load_tracker,
760 				    NULL,
761 				    &vc4_load_tracker_state_funcs);
762 
763 	return drmm_add_action_or_reset(&vc4->base, vc4_load_tracker_obj_fini, NULL);
764 }
765 
766 static struct drm_private_state *
767 vc4_hvs_channels_duplicate_state(struct drm_private_obj *obj)
768 {
769 	struct vc4_hvs_state *old_state = to_vc4_hvs_state(obj->state);
770 	struct vc4_hvs_state *state;
771 	unsigned int i;
772 
773 	state = kzalloc_obj(*state);
774 	if (!state)
775 		return NULL;
776 
777 	__drm_atomic_helper_private_obj_duplicate_state(obj, &state->base);
778 
779 	for (i = 0; i < HVS_NUM_CHANNELS; i++) {
780 		state->fifo_state[i].in_use = old_state->fifo_state[i].in_use;
781 		state->fifo_state[i].fifo_load = old_state->fifo_state[i].fifo_load;
782 	}
783 
784 	state->core_clock_rate = old_state->core_clock_rate;
785 
786 	return &state->base;
787 }
788 
789 static void vc4_hvs_channels_destroy_state(struct drm_private_obj *obj,
790 					   struct drm_private_state *state)
791 {
792 	struct vc4_hvs_state *hvs_state = to_vc4_hvs_state(state);
793 	unsigned int i;
794 
795 	for (i = 0; i < HVS_NUM_CHANNELS; i++) {
796 		if (!hvs_state->fifo_state[i].pending_commit)
797 			continue;
798 
799 		drm_crtc_commit_put(hvs_state->fifo_state[i].pending_commit);
800 	}
801 
802 	kfree(hvs_state);
803 }
804 
805 static void vc4_hvs_channels_print_state(struct drm_printer *p,
806 					 const struct drm_private_state *state)
807 {
808 	const struct vc4_hvs_state *hvs_state = to_vc4_hvs_state(state);
809 	unsigned int i;
810 
811 	drm_printf(p, "HVS State\n");
812 	drm_printf(p, "\tCore Clock Rate: %lu\n", hvs_state->core_clock_rate);
813 
814 	for (i = 0; i < HVS_NUM_CHANNELS; i++) {
815 		drm_printf(p, "\tChannel %d\n", i);
816 		drm_printf(p, "\t\tin use=%d\n", hvs_state->fifo_state[i].in_use);
817 		drm_printf(p, "\t\tload=%lu\n", hvs_state->fifo_state[i].fifo_load);
818 	}
819 }
820 
821 static struct drm_private_state *
822 vc4_hvs_channels_create_state(struct drm_private_obj *obj)
823 {
824 	struct vc4_hvs_state *hvs_state;
825 
826 	hvs_state = kzalloc_obj(*hvs_state);
827 	if (!hvs_state)
828 		return ERR_PTR(-ENOMEM);
829 
830 	__drm_atomic_helper_private_obj_create_state(obj, &hvs_state->base);
831 
832 	return &hvs_state->base;
833 }
834 
835 static const struct drm_private_state_funcs vc4_hvs_state_funcs = {
836 	.atomic_create_state = vc4_hvs_channels_create_state,
837 	.atomic_duplicate_state = vc4_hvs_channels_duplicate_state,
838 	.atomic_destroy_state = vc4_hvs_channels_destroy_state,
839 	.atomic_print_state = vc4_hvs_channels_print_state,
840 };
841 
842 static void vc4_hvs_channels_obj_fini(struct drm_device *dev, void *unused)
843 {
844 	struct vc4_dev *vc4 = to_vc4_dev(dev);
845 
846 	drm_atomic_private_obj_fini(&vc4->hvs_channels);
847 }
848 
849 static int vc4_hvs_channels_obj_init(struct vc4_dev *vc4)
850 {
851 	drm_atomic_private_obj_init(&vc4->base, &vc4->hvs_channels,
852 				    NULL,
853 				    &vc4_hvs_state_funcs);
854 
855 	return drmm_add_action_or_reset(&vc4->base, vc4_hvs_channels_obj_fini, NULL);
856 }
857 
858 static int cmp_vc4_crtc_hvs_output(const void *a, const void *b)
859 {
860 	const struct vc4_crtc *crtc_a =
861 		to_vc4_crtc(*(const struct drm_crtc **)a);
862 	const struct vc4_crtc_data *data_a =
863 		vc4_crtc_to_vc4_crtc_data(crtc_a);
864 	const struct vc4_crtc *crtc_b =
865 		to_vc4_crtc(*(const struct drm_crtc **)b);
866 	const struct vc4_crtc_data *data_b =
867 		vc4_crtc_to_vc4_crtc_data(crtc_b);
868 
869 	return data_a->hvs_output - data_b->hvs_output;
870 }
871 
872 /*
873  * The BCM2711 HVS has up to 7 outputs connected to the pixelvalves and
874  * the TXP (and therefore all the CRTCs found on that platform).
875  *
876  * The naive (and our initial) implementation would just iterate over
877  * all the active CRTCs, try to find a suitable FIFO, and then remove it
878  * from the pool of available FIFOs. However, there are a few corner
879  * cases that need to be considered:
880  *
881  * - When running in a dual-display setup (so with two CRTCs involved),
882  *   we can update the state of a single CRTC (for example by changing
883  *   its mode using xrandr under X11) without affecting the other. In
884  *   this case, the other CRTC wouldn't be in the state at all, so we
885  *   need to consider all the running CRTCs in the DRM device to assign
886  *   a FIFO, not just the one in the state.
887  *
888  * - To fix the above, we can't use drm_atomic_get_crtc_state on all
889  *   enabled CRTCs to pull their CRTC state into the global state, since
890  *   a page flip would start considering their vblank to complete. Since
891  *   we don't have a guarantee that they are actually active, that
892  *   vblank might never happen, and shouldn't even be considered if we
893  *   want to do a page flip on a single CRTC. That can be tested by
894  *   doing a modetest -v first on HDMI1 and then on HDMI0.
895  *
896  * - Since we need the pixelvalve to be disabled and enabled back when
897  *   the FIFO is changed, we should keep the FIFO assigned for as long
898  *   as the CRTC is enabled, only considering it free again once that
899  *   CRTC has been disabled. This can be tested by booting X11 on a
900  *   single display, and changing the resolution down and then back up.
901  */
902 static int vc4_pv_muxing_atomic_check(struct drm_device *dev,
903 				      struct drm_atomic_state *state)
904 {
905 	struct vc4_hvs_state *hvs_new_state;
906 	struct drm_crtc **sorted_crtcs;
907 	struct drm_crtc *crtc;
908 	unsigned int unassigned_channels = 0;
909 	unsigned int i;
910 	int ret;
911 
912 	hvs_new_state = vc4_hvs_get_global_state(state);
913 	if (IS_ERR(hvs_new_state))
914 		return PTR_ERR(hvs_new_state);
915 
916 	for (i = 0; i < ARRAY_SIZE(hvs_new_state->fifo_state); i++)
917 		if (!hvs_new_state->fifo_state[i].in_use)
918 			unassigned_channels |= BIT(i);
919 
920 	/*
921 	 * The problem we have to solve here is that we have up to 7
922 	 * encoders, connected to up to 6 CRTCs.
923 	 *
924 	 * Those CRTCs, depending on the instance, can be routed to 1, 2
925 	 * or 3 HVS FIFOs, and we need to set the muxing between FIFOs and
926 	 * outputs in the HVS accordingly.
927 	 *
928 	 * It would be pretty hard to come up with an algorithm that
929 	 * would generically solve this. However, the current routing
930 	 * trees we support allow us to simplify a bit the problem.
931 	 *
932 	 * Indeed, with the current supported layouts, if we try to
933 	 * assign in the ascending crtc index order the FIFOs, we can't
934 	 * fall into the situation where an earlier CRTC that had
935 	 * multiple routes is assigned one that was the only option for
936 	 * a later CRTC.
937 	 *
938 	 * If the layout changes and doesn't give us that in the future,
939 	 * we will need to have something smarter, but it works so far.
940 	 */
941 	sorted_crtcs = kmalloc_objs(*sorted_crtcs, dev->num_crtcs);
942 	if (!sorted_crtcs)
943 		return -ENOMEM;
944 
945 	i = 0;
946 	drm_for_each_crtc(crtc, dev)
947 		sorted_crtcs[i++] = crtc;
948 
949 	sort(sorted_crtcs, i, sizeof(*sorted_crtcs), cmp_vc4_crtc_hvs_output, NULL);
950 
951 	for (i = 0; i < dev->num_crtcs; i++) {
952 		struct vc4_crtc_state *old_vc4_crtc_state, *new_vc4_crtc_state;
953 		struct drm_crtc_state *old_crtc_state, *new_crtc_state;
954 		struct vc4_crtc *vc4_crtc;
955 		unsigned int matching_channels;
956 		unsigned int channel;
957 
958 		crtc = sorted_crtcs[i];
959 		if (!crtc)
960 			continue;
961 		vc4_crtc = to_vc4_crtc(crtc);
962 
963 		old_crtc_state = drm_atomic_get_old_crtc_state(state, crtc);
964 		if (!old_crtc_state)
965 			continue;
966 		old_vc4_crtc_state = to_vc4_crtc_state(old_crtc_state);
967 
968 		new_crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
969 		if (!new_crtc_state)
970 			continue;
971 		new_vc4_crtc_state = to_vc4_crtc_state(new_crtc_state);
972 
973 		drm_dbg(dev, "%s: Trying to find a channel.\n", crtc->name);
974 
975 		/* Nothing to do here, let's skip it */
976 		if (old_crtc_state->enable == new_crtc_state->enable) {
977 			if (new_crtc_state->enable)
978 				drm_dbg(dev, "%s: Already enabled, reusing channel %d.\n",
979 					crtc->name, new_vc4_crtc_state->assigned_channel);
980 			else
981 				drm_dbg(dev, "%s: Disabled, ignoring.\n", crtc->name);
982 
983 			continue;
984 		}
985 
986 		/* Muxing will need to be modified, mark it as such */
987 		new_vc4_crtc_state->update_muxing = true;
988 
989 		/* If we're disabling our CRTC, we put back our channel */
990 		if (!new_crtc_state->enable) {
991 			channel = old_vc4_crtc_state->assigned_channel;
992 
993 			drm_dbg(dev, "%s: Disabling, Freeing channel %d\n",
994 				crtc->name, channel);
995 
996 			hvs_new_state->fifo_state[channel].in_use = false;
997 			new_vc4_crtc_state->assigned_channel = VC4_HVS_CHANNEL_DISABLED;
998 			continue;
999 		}
1000 
1001 		matching_channels = unassigned_channels & vc4_crtc->data->hvs_available_channels;
1002 		if (!matching_channels) {
1003 			ret = -EINVAL;
1004 			goto err_free_crtc_array;
1005 		}
1006 
1007 		channel = ffs(matching_channels) - 1;
1008 
1009 		drm_dbg(dev, "Assigned HVS channel %d to CRTC %s\n", channel, crtc->name);
1010 		new_vc4_crtc_state->assigned_channel = channel;
1011 		unassigned_channels &= ~BIT(channel);
1012 		hvs_new_state->fifo_state[channel].in_use = true;
1013 	}
1014 
1015 	kfree(sorted_crtcs);
1016 	return 0;
1017 
1018 err_free_crtc_array:
1019 	kfree(sorted_crtcs);
1020 	return ret;
1021 }
1022 
1023 static int
1024 vc4_core_clock_atomic_check(struct drm_atomic_state *state)
1025 {
1026 	struct vc4_dev *vc4 = to_vc4_dev(state->dev);
1027 	struct drm_private_state *priv_state;
1028 	struct vc4_hvs_state *hvs_new_state;
1029 	struct vc4_load_tracker_state *load_state;
1030 	struct drm_crtc_state *old_crtc_state, *new_crtc_state;
1031 	struct drm_crtc *crtc;
1032 	unsigned int num_outputs;
1033 	unsigned long pixel_rate;
1034 	unsigned long cob_rate;
1035 	unsigned int i;
1036 
1037 	priv_state = drm_atomic_get_private_obj_state(state,
1038 						      &vc4->load_tracker);
1039 	if (IS_ERR(priv_state))
1040 		return PTR_ERR(priv_state);
1041 
1042 	load_state = to_vc4_load_tracker_state(priv_state);
1043 
1044 	hvs_new_state = vc4_hvs_get_global_state(state);
1045 	if (IS_ERR(hvs_new_state))
1046 		return PTR_ERR(hvs_new_state);
1047 
1048 	for_each_oldnew_crtc_in_state(state, crtc,
1049 				      old_crtc_state,
1050 				      new_crtc_state,
1051 				      i) {
1052 		if (old_crtc_state->active) {
1053 			struct vc4_crtc_state *old_vc4_state =
1054 				to_vc4_crtc_state(old_crtc_state);
1055 			unsigned int channel = old_vc4_state->assigned_channel;
1056 
1057 			hvs_new_state->fifo_state[channel].fifo_load = 0;
1058 		}
1059 
1060 		if (new_crtc_state->active) {
1061 			struct vc4_crtc_state *new_vc4_state =
1062 				to_vc4_crtc_state(new_crtc_state);
1063 			unsigned int channel = new_vc4_state->assigned_channel;
1064 
1065 			hvs_new_state->fifo_state[channel].fifo_load =
1066 				new_vc4_state->hvs_load;
1067 		}
1068 	}
1069 
1070 	cob_rate = 0;
1071 	num_outputs = 0;
1072 	for (i = 0; i < HVS_NUM_CHANNELS; i++) {
1073 		if (!hvs_new_state->fifo_state[i].in_use)
1074 			continue;
1075 
1076 		num_outputs++;
1077 		cob_rate = max_t(unsigned long,
1078 				 hvs_new_state->fifo_state[i].fifo_load,
1079 				 cob_rate);
1080 	}
1081 
1082 	pixel_rate = load_state->hvs_load;
1083 	if (num_outputs > 1) {
1084 		pixel_rate = (pixel_rate * 40) / 100;
1085 	} else {
1086 		pixel_rate = (pixel_rate * 60) / 100;
1087 	}
1088 
1089 	hvs_new_state->core_clock_rate = max(cob_rate, pixel_rate);
1090 
1091 	return 0;
1092 }
1093 
1094 
1095 static int
1096 vc4_atomic_check(struct drm_device *dev, struct drm_atomic_state *state)
1097 {
1098 	int ret;
1099 
1100 	ret = vc4_pv_muxing_atomic_check(dev, state);
1101 	if (ret)
1102 		return ret;
1103 
1104 	ret = vc4_ctm_atomic_check(dev, state);
1105 	if (ret < 0)
1106 		return ret;
1107 
1108 	ret = drm_atomic_helper_check(dev, state);
1109 	if (ret)
1110 		return ret;
1111 
1112 	ret = vc4_load_tracker_atomic_check(state);
1113 	if (ret)
1114 		return ret;
1115 
1116 	return vc4_core_clock_atomic_check(state);
1117 }
1118 
1119 static struct drm_mode_config_helper_funcs vc4_mode_config_helpers = {
1120 	.atomic_commit_setup	= vc4_atomic_commit_setup,
1121 	.atomic_commit_tail	= vc4_atomic_commit_tail,
1122 };
1123 
1124 static const struct drm_mode_config_funcs vc4_mode_funcs = {
1125 	.atomic_check = vc4_atomic_check,
1126 	.atomic_commit = drm_atomic_helper_commit,
1127 	.fb_create = vc4_fb_create,
1128 };
1129 
1130 static const struct drm_mode_config_funcs vc5_mode_funcs = {
1131 	.atomic_check = vc4_atomic_check,
1132 	.atomic_commit = drm_atomic_helper_commit,
1133 	.fb_create = drm_gem_fb_create,
1134 };
1135 
1136 int vc4_kms_load(struct drm_device *dev)
1137 {
1138 	struct vc4_dev *vc4 = to_vc4_dev(dev);
1139 	int ret;
1140 
1141 	/*
1142 	 * The limits enforced by the load tracker aren't relevant for
1143 	 * the BCM2711, but the load tracker computations are used for
1144 	 * the core clock rate calculation.
1145 	 */
1146 	if (vc4->gen == VC4_GEN_4) {
1147 		/* Start with the load tracker enabled. Can be
1148 		 * disabled through the debugfs load_tracker file.
1149 		 */
1150 		vc4->load_tracker_enabled = true;
1151 	}
1152 
1153 	/* Set support for vblank irq fast disable, before drm_vblank_init() */
1154 	dev->vblank_disable_immediate = true;
1155 
1156 	ret = drm_vblank_init(dev, dev->mode_config.num_crtc);
1157 	if (ret < 0) {
1158 		dev_err(dev->dev, "failed to initialize vblank\n");
1159 		return ret;
1160 	}
1161 
1162 	if (vc4->gen >= VC4_GEN_6_C) {
1163 		dev->mode_config.max_width = 8192;
1164 		dev->mode_config.max_height = 8192;
1165 	} else if (vc4->gen >= VC4_GEN_5) {
1166 		dev->mode_config.max_width = 7680;
1167 		dev->mode_config.max_height = 7680;
1168 	} else {
1169 		dev->mode_config.max_width = 2048;
1170 		dev->mode_config.max_height = 2048;
1171 	}
1172 
1173 	dev->mode_config.funcs = (vc4->gen > VC4_GEN_4) ? &vc5_mode_funcs : &vc4_mode_funcs;
1174 	dev->mode_config.helper_private = &vc4_mode_config_helpers;
1175 	dev->mode_config.preferred_depth = 24;
1176 	dev->mode_config.async_page_flip = true;
1177 	dev->mode_config.normalize_zpos = true;
1178 
1179 	ret = vc4_ctm_obj_init(vc4);
1180 	if (ret)
1181 		return ret;
1182 
1183 	ret = vc4_load_tracker_obj_init(vc4);
1184 	if (ret)
1185 		return ret;
1186 
1187 	ret = vc4_hvs_channels_obj_init(vc4);
1188 	if (ret)
1189 		return ret;
1190 
1191 	drm_mode_config_reset(dev);
1192 
1193 	drm_kms_helper_poll_init(dev);
1194 
1195 	return 0;
1196 }
1197