1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 2c8b75bcaSEric Anholt /* 3c8b75bcaSEric Anholt * Copyright (C) 2015 Broadcom 4c8b75bcaSEric Anholt */ 5c8b75bcaSEric Anholt 6c8b75bcaSEric Anholt /** 7c8b75bcaSEric Anholt * DOC: VC4 KMS 8c8b75bcaSEric Anholt * 9c8b75bcaSEric Anholt * This is the general code for implementing KMS mode setting that 10c8b75bcaSEric Anholt * doesn't clearly associate with any of the other objects (plane, 11c8b75bcaSEric Anholt * crtc, HDMI encoder). 12c8b75bcaSEric Anholt */ 13c8b75bcaSEric Anholt 14d7d96c00SMaxime Ripard #include <linux/clk.h> 15d7d96c00SMaxime Ripard 16b7e8e25bSMasahiro Yamada #include <drm/drm_atomic.h> 17b7e8e25bSMasahiro Yamada #include <drm/drm_atomic_helper.h> 18fd6d6d80SSam Ravnborg #include <drm/drm_crtc.h> 199762477cSNoralf Trønnes #include <drm/drm_gem_framebuffer_helper.h> 20fcd70cd3SDaniel Vetter #include <drm/drm_plane_helper.h> 21fcd70cd3SDaniel Vetter #include <drm/drm_probe_helper.h> 22fd6d6d80SSam Ravnborg #include <drm/drm_vblank.h> 23fd6d6d80SSam Ravnborg 24c8b75bcaSEric Anholt #include "vc4_drv.h" 25766cc6b1SStefan Schake #include "vc4_regs.h" 26766cc6b1SStefan Schake 27a9661f27SMaxime Ripard #define HVS_NUM_CHANNELS 3 28a9661f27SMaxime Ripard 29766cc6b1SStefan Schake struct vc4_ctm_state { 30766cc6b1SStefan Schake struct drm_private_state base; 31766cc6b1SStefan Schake struct drm_color_ctm *ctm; 32766cc6b1SStefan Schake int fifo; 33766cc6b1SStefan Schake }; 34766cc6b1SStefan Schake 35766cc6b1SStefan Schake static struct vc4_ctm_state *to_vc4_ctm_state(struct drm_private_state *priv) 36766cc6b1SStefan Schake { 37766cc6b1SStefan Schake return container_of(priv, struct vc4_ctm_state, base); 38766cc6b1SStefan Schake } 39766cc6b1SStefan Schake 40f2df84e0SMaxime Ripard struct vc4_hvs_state { 41f2df84e0SMaxime Ripard struct drm_private_state base; 429ec03d7fSMaxime Ripard 439ec03d7fSMaxime Ripard struct { 449ec03d7fSMaxime Ripard unsigned in_use: 1; 459ec03d7fSMaxime Ripard struct drm_crtc_commit *pending_commit; 469ec03d7fSMaxime Ripard } fifo_state[HVS_NUM_CHANNELS]; 47f2df84e0SMaxime Ripard }; 48f2df84e0SMaxime Ripard 49f2df84e0SMaxime Ripard static struct vc4_hvs_state * 50f2df84e0SMaxime Ripard to_vc4_hvs_state(struct drm_private_state *priv) 51f2df84e0SMaxime Ripard { 52f2df84e0SMaxime Ripard return container_of(priv, struct vc4_hvs_state, base); 53f2df84e0SMaxime Ripard } 54f2df84e0SMaxime Ripard 554686da83SBoris Brezillon struct vc4_load_tracker_state { 564686da83SBoris Brezillon struct drm_private_state base; 574686da83SBoris Brezillon u64 hvs_load; 584686da83SBoris Brezillon u64 membus_load; 594686da83SBoris Brezillon }; 604686da83SBoris Brezillon 614686da83SBoris Brezillon static struct vc4_load_tracker_state * 624686da83SBoris Brezillon to_vc4_load_tracker_state(struct drm_private_state *priv) 634686da83SBoris Brezillon { 644686da83SBoris Brezillon return container_of(priv, struct vc4_load_tracker_state, base); 654686da83SBoris Brezillon } 664686da83SBoris Brezillon 67766cc6b1SStefan Schake static struct vc4_ctm_state *vc4_get_ctm_state(struct drm_atomic_state *state, 68766cc6b1SStefan Schake struct drm_private_obj *manager) 69766cc6b1SStefan Schake { 70766cc6b1SStefan Schake struct drm_device *dev = state->dev; 7188e08589SMaxime Ripard struct vc4_dev *vc4 = to_vc4_dev(dev); 72766cc6b1SStefan Schake struct drm_private_state *priv_state; 73766cc6b1SStefan Schake int ret; 74766cc6b1SStefan Schake 75766cc6b1SStefan Schake ret = drm_modeset_lock(&vc4->ctm_state_lock, state->acquire_ctx); 76766cc6b1SStefan Schake if (ret) 77766cc6b1SStefan Schake return ERR_PTR(ret); 78766cc6b1SStefan Schake 79766cc6b1SStefan Schake priv_state = drm_atomic_get_private_obj_state(state, manager); 80766cc6b1SStefan Schake if (IS_ERR(priv_state)) 81766cc6b1SStefan Schake return ERR_CAST(priv_state); 82766cc6b1SStefan Schake 83766cc6b1SStefan Schake return to_vc4_ctm_state(priv_state); 84766cc6b1SStefan Schake } 85766cc6b1SStefan Schake 86766cc6b1SStefan Schake static struct drm_private_state * 87766cc6b1SStefan Schake vc4_ctm_duplicate_state(struct drm_private_obj *obj) 88766cc6b1SStefan Schake { 89766cc6b1SStefan Schake struct vc4_ctm_state *state; 90766cc6b1SStefan Schake 91766cc6b1SStefan Schake state = kmemdup(obj->state, sizeof(*state), GFP_KERNEL); 92766cc6b1SStefan Schake if (!state) 93766cc6b1SStefan Schake return NULL; 94766cc6b1SStefan Schake 95766cc6b1SStefan Schake __drm_atomic_helper_private_obj_duplicate_state(obj, &state->base); 96766cc6b1SStefan Schake 97766cc6b1SStefan Schake return &state->base; 98766cc6b1SStefan Schake } 99766cc6b1SStefan Schake 100766cc6b1SStefan Schake static void vc4_ctm_destroy_state(struct drm_private_obj *obj, 101766cc6b1SStefan Schake struct drm_private_state *state) 102766cc6b1SStefan Schake { 103766cc6b1SStefan Schake struct vc4_ctm_state *ctm_state = to_vc4_ctm_state(state); 104766cc6b1SStefan Schake 105766cc6b1SStefan Schake kfree(ctm_state); 106766cc6b1SStefan Schake } 107766cc6b1SStefan Schake 108766cc6b1SStefan Schake static const struct drm_private_state_funcs vc4_ctm_state_funcs = { 109766cc6b1SStefan Schake .atomic_duplicate_state = vc4_ctm_duplicate_state, 110766cc6b1SStefan Schake .atomic_destroy_state = vc4_ctm_destroy_state, 111766cc6b1SStefan Schake }; 112766cc6b1SStefan Schake 113dcda7c28SMaxime Ripard static void vc4_ctm_obj_fini(struct drm_device *dev, void *unused) 114dcda7c28SMaxime Ripard { 115dcda7c28SMaxime Ripard struct vc4_dev *vc4 = to_vc4_dev(dev); 116dcda7c28SMaxime Ripard 117dcda7c28SMaxime Ripard drm_atomic_private_obj_fini(&vc4->ctm_manager); 118dcda7c28SMaxime Ripard } 119dcda7c28SMaxime Ripard 120dcda7c28SMaxime Ripard static int vc4_ctm_obj_init(struct vc4_dev *vc4) 121dcda7c28SMaxime Ripard { 122dcda7c28SMaxime Ripard struct vc4_ctm_state *ctm_state; 123dcda7c28SMaxime Ripard 124dcda7c28SMaxime Ripard drm_modeset_lock_init(&vc4->ctm_state_lock); 125dcda7c28SMaxime Ripard 126dcda7c28SMaxime Ripard ctm_state = kzalloc(sizeof(*ctm_state), GFP_KERNEL); 127dcda7c28SMaxime Ripard if (!ctm_state) 128dcda7c28SMaxime Ripard return -ENOMEM; 129dcda7c28SMaxime Ripard 130dcda7c28SMaxime Ripard drm_atomic_private_obj_init(&vc4->base, &vc4->ctm_manager, &ctm_state->base, 131dcda7c28SMaxime Ripard &vc4_ctm_state_funcs); 132dcda7c28SMaxime Ripard 1333c354ed1SMaxime Ripard return drmm_add_action_or_reset(&vc4->base, vc4_ctm_obj_fini, NULL); 134dcda7c28SMaxime Ripard } 135dcda7c28SMaxime Ripard 136766cc6b1SStefan Schake /* Converts a DRM S31.32 value to the HW S0.9 format. */ 137766cc6b1SStefan Schake static u16 vc4_ctm_s31_32_to_s0_9(u64 in) 138766cc6b1SStefan Schake { 139766cc6b1SStefan Schake u16 r; 140766cc6b1SStefan Schake 141766cc6b1SStefan Schake /* Sign bit. */ 142766cc6b1SStefan Schake r = in & BIT_ULL(63) ? BIT(9) : 0; 143766cc6b1SStefan Schake 144766cc6b1SStefan Schake if ((in & GENMASK_ULL(62, 32)) > 0) { 145766cc6b1SStefan Schake /* We have zero integer bits so we can only saturate here. */ 146766cc6b1SStefan Schake r |= GENMASK(8, 0); 147766cc6b1SStefan Schake } else { 148766cc6b1SStefan Schake /* Otherwise take the 9 most important fractional bits. */ 149766cc6b1SStefan Schake r |= (in >> 23) & GENMASK(8, 0); 150766cc6b1SStefan Schake } 151766cc6b1SStefan Schake 152766cc6b1SStefan Schake return r; 153766cc6b1SStefan Schake } 154766cc6b1SStefan Schake 155766cc6b1SStefan Schake static void 156766cc6b1SStefan Schake vc4_ctm_commit(struct vc4_dev *vc4, struct drm_atomic_state *state) 157766cc6b1SStefan Schake { 158766cc6b1SStefan Schake struct vc4_ctm_state *ctm_state = to_vc4_ctm_state(vc4->ctm_manager.state); 159766cc6b1SStefan Schake struct drm_color_ctm *ctm = ctm_state->ctm; 160766cc6b1SStefan Schake 161766cc6b1SStefan Schake if (ctm_state->fifo) { 162766cc6b1SStefan Schake HVS_WRITE(SCALER_OLEDCOEF2, 163766cc6b1SStefan Schake VC4_SET_FIELD(vc4_ctm_s31_32_to_s0_9(ctm->matrix[0]), 164766cc6b1SStefan Schake SCALER_OLEDCOEF2_R_TO_R) | 165766cc6b1SStefan Schake VC4_SET_FIELD(vc4_ctm_s31_32_to_s0_9(ctm->matrix[3]), 166766cc6b1SStefan Schake SCALER_OLEDCOEF2_R_TO_G) | 167766cc6b1SStefan Schake VC4_SET_FIELD(vc4_ctm_s31_32_to_s0_9(ctm->matrix[6]), 168766cc6b1SStefan Schake SCALER_OLEDCOEF2_R_TO_B)); 169766cc6b1SStefan Schake HVS_WRITE(SCALER_OLEDCOEF1, 170766cc6b1SStefan Schake VC4_SET_FIELD(vc4_ctm_s31_32_to_s0_9(ctm->matrix[1]), 171766cc6b1SStefan Schake SCALER_OLEDCOEF1_G_TO_R) | 172766cc6b1SStefan Schake VC4_SET_FIELD(vc4_ctm_s31_32_to_s0_9(ctm->matrix[4]), 173766cc6b1SStefan Schake SCALER_OLEDCOEF1_G_TO_G) | 174766cc6b1SStefan Schake VC4_SET_FIELD(vc4_ctm_s31_32_to_s0_9(ctm->matrix[7]), 175766cc6b1SStefan Schake SCALER_OLEDCOEF1_G_TO_B)); 176766cc6b1SStefan Schake HVS_WRITE(SCALER_OLEDCOEF0, 177766cc6b1SStefan Schake VC4_SET_FIELD(vc4_ctm_s31_32_to_s0_9(ctm->matrix[2]), 178766cc6b1SStefan Schake SCALER_OLEDCOEF0_B_TO_R) | 179766cc6b1SStefan Schake VC4_SET_FIELD(vc4_ctm_s31_32_to_s0_9(ctm->matrix[5]), 180766cc6b1SStefan Schake SCALER_OLEDCOEF0_B_TO_G) | 181766cc6b1SStefan Schake VC4_SET_FIELD(vc4_ctm_s31_32_to_s0_9(ctm->matrix[8]), 182766cc6b1SStefan Schake SCALER_OLEDCOEF0_B_TO_B)); 183766cc6b1SStefan Schake } 184766cc6b1SStefan Schake 185766cc6b1SStefan Schake HVS_WRITE(SCALER_OLEDOFFS, 186766cc6b1SStefan Schake VC4_SET_FIELD(ctm_state->fifo, SCALER_OLEDOFFS_DISPFIFO)); 187766cc6b1SStefan Schake } 188c8b75bcaSEric Anholt 189f2df84e0SMaxime Ripard static struct vc4_hvs_state * 1909ec03d7fSMaxime Ripard vc4_hvs_get_new_global_state(struct drm_atomic_state *state) 1919ec03d7fSMaxime Ripard { 1929ec03d7fSMaxime Ripard struct vc4_dev *vc4 = to_vc4_dev(state->dev); 1939ec03d7fSMaxime Ripard struct drm_private_state *priv_state; 1949ec03d7fSMaxime Ripard 1959ec03d7fSMaxime Ripard priv_state = drm_atomic_get_new_private_obj_state(state, &vc4->hvs_channels); 1969ec03d7fSMaxime Ripard if (IS_ERR(priv_state)) 1979ec03d7fSMaxime Ripard return ERR_CAST(priv_state); 1989ec03d7fSMaxime Ripard 1999ec03d7fSMaxime Ripard return to_vc4_hvs_state(priv_state); 2009ec03d7fSMaxime Ripard } 2019ec03d7fSMaxime Ripard 2029ec03d7fSMaxime Ripard static struct vc4_hvs_state * 2039ec03d7fSMaxime Ripard vc4_hvs_get_old_global_state(struct drm_atomic_state *state) 2049ec03d7fSMaxime Ripard { 2059ec03d7fSMaxime Ripard struct vc4_dev *vc4 = to_vc4_dev(state->dev); 2069ec03d7fSMaxime Ripard struct drm_private_state *priv_state; 2079ec03d7fSMaxime Ripard 2089ec03d7fSMaxime Ripard priv_state = drm_atomic_get_old_private_obj_state(state, &vc4->hvs_channels); 2099ec03d7fSMaxime Ripard if (IS_ERR(priv_state)) 2109ec03d7fSMaxime Ripard return ERR_CAST(priv_state); 2119ec03d7fSMaxime Ripard 2129ec03d7fSMaxime Ripard return to_vc4_hvs_state(priv_state); 2139ec03d7fSMaxime Ripard } 2149ec03d7fSMaxime Ripard 2159ec03d7fSMaxime Ripard static struct vc4_hvs_state * 216f2df84e0SMaxime Ripard vc4_hvs_get_global_state(struct drm_atomic_state *state) 217f2df84e0SMaxime Ripard { 218f2df84e0SMaxime Ripard struct vc4_dev *vc4 = to_vc4_dev(state->dev); 219f2df84e0SMaxime Ripard struct drm_private_state *priv_state; 220f2df84e0SMaxime Ripard 221f2df84e0SMaxime Ripard priv_state = drm_atomic_get_private_obj_state(state, &vc4->hvs_channels); 222f2df84e0SMaxime Ripard if (IS_ERR(priv_state)) 223f2df84e0SMaxime Ripard return ERR_CAST(priv_state); 224f2df84e0SMaxime Ripard 225f2df84e0SMaxime Ripard return to_vc4_hvs_state(priv_state); 226f2df84e0SMaxime Ripard } 227f2df84e0SMaxime Ripard 22887ebcd42SMaxime Ripard static void vc4_hvs_pv_muxing_commit(struct vc4_dev *vc4, 22987ebcd42SMaxime Ripard struct drm_atomic_state *state) 23087ebcd42SMaxime Ripard { 23187ebcd42SMaxime Ripard struct drm_crtc_state *crtc_state; 23287ebcd42SMaxime Ripard struct drm_crtc *crtc; 23387ebcd42SMaxime Ripard unsigned int i; 23487ebcd42SMaxime Ripard 23587ebcd42SMaxime Ripard for_each_new_crtc_in_state(state, crtc, crtc_state, i) { 23687ebcd42SMaxime Ripard struct vc4_crtc_state *vc4_state = to_vc4_crtc_state(crtc_state); 23787ebcd42SMaxime Ripard u32 dispctrl; 23887ebcd42SMaxime Ripard u32 dsp3_mux; 23987ebcd42SMaxime Ripard 24087ebcd42SMaxime Ripard if (!crtc_state->active) 24187ebcd42SMaxime Ripard continue; 24287ebcd42SMaxime Ripard 24387ebcd42SMaxime Ripard if (vc4_state->assigned_channel != 2) 24487ebcd42SMaxime Ripard continue; 24587ebcd42SMaxime Ripard 24687ebcd42SMaxime Ripard /* 24787ebcd42SMaxime Ripard * SCALER_DISPCTRL_DSP3 = X, where X < 2 means 'connect DSP3 to 24887ebcd42SMaxime Ripard * FIFO X'. 24987ebcd42SMaxime Ripard * SCALER_DISPCTRL_DSP3 = 3 means 'disable DSP 3'. 25087ebcd42SMaxime Ripard * 25187ebcd42SMaxime Ripard * DSP3 is connected to FIFO2 unless the transposer is 25287ebcd42SMaxime Ripard * enabled. In this case, FIFO 2 is directly accessed by the 25387ebcd42SMaxime Ripard * TXP IP, and we need to disable the FIFO2 -> pixelvalve1 25487ebcd42SMaxime Ripard * route. 25587ebcd42SMaxime Ripard */ 25687ebcd42SMaxime Ripard if (vc4_state->feed_txp) 25787ebcd42SMaxime Ripard dsp3_mux = VC4_SET_FIELD(3, SCALER_DISPCTRL_DSP3_MUX); 25887ebcd42SMaxime Ripard else 25987ebcd42SMaxime Ripard dsp3_mux = VC4_SET_FIELD(2, SCALER_DISPCTRL_DSP3_MUX); 26087ebcd42SMaxime Ripard 26187ebcd42SMaxime Ripard dispctrl = HVS_READ(SCALER_DISPCTRL) & 26287ebcd42SMaxime Ripard ~SCALER_DISPCTRL_DSP3_MUX_MASK; 26387ebcd42SMaxime Ripard HVS_WRITE(SCALER_DISPCTRL, dispctrl | dsp3_mux); 26487ebcd42SMaxime Ripard } 26587ebcd42SMaxime Ripard } 26687ebcd42SMaxime Ripard 26787ebcd42SMaxime Ripard static void vc5_hvs_pv_muxing_commit(struct vc4_dev *vc4, 26887ebcd42SMaxime Ripard struct drm_atomic_state *state) 26987ebcd42SMaxime Ripard { 27087ebcd42SMaxime Ripard struct drm_crtc_state *crtc_state; 27187ebcd42SMaxime Ripard struct drm_crtc *crtc; 2722820526dSMaxime Ripard unsigned char mux; 27387ebcd42SMaxime Ripard unsigned int i; 27487ebcd42SMaxime Ripard u32 reg; 27587ebcd42SMaxime Ripard 27687ebcd42SMaxime Ripard for_each_new_crtc_in_state(state, crtc, crtc_state, i) { 27787ebcd42SMaxime Ripard struct vc4_crtc_state *vc4_state = to_vc4_crtc_state(crtc_state); 27887ebcd42SMaxime Ripard struct vc4_crtc *vc4_crtc = to_vc4_crtc(crtc); 27987ebcd42SMaxime Ripard 2802820526dSMaxime Ripard if (!vc4_state->update_muxing) 28187ebcd42SMaxime Ripard continue; 28287ebcd42SMaxime Ripard 28387ebcd42SMaxime Ripard switch (vc4_crtc->data->hvs_output) { 28487ebcd42SMaxime Ripard case 2: 2852820526dSMaxime Ripard mux = (vc4_state->assigned_channel == 2) ? 0 : 1; 2862820526dSMaxime Ripard reg = HVS_READ(SCALER_DISPECTRL); 2872820526dSMaxime Ripard HVS_WRITE(SCALER_DISPECTRL, 2882820526dSMaxime Ripard (reg & ~SCALER_DISPECTRL_DSP2_MUX_MASK) | 2892820526dSMaxime Ripard VC4_SET_FIELD(mux, SCALER_DISPECTRL_DSP2_MUX)); 29087ebcd42SMaxime Ripard break; 29187ebcd42SMaxime Ripard 29287ebcd42SMaxime Ripard case 3: 2932820526dSMaxime Ripard if (vc4_state->assigned_channel == VC4_HVS_CHANNEL_DISABLED) 2942820526dSMaxime Ripard mux = 3; 2952820526dSMaxime Ripard else 2962820526dSMaxime Ripard mux = vc4_state->assigned_channel; 2972820526dSMaxime Ripard 2982820526dSMaxime Ripard reg = HVS_READ(SCALER_DISPCTRL); 2992820526dSMaxime Ripard HVS_WRITE(SCALER_DISPCTRL, 3002820526dSMaxime Ripard (reg & ~SCALER_DISPCTRL_DSP3_MUX_MASK) | 3012820526dSMaxime Ripard VC4_SET_FIELD(mux, SCALER_DISPCTRL_DSP3_MUX)); 30287ebcd42SMaxime Ripard break; 30387ebcd42SMaxime Ripard 30487ebcd42SMaxime Ripard case 4: 3052820526dSMaxime Ripard if (vc4_state->assigned_channel == VC4_HVS_CHANNEL_DISABLED) 3062820526dSMaxime Ripard mux = 3; 3072820526dSMaxime Ripard else 3082820526dSMaxime Ripard mux = vc4_state->assigned_channel; 3092820526dSMaxime Ripard 3102820526dSMaxime Ripard reg = HVS_READ(SCALER_DISPEOLN); 3112820526dSMaxime Ripard HVS_WRITE(SCALER_DISPEOLN, 3122820526dSMaxime Ripard (reg & ~SCALER_DISPEOLN_DSP4_MUX_MASK) | 3132820526dSMaxime Ripard VC4_SET_FIELD(mux, SCALER_DISPEOLN_DSP4_MUX)); 3142820526dSMaxime Ripard 31587ebcd42SMaxime Ripard break; 31687ebcd42SMaxime Ripard 31787ebcd42SMaxime Ripard case 5: 3182820526dSMaxime Ripard if (vc4_state->assigned_channel == VC4_HVS_CHANNEL_DISABLED) 3192820526dSMaxime Ripard mux = 3; 3202820526dSMaxime Ripard else 3212820526dSMaxime Ripard mux = vc4_state->assigned_channel; 3222820526dSMaxime Ripard 3232820526dSMaxime Ripard reg = HVS_READ(SCALER_DISPDITHER); 3242820526dSMaxime Ripard HVS_WRITE(SCALER_DISPDITHER, 3252820526dSMaxime Ripard (reg & ~SCALER_DISPDITHER_DSP5_MUX_MASK) | 3262820526dSMaxime Ripard VC4_SET_FIELD(mux, SCALER_DISPDITHER_DSP5_MUX)); 32787ebcd42SMaxime Ripard break; 32887ebcd42SMaxime Ripard 32987ebcd42SMaxime Ripard default: 33087ebcd42SMaxime Ripard break; 33187ebcd42SMaxime Ripard } 33287ebcd42SMaxime Ripard } 33387ebcd42SMaxime Ripard } 33487ebcd42SMaxime Ripard 335f3c420feSMaxime Ripard static void vc4_atomic_commit_tail(struct drm_atomic_state *state) 336b501baccSEric Anholt { 337b501baccSEric Anholt struct drm_device *dev = state->dev; 338b501baccSEric Anholt struct vc4_dev *vc4 = to_vc4_dev(dev); 339d7d96c00SMaxime Ripard struct vc4_hvs *hvs = vc4->hvs; 3409ec03d7fSMaxime Ripard struct drm_crtc_state *old_crtc_state; 34159635667SMaxime Ripard struct drm_crtc_state *new_crtc_state; 34259635667SMaxime Ripard struct drm_crtc *crtc; 3439ec03d7fSMaxime Ripard struct vc4_hvs_state *old_hvs_state; 344531a1b62SBoris Brezillon int i; 345531a1b62SBoris Brezillon 34659635667SMaxime Ripard for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) { 34787ebcd42SMaxime Ripard struct vc4_crtc_state *vc4_crtc_state; 34859635667SMaxime Ripard 34959635667SMaxime Ripard if (!new_crtc_state->commit) 350531a1b62SBoris Brezillon continue; 351531a1b62SBoris Brezillon 35287ebcd42SMaxime Ripard vc4_crtc_state = to_vc4_crtc_state(new_crtc_state); 35387ebcd42SMaxime Ripard vc4_hvs_mask_underrun(dev, vc4_crtc_state->assigned_channel); 354531a1b62SBoris Brezillon } 355b501baccSEric Anholt 3569ec03d7fSMaxime Ripard old_hvs_state = vc4_hvs_get_old_global_state(state); 357f9277679SMaxime Ripard if (IS_ERR(old_hvs_state)) 3589ec03d7fSMaxime Ripard return; 3599ec03d7fSMaxime Ripard 3609ec03d7fSMaxime Ripard for_each_old_crtc_in_state(state, crtc, old_crtc_state, i) { 3619ec03d7fSMaxime Ripard struct vc4_crtc_state *vc4_crtc_state = 3629ec03d7fSMaxime Ripard to_vc4_crtc_state(old_crtc_state); 3639ec03d7fSMaxime Ripard unsigned int channel = vc4_crtc_state->assigned_channel; 364*049cfff8SMaxime Ripard struct drm_crtc_commit *commit; 365b99c2c95SMaxime Ripard int ret; 3669ec03d7fSMaxime Ripard 3679ec03d7fSMaxime Ripard if (channel == VC4_HVS_CHANNEL_DISABLED) 3689ec03d7fSMaxime Ripard continue; 3699ec03d7fSMaxime Ripard 3709ec03d7fSMaxime Ripard if (!old_hvs_state->fifo_state[channel].in_use) 3719ec03d7fSMaxime Ripard continue; 3729ec03d7fSMaxime Ripard 373*049cfff8SMaxime Ripard commit = old_hvs_state->fifo_state[channel].pending_commit; 374*049cfff8SMaxime Ripard if (!commit) 375*049cfff8SMaxime Ripard continue; 376*049cfff8SMaxime Ripard 377*049cfff8SMaxime Ripard ret = drm_crtc_commit_wait(commit); 378b99c2c95SMaxime Ripard if (ret) 379b99c2c95SMaxime Ripard drm_err(dev, "Timed out waiting for commit\n"); 380*049cfff8SMaxime Ripard 381*049cfff8SMaxime Ripard drm_crtc_commit_put(commit); 3829ec03d7fSMaxime Ripard } 3839ec03d7fSMaxime Ripard 3840c980a00SMaxime Ripard if (vc4->hvs->hvs5) 3850c980a00SMaxime Ripard clk_set_min_rate(hvs->core_clk, 500000000); 3860c980a00SMaxime Ripard 387b501baccSEric Anholt drm_atomic_helper_commit_modeset_disables(dev, state); 388b501baccSEric Anholt 389766cc6b1SStefan Schake vc4_ctm_commit(vc4, state); 390766cc6b1SStefan Schake 39187ebcd42SMaxime Ripard if (vc4->hvs->hvs5) 39287ebcd42SMaxime Ripard vc5_hvs_pv_muxing_commit(vc4, state); 39387ebcd42SMaxime Ripard else 39487ebcd42SMaxime Ripard vc4_hvs_pv_muxing_commit(vc4, state); 39587ebcd42SMaxime Ripard 3962b58e98dSLiu Ying drm_atomic_helper_commit_planes(dev, state, 0); 397b501baccSEric Anholt 398b501baccSEric Anholt drm_atomic_helper_commit_modeset_enables(dev, state); 399b501baccSEric Anholt 4001ebe99a7SBoris Brezillon drm_atomic_helper_fake_vblank(state); 4011ebe99a7SBoris Brezillon 40234c8ea40SBoris Brezillon drm_atomic_helper_commit_hw_done(state); 40334c8ea40SBoris Brezillon 404184d3cf4SBoris Brezillon drm_atomic_helper_wait_for_flip_done(dev, state); 405b501baccSEric Anholt 406b501baccSEric Anholt drm_atomic_helper_cleanup_planes(dev, state); 407b501baccSEric Anholt 408d7d96c00SMaxime Ripard if (vc4->hvs->hvs5) 409d7d96c00SMaxime Ripard clk_set_min_rate(hvs->core_clk, 0); 410b501baccSEric Anholt } 411b501baccSEric Anholt 4129ec03d7fSMaxime Ripard static int vc4_atomic_commit_setup(struct drm_atomic_state *state) 4139ec03d7fSMaxime Ripard { 4149ec03d7fSMaxime Ripard struct drm_crtc_state *crtc_state; 4159ec03d7fSMaxime Ripard struct vc4_hvs_state *hvs_state; 4169ec03d7fSMaxime Ripard struct drm_crtc *crtc; 4179ec03d7fSMaxime Ripard unsigned int i; 4189ec03d7fSMaxime Ripard 4199ec03d7fSMaxime Ripard hvs_state = vc4_hvs_get_new_global_state(state); 420f9277679SMaxime Ripard if (WARN_ON(IS_ERR(hvs_state))) 421f9277679SMaxime Ripard return PTR_ERR(hvs_state); 4229ec03d7fSMaxime Ripard 4239ec03d7fSMaxime Ripard for_each_new_crtc_in_state(state, crtc, crtc_state, i) { 4249ec03d7fSMaxime Ripard struct vc4_crtc_state *vc4_crtc_state = 4259ec03d7fSMaxime Ripard to_vc4_crtc_state(crtc_state); 4269ec03d7fSMaxime Ripard unsigned int channel = 4279ec03d7fSMaxime Ripard vc4_crtc_state->assigned_channel; 4289ec03d7fSMaxime Ripard 4299ec03d7fSMaxime Ripard if (channel == VC4_HVS_CHANNEL_DISABLED) 4309ec03d7fSMaxime Ripard continue; 4319ec03d7fSMaxime Ripard 4329ec03d7fSMaxime Ripard if (!hvs_state->fifo_state[channel].in_use) 4339ec03d7fSMaxime Ripard continue; 4349ec03d7fSMaxime Ripard 4359ec03d7fSMaxime Ripard hvs_state->fifo_state[channel].pending_commit = 4369ec03d7fSMaxime Ripard drm_crtc_commit_get(crtc_state->commit); 4379ec03d7fSMaxime Ripard } 4389ec03d7fSMaxime Ripard 4399ec03d7fSMaxime Ripard return 0; 4409ec03d7fSMaxime Ripard } 4419ec03d7fSMaxime Ripard 44283753117SEric Anholt static struct drm_framebuffer *vc4_fb_create(struct drm_device *dev, 44383753117SEric Anholt struct drm_file *file_priv, 44483753117SEric Anholt const struct drm_mode_fb_cmd2 *mode_cmd) 44583753117SEric Anholt { 44683753117SEric Anholt struct drm_mode_fb_cmd2 mode_cmd_local; 44783753117SEric Anholt 44883753117SEric Anholt /* If the user didn't specify a modifier, use the 44983753117SEric Anholt * vc4_set_tiling_ioctl() state for the BO. 45083753117SEric Anholt */ 45183753117SEric Anholt if (!(mode_cmd->flags & DRM_MODE_FB_MODIFIERS)) { 45283753117SEric Anholt struct drm_gem_object *gem_obj; 45383753117SEric Anholt struct vc4_bo *bo; 45483753117SEric Anholt 45583753117SEric Anholt gem_obj = drm_gem_object_lookup(file_priv, 45683753117SEric Anholt mode_cmd->handles[0]); 45783753117SEric Anholt if (!gem_obj) { 458fb95992aSEric Anholt DRM_DEBUG("Failed to look up GEM BO %d\n", 45983753117SEric Anholt mode_cmd->handles[0]); 46083753117SEric Anholt return ERR_PTR(-ENOENT); 46183753117SEric Anholt } 46283753117SEric Anholt bo = to_vc4_bo(gem_obj); 46383753117SEric Anholt 46483753117SEric Anholt mode_cmd_local = *mode_cmd; 46583753117SEric Anholt 46683753117SEric Anholt if (bo->t_format) { 46783753117SEric Anholt mode_cmd_local.modifier[0] = 46883753117SEric Anholt DRM_FORMAT_MOD_BROADCOM_VC4_T_TILED; 46983753117SEric Anholt } else { 47083753117SEric Anholt mode_cmd_local.modifier[0] = DRM_FORMAT_MOD_NONE; 47183753117SEric Anholt } 47283753117SEric Anholt 473f7a8cd30SEmil Velikov drm_gem_object_put(gem_obj); 47483753117SEric Anholt 47583753117SEric Anholt mode_cmd = &mode_cmd_local; 47683753117SEric Anholt } 47783753117SEric Anholt 4789762477cSNoralf Trønnes return drm_gem_fb_create(dev, file_priv, mode_cmd); 47983753117SEric Anholt } 48083753117SEric Anholt 481766cc6b1SStefan Schake /* Our CTM has some peculiar limitations: we can only enable it for one CRTC 482766cc6b1SStefan Schake * at a time and the HW only supports S0.9 scalars. To account for the latter, 483766cc6b1SStefan Schake * we don't allow userland to set a CTM that we have no hope of approximating. 484766cc6b1SStefan Schake */ 485766cc6b1SStefan Schake static int 486766cc6b1SStefan Schake vc4_ctm_atomic_check(struct drm_device *dev, struct drm_atomic_state *state) 487766cc6b1SStefan Schake { 488766cc6b1SStefan Schake struct vc4_dev *vc4 = to_vc4_dev(dev); 489766cc6b1SStefan Schake struct vc4_ctm_state *ctm_state = NULL; 490766cc6b1SStefan Schake struct drm_crtc *crtc; 491766cc6b1SStefan Schake struct drm_crtc_state *old_crtc_state, *new_crtc_state; 492766cc6b1SStefan Schake struct drm_color_ctm *ctm; 493766cc6b1SStefan Schake int i; 494766cc6b1SStefan Schake 495766cc6b1SStefan Schake for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) { 496766cc6b1SStefan Schake /* CTM is being disabled. */ 497766cc6b1SStefan Schake if (!new_crtc_state->ctm && old_crtc_state->ctm) { 498766cc6b1SStefan Schake ctm_state = vc4_get_ctm_state(state, &vc4->ctm_manager); 499766cc6b1SStefan Schake if (IS_ERR(ctm_state)) 500766cc6b1SStefan Schake return PTR_ERR(ctm_state); 501766cc6b1SStefan Schake ctm_state->fifo = 0; 502766cc6b1SStefan Schake } 503766cc6b1SStefan Schake } 504766cc6b1SStefan Schake 505766cc6b1SStefan Schake for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) { 506766cc6b1SStefan Schake if (new_crtc_state->ctm == old_crtc_state->ctm) 507766cc6b1SStefan Schake continue; 508766cc6b1SStefan Schake 509766cc6b1SStefan Schake if (!ctm_state) { 510766cc6b1SStefan Schake ctm_state = vc4_get_ctm_state(state, &vc4->ctm_manager); 511766cc6b1SStefan Schake if (IS_ERR(ctm_state)) 512766cc6b1SStefan Schake return PTR_ERR(ctm_state); 513766cc6b1SStefan Schake } 514766cc6b1SStefan Schake 515766cc6b1SStefan Schake /* CTM is being enabled or the matrix changed. */ 516766cc6b1SStefan Schake if (new_crtc_state->ctm) { 51787ebcd42SMaxime Ripard struct vc4_crtc_state *vc4_crtc_state = 51887ebcd42SMaxime Ripard to_vc4_crtc_state(new_crtc_state); 51987ebcd42SMaxime Ripard 520766cc6b1SStefan Schake /* fifo is 1-based since 0 disables CTM. */ 52187ebcd42SMaxime Ripard int fifo = vc4_crtc_state->assigned_channel + 1; 522766cc6b1SStefan Schake 523766cc6b1SStefan Schake /* Check userland isn't trying to turn on CTM for more 524766cc6b1SStefan Schake * than one CRTC at a time. 525766cc6b1SStefan Schake */ 526766cc6b1SStefan Schake if (ctm_state->fifo && ctm_state->fifo != fifo) { 527766cc6b1SStefan Schake DRM_DEBUG_DRIVER("Too many CTM configured\n"); 528766cc6b1SStefan Schake return -EINVAL; 529766cc6b1SStefan Schake } 530766cc6b1SStefan Schake 531766cc6b1SStefan Schake /* Check we can approximate the specified CTM. 532766cc6b1SStefan Schake * We disallow scalars |c| > 1.0 since the HW has 533766cc6b1SStefan Schake * no integer bits. 534766cc6b1SStefan Schake */ 535766cc6b1SStefan Schake ctm = new_crtc_state->ctm->data; 536766cc6b1SStefan Schake for (i = 0; i < ARRAY_SIZE(ctm->matrix); i++) { 537766cc6b1SStefan Schake u64 val = ctm->matrix[i]; 538766cc6b1SStefan Schake 539766cc6b1SStefan Schake val &= ~BIT_ULL(63); 540766cc6b1SStefan Schake if (val > BIT_ULL(32)) 541766cc6b1SStefan Schake return -EINVAL; 542766cc6b1SStefan Schake } 543766cc6b1SStefan Schake 544766cc6b1SStefan Schake ctm_state->fifo = fifo; 545766cc6b1SStefan Schake ctm_state->ctm = ctm; 546766cc6b1SStefan Schake } 547766cc6b1SStefan Schake } 548766cc6b1SStefan Schake 549766cc6b1SStefan Schake return 0; 550766cc6b1SStefan Schake } 551766cc6b1SStefan Schake 5524686da83SBoris Brezillon static int vc4_load_tracker_atomic_check(struct drm_atomic_state *state) 5534686da83SBoris Brezillon { 5544686da83SBoris Brezillon struct drm_plane_state *old_plane_state, *new_plane_state; 5554686da83SBoris Brezillon struct vc4_dev *vc4 = to_vc4_dev(state->dev); 5564686da83SBoris Brezillon struct vc4_load_tracker_state *load_state; 5574686da83SBoris Brezillon struct drm_private_state *priv_state; 5584686da83SBoris Brezillon struct drm_plane *plane; 5594686da83SBoris Brezillon int i; 5604686da83SBoris Brezillon 561f437bc1eSMaxime Ripard if (!vc4->load_tracker_available) 562f437bc1eSMaxime Ripard return 0; 563f437bc1eSMaxime Ripard 5644686da83SBoris Brezillon priv_state = drm_atomic_get_private_obj_state(state, 5654686da83SBoris Brezillon &vc4->load_tracker); 5664686da83SBoris Brezillon if (IS_ERR(priv_state)) 5674686da83SBoris Brezillon return PTR_ERR(priv_state); 5684686da83SBoris Brezillon 5694686da83SBoris Brezillon load_state = to_vc4_load_tracker_state(priv_state); 5704686da83SBoris Brezillon for_each_oldnew_plane_in_state(state, plane, old_plane_state, 5714686da83SBoris Brezillon new_plane_state, i) { 5724686da83SBoris Brezillon struct vc4_plane_state *vc4_plane_state; 5734686da83SBoris Brezillon 5744686da83SBoris Brezillon if (old_plane_state->fb && old_plane_state->crtc) { 5754686da83SBoris Brezillon vc4_plane_state = to_vc4_plane_state(old_plane_state); 5764686da83SBoris Brezillon load_state->membus_load -= vc4_plane_state->membus_load; 5774686da83SBoris Brezillon load_state->hvs_load -= vc4_plane_state->hvs_load; 5784686da83SBoris Brezillon } 5794686da83SBoris Brezillon 5804686da83SBoris Brezillon if (new_plane_state->fb && new_plane_state->crtc) { 5814686da83SBoris Brezillon vc4_plane_state = to_vc4_plane_state(new_plane_state); 5824686da83SBoris Brezillon load_state->membus_load += vc4_plane_state->membus_load; 5834686da83SBoris Brezillon load_state->hvs_load += vc4_plane_state->hvs_load; 5844686da83SBoris Brezillon } 5854686da83SBoris Brezillon } 5864686da83SBoris Brezillon 5876b5c029dSPaul Kocialkowski /* Don't check the load when the tracker is disabled. */ 5886b5c029dSPaul Kocialkowski if (!vc4->load_tracker_enabled) 5896b5c029dSPaul Kocialkowski return 0; 5906b5c029dSPaul Kocialkowski 5914686da83SBoris Brezillon /* The absolute limit is 2Gbyte/sec, but let's take a margin to let 5924686da83SBoris Brezillon * the system work when other blocks are accessing the memory. 5934686da83SBoris Brezillon */ 5944686da83SBoris Brezillon if (load_state->membus_load > SZ_1G + SZ_512M) 5954686da83SBoris Brezillon return -ENOSPC; 5964686da83SBoris Brezillon 5974686da83SBoris Brezillon /* HVS clock is supposed to run @ 250Mhz, let's take a margin and 5984686da83SBoris Brezillon * consider the maximum number of cycles is 240M. 5994686da83SBoris Brezillon */ 6004686da83SBoris Brezillon if (load_state->hvs_load > 240000000ULL) 6014686da83SBoris Brezillon return -ENOSPC; 6024686da83SBoris Brezillon 6034686da83SBoris Brezillon return 0; 6044686da83SBoris Brezillon } 6054686da83SBoris Brezillon 6064686da83SBoris Brezillon static struct drm_private_state * 6074686da83SBoris Brezillon vc4_load_tracker_duplicate_state(struct drm_private_obj *obj) 6084686da83SBoris Brezillon { 6094686da83SBoris Brezillon struct vc4_load_tracker_state *state; 6104686da83SBoris Brezillon 6114686da83SBoris Brezillon state = kmemdup(obj->state, sizeof(*state), GFP_KERNEL); 6124686da83SBoris Brezillon if (!state) 6134686da83SBoris Brezillon return NULL; 6144686da83SBoris Brezillon 6154686da83SBoris Brezillon __drm_atomic_helper_private_obj_duplicate_state(obj, &state->base); 6164686da83SBoris Brezillon 6174686da83SBoris Brezillon return &state->base; 6184686da83SBoris Brezillon } 6194686da83SBoris Brezillon 6204686da83SBoris Brezillon static void vc4_load_tracker_destroy_state(struct drm_private_obj *obj, 6214686da83SBoris Brezillon struct drm_private_state *state) 6224686da83SBoris Brezillon { 6234686da83SBoris Brezillon struct vc4_load_tracker_state *load_state; 6244686da83SBoris Brezillon 6254686da83SBoris Brezillon load_state = to_vc4_load_tracker_state(state); 6264686da83SBoris Brezillon kfree(load_state); 6274686da83SBoris Brezillon } 6284686da83SBoris Brezillon 6294686da83SBoris Brezillon static const struct drm_private_state_funcs vc4_load_tracker_state_funcs = { 6304686da83SBoris Brezillon .atomic_duplicate_state = vc4_load_tracker_duplicate_state, 6314686da83SBoris Brezillon .atomic_destroy_state = vc4_load_tracker_destroy_state, 6324686da83SBoris Brezillon }; 6334686da83SBoris Brezillon 634dcda7c28SMaxime Ripard static void vc4_load_tracker_obj_fini(struct drm_device *dev, void *unused) 635dcda7c28SMaxime Ripard { 636dcda7c28SMaxime Ripard struct vc4_dev *vc4 = to_vc4_dev(dev); 637dcda7c28SMaxime Ripard 638dcda7c28SMaxime Ripard if (!vc4->load_tracker_available) 639dcda7c28SMaxime Ripard return; 640dcda7c28SMaxime Ripard 641dcda7c28SMaxime Ripard drm_atomic_private_obj_fini(&vc4->load_tracker); 642dcda7c28SMaxime Ripard } 643dcda7c28SMaxime Ripard 644dcda7c28SMaxime Ripard static int vc4_load_tracker_obj_init(struct vc4_dev *vc4) 645dcda7c28SMaxime Ripard { 646dcda7c28SMaxime Ripard struct vc4_load_tracker_state *load_state; 647dcda7c28SMaxime Ripard 648dcda7c28SMaxime Ripard if (!vc4->load_tracker_available) 649dcda7c28SMaxime Ripard return 0; 650dcda7c28SMaxime Ripard 651dcda7c28SMaxime Ripard load_state = kzalloc(sizeof(*load_state), GFP_KERNEL); 652dcda7c28SMaxime Ripard if (!load_state) 653dcda7c28SMaxime Ripard return -ENOMEM; 654dcda7c28SMaxime Ripard 655dcda7c28SMaxime Ripard drm_atomic_private_obj_init(&vc4->base, &vc4->load_tracker, 656dcda7c28SMaxime Ripard &load_state->base, 657dcda7c28SMaxime Ripard &vc4_load_tracker_state_funcs); 658dcda7c28SMaxime Ripard 6593c354ed1SMaxime Ripard return drmm_add_action_or_reset(&vc4->base, vc4_load_tracker_obj_fini, NULL); 660dcda7c28SMaxime Ripard } 661dcda7c28SMaxime Ripard 662f2df84e0SMaxime Ripard static struct drm_private_state * 663f2df84e0SMaxime Ripard vc4_hvs_channels_duplicate_state(struct drm_private_obj *obj) 664f2df84e0SMaxime Ripard { 665f2df84e0SMaxime Ripard struct vc4_hvs_state *old_state = to_vc4_hvs_state(obj->state); 666f2df84e0SMaxime Ripard struct vc4_hvs_state *state; 6679ec03d7fSMaxime Ripard unsigned int i; 668f2df84e0SMaxime Ripard 669f2df84e0SMaxime Ripard state = kzalloc(sizeof(*state), GFP_KERNEL); 670f2df84e0SMaxime Ripard if (!state) 671f2df84e0SMaxime Ripard return NULL; 672f2df84e0SMaxime Ripard 673f2df84e0SMaxime Ripard __drm_atomic_helper_private_obj_duplicate_state(obj, &state->base); 674f2df84e0SMaxime Ripard 675f2df84e0SMaxime Ripard 6769ec03d7fSMaxime Ripard for (i = 0; i < HVS_NUM_CHANNELS; i++) { 6779ec03d7fSMaxime Ripard state->fifo_state[i].in_use = old_state->fifo_state[i].in_use; 6789ec03d7fSMaxime Ripard 6799ec03d7fSMaxime Ripard if (!old_state->fifo_state[i].pending_commit) 6809ec03d7fSMaxime Ripard continue; 6819ec03d7fSMaxime Ripard 6829ec03d7fSMaxime Ripard state->fifo_state[i].pending_commit = 6839ec03d7fSMaxime Ripard drm_crtc_commit_get(old_state->fifo_state[i].pending_commit); 6849ec03d7fSMaxime Ripard } 6859ec03d7fSMaxime Ripard 686f2df84e0SMaxime Ripard return &state->base; 687f2df84e0SMaxime Ripard } 688f2df84e0SMaxime Ripard 689f2df84e0SMaxime Ripard static void vc4_hvs_channels_destroy_state(struct drm_private_obj *obj, 690f2df84e0SMaxime Ripard struct drm_private_state *state) 691f2df84e0SMaxime Ripard { 692f2df84e0SMaxime Ripard struct vc4_hvs_state *hvs_state = to_vc4_hvs_state(state); 6939ec03d7fSMaxime Ripard unsigned int i; 6949ec03d7fSMaxime Ripard 6959ec03d7fSMaxime Ripard for (i = 0; i < HVS_NUM_CHANNELS; i++) { 6969ec03d7fSMaxime Ripard if (!hvs_state->fifo_state[i].pending_commit) 6979ec03d7fSMaxime Ripard continue; 6989ec03d7fSMaxime Ripard 6999ec03d7fSMaxime Ripard drm_crtc_commit_put(hvs_state->fifo_state[i].pending_commit); 7009ec03d7fSMaxime Ripard } 701f2df84e0SMaxime Ripard 702f2df84e0SMaxime Ripard kfree(hvs_state); 703f2df84e0SMaxime Ripard } 704f2df84e0SMaxime Ripard 705f2df84e0SMaxime Ripard static const struct drm_private_state_funcs vc4_hvs_state_funcs = { 706f2df84e0SMaxime Ripard .atomic_duplicate_state = vc4_hvs_channels_duplicate_state, 707f2df84e0SMaxime Ripard .atomic_destroy_state = vc4_hvs_channels_destroy_state, 708f2df84e0SMaxime Ripard }; 709f2df84e0SMaxime Ripard 710f2df84e0SMaxime Ripard static void vc4_hvs_channels_obj_fini(struct drm_device *dev, void *unused) 711f2df84e0SMaxime Ripard { 712f2df84e0SMaxime Ripard struct vc4_dev *vc4 = to_vc4_dev(dev); 713f2df84e0SMaxime Ripard 714f2df84e0SMaxime Ripard drm_atomic_private_obj_fini(&vc4->hvs_channels); 715f2df84e0SMaxime Ripard } 716f2df84e0SMaxime Ripard 717f2df84e0SMaxime Ripard static int vc4_hvs_channels_obj_init(struct vc4_dev *vc4) 718f2df84e0SMaxime Ripard { 719f2df84e0SMaxime Ripard struct vc4_hvs_state *state; 720f2df84e0SMaxime Ripard 721f2df84e0SMaxime Ripard state = kzalloc(sizeof(*state), GFP_KERNEL); 722f2df84e0SMaxime Ripard if (!state) 723f2df84e0SMaxime Ripard return -ENOMEM; 724f2df84e0SMaxime Ripard 725f2df84e0SMaxime Ripard drm_atomic_private_obj_init(&vc4->base, &vc4->hvs_channels, 726f2df84e0SMaxime Ripard &state->base, 727f2df84e0SMaxime Ripard &vc4_hvs_state_funcs); 728f2df84e0SMaxime Ripard 729f2df84e0SMaxime Ripard return drmm_add_action_or_reset(&vc4->base, vc4_hvs_channels_obj_fini, NULL); 730f2df84e0SMaxime Ripard } 731f2df84e0SMaxime Ripard 732b5dbc4d3SMaxime Ripard /* 733b5dbc4d3SMaxime Ripard * The BCM2711 HVS has up to 7 outputs connected to the pixelvalves and 734b5dbc4d3SMaxime Ripard * the TXP (and therefore all the CRTCs found on that platform). 735b5dbc4d3SMaxime Ripard * 736b5dbc4d3SMaxime Ripard * The naive (and our initial) implementation would just iterate over 737b5dbc4d3SMaxime Ripard * all the active CRTCs, try to find a suitable FIFO, and then remove it 738b5dbc4d3SMaxime Ripard * from the pool of available FIFOs. However, there are a few corner 739b5dbc4d3SMaxime Ripard * cases that need to be considered: 740b5dbc4d3SMaxime Ripard * 741b5dbc4d3SMaxime Ripard * - When running in a dual-display setup (so with two CRTCs involved), 742b5dbc4d3SMaxime Ripard * we can update the state of a single CRTC (for example by changing 743b5dbc4d3SMaxime Ripard * its mode using xrandr under X11) without affecting the other. In 744b5dbc4d3SMaxime Ripard * this case, the other CRTC wouldn't be in the state at all, so we 745b5dbc4d3SMaxime Ripard * need to consider all the running CRTCs in the DRM device to assign 746b5dbc4d3SMaxime Ripard * a FIFO, not just the one in the state. 747b5dbc4d3SMaxime Ripard * 748f2df84e0SMaxime Ripard * - To fix the above, we can't use drm_atomic_get_crtc_state on all 749f2df84e0SMaxime Ripard * enabled CRTCs to pull their CRTC state into the global state, since 750f2df84e0SMaxime Ripard * a page flip would start considering their vblank to complete. Since 751f2df84e0SMaxime Ripard * we don't have a guarantee that they are actually active, that 752f2df84e0SMaxime Ripard * vblank might never happen, and shouldn't even be considered if we 753f2df84e0SMaxime Ripard * want to do a page flip on a single CRTC. That can be tested by 754f2df84e0SMaxime Ripard * doing a modetest -v first on HDMI1 and then on HDMI0. 755f2df84e0SMaxime Ripard * 756b5dbc4d3SMaxime Ripard * - Since we need the pixelvalve to be disabled and enabled back when 757b5dbc4d3SMaxime Ripard * the FIFO is changed, we should keep the FIFO assigned for as long 758b5dbc4d3SMaxime Ripard * as the CRTC is enabled, only considering it free again once that 759b5dbc4d3SMaxime Ripard * CRTC has been disabled. This can be tested by booting X11 on a 760b5dbc4d3SMaxime Ripard * single display, and changing the resolution down and then back up. 761b5dbc4d3SMaxime Ripard */ 762a72b0458SMaxime Ripard static int vc4_pv_muxing_atomic_check(struct drm_device *dev, 763a72b0458SMaxime Ripard struct drm_atomic_state *state) 764766cc6b1SStefan Schake { 765f2df84e0SMaxime Ripard struct vc4_hvs_state *hvs_new_state; 7668ba0b6d1SMaxime Ripard struct drm_crtc_state *old_crtc_state, *new_crtc_state; 76787ebcd42SMaxime Ripard struct drm_crtc *crtc; 76803b03efeSMaxime Ripard unsigned int unassigned_channels = 0; 769a72b0458SMaxime Ripard unsigned int i; 77087ebcd42SMaxime Ripard 771f2df84e0SMaxime Ripard hvs_new_state = vc4_hvs_get_global_state(state); 772f9277679SMaxime Ripard if (IS_ERR(hvs_new_state)) 773f9277679SMaxime Ripard return PTR_ERR(hvs_new_state); 774089d8341SMaxime Ripard 77503b03efeSMaxime Ripard for (i = 0; i < ARRAY_SIZE(hvs_new_state->fifo_state); i++) 77603b03efeSMaxime Ripard if (!hvs_new_state->fifo_state[i].in_use) 77703b03efeSMaxime Ripard unassigned_channels |= BIT(i); 77803b03efeSMaxime Ripard 7798ba0b6d1SMaxime Ripard for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) { 780f2df84e0SMaxime Ripard struct vc4_crtc_state *old_vc4_crtc_state = 781f2df84e0SMaxime Ripard to_vc4_crtc_state(old_crtc_state); 7828ba0b6d1SMaxime Ripard struct vc4_crtc_state *new_vc4_crtc_state = 7838ba0b6d1SMaxime Ripard to_vc4_crtc_state(new_crtc_state); 78487ebcd42SMaxime Ripard struct vc4_crtc *vc4_crtc = to_vc4_crtc(crtc); 78587ebcd42SMaxime Ripard unsigned int matching_channels; 786d62a8ed7SMaxime Ripard unsigned int channel; 78787ebcd42SMaxime Ripard 7882820526dSMaxime Ripard /* Nothing to do here, let's skip it */ 7892820526dSMaxime Ripard if (old_crtc_state->enable == new_crtc_state->enable) 7902820526dSMaxime Ripard continue; 7912820526dSMaxime Ripard 7922820526dSMaxime Ripard /* Muxing will need to be modified, mark it as such */ 7932820526dSMaxime Ripard new_vc4_crtc_state->update_muxing = true; 7942820526dSMaxime Ripard 7952820526dSMaxime Ripard /* If we're disabling our CRTC, we put back our channel */ 7962820526dSMaxime Ripard if (!new_crtc_state->enable) { 7979ec03d7fSMaxime Ripard channel = old_vc4_crtc_state->assigned_channel; 7989ec03d7fSMaxime Ripard hvs_new_state->fifo_state[channel].in_use = false; 7998ba0b6d1SMaxime Ripard new_vc4_crtc_state->assigned_channel = VC4_HVS_CHANNEL_DISABLED; 8002820526dSMaxime Ripard continue; 801f2df84e0SMaxime Ripard } 8028ba0b6d1SMaxime Ripard 80387ebcd42SMaxime Ripard /* 80487ebcd42SMaxime Ripard * The problem we have to solve here is that we have 80587ebcd42SMaxime Ripard * up to 7 encoders, connected to up to 6 CRTCs. 80687ebcd42SMaxime Ripard * 80787ebcd42SMaxime Ripard * Those CRTCs, depending on the instance, can be 80887ebcd42SMaxime Ripard * routed to 1, 2 or 3 HVS FIFOs, and we need to set 80987ebcd42SMaxime Ripard * the change the muxing between FIFOs and outputs in 81087ebcd42SMaxime Ripard * the HVS accordingly. 81187ebcd42SMaxime Ripard * 81287ebcd42SMaxime Ripard * It would be pretty hard to come up with an 81387ebcd42SMaxime Ripard * algorithm that would generically solve 81487ebcd42SMaxime Ripard * this. However, the current routing trees we support 81587ebcd42SMaxime Ripard * allow us to simplify a bit the problem. 81687ebcd42SMaxime Ripard * 81787ebcd42SMaxime Ripard * Indeed, with the current supported layouts, if we 81887ebcd42SMaxime Ripard * try to assign in the ascending crtc index order the 81987ebcd42SMaxime Ripard * FIFOs, we can't fall into the situation where an 82087ebcd42SMaxime Ripard * earlier CRTC that had multiple routes is assigned 82187ebcd42SMaxime Ripard * one that was the only option for a later CRTC. 82287ebcd42SMaxime Ripard * 82387ebcd42SMaxime Ripard * If the layout changes and doesn't give us that in 82487ebcd42SMaxime Ripard * the future, we will need to have something smarter, 82587ebcd42SMaxime Ripard * but it works so far. 82687ebcd42SMaxime Ripard */ 82703b03efeSMaxime Ripard matching_channels = unassigned_channels & vc4_crtc->data->hvs_available_channels; 828d62a8ed7SMaxime Ripard if (!matching_channels) 829d62a8ed7SMaxime Ripard return -EINVAL; 83087ebcd42SMaxime Ripard 831d62a8ed7SMaxime Ripard channel = ffs(matching_channels) - 1; 8328ba0b6d1SMaxime Ripard new_vc4_crtc_state->assigned_channel = channel; 83303b03efeSMaxime Ripard unassigned_channels &= ~BIT(channel); 8349ec03d7fSMaxime Ripard hvs_new_state->fifo_state[channel].in_use = true; 83587ebcd42SMaxime Ripard } 836766cc6b1SStefan Schake 837a72b0458SMaxime Ripard return 0; 838a72b0458SMaxime Ripard } 839a72b0458SMaxime Ripard 840a72b0458SMaxime Ripard static int 841a72b0458SMaxime Ripard vc4_atomic_check(struct drm_device *dev, struct drm_atomic_state *state) 842a72b0458SMaxime Ripard { 843a72b0458SMaxime Ripard int ret; 844a72b0458SMaxime Ripard 845a72b0458SMaxime Ripard ret = vc4_pv_muxing_atomic_check(dev, state); 846a72b0458SMaxime Ripard if (ret) 847a72b0458SMaxime Ripard return ret; 848a72b0458SMaxime Ripard 849766cc6b1SStefan Schake ret = vc4_ctm_atomic_check(dev, state); 850766cc6b1SStefan Schake if (ret < 0) 851766cc6b1SStefan Schake return ret; 852766cc6b1SStefan Schake 8534686da83SBoris Brezillon ret = drm_atomic_helper_check(dev, state); 8544686da83SBoris Brezillon if (ret) 8554686da83SBoris Brezillon return ret; 8564686da83SBoris Brezillon 8574686da83SBoris Brezillon return vc4_load_tracker_atomic_check(state); 858766cc6b1SStefan Schake } 859766cc6b1SStefan Schake 8609ec03d7fSMaxime Ripard static struct drm_mode_config_helper_funcs vc4_mode_config_helpers = { 8619ec03d7fSMaxime Ripard .atomic_commit_setup = vc4_atomic_commit_setup, 862f3c420feSMaxime Ripard .atomic_commit_tail = vc4_atomic_commit_tail, 8639ec03d7fSMaxime Ripard }; 8649ec03d7fSMaxime Ripard 865c8b75bcaSEric Anholt static const struct drm_mode_config_funcs vc4_mode_funcs = { 866766cc6b1SStefan Schake .atomic_check = vc4_atomic_check, 867f3c420feSMaxime Ripard .atomic_commit = drm_atomic_helper_commit, 86883753117SEric Anholt .fb_create = vc4_fb_create, 869c8b75bcaSEric Anholt }; 870c8b75bcaSEric Anholt 871c8b75bcaSEric Anholt int vc4_kms_load(struct drm_device *dev) 872c8b75bcaSEric Anholt { 87348666d56SDerek Foreman struct vc4_dev *vc4 = to_vc4_dev(dev); 874f437bc1eSMaxime Ripard bool is_vc5 = of_device_is_compatible(dev->dev->of_node, 875f437bc1eSMaxime Ripard "brcm,bcm2711-vc5"); 876c8b75bcaSEric Anholt int ret; 877c8b75bcaSEric Anholt 878f437bc1eSMaxime Ripard if (!is_vc5) { 879f437bc1eSMaxime Ripard vc4->load_tracker_available = true; 880f437bc1eSMaxime Ripard 881f437bc1eSMaxime Ripard /* Start with the load tracker enabled. Can be 882f437bc1eSMaxime Ripard * disabled through the debugfs load_tracker file. 8836b5c029dSPaul Kocialkowski */ 8846b5c029dSPaul Kocialkowski vc4->load_tracker_enabled = true; 885f437bc1eSMaxime Ripard } 8866b5c029dSPaul Kocialkowski 8877d2818f5SMario Kleiner /* Set support for vblank irq fast disable, before drm_vblank_init() */ 8887d2818f5SMario Kleiner dev->vblank_disable_immediate = true; 8897d2818f5SMario Kleiner 890c8b75bcaSEric Anholt ret = drm_vblank_init(dev, dev->mode_config.num_crtc); 891c8b75bcaSEric Anholt if (ret < 0) { 892c8b75bcaSEric Anholt dev_err(dev->dev, "failed to initialize vblank\n"); 893c8b75bcaSEric Anholt return ret; 894c8b75bcaSEric Anholt } 895c8b75bcaSEric Anholt 896f437bc1eSMaxime Ripard if (is_vc5) { 897f437bc1eSMaxime Ripard dev->mode_config.max_width = 7680; 898f437bc1eSMaxime Ripard dev->mode_config.max_height = 7680; 899f437bc1eSMaxime Ripard } else { 900c8b75bcaSEric Anholt dev->mode_config.max_width = 2048; 901c8b75bcaSEric Anholt dev->mode_config.max_height = 2048; 902f437bc1eSMaxime Ripard } 903f437bc1eSMaxime Ripard 904c8b75bcaSEric Anholt dev->mode_config.funcs = &vc4_mode_funcs; 9059ec03d7fSMaxime Ripard dev->mode_config.helper_private = &vc4_mode_config_helpers; 906c8b75bcaSEric Anholt dev->mode_config.preferred_depth = 24; 907b501baccSEric Anholt dev->mode_config.async_page_flip = true; 908b501baccSEric Anholt 909dcda7c28SMaxime Ripard ret = vc4_ctm_obj_init(vc4); 910dcda7c28SMaxime Ripard if (ret) 911dcda7c28SMaxime Ripard return ret; 912766cc6b1SStefan Schake 913dcda7c28SMaxime Ripard ret = vc4_load_tracker_obj_init(vc4); 914dcda7c28SMaxime Ripard if (ret) 915dcda7c28SMaxime Ripard return ret; 9164686da83SBoris Brezillon 917f2df84e0SMaxime Ripard ret = vc4_hvs_channels_obj_init(vc4); 918f2df84e0SMaxime Ripard if (ret) 919f2df84e0SMaxime Ripard return ret; 920f2df84e0SMaxime Ripard 921c8b75bcaSEric Anholt drm_mode_config_reset(dev); 922c8b75bcaSEric Anholt 923c8b75bcaSEric Anholt drm_kms_helper_poll_init(dev); 924c8b75bcaSEric Anholt 925c8b75bcaSEric Anholt return 0; 926c8b75bcaSEric Anholt } 927