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