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>
15 #include <linux/sort.h>
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_probe_helper.h>
23 #include <drm/drm_vblank.h>
28 struct vc4_ctm_state {
29 struct drm_private_state base;
30 struct drm_color_ctm *ctm;
34 #define to_vc4_ctm_state(_state) \
35 container_of_const(_state, struct vc4_ctm_state, base)
37 struct vc4_load_tracker_state {
38 struct drm_private_state base;
43 #define to_vc4_load_tracker_state(_state) \
44 container_of_const(_state, struct vc4_load_tracker_state, base)
46 static struct vc4_ctm_state *vc4_get_ctm_state(struct drm_atomic_state *state,
47 struct drm_private_obj *manager)
49 struct drm_device *dev = state->dev;
50 struct vc4_dev *vc4 = to_vc4_dev(dev);
51 struct drm_private_state *priv_state;
54 ret = drm_modeset_lock(&vc4->ctm_state_lock, state->acquire_ctx);
58 priv_state = drm_atomic_get_private_obj_state(state, manager);
59 if (IS_ERR(priv_state))
60 return ERR_CAST(priv_state);
62 return to_vc4_ctm_state(priv_state);
65 static struct drm_private_state *
66 vc4_ctm_duplicate_state(struct drm_private_obj *obj)
68 struct vc4_ctm_state *state;
70 state = kmemdup(obj->state, sizeof(*state), GFP_KERNEL);
74 __drm_atomic_helper_private_obj_duplicate_state(obj, &state->base);
79 static void vc4_ctm_destroy_state(struct drm_private_obj *obj,
80 struct drm_private_state *state)
82 struct vc4_ctm_state *ctm_state = to_vc4_ctm_state(state);
87 static const struct drm_private_state_funcs vc4_ctm_state_funcs = {
88 .atomic_duplicate_state = vc4_ctm_duplicate_state,
89 .atomic_destroy_state = vc4_ctm_destroy_state,
92 static void vc4_ctm_obj_fini(struct drm_device *dev, void *unused)
94 struct vc4_dev *vc4 = to_vc4_dev(dev);
96 drm_atomic_private_obj_fini(&vc4->ctm_manager);
99 static int vc4_ctm_obj_init(struct vc4_dev *vc4)
101 struct vc4_ctm_state *ctm_state;
103 drm_modeset_lock_init(&vc4->ctm_state_lock);
105 ctm_state = kzalloc(sizeof(*ctm_state), GFP_KERNEL);
109 drm_atomic_private_obj_init(&vc4->base, &vc4->ctm_manager, &ctm_state->base,
110 &vc4_ctm_state_funcs);
112 return drmm_add_action_or_reset(&vc4->base, vc4_ctm_obj_fini, NULL);
115 /* Converts a DRM S31.32 value to the HW S0.9 format. */
116 static u16 vc4_ctm_s31_32_to_s0_9(u64 in)
121 r = in & BIT_ULL(63) ? BIT(9) : 0;
123 if ((in & GENMASK_ULL(62, 32)) > 0) {
124 /* We have zero integer bits so we can only saturate here. */
127 /* Otherwise take the 9 most important fractional bits. */
128 r |= (in >> 23) & GENMASK(8, 0);
135 vc4_ctm_commit(struct vc4_dev *vc4, struct drm_atomic_state *state)
137 struct vc4_hvs *hvs = vc4->hvs;
138 struct vc4_ctm_state *ctm_state = to_vc4_ctm_state(vc4->ctm_manager.state);
139 struct drm_color_ctm *ctm = ctm_state->ctm;
141 WARN_ON_ONCE(vc4->gen > VC4_GEN_5);
143 if (ctm_state->fifo) {
144 HVS_WRITE(SCALER_OLEDCOEF2,
145 VC4_SET_FIELD(vc4_ctm_s31_32_to_s0_9(ctm->matrix[0]),
146 SCALER_OLEDCOEF2_R_TO_R) |
147 VC4_SET_FIELD(vc4_ctm_s31_32_to_s0_9(ctm->matrix[3]),
148 SCALER_OLEDCOEF2_R_TO_G) |
149 VC4_SET_FIELD(vc4_ctm_s31_32_to_s0_9(ctm->matrix[6]),
150 SCALER_OLEDCOEF2_R_TO_B));
151 HVS_WRITE(SCALER_OLEDCOEF1,
152 VC4_SET_FIELD(vc4_ctm_s31_32_to_s0_9(ctm->matrix[1]),
153 SCALER_OLEDCOEF1_G_TO_R) |
154 VC4_SET_FIELD(vc4_ctm_s31_32_to_s0_9(ctm->matrix[4]),
155 SCALER_OLEDCOEF1_G_TO_G) |
156 VC4_SET_FIELD(vc4_ctm_s31_32_to_s0_9(ctm->matrix[7]),
157 SCALER_OLEDCOEF1_G_TO_B));
158 HVS_WRITE(SCALER_OLEDCOEF0,
159 VC4_SET_FIELD(vc4_ctm_s31_32_to_s0_9(ctm->matrix[2]),
160 SCALER_OLEDCOEF0_B_TO_R) |
161 VC4_SET_FIELD(vc4_ctm_s31_32_to_s0_9(ctm->matrix[5]),
162 SCALER_OLEDCOEF0_B_TO_G) |
163 VC4_SET_FIELD(vc4_ctm_s31_32_to_s0_9(ctm->matrix[8]),
164 SCALER_OLEDCOEF0_B_TO_B));
167 HVS_WRITE(SCALER_OLEDOFFS,
168 VC4_SET_FIELD(ctm_state->fifo, SCALER_OLEDOFFS_DISPFIFO));
171 struct vc4_hvs_state *
172 vc4_hvs_get_new_global_state(const struct drm_atomic_state *state)
174 struct vc4_dev *vc4 = to_vc4_dev(state->dev);
175 struct drm_private_state *priv_state;
177 priv_state = drm_atomic_get_new_private_obj_state(state, &vc4->hvs_channels);
179 return ERR_PTR(-EINVAL);
181 return to_vc4_hvs_state(priv_state);
184 struct vc4_hvs_state *
185 vc4_hvs_get_old_global_state(const struct drm_atomic_state *state)
187 struct vc4_dev *vc4 = to_vc4_dev(state->dev);
188 struct drm_private_state *priv_state;
190 priv_state = drm_atomic_get_old_private_obj_state(state, &vc4->hvs_channels);
192 return ERR_PTR(-EINVAL);
194 return to_vc4_hvs_state(priv_state);
197 struct vc4_hvs_state *
198 vc4_hvs_get_global_state(struct drm_atomic_state *state)
200 struct vc4_dev *vc4 = to_vc4_dev(state->dev);
201 struct drm_private_state *priv_state;
203 priv_state = drm_atomic_get_private_obj_state(state, &vc4->hvs_channels);
204 if (IS_ERR(priv_state))
205 return ERR_CAST(priv_state);
207 return to_vc4_hvs_state(priv_state);
210 static void vc4_hvs_pv_muxing_commit(struct vc4_dev *vc4,
211 struct drm_atomic_state *state)
213 struct vc4_hvs *hvs = vc4->hvs;
214 struct drm_crtc_state *crtc_state;
215 struct drm_crtc *crtc;
218 WARN_ON_ONCE(vc4->gen != VC4_GEN_4);
220 for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
221 struct vc4_crtc *vc4_crtc = to_vc4_crtc(crtc);
222 struct vc4_crtc_state *vc4_state = to_vc4_crtc_state(crtc_state);
226 if (!crtc_state->active)
229 if (vc4_state->assigned_channel != 2)
233 * SCALER_DISPCTRL_DSP3 = X, where X < 2 means 'connect DSP3 to
235 * SCALER_DISPCTRL_DSP3 = 3 means 'disable DSP 3'.
237 * DSP3 is connected to FIFO2 unless the transposer is
238 * enabled. In this case, FIFO 2 is directly accessed by the
239 * TXP IP, and we need to disable the FIFO2 -> pixelvalve1
242 if (vc4_crtc->feeds_txp)
243 dsp3_mux = VC4_SET_FIELD(3, SCALER_DISPCTRL_DSP3_MUX);
245 dsp3_mux = VC4_SET_FIELD(2, SCALER_DISPCTRL_DSP3_MUX);
247 dispctrl = HVS_READ(SCALER_DISPCTRL) &
248 ~SCALER_DISPCTRL_DSP3_MUX_MASK;
249 HVS_WRITE(SCALER_DISPCTRL, dispctrl | dsp3_mux);
253 static void vc5_hvs_pv_muxing_commit(struct vc4_dev *vc4,
254 struct drm_atomic_state *state)
256 struct vc4_hvs *hvs = vc4->hvs;
257 struct drm_crtc_state *crtc_state;
258 struct drm_crtc *crtc;
263 WARN_ON_ONCE(vc4->gen != VC4_GEN_5);
265 for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
266 struct vc4_crtc_state *vc4_state = to_vc4_crtc_state(crtc_state);
267 struct vc4_crtc *vc4_crtc = to_vc4_crtc(crtc);
268 unsigned int channel = vc4_state->assigned_channel;
270 if (!vc4_state->update_muxing)
273 switch (vc4_crtc->data->hvs_output) {
275 drm_WARN_ON(&vc4->base,
276 VC4_GET_FIELD(HVS_READ(SCALER_DISPCTRL),
277 SCALER_DISPCTRL_DSP3_MUX) == channel);
279 mux = (channel == 2) ? 0 : 1;
280 reg = HVS_READ(SCALER_DISPECTRL);
281 HVS_WRITE(SCALER_DISPECTRL,
282 (reg & ~SCALER_DISPECTRL_DSP2_MUX_MASK) |
283 VC4_SET_FIELD(mux, SCALER_DISPECTRL_DSP2_MUX));
287 if (channel == VC4_HVS_CHANNEL_DISABLED)
292 reg = HVS_READ(SCALER_DISPCTRL);
293 HVS_WRITE(SCALER_DISPCTRL,
294 (reg & ~SCALER_DISPCTRL_DSP3_MUX_MASK) |
295 VC4_SET_FIELD(mux, SCALER_DISPCTRL_DSP3_MUX));
299 if (channel == VC4_HVS_CHANNEL_DISABLED)
304 reg = HVS_READ(SCALER_DISPEOLN);
305 HVS_WRITE(SCALER_DISPEOLN,
306 (reg & ~SCALER_DISPEOLN_DSP4_MUX_MASK) |
307 VC4_SET_FIELD(mux, SCALER_DISPEOLN_DSP4_MUX));
312 if (channel == VC4_HVS_CHANNEL_DISABLED)
317 reg = HVS_READ(SCALER_DISPDITHER);
318 HVS_WRITE(SCALER_DISPDITHER,
319 (reg & ~SCALER_DISPDITHER_DSP5_MUX_MASK) |
320 VC4_SET_FIELD(mux, SCALER_DISPDITHER_DSP5_MUX));
329 static void vc6_hvs_pv_muxing_commit(struct vc4_dev *vc4,
330 struct drm_atomic_state *state)
332 struct vc4_hvs *hvs = vc4->hvs;
333 struct drm_crtc_state *crtc_state;
334 struct drm_crtc *crtc;
337 WARN_ON_ONCE(vc4->gen != VC4_GEN_6_C && vc4->gen != VC4_GEN_6_D);
339 for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
340 struct vc4_crtc_state *vc4_state = to_vc4_crtc_state(crtc_state);
341 struct vc4_encoder *vc4_encoder;
342 struct drm_encoder *encoder;
346 if (!vc4_state->update_muxing)
349 if (vc4_state->assigned_channel != 1)
352 encoder = vc4_get_crtc_encoder(crtc, crtc_state);
353 vc4_encoder = to_vc4_encoder(encoder);
354 switch (vc4_encoder->type) {
355 case VC4_ENCODER_TYPE_HDMI1:
359 case VC4_ENCODER_TYPE_TXP1:
364 drm_err(&vc4->base, "Unhandled encoder type for PV muxing %d",
370 reg = HVS_READ(SCALER6_CONTROL);
371 HVS_WRITE(SCALER6_CONTROL,
372 (reg & ~SCALER6_CONTROL_DSP1_TARGET_MASK) |
373 VC4_SET_FIELD(mux, SCALER6_CONTROL_DSP1_TARGET));
377 static void vc4_atomic_commit_tail(struct drm_atomic_state *state)
379 struct drm_device *dev = state->dev;
380 struct vc4_dev *vc4 = to_vc4_dev(dev);
381 struct vc4_hvs *hvs = vc4->hvs;
382 struct vc4_hvs_state *new_hvs_state;
383 struct vc4_hvs_state *old_hvs_state;
384 unsigned int channel;
386 old_hvs_state = vc4_hvs_get_old_global_state(state);
387 if (WARN_ON(IS_ERR(old_hvs_state)))
390 new_hvs_state = vc4_hvs_get_new_global_state(state);
391 if (WARN_ON(IS_ERR(new_hvs_state)))
394 if (vc4->gen < VC4_GEN_6_C) {
395 struct drm_crtc_state *new_crtc_state;
396 struct drm_crtc *crtc;
399 for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
400 struct vc4_crtc_state *vc4_crtc_state;
402 if (!new_crtc_state->commit)
405 vc4_crtc_state = to_vc4_crtc_state(new_crtc_state);
406 vc4_hvs_mask_underrun(hvs, vc4_crtc_state->assigned_channel);
410 for (channel = 0; channel < HVS_NUM_CHANNELS; channel++) {
411 struct drm_crtc_commit *commit;
414 if (!old_hvs_state->fifo_state[channel].in_use)
417 commit = old_hvs_state->fifo_state[channel].pending_commit;
421 ret = drm_crtc_commit_wait(commit);
423 drm_err(dev, "Timed out waiting for commit\n");
425 drm_crtc_commit_put(commit);
426 old_hvs_state->fifo_state[channel].pending_commit = NULL;
429 if (vc4->gen == VC4_GEN_5) {
430 unsigned long state_rate = max(old_hvs_state->core_clock_rate,
431 new_hvs_state->core_clock_rate);
432 unsigned long core_rate = clamp_t(unsigned long, state_rate,
433 500000000, hvs->max_core_rate);
435 drm_dbg(dev, "Raising the core clock at %lu Hz\n", core_rate);
438 * Do a temporary request on the core clock during the
441 WARN_ON(clk_set_min_rate(hvs->core_clk, core_rate));
442 WARN_ON(clk_set_min_rate(hvs->disp_clk, core_rate));
445 drm_atomic_helper_commit_modeset_disables(dev, state);
447 if (vc4->gen <= VC4_GEN_5)
448 vc4_ctm_commit(vc4, state);
452 vc4_hvs_pv_muxing_commit(vc4, state);
456 vc5_hvs_pv_muxing_commit(vc4, state);
461 vc6_hvs_pv_muxing_commit(vc4, state);
465 drm_err(dev, "Unknown VC4 generation: %d", vc4->gen);
469 drm_atomic_helper_commit_planes(dev, state,
470 DRM_PLANE_COMMIT_ACTIVE_ONLY);
472 drm_atomic_helper_commit_modeset_enables(dev, state);
474 drm_atomic_helper_fake_vblank(state);
476 drm_atomic_helper_commit_hw_done(state);
478 drm_atomic_helper_wait_for_flip_done(dev, state);
480 drm_atomic_helper_cleanup_planes(dev, state);
482 if (vc4->gen == VC4_GEN_5) {
483 unsigned long core_rate = min_t(unsigned long,
485 new_hvs_state->core_clock_rate);
487 drm_dbg(dev, "Running the core clock at %lu Hz\n", core_rate);
490 * Request a clock rate based on the current HVS
493 WARN_ON(clk_set_min_rate(hvs->core_clk, core_rate));
494 WARN_ON(clk_set_min_rate(hvs->disp_clk, core_rate));
496 drm_dbg(dev, "Core clock actual rate: %lu Hz\n",
497 clk_get_rate(hvs->core_clk));
501 static int vc4_atomic_commit_setup(struct drm_atomic_state *state)
503 struct drm_crtc_state *crtc_state;
504 struct vc4_hvs_state *hvs_state;
505 struct drm_crtc *crtc;
508 hvs_state = vc4_hvs_get_new_global_state(state);
509 if (WARN_ON(IS_ERR(hvs_state)))
510 return PTR_ERR(hvs_state);
512 for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
513 struct vc4_crtc_state *vc4_crtc_state =
514 to_vc4_crtc_state(crtc_state);
515 unsigned int channel =
516 vc4_crtc_state->assigned_channel;
518 if (channel == VC4_HVS_CHANNEL_DISABLED)
521 if (!hvs_state->fifo_state[channel].in_use)
524 hvs_state->fifo_state[channel].pending_commit =
525 drm_crtc_commit_get(crtc_state->commit);
531 static struct drm_framebuffer *vc4_fb_create(struct drm_device *dev,
532 struct drm_file *file_priv,
533 const struct drm_mode_fb_cmd2 *mode_cmd)
535 struct vc4_dev *vc4 = to_vc4_dev(dev);
536 struct drm_mode_fb_cmd2 mode_cmd_local;
538 if (WARN_ON_ONCE(vc4->gen > VC4_GEN_4))
539 return ERR_PTR(-ENODEV);
541 /* If the user didn't specify a modifier, use the
542 * vc4_set_tiling_ioctl() state for the BO.
544 if (!(mode_cmd->flags & DRM_MODE_FB_MODIFIERS)) {
545 struct drm_gem_object *gem_obj;
548 gem_obj = drm_gem_object_lookup(file_priv,
549 mode_cmd->handles[0]);
551 DRM_DEBUG("Failed to look up GEM BO %d\n",
552 mode_cmd->handles[0]);
553 return ERR_PTR(-ENOENT);
555 bo = to_vc4_bo(gem_obj);
557 mode_cmd_local = *mode_cmd;
560 mode_cmd_local.modifier[0] =
561 DRM_FORMAT_MOD_BROADCOM_VC4_T_TILED;
563 mode_cmd_local.modifier[0] = DRM_FORMAT_MOD_NONE;
566 drm_gem_object_put(gem_obj);
568 mode_cmd = &mode_cmd_local;
571 return drm_gem_fb_create(dev, file_priv, mode_cmd);
574 /* Our CTM has some peculiar limitations: we can only enable it for one CRTC
575 * at a time and the HW only supports S0.9 scalars. To account for the latter,
576 * we don't allow userland to set a CTM that we have no hope of approximating.
579 vc4_ctm_atomic_check(struct drm_device *dev, struct drm_atomic_state *state)
581 struct vc4_dev *vc4 = to_vc4_dev(dev);
582 struct vc4_ctm_state *ctm_state = NULL;
583 struct drm_crtc *crtc;
584 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
585 struct drm_color_ctm *ctm;
588 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
589 /* CTM is being disabled. */
590 if (!new_crtc_state->ctm && old_crtc_state->ctm) {
591 ctm_state = vc4_get_ctm_state(state, &vc4->ctm_manager);
592 if (IS_ERR(ctm_state))
593 return PTR_ERR(ctm_state);
598 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
599 if (new_crtc_state->ctm == old_crtc_state->ctm)
603 ctm_state = vc4_get_ctm_state(state, &vc4->ctm_manager);
604 if (IS_ERR(ctm_state))
605 return PTR_ERR(ctm_state);
608 /* CTM is being enabled or the matrix changed. */
609 if (new_crtc_state->ctm) {
610 struct vc4_crtc_state *vc4_crtc_state =
611 to_vc4_crtc_state(new_crtc_state);
613 /* fifo is 1-based since 0 disables CTM. */
614 int fifo = vc4_crtc_state->assigned_channel + 1;
616 /* Check userland isn't trying to turn on CTM for more
617 * than one CRTC at a time.
619 if (ctm_state->fifo && ctm_state->fifo != fifo) {
620 DRM_DEBUG_DRIVER("Too many CTM configured\n");
624 /* Check we can approximate the specified CTM.
625 * We disallow scalars |c| > 1.0 since the HW has
628 ctm = new_crtc_state->ctm->data;
629 for (i = 0; i < ARRAY_SIZE(ctm->matrix); i++) {
630 u64 val = ctm->matrix[i];
633 if (val > BIT_ULL(32))
637 ctm_state->fifo = fifo;
638 ctm_state->ctm = ctm;
645 static int vc4_load_tracker_atomic_check(struct drm_atomic_state *state)
647 struct drm_plane_state *old_plane_state, *new_plane_state;
648 struct vc4_dev *vc4 = to_vc4_dev(state->dev);
649 struct vc4_load_tracker_state *load_state;
650 struct drm_private_state *priv_state;
651 struct drm_plane *plane;
654 priv_state = drm_atomic_get_private_obj_state(state,
656 if (IS_ERR(priv_state))
657 return PTR_ERR(priv_state);
659 load_state = to_vc4_load_tracker_state(priv_state);
660 for_each_oldnew_plane_in_state(state, plane, old_plane_state,
661 new_plane_state, i) {
662 struct vc4_plane_state *vc4_plane_state;
664 if (old_plane_state->fb && old_plane_state->crtc) {
665 vc4_plane_state = to_vc4_plane_state(old_plane_state);
666 load_state->membus_load -= vc4_plane_state->membus_load;
667 load_state->hvs_load -= vc4_plane_state->hvs_load;
670 if (new_plane_state->fb && new_plane_state->crtc) {
671 vc4_plane_state = to_vc4_plane_state(new_plane_state);
672 load_state->membus_load += vc4_plane_state->membus_load;
673 load_state->hvs_load += vc4_plane_state->hvs_load;
677 /* Don't check the load when the tracker is disabled. */
678 if (!vc4->load_tracker_enabled)
681 /* The absolute limit is 2Gbyte/sec, but let's take a margin to let
682 * the system work when other blocks are accessing the memory.
684 if (load_state->membus_load > SZ_1G + SZ_512M)
687 /* HVS clock is supposed to run @ 250Mhz, let's take a margin and
688 * consider the maximum number of cycles is 240M.
690 if (load_state->hvs_load > 240000000ULL)
696 static struct drm_private_state *
697 vc4_load_tracker_duplicate_state(struct drm_private_obj *obj)
699 struct vc4_load_tracker_state *state;
701 state = kmemdup(obj->state, sizeof(*state), GFP_KERNEL);
705 __drm_atomic_helper_private_obj_duplicate_state(obj, &state->base);
710 static void vc4_load_tracker_destroy_state(struct drm_private_obj *obj,
711 struct drm_private_state *state)
713 struct vc4_load_tracker_state *load_state;
715 load_state = to_vc4_load_tracker_state(state);
719 static const struct drm_private_state_funcs vc4_load_tracker_state_funcs = {
720 .atomic_duplicate_state = vc4_load_tracker_duplicate_state,
721 .atomic_destroy_state = vc4_load_tracker_destroy_state,
724 static void vc4_load_tracker_obj_fini(struct drm_device *dev, void *unused)
726 struct vc4_dev *vc4 = to_vc4_dev(dev);
728 drm_atomic_private_obj_fini(&vc4->load_tracker);
731 static int vc4_load_tracker_obj_init(struct vc4_dev *vc4)
733 struct vc4_load_tracker_state *load_state;
735 load_state = kzalloc(sizeof(*load_state), GFP_KERNEL);
739 drm_atomic_private_obj_init(&vc4->base, &vc4->load_tracker,
741 &vc4_load_tracker_state_funcs);
743 return drmm_add_action_or_reset(&vc4->base, vc4_load_tracker_obj_fini, NULL);
746 static struct drm_private_state *
747 vc4_hvs_channels_duplicate_state(struct drm_private_obj *obj)
749 struct vc4_hvs_state *old_state = to_vc4_hvs_state(obj->state);
750 struct vc4_hvs_state *state;
753 state = kzalloc(sizeof(*state), GFP_KERNEL);
757 __drm_atomic_helper_private_obj_duplicate_state(obj, &state->base);
759 for (i = 0; i < HVS_NUM_CHANNELS; i++) {
760 state->fifo_state[i].in_use = old_state->fifo_state[i].in_use;
761 state->fifo_state[i].fifo_load = old_state->fifo_state[i].fifo_load;
764 state->core_clock_rate = old_state->core_clock_rate;
769 static void vc4_hvs_channels_destroy_state(struct drm_private_obj *obj,
770 struct drm_private_state *state)
772 struct vc4_hvs_state *hvs_state = to_vc4_hvs_state(state);
775 for (i = 0; i < HVS_NUM_CHANNELS; i++) {
776 if (!hvs_state->fifo_state[i].pending_commit)
779 drm_crtc_commit_put(hvs_state->fifo_state[i].pending_commit);
785 static void vc4_hvs_channels_print_state(struct drm_printer *p,
786 const struct drm_private_state *state)
788 const struct vc4_hvs_state *hvs_state = to_vc4_hvs_state(state);
791 drm_printf(p, "HVS State\n");
792 drm_printf(p, "\tCore Clock Rate: %lu\n", hvs_state->core_clock_rate);
794 for (i = 0; i < HVS_NUM_CHANNELS; i++) {
795 drm_printf(p, "\tChannel %d\n", i);
796 drm_printf(p, "\t\tin use=%d\n", hvs_state->fifo_state[i].in_use);
797 drm_printf(p, "\t\tload=%lu\n", hvs_state->fifo_state[i].fifo_load);
801 static const struct drm_private_state_funcs vc4_hvs_state_funcs = {
802 .atomic_duplicate_state = vc4_hvs_channels_duplicate_state,
803 .atomic_destroy_state = vc4_hvs_channels_destroy_state,
804 .atomic_print_state = vc4_hvs_channels_print_state,
807 static void vc4_hvs_channels_obj_fini(struct drm_device *dev, void *unused)
809 struct vc4_dev *vc4 = to_vc4_dev(dev);
811 drm_atomic_private_obj_fini(&vc4->hvs_channels);
814 static int vc4_hvs_channels_obj_init(struct vc4_dev *vc4)
816 struct vc4_hvs_state *state;
818 state = kzalloc(sizeof(*state), GFP_KERNEL);
822 drm_atomic_private_obj_init(&vc4->base, &vc4->hvs_channels,
824 &vc4_hvs_state_funcs);
826 return drmm_add_action_or_reset(&vc4->base, vc4_hvs_channels_obj_fini, NULL);
829 static int cmp_vc4_crtc_hvs_output(const void *a, const void *b)
831 const struct vc4_crtc *crtc_a =
832 to_vc4_crtc(*(const struct drm_crtc **)a);
833 const struct vc4_crtc_data *data_a =
834 vc4_crtc_to_vc4_crtc_data(crtc_a);
835 const struct vc4_crtc *crtc_b =
836 to_vc4_crtc(*(const struct drm_crtc **)b);
837 const struct vc4_crtc_data *data_b =
838 vc4_crtc_to_vc4_crtc_data(crtc_b);
840 return data_a->hvs_output - data_b->hvs_output;
844 * The BCM2711 HVS has up to 7 outputs connected to the pixelvalves and
845 * the TXP (and therefore all the CRTCs found on that platform).
847 * The naive (and our initial) implementation would just iterate over
848 * all the active CRTCs, try to find a suitable FIFO, and then remove it
849 * from the pool of available FIFOs. However, there are a few corner
850 * cases that need to be considered:
852 * - When running in a dual-display setup (so with two CRTCs involved),
853 * we can update the state of a single CRTC (for example by changing
854 * its mode using xrandr under X11) without affecting the other. In
855 * this case, the other CRTC wouldn't be in the state at all, so we
856 * need to consider all the running CRTCs in the DRM device to assign
857 * a FIFO, not just the one in the state.
859 * - To fix the above, we can't use drm_atomic_get_crtc_state on all
860 * enabled CRTCs to pull their CRTC state into the global state, since
861 * a page flip would start considering their vblank to complete. Since
862 * we don't have a guarantee that they are actually active, that
863 * vblank might never happen, and shouldn't even be considered if we
864 * want to do a page flip on a single CRTC. That can be tested by
865 * doing a modetest -v first on HDMI1 and then on HDMI0.
867 * - Since we need the pixelvalve to be disabled and enabled back when
868 * the FIFO is changed, we should keep the FIFO assigned for as long
869 * as the CRTC is enabled, only considering it free again once that
870 * CRTC has been disabled. This can be tested by booting X11 on a
871 * single display, and changing the resolution down and then back up.
873 static int vc4_pv_muxing_atomic_check(struct drm_device *dev,
874 struct drm_atomic_state *state)
876 struct vc4_hvs_state *hvs_new_state;
877 struct drm_crtc **sorted_crtcs;
878 struct drm_crtc *crtc;
879 unsigned int unassigned_channels = 0;
883 hvs_new_state = vc4_hvs_get_global_state(state);
884 if (IS_ERR(hvs_new_state))
885 return PTR_ERR(hvs_new_state);
887 for (i = 0; i < ARRAY_SIZE(hvs_new_state->fifo_state); i++)
888 if (!hvs_new_state->fifo_state[i].in_use)
889 unassigned_channels |= BIT(i);
892 * The problem we have to solve here is that we have up to 7
893 * encoders, connected to up to 6 CRTCs.
895 * Those CRTCs, depending on the instance, can be routed to 1, 2
896 * or 3 HVS FIFOs, and we need to set the muxing between FIFOs and
897 * outputs in the HVS accordingly.
899 * It would be pretty hard to come up with an algorithm that
900 * would generically solve this. However, the current routing
901 * trees we support allow us to simplify a bit the problem.
903 * Indeed, with the current supported layouts, if we try to
904 * assign in the ascending crtc index order the FIFOs, we can't
905 * fall into the situation where an earlier CRTC that had
906 * multiple routes is assigned one that was the only option for
909 * If the layout changes and doesn't give us that in the future,
910 * we will need to have something smarter, but it works so far.
912 sorted_crtcs = kmalloc_array(dev->num_crtcs, sizeof(*sorted_crtcs), GFP_KERNEL);
917 drm_for_each_crtc(crtc, dev)
918 sorted_crtcs[i++] = crtc;
920 sort(sorted_crtcs, i, sizeof(*sorted_crtcs), cmp_vc4_crtc_hvs_output, NULL);
922 for (i = 0; i < dev->num_crtcs; i++) {
923 struct vc4_crtc_state *old_vc4_crtc_state, *new_vc4_crtc_state;
924 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
925 struct vc4_crtc *vc4_crtc;
926 unsigned int matching_channels;
927 unsigned int channel;
929 crtc = sorted_crtcs[i];
932 vc4_crtc = to_vc4_crtc(crtc);
934 old_crtc_state = drm_atomic_get_old_crtc_state(state, crtc);
937 old_vc4_crtc_state = to_vc4_crtc_state(old_crtc_state);
939 new_crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
942 new_vc4_crtc_state = to_vc4_crtc_state(new_crtc_state);
944 drm_dbg(dev, "%s: Trying to find a channel.\n", crtc->name);
946 /* Nothing to do here, let's skip it */
947 if (old_crtc_state->enable == new_crtc_state->enable) {
948 if (new_crtc_state->enable)
949 drm_dbg(dev, "%s: Already enabled, reusing channel %d.\n",
950 crtc->name, new_vc4_crtc_state->assigned_channel);
952 drm_dbg(dev, "%s: Disabled, ignoring.\n", crtc->name);
957 /* Muxing will need to be modified, mark it as such */
958 new_vc4_crtc_state->update_muxing = true;
960 /* If we're disabling our CRTC, we put back our channel */
961 if (!new_crtc_state->enable) {
962 channel = old_vc4_crtc_state->assigned_channel;
964 drm_dbg(dev, "%s: Disabling, Freeing channel %d\n",
965 crtc->name, channel);
967 hvs_new_state->fifo_state[channel].in_use = false;
968 new_vc4_crtc_state->assigned_channel = VC4_HVS_CHANNEL_DISABLED;
972 matching_channels = unassigned_channels & vc4_crtc->data->hvs_available_channels;
973 if (!matching_channels) {
975 goto err_free_crtc_array;
978 channel = ffs(matching_channels) - 1;
980 drm_dbg(dev, "Assigned HVS channel %d to CRTC %s\n", channel, crtc->name);
981 new_vc4_crtc_state->assigned_channel = channel;
982 unassigned_channels &= ~BIT(channel);
983 hvs_new_state->fifo_state[channel].in_use = true;
995 vc4_core_clock_atomic_check(struct drm_atomic_state *state)
997 struct vc4_dev *vc4 = to_vc4_dev(state->dev);
998 struct drm_private_state *priv_state;
999 struct vc4_hvs_state *hvs_new_state;
1000 struct vc4_load_tracker_state *load_state;
1001 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
1002 struct drm_crtc *crtc;
1003 unsigned int num_outputs;
1004 unsigned long pixel_rate;
1005 unsigned long cob_rate;
1008 priv_state = drm_atomic_get_private_obj_state(state,
1009 &vc4->load_tracker);
1010 if (IS_ERR(priv_state))
1011 return PTR_ERR(priv_state);
1013 load_state = to_vc4_load_tracker_state(priv_state);
1015 hvs_new_state = vc4_hvs_get_global_state(state);
1016 if (IS_ERR(hvs_new_state))
1017 return PTR_ERR(hvs_new_state);
1019 for_each_oldnew_crtc_in_state(state, crtc,
1023 if (old_crtc_state->active) {
1024 struct vc4_crtc_state *old_vc4_state =
1025 to_vc4_crtc_state(old_crtc_state);
1026 unsigned int channel = old_vc4_state->assigned_channel;
1028 hvs_new_state->fifo_state[channel].fifo_load = 0;
1031 if (new_crtc_state->active) {
1032 struct vc4_crtc_state *new_vc4_state =
1033 to_vc4_crtc_state(new_crtc_state);
1034 unsigned int channel = new_vc4_state->assigned_channel;
1036 hvs_new_state->fifo_state[channel].fifo_load =
1037 new_vc4_state->hvs_load;
1043 for (i = 0; i < HVS_NUM_CHANNELS; i++) {
1044 if (!hvs_new_state->fifo_state[i].in_use)
1048 cob_rate = max_t(unsigned long,
1049 hvs_new_state->fifo_state[i].fifo_load,
1053 pixel_rate = load_state->hvs_load;
1054 if (num_outputs > 1) {
1055 pixel_rate = (pixel_rate * 40) / 100;
1057 pixel_rate = (pixel_rate * 60) / 100;
1060 hvs_new_state->core_clock_rate = max(cob_rate, pixel_rate);
1067 vc4_atomic_check(struct drm_device *dev, struct drm_atomic_state *state)
1071 ret = vc4_pv_muxing_atomic_check(dev, state);
1075 ret = vc4_ctm_atomic_check(dev, state);
1079 ret = drm_atomic_helper_check(dev, state);
1083 ret = vc4_load_tracker_atomic_check(state);
1087 return vc4_core_clock_atomic_check(state);
1090 static struct drm_mode_config_helper_funcs vc4_mode_config_helpers = {
1091 .atomic_commit_setup = vc4_atomic_commit_setup,
1092 .atomic_commit_tail = vc4_atomic_commit_tail,
1095 static const struct drm_mode_config_funcs vc4_mode_funcs = {
1096 .atomic_check = vc4_atomic_check,
1097 .atomic_commit = drm_atomic_helper_commit,
1098 .fb_create = vc4_fb_create,
1101 static const struct drm_mode_config_funcs vc5_mode_funcs = {
1102 .atomic_check = vc4_atomic_check,
1103 .atomic_commit = drm_atomic_helper_commit,
1104 .fb_create = drm_gem_fb_create,
1107 int vc4_kms_load(struct drm_device *dev)
1109 struct vc4_dev *vc4 = to_vc4_dev(dev);
1113 * The limits enforced by the load tracker aren't relevant for
1114 * the BCM2711, but the load tracker computations are used for
1115 * the core clock rate calculation.
1117 if (vc4->gen == VC4_GEN_4) {
1118 /* Start with the load tracker enabled. Can be
1119 * disabled through the debugfs load_tracker file.
1121 vc4->load_tracker_enabled = true;
1124 /* Set support for vblank irq fast disable, before drm_vblank_init() */
1125 dev->vblank_disable_immediate = true;
1127 ret = drm_vblank_init(dev, dev->mode_config.num_crtc);
1129 dev_err(dev->dev, "failed to initialize vblank\n");
1133 if (vc4->gen >= VC4_GEN_6_C) {
1134 dev->mode_config.max_width = 8192;
1135 dev->mode_config.max_height = 8192;
1136 } else if (vc4->gen >= VC4_GEN_5) {
1137 dev->mode_config.max_width = 7680;
1138 dev->mode_config.max_height = 7680;
1140 dev->mode_config.max_width = 2048;
1141 dev->mode_config.max_height = 2048;
1144 dev->mode_config.funcs = (vc4->gen > VC4_GEN_4) ? &vc5_mode_funcs : &vc4_mode_funcs;
1145 dev->mode_config.helper_private = &vc4_mode_config_helpers;
1146 dev->mode_config.preferred_depth = 24;
1147 dev->mode_config.async_page_flip = true;
1148 dev->mode_config.normalize_zpos = true;
1150 ret = vc4_ctm_obj_init(vc4);
1154 ret = vc4_load_tracker_obj_init(vc4);
1158 ret = vc4_hvs_channels_obj_init(vc4);
1162 drm_mode_config_reset(dev);
1164 drm_kms_helper_poll_init(dev);