1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2015 Broadcom
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).
14 #include <linux/clk.h>
16 #include <drm/drm_atomic.h>
17 #include <drm/drm_atomic_helper.h>
18 #include <drm/drm_crtc.h>
19 #include <drm/drm_fourcc.h>
20 #include <drm/drm_gem_framebuffer_helper.h>
21 #include <drm/drm_plane_helper.h>
22 #include <drm/drm_probe_helper.h>
23 #include <drm/drm_vblank.h>
28 #define HVS_NUM_CHANNELS 3
30 struct vc4_ctm_state {
31 struct drm_private_state base;
32 struct drm_color_ctm *ctm;
36 static struct vc4_ctm_state *
37 to_vc4_ctm_state(const struct drm_private_state *priv)
39 return container_of(priv, struct vc4_ctm_state, base);
42 struct vc4_hvs_state {
43 struct drm_private_state base;
44 unsigned long core_clock_rate;
48 unsigned long fifo_load;
49 struct drm_crtc_commit *pending_commit;
50 } fifo_state[HVS_NUM_CHANNELS];
53 static struct vc4_hvs_state *
54 to_vc4_hvs_state(const struct drm_private_state *priv)
56 return container_of(priv, struct vc4_hvs_state, base);
59 struct vc4_load_tracker_state {
60 struct drm_private_state base;
65 static struct vc4_load_tracker_state *
66 to_vc4_load_tracker_state(const struct drm_private_state *priv)
68 return container_of(priv, struct vc4_load_tracker_state, base);
71 static struct vc4_ctm_state *vc4_get_ctm_state(struct drm_atomic_state *state,
72 struct drm_private_obj *manager)
74 struct drm_device *dev = state->dev;
75 struct vc4_dev *vc4 = to_vc4_dev(dev);
76 struct drm_private_state *priv_state;
79 ret = drm_modeset_lock(&vc4->ctm_state_lock, state->acquire_ctx);
83 priv_state = drm_atomic_get_private_obj_state(state, manager);
84 if (IS_ERR(priv_state))
85 return ERR_CAST(priv_state);
87 return to_vc4_ctm_state(priv_state);
90 static struct drm_private_state *
91 vc4_ctm_duplicate_state(struct drm_private_obj *obj)
93 struct vc4_ctm_state *state;
95 state = kmemdup(obj->state, sizeof(*state), GFP_KERNEL);
99 __drm_atomic_helper_private_obj_duplicate_state(obj, &state->base);
104 static void vc4_ctm_destroy_state(struct drm_private_obj *obj,
105 struct drm_private_state *state)
107 struct vc4_ctm_state *ctm_state = to_vc4_ctm_state(state);
112 static const struct drm_private_state_funcs vc4_ctm_state_funcs = {
113 .atomic_duplicate_state = vc4_ctm_duplicate_state,
114 .atomic_destroy_state = vc4_ctm_destroy_state,
117 static void vc4_ctm_obj_fini(struct drm_device *dev, void *unused)
119 struct vc4_dev *vc4 = to_vc4_dev(dev);
121 drm_atomic_private_obj_fini(&vc4->ctm_manager);
124 static int vc4_ctm_obj_init(struct vc4_dev *vc4)
126 struct vc4_ctm_state *ctm_state;
128 drm_modeset_lock_init(&vc4->ctm_state_lock);
130 ctm_state = kzalloc(sizeof(*ctm_state), GFP_KERNEL);
134 drm_atomic_private_obj_init(&vc4->base, &vc4->ctm_manager, &ctm_state->base,
135 &vc4_ctm_state_funcs);
137 return drmm_add_action_or_reset(&vc4->base, vc4_ctm_obj_fini, NULL);
140 /* Converts a DRM S31.32 value to the HW S0.9 format. */
141 static u16 vc4_ctm_s31_32_to_s0_9(u64 in)
146 r = in & BIT_ULL(63) ? BIT(9) : 0;
148 if ((in & GENMASK_ULL(62, 32)) > 0) {
149 /* We have zero integer bits so we can only saturate here. */
152 /* Otherwise take the 9 most important fractional bits. */
153 r |= (in >> 23) & GENMASK(8, 0);
160 vc4_ctm_commit(struct vc4_dev *vc4, struct drm_atomic_state *state)
162 struct vc4_hvs *hvs = vc4->hvs;
163 struct vc4_ctm_state *ctm_state = to_vc4_ctm_state(vc4->ctm_manager.state);
164 struct drm_color_ctm *ctm = ctm_state->ctm;
166 if (ctm_state->fifo) {
167 HVS_WRITE(SCALER_OLEDCOEF2,
168 VC4_SET_FIELD(vc4_ctm_s31_32_to_s0_9(ctm->matrix[0]),
169 SCALER_OLEDCOEF2_R_TO_R) |
170 VC4_SET_FIELD(vc4_ctm_s31_32_to_s0_9(ctm->matrix[3]),
171 SCALER_OLEDCOEF2_R_TO_G) |
172 VC4_SET_FIELD(vc4_ctm_s31_32_to_s0_9(ctm->matrix[6]),
173 SCALER_OLEDCOEF2_R_TO_B));
174 HVS_WRITE(SCALER_OLEDCOEF1,
175 VC4_SET_FIELD(vc4_ctm_s31_32_to_s0_9(ctm->matrix[1]),
176 SCALER_OLEDCOEF1_G_TO_R) |
177 VC4_SET_FIELD(vc4_ctm_s31_32_to_s0_9(ctm->matrix[4]),
178 SCALER_OLEDCOEF1_G_TO_G) |
179 VC4_SET_FIELD(vc4_ctm_s31_32_to_s0_9(ctm->matrix[7]),
180 SCALER_OLEDCOEF1_G_TO_B));
181 HVS_WRITE(SCALER_OLEDCOEF0,
182 VC4_SET_FIELD(vc4_ctm_s31_32_to_s0_9(ctm->matrix[2]),
183 SCALER_OLEDCOEF0_B_TO_R) |
184 VC4_SET_FIELD(vc4_ctm_s31_32_to_s0_9(ctm->matrix[5]),
185 SCALER_OLEDCOEF0_B_TO_G) |
186 VC4_SET_FIELD(vc4_ctm_s31_32_to_s0_9(ctm->matrix[8]),
187 SCALER_OLEDCOEF0_B_TO_B));
190 HVS_WRITE(SCALER_OLEDOFFS,
191 VC4_SET_FIELD(ctm_state->fifo, SCALER_OLEDOFFS_DISPFIFO));
194 static struct vc4_hvs_state *
195 vc4_hvs_get_new_global_state(struct drm_atomic_state *state)
197 struct vc4_dev *vc4 = to_vc4_dev(state->dev);
198 struct drm_private_state *priv_state;
200 priv_state = drm_atomic_get_new_private_obj_state(state, &vc4->hvs_channels);
201 if (IS_ERR(priv_state))
202 return ERR_CAST(priv_state);
204 return to_vc4_hvs_state(priv_state);
207 static struct vc4_hvs_state *
208 vc4_hvs_get_old_global_state(struct drm_atomic_state *state)
210 struct vc4_dev *vc4 = to_vc4_dev(state->dev);
211 struct drm_private_state *priv_state;
213 priv_state = drm_atomic_get_old_private_obj_state(state, &vc4->hvs_channels);
214 if (IS_ERR(priv_state))
215 return ERR_CAST(priv_state);
217 return to_vc4_hvs_state(priv_state);
220 static struct vc4_hvs_state *
221 vc4_hvs_get_global_state(struct drm_atomic_state *state)
223 struct vc4_dev *vc4 = to_vc4_dev(state->dev);
224 struct drm_private_state *priv_state;
226 priv_state = drm_atomic_get_private_obj_state(state, &vc4->hvs_channels);
227 if (IS_ERR(priv_state))
228 return ERR_CAST(priv_state);
230 return to_vc4_hvs_state(priv_state);
233 static void vc4_hvs_pv_muxing_commit(struct vc4_dev *vc4,
234 struct drm_atomic_state *state)
236 struct vc4_hvs *hvs = vc4->hvs;
237 struct drm_crtc_state *crtc_state;
238 struct drm_crtc *crtc;
241 for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
242 struct vc4_crtc *vc4_crtc = to_vc4_crtc(crtc);
243 struct vc4_crtc_state *vc4_state = to_vc4_crtc_state(crtc_state);
247 if (!crtc_state->active)
250 if (vc4_state->assigned_channel != 2)
254 * SCALER_DISPCTRL_DSP3 = X, where X < 2 means 'connect DSP3 to
256 * SCALER_DISPCTRL_DSP3 = 3 means 'disable DSP 3'.
258 * DSP3 is connected to FIFO2 unless the transposer is
259 * enabled. In this case, FIFO 2 is directly accessed by the
260 * TXP IP, and we need to disable the FIFO2 -> pixelvalve1
263 if (vc4_crtc->feeds_txp)
264 dsp3_mux = VC4_SET_FIELD(3, SCALER_DISPCTRL_DSP3_MUX);
266 dsp3_mux = VC4_SET_FIELD(2, SCALER_DISPCTRL_DSP3_MUX);
268 dispctrl = HVS_READ(SCALER_DISPCTRL) &
269 ~SCALER_DISPCTRL_DSP3_MUX_MASK;
270 HVS_WRITE(SCALER_DISPCTRL, dispctrl | dsp3_mux);
274 static void vc5_hvs_pv_muxing_commit(struct vc4_dev *vc4,
275 struct drm_atomic_state *state)
277 struct vc4_hvs *hvs = vc4->hvs;
278 struct drm_crtc_state *crtc_state;
279 struct drm_crtc *crtc;
284 for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
285 struct vc4_crtc_state *vc4_state = to_vc4_crtc_state(crtc_state);
286 struct vc4_crtc *vc4_crtc = to_vc4_crtc(crtc);
287 unsigned int channel = vc4_state->assigned_channel;
289 if (!vc4_state->update_muxing)
292 switch (vc4_crtc->data->hvs_output) {
294 drm_WARN_ON(&vc4->base,
295 VC4_GET_FIELD(HVS_READ(SCALER_DISPCTRL),
296 SCALER_DISPCTRL_DSP3_MUX) == channel);
298 mux = (channel == 2) ? 0 : 1;
299 reg = HVS_READ(SCALER_DISPECTRL);
300 HVS_WRITE(SCALER_DISPECTRL,
301 (reg & ~SCALER_DISPECTRL_DSP2_MUX_MASK) |
302 VC4_SET_FIELD(mux, SCALER_DISPECTRL_DSP2_MUX));
306 if (channel == VC4_HVS_CHANNEL_DISABLED)
311 reg = HVS_READ(SCALER_DISPCTRL);
312 HVS_WRITE(SCALER_DISPCTRL,
313 (reg & ~SCALER_DISPCTRL_DSP3_MUX_MASK) |
314 VC4_SET_FIELD(mux, SCALER_DISPCTRL_DSP3_MUX));
318 if (channel == VC4_HVS_CHANNEL_DISABLED)
323 reg = HVS_READ(SCALER_DISPEOLN);
324 HVS_WRITE(SCALER_DISPEOLN,
325 (reg & ~SCALER_DISPEOLN_DSP4_MUX_MASK) |
326 VC4_SET_FIELD(mux, SCALER_DISPEOLN_DSP4_MUX));
331 if (channel == VC4_HVS_CHANNEL_DISABLED)
336 reg = HVS_READ(SCALER_DISPDITHER);
337 HVS_WRITE(SCALER_DISPDITHER,
338 (reg & ~SCALER_DISPDITHER_DSP5_MUX_MASK) |
339 VC4_SET_FIELD(mux, SCALER_DISPDITHER_DSP5_MUX));
348 static void vc4_atomic_commit_tail(struct drm_atomic_state *state)
350 struct drm_device *dev = state->dev;
351 struct vc4_dev *vc4 = to_vc4_dev(dev);
352 struct vc4_hvs *hvs = vc4->hvs;
353 struct drm_crtc_state *new_crtc_state;
354 struct vc4_hvs_state *new_hvs_state;
355 struct drm_crtc *crtc;
356 struct vc4_hvs_state *old_hvs_state;
357 unsigned int channel;
360 old_hvs_state = vc4_hvs_get_old_global_state(state);
361 if (WARN_ON(IS_ERR(old_hvs_state)))
364 new_hvs_state = vc4_hvs_get_new_global_state(state);
365 if (WARN_ON(IS_ERR(new_hvs_state)))
368 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
369 struct vc4_crtc_state *vc4_crtc_state;
371 if (!new_crtc_state->commit)
374 vc4_crtc_state = to_vc4_crtc_state(new_crtc_state);
375 vc4_hvs_mask_underrun(hvs, vc4_crtc_state->assigned_channel);
378 for (channel = 0; channel < HVS_NUM_CHANNELS; channel++) {
379 struct drm_crtc_commit *commit;
382 if (!old_hvs_state->fifo_state[channel].in_use)
385 commit = old_hvs_state->fifo_state[channel].pending_commit;
389 ret = drm_crtc_commit_wait(commit);
391 drm_err(dev, "Timed out waiting for commit\n");
393 drm_crtc_commit_put(commit);
394 old_hvs_state->fifo_state[channel].pending_commit = NULL;
398 unsigned long state_rate = max(old_hvs_state->core_clock_rate,
399 new_hvs_state->core_clock_rate);
400 unsigned long core_rate = max_t(unsigned long,
401 500000000, state_rate);
403 drm_dbg(dev, "Raising the core clock at %lu Hz\n", core_rate);
406 * Do a temporary request on the core clock during the
409 WARN_ON(clk_set_min_rate(hvs->core_clk, core_rate));
412 drm_atomic_helper_commit_modeset_disables(dev, state);
414 vc4_ctm_commit(vc4, state);
417 vc5_hvs_pv_muxing_commit(vc4, state);
419 vc4_hvs_pv_muxing_commit(vc4, state);
421 drm_atomic_helper_commit_planes(dev, state,
422 DRM_PLANE_COMMIT_ACTIVE_ONLY);
424 drm_atomic_helper_commit_modeset_enables(dev, state);
426 drm_atomic_helper_fake_vblank(state);
428 drm_atomic_helper_commit_hw_done(state);
430 drm_atomic_helper_wait_for_flip_done(dev, state);
432 drm_atomic_helper_cleanup_planes(dev, state);
435 drm_dbg(dev, "Running the core clock at %lu Hz\n",
436 new_hvs_state->core_clock_rate);
439 * Request a clock rate based on the current HVS
442 WARN_ON(clk_set_min_rate(hvs->core_clk, new_hvs_state->core_clock_rate));
444 drm_dbg(dev, "Core clock actual rate: %lu Hz\n",
445 clk_get_rate(hvs->core_clk));
449 static int vc4_atomic_commit_setup(struct drm_atomic_state *state)
451 struct drm_crtc_state *crtc_state;
452 struct vc4_hvs_state *hvs_state;
453 struct drm_crtc *crtc;
456 hvs_state = vc4_hvs_get_new_global_state(state);
457 if (WARN_ON(IS_ERR(hvs_state)))
458 return PTR_ERR(hvs_state);
460 for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
461 struct vc4_crtc_state *vc4_crtc_state =
462 to_vc4_crtc_state(crtc_state);
463 unsigned int channel =
464 vc4_crtc_state->assigned_channel;
466 if (channel == VC4_HVS_CHANNEL_DISABLED)
469 if (!hvs_state->fifo_state[channel].in_use)
472 hvs_state->fifo_state[channel].pending_commit =
473 drm_crtc_commit_get(crtc_state->commit);
479 static struct drm_framebuffer *vc4_fb_create(struct drm_device *dev,
480 struct drm_file *file_priv,
481 const struct drm_mode_fb_cmd2 *mode_cmd)
483 struct vc4_dev *vc4 = to_vc4_dev(dev);
484 struct drm_mode_fb_cmd2 mode_cmd_local;
486 if (WARN_ON_ONCE(vc4->is_vc5))
487 return ERR_PTR(-ENODEV);
489 /* If the user didn't specify a modifier, use the
490 * vc4_set_tiling_ioctl() state for the BO.
492 if (!(mode_cmd->flags & DRM_MODE_FB_MODIFIERS)) {
493 struct drm_gem_object *gem_obj;
496 gem_obj = drm_gem_object_lookup(file_priv,
497 mode_cmd->handles[0]);
499 DRM_DEBUG("Failed to look up GEM BO %d\n",
500 mode_cmd->handles[0]);
501 return ERR_PTR(-ENOENT);
503 bo = to_vc4_bo(gem_obj);
505 mode_cmd_local = *mode_cmd;
508 mode_cmd_local.modifier[0] =
509 DRM_FORMAT_MOD_BROADCOM_VC4_T_TILED;
511 mode_cmd_local.modifier[0] = DRM_FORMAT_MOD_NONE;
514 drm_gem_object_put(gem_obj);
516 mode_cmd = &mode_cmd_local;
519 return drm_gem_fb_create(dev, file_priv, mode_cmd);
522 /* Our CTM has some peculiar limitations: we can only enable it for one CRTC
523 * at a time and the HW only supports S0.9 scalars. To account for the latter,
524 * we don't allow userland to set a CTM that we have no hope of approximating.
527 vc4_ctm_atomic_check(struct drm_device *dev, struct drm_atomic_state *state)
529 struct vc4_dev *vc4 = to_vc4_dev(dev);
530 struct vc4_ctm_state *ctm_state = NULL;
531 struct drm_crtc *crtc;
532 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
533 struct drm_color_ctm *ctm;
536 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
537 /* CTM is being disabled. */
538 if (!new_crtc_state->ctm && old_crtc_state->ctm) {
539 ctm_state = vc4_get_ctm_state(state, &vc4->ctm_manager);
540 if (IS_ERR(ctm_state))
541 return PTR_ERR(ctm_state);
546 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
547 if (new_crtc_state->ctm == old_crtc_state->ctm)
551 ctm_state = vc4_get_ctm_state(state, &vc4->ctm_manager);
552 if (IS_ERR(ctm_state))
553 return PTR_ERR(ctm_state);
556 /* CTM is being enabled or the matrix changed. */
557 if (new_crtc_state->ctm) {
558 struct vc4_crtc_state *vc4_crtc_state =
559 to_vc4_crtc_state(new_crtc_state);
561 /* fifo is 1-based since 0 disables CTM. */
562 int fifo = vc4_crtc_state->assigned_channel + 1;
564 /* Check userland isn't trying to turn on CTM for more
565 * than one CRTC at a time.
567 if (ctm_state->fifo && ctm_state->fifo != fifo) {
568 DRM_DEBUG_DRIVER("Too many CTM configured\n");
572 /* Check we can approximate the specified CTM.
573 * We disallow scalars |c| > 1.0 since the HW has
576 ctm = new_crtc_state->ctm->data;
577 for (i = 0; i < ARRAY_SIZE(ctm->matrix); i++) {
578 u64 val = ctm->matrix[i];
581 if (val > BIT_ULL(32))
585 ctm_state->fifo = fifo;
586 ctm_state->ctm = ctm;
593 static int vc4_load_tracker_atomic_check(struct drm_atomic_state *state)
595 struct drm_plane_state *old_plane_state, *new_plane_state;
596 struct vc4_dev *vc4 = to_vc4_dev(state->dev);
597 struct vc4_load_tracker_state *load_state;
598 struct drm_private_state *priv_state;
599 struct drm_plane *plane;
602 priv_state = drm_atomic_get_private_obj_state(state,
604 if (IS_ERR(priv_state))
605 return PTR_ERR(priv_state);
607 load_state = to_vc4_load_tracker_state(priv_state);
608 for_each_oldnew_plane_in_state(state, plane, old_plane_state,
609 new_plane_state, i) {
610 struct vc4_plane_state *vc4_plane_state;
612 if (old_plane_state->fb && old_plane_state->crtc) {
613 vc4_plane_state = to_vc4_plane_state(old_plane_state);
614 load_state->membus_load -= vc4_plane_state->membus_load;
615 load_state->hvs_load -= vc4_plane_state->hvs_load;
618 if (new_plane_state->fb && new_plane_state->crtc) {
619 vc4_plane_state = to_vc4_plane_state(new_plane_state);
620 load_state->membus_load += vc4_plane_state->membus_load;
621 load_state->hvs_load += vc4_plane_state->hvs_load;
625 /* Don't check the load when the tracker is disabled. */
626 if (!vc4->load_tracker_enabled)
629 /* The absolute limit is 2Gbyte/sec, but let's take a margin to let
630 * the system work when other blocks are accessing the memory.
632 if (load_state->membus_load > SZ_1G + SZ_512M)
635 /* HVS clock is supposed to run @ 250Mhz, let's take a margin and
636 * consider the maximum number of cycles is 240M.
638 if (load_state->hvs_load > 240000000ULL)
644 static struct drm_private_state *
645 vc4_load_tracker_duplicate_state(struct drm_private_obj *obj)
647 struct vc4_load_tracker_state *state;
649 state = kmemdup(obj->state, sizeof(*state), GFP_KERNEL);
653 __drm_atomic_helper_private_obj_duplicate_state(obj, &state->base);
658 static void vc4_load_tracker_destroy_state(struct drm_private_obj *obj,
659 struct drm_private_state *state)
661 struct vc4_load_tracker_state *load_state;
663 load_state = to_vc4_load_tracker_state(state);
667 static const struct drm_private_state_funcs vc4_load_tracker_state_funcs = {
668 .atomic_duplicate_state = vc4_load_tracker_duplicate_state,
669 .atomic_destroy_state = vc4_load_tracker_destroy_state,
672 static void vc4_load_tracker_obj_fini(struct drm_device *dev, void *unused)
674 struct vc4_dev *vc4 = to_vc4_dev(dev);
676 drm_atomic_private_obj_fini(&vc4->load_tracker);
679 static int vc4_load_tracker_obj_init(struct vc4_dev *vc4)
681 struct vc4_load_tracker_state *load_state;
683 load_state = kzalloc(sizeof(*load_state), GFP_KERNEL);
687 drm_atomic_private_obj_init(&vc4->base, &vc4->load_tracker,
689 &vc4_load_tracker_state_funcs);
691 return drmm_add_action_or_reset(&vc4->base, vc4_load_tracker_obj_fini, NULL);
694 static struct drm_private_state *
695 vc4_hvs_channels_duplicate_state(struct drm_private_obj *obj)
697 struct vc4_hvs_state *old_state = to_vc4_hvs_state(obj->state);
698 struct vc4_hvs_state *state;
701 state = kzalloc(sizeof(*state), GFP_KERNEL);
705 __drm_atomic_helper_private_obj_duplicate_state(obj, &state->base);
707 for (i = 0; i < HVS_NUM_CHANNELS; i++) {
708 state->fifo_state[i].in_use = old_state->fifo_state[i].in_use;
709 state->fifo_state[i].fifo_load = old_state->fifo_state[i].fifo_load;
712 state->core_clock_rate = old_state->core_clock_rate;
717 static void vc4_hvs_channels_destroy_state(struct drm_private_obj *obj,
718 struct drm_private_state *state)
720 struct vc4_hvs_state *hvs_state = to_vc4_hvs_state(state);
723 for (i = 0; i < HVS_NUM_CHANNELS; i++) {
724 if (!hvs_state->fifo_state[i].pending_commit)
727 drm_crtc_commit_put(hvs_state->fifo_state[i].pending_commit);
733 static void vc4_hvs_channels_print_state(struct drm_printer *p,
734 const struct drm_private_state *state)
736 struct vc4_hvs_state *hvs_state = to_vc4_hvs_state(state);
739 drm_printf(p, "HVS State\n");
740 drm_printf(p, "\tCore Clock Rate: %lu\n", hvs_state->core_clock_rate);
742 for (i = 0; i < HVS_NUM_CHANNELS; i++) {
743 drm_printf(p, "\tChannel %d\n", i);
744 drm_printf(p, "\t\tin use=%d\n", hvs_state->fifo_state[i].in_use);
745 drm_printf(p, "\t\tload=%lu\n", hvs_state->fifo_state[i].fifo_load);
749 static const struct drm_private_state_funcs vc4_hvs_state_funcs = {
750 .atomic_duplicate_state = vc4_hvs_channels_duplicate_state,
751 .atomic_destroy_state = vc4_hvs_channels_destroy_state,
752 .atomic_print_state = vc4_hvs_channels_print_state,
755 static void vc4_hvs_channels_obj_fini(struct drm_device *dev, void *unused)
757 struct vc4_dev *vc4 = to_vc4_dev(dev);
759 drm_atomic_private_obj_fini(&vc4->hvs_channels);
762 static int vc4_hvs_channels_obj_init(struct vc4_dev *vc4)
764 struct vc4_hvs_state *state;
766 state = kzalloc(sizeof(*state), GFP_KERNEL);
770 drm_atomic_private_obj_init(&vc4->base, &vc4->hvs_channels,
772 &vc4_hvs_state_funcs);
774 return drmm_add_action_or_reset(&vc4->base, vc4_hvs_channels_obj_fini, NULL);
778 * The BCM2711 HVS has up to 7 outputs connected to the pixelvalves and
779 * the TXP (and therefore all the CRTCs found on that platform).
781 * The naive (and our initial) implementation would just iterate over
782 * all the active CRTCs, try to find a suitable FIFO, and then remove it
783 * from the pool of available FIFOs. However, there are a few corner
784 * cases that need to be considered:
786 * - When running in a dual-display setup (so with two CRTCs involved),
787 * we can update the state of a single CRTC (for example by changing
788 * its mode using xrandr under X11) without affecting the other. In
789 * this case, the other CRTC wouldn't be in the state at all, so we
790 * need to consider all the running CRTCs in the DRM device to assign
791 * a FIFO, not just the one in the state.
793 * - To fix the above, we can't use drm_atomic_get_crtc_state on all
794 * enabled CRTCs to pull their CRTC state into the global state, since
795 * a page flip would start considering their vblank to complete. Since
796 * we don't have a guarantee that they are actually active, that
797 * vblank might never happen, and shouldn't even be considered if we
798 * want to do a page flip on a single CRTC. That can be tested by
799 * doing a modetest -v first on HDMI1 and then on HDMI0.
801 * - Since we need the pixelvalve to be disabled and enabled back when
802 * the FIFO is changed, we should keep the FIFO assigned for as long
803 * as the CRTC is enabled, only considering it free again once that
804 * CRTC has been disabled. This can be tested by booting X11 on a
805 * single display, and changing the resolution down and then back up.
807 static int vc4_pv_muxing_atomic_check(struct drm_device *dev,
808 struct drm_atomic_state *state)
810 struct vc4_hvs_state *hvs_new_state;
811 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
812 struct drm_crtc *crtc;
813 unsigned int unassigned_channels = 0;
816 hvs_new_state = vc4_hvs_get_global_state(state);
817 if (IS_ERR(hvs_new_state))
818 return PTR_ERR(hvs_new_state);
820 for (i = 0; i < ARRAY_SIZE(hvs_new_state->fifo_state); i++)
821 if (!hvs_new_state->fifo_state[i].in_use)
822 unassigned_channels |= BIT(i);
824 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
825 struct vc4_crtc_state *old_vc4_crtc_state =
826 to_vc4_crtc_state(old_crtc_state);
827 struct vc4_crtc_state *new_vc4_crtc_state =
828 to_vc4_crtc_state(new_crtc_state);
829 struct vc4_crtc *vc4_crtc = to_vc4_crtc(crtc);
830 unsigned int matching_channels;
831 unsigned int channel;
833 drm_dbg(dev, "%s: Trying to find a channel.\n", crtc->name);
835 /* Nothing to do here, let's skip it */
836 if (old_crtc_state->enable == new_crtc_state->enable) {
837 if (new_crtc_state->enable)
838 drm_dbg(dev, "%s: Already enabled, reusing channel %d.\n",
839 crtc->name, new_vc4_crtc_state->assigned_channel);
841 drm_dbg(dev, "%s: Disabled, ignoring.\n", crtc->name);
846 /* Muxing will need to be modified, mark it as such */
847 new_vc4_crtc_state->update_muxing = true;
849 /* If we're disabling our CRTC, we put back our channel */
850 if (!new_crtc_state->enable) {
851 channel = old_vc4_crtc_state->assigned_channel;
853 drm_dbg(dev, "%s: Disabling, Freeing channel %d\n",
854 crtc->name, channel);
856 hvs_new_state->fifo_state[channel].in_use = false;
857 new_vc4_crtc_state->assigned_channel = VC4_HVS_CHANNEL_DISABLED;
862 * The problem we have to solve here is that we have
863 * up to 7 encoders, connected to up to 6 CRTCs.
865 * Those CRTCs, depending on the instance, can be
866 * routed to 1, 2 or 3 HVS FIFOs, and we need to set
867 * the change the muxing between FIFOs and outputs in
868 * the HVS accordingly.
870 * It would be pretty hard to come up with an
871 * algorithm that would generically solve
872 * this. However, the current routing trees we support
873 * allow us to simplify a bit the problem.
875 * Indeed, with the current supported layouts, if we
876 * try to assign in the ascending crtc index order the
877 * FIFOs, we can't fall into the situation where an
878 * earlier CRTC that had multiple routes is assigned
879 * one that was the only option for a later CRTC.
881 * If the layout changes and doesn't give us that in
882 * the future, we will need to have something smarter,
883 * but it works so far.
885 matching_channels = unassigned_channels & vc4_crtc->data->hvs_available_channels;
886 if (!matching_channels)
889 channel = ffs(matching_channels) - 1;
891 drm_dbg(dev, "Assigned HVS channel %d to CRTC %s\n", channel, crtc->name);
892 new_vc4_crtc_state->assigned_channel = channel;
893 unassigned_channels &= ~BIT(channel);
894 hvs_new_state->fifo_state[channel].in_use = true;
901 vc4_core_clock_atomic_check(struct drm_atomic_state *state)
903 struct vc4_dev *vc4 = to_vc4_dev(state->dev);
904 struct drm_private_state *priv_state;
905 struct vc4_hvs_state *hvs_new_state;
906 struct vc4_load_tracker_state *load_state;
907 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
908 struct drm_crtc *crtc;
909 unsigned int num_outputs;
910 unsigned long pixel_rate;
911 unsigned long cob_rate;
914 priv_state = drm_atomic_get_private_obj_state(state,
916 if (IS_ERR(priv_state))
917 return PTR_ERR(priv_state);
919 load_state = to_vc4_load_tracker_state(priv_state);
921 hvs_new_state = vc4_hvs_get_global_state(state);
922 if (IS_ERR(hvs_new_state))
923 return PTR_ERR(hvs_new_state);
925 for_each_oldnew_crtc_in_state(state, crtc,
929 if (old_crtc_state->active) {
930 struct vc4_crtc_state *old_vc4_state =
931 to_vc4_crtc_state(old_crtc_state);
932 unsigned int channel = old_vc4_state->assigned_channel;
934 hvs_new_state->fifo_state[channel].fifo_load = 0;
937 if (new_crtc_state->active) {
938 struct vc4_crtc_state *new_vc4_state =
939 to_vc4_crtc_state(new_crtc_state);
940 unsigned int channel = new_vc4_state->assigned_channel;
942 hvs_new_state->fifo_state[channel].fifo_load =
943 new_vc4_state->hvs_load;
949 for (i = 0; i < HVS_NUM_CHANNELS; i++) {
950 if (!hvs_new_state->fifo_state[i].in_use)
954 cob_rate = max_t(unsigned long,
955 hvs_new_state->fifo_state[i].fifo_load,
959 pixel_rate = load_state->hvs_load;
960 if (num_outputs > 1) {
961 pixel_rate = (pixel_rate * 40) / 100;
963 pixel_rate = (pixel_rate * 60) / 100;
966 hvs_new_state->core_clock_rate = max(cob_rate, pixel_rate);
973 vc4_atomic_check(struct drm_device *dev, struct drm_atomic_state *state)
977 ret = vc4_pv_muxing_atomic_check(dev, state);
981 ret = vc4_ctm_atomic_check(dev, state);
985 ret = drm_atomic_helper_check(dev, state);
989 ret = vc4_load_tracker_atomic_check(state);
993 return vc4_core_clock_atomic_check(state);
996 static struct drm_mode_config_helper_funcs vc4_mode_config_helpers = {
997 .atomic_commit_setup = vc4_atomic_commit_setup,
998 .atomic_commit_tail = vc4_atomic_commit_tail,
1001 static const struct drm_mode_config_funcs vc4_mode_funcs = {
1002 .atomic_check = vc4_atomic_check,
1003 .atomic_commit = drm_atomic_helper_commit,
1004 .fb_create = vc4_fb_create,
1007 static const struct drm_mode_config_funcs vc5_mode_funcs = {
1008 .atomic_check = vc4_atomic_check,
1009 .atomic_commit = drm_atomic_helper_commit,
1010 .fb_create = drm_gem_fb_create,
1013 int vc4_kms_load(struct drm_device *dev)
1015 struct vc4_dev *vc4 = to_vc4_dev(dev);
1019 * The limits enforced by the load tracker aren't relevant for
1020 * the BCM2711, but the load tracker computations are used for
1021 * the core clock rate calculation.
1024 /* Start with the load tracker enabled. Can be
1025 * disabled through the debugfs load_tracker file.
1027 vc4->load_tracker_enabled = true;
1030 /* Set support for vblank irq fast disable, before drm_vblank_init() */
1031 dev->vblank_disable_immediate = true;
1033 ret = drm_vblank_init(dev, dev->mode_config.num_crtc);
1035 dev_err(dev->dev, "failed to initialize vblank\n");
1040 dev->mode_config.max_width = 7680;
1041 dev->mode_config.max_height = 7680;
1043 dev->mode_config.max_width = 2048;
1044 dev->mode_config.max_height = 2048;
1047 dev->mode_config.funcs = vc4->is_vc5 ? &vc5_mode_funcs : &vc4_mode_funcs;
1048 dev->mode_config.helper_private = &vc4_mode_config_helpers;
1049 dev->mode_config.preferred_depth = 24;
1050 dev->mode_config.async_page_flip = true;
1052 ret = vc4_ctm_obj_init(vc4);
1056 ret = vc4_load_tracker_obj_init(vc4);
1060 ret = vc4_hvs_channels_obj_init(vc4);
1064 drm_mode_config_reset(dev);
1066 drm_kms_helper_poll_init(dev);