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 35220f125cSMaxime Ripard static struct vc4_ctm_state * 36220f125cSMaxime Ripard to_vc4_ctm_state(const struct drm_private_state *priv) 37766cc6b1SStefan Schake { 38766cc6b1SStefan Schake return container_of(priv, struct vc4_ctm_state, base); 39766cc6b1SStefan Schake } 40766cc6b1SStefan Schake 41f2df84e0SMaxime Ripard struct vc4_hvs_state { 42f2df84e0SMaxime Ripard struct drm_private_state base; 4316e10105SMaxime Ripard unsigned long core_clock_rate; 449ec03d7fSMaxime Ripard 459ec03d7fSMaxime Ripard struct { 469ec03d7fSMaxime Ripard unsigned in_use: 1; 4716e10105SMaxime Ripard unsigned long fifo_load; 489ec03d7fSMaxime Ripard struct drm_crtc_commit *pending_commit; 499ec03d7fSMaxime Ripard } fifo_state[HVS_NUM_CHANNELS]; 50f2df84e0SMaxime Ripard }; 51f2df84e0SMaxime Ripard 52f2df84e0SMaxime Ripard static struct vc4_hvs_state * 53220f125cSMaxime Ripard to_vc4_hvs_state(const struct drm_private_state *priv) 54f2df84e0SMaxime Ripard { 55f2df84e0SMaxime Ripard return container_of(priv, struct vc4_hvs_state, base); 56f2df84e0SMaxime Ripard } 57f2df84e0SMaxime Ripard 584686da83SBoris Brezillon struct vc4_load_tracker_state { 594686da83SBoris Brezillon struct drm_private_state base; 604686da83SBoris Brezillon u64 hvs_load; 614686da83SBoris Brezillon u64 membus_load; 624686da83SBoris Brezillon }; 634686da83SBoris Brezillon 644686da83SBoris Brezillon static struct vc4_load_tracker_state * 65220f125cSMaxime Ripard to_vc4_load_tracker_state(const struct drm_private_state *priv) 664686da83SBoris Brezillon { 674686da83SBoris Brezillon return container_of(priv, struct vc4_load_tracker_state, base); 684686da83SBoris Brezillon } 694686da83SBoris Brezillon 70766cc6b1SStefan Schake static struct vc4_ctm_state *vc4_get_ctm_state(struct drm_atomic_state *state, 71766cc6b1SStefan Schake struct drm_private_obj *manager) 72766cc6b1SStefan Schake { 73766cc6b1SStefan Schake struct drm_device *dev = state->dev; 7488e08589SMaxime Ripard struct vc4_dev *vc4 = to_vc4_dev(dev); 75766cc6b1SStefan Schake struct drm_private_state *priv_state; 76766cc6b1SStefan Schake int ret; 77766cc6b1SStefan Schake 78766cc6b1SStefan Schake ret = drm_modeset_lock(&vc4->ctm_state_lock, state->acquire_ctx); 79766cc6b1SStefan Schake if (ret) 80766cc6b1SStefan Schake return ERR_PTR(ret); 81766cc6b1SStefan Schake 82766cc6b1SStefan Schake priv_state = drm_atomic_get_private_obj_state(state, manager); 83766cc6b1SStefan Schake if (IS_ERR(priv_state)) 84766cc6b1SStefan Schake return ERR_CAST(priv_state); 85766cc6b1SStefan Schake 86766cc6b1SStefan Schake return to_vc4_ctm_state(priv_state); 87766cc6b1SStefan Schake } 88766cc6b1SStefan Schake 89766cc6b1SStefan Schake static struct drm_private_state * 90766cc6b1SStefan Schake vc4_ctm_duplicate_state(struct drm_private_obj *obj) 91766cc6b1SStefan Schake { 92766cc6b1SStefan Schake struct vc4_ctm_state *state; 93766cc6b1SStefan Schake 94766cc6b1SStefan Schake state = kmemdup(obj->state, sizeof(*state), GFP_KERNEL); 95766cc6b1SStefan Schake if (!state) 96766cc6b1SStefan Schake return NULL; 97766cc6b1SStefan Schake 98766cc6b1SStefan Schake __drm_atomic_helper_private_obj_duplicate_state(obj, &state->base); 99766cc6b1SStefan Schake 100766cc6b1SStefan Schake return &state->base; 101766cc6b1SStefan Schake } 102766cc6b1SStefan Schake 103766cc6b1SStefan Schake static void vc4_ctm_destroy_state(struct drm_private_obj *obj, 104766cc6b1SStefan Schake struct drm_private_state *state) 105766cc6b1SStefan Schake { 106766cc6b1SStefan Schake struct vc4_ctm_state *ctm_state = to_vc4_ctm_state(state); 107766cc6b1SStefan Schake 108766cc6b1SStefan Schake kfree(ctm_state); 109766cc6b1SStefan Schake } 110766cc6b1SStefan Schake 111766cc6b1SStefan Schake static const struct drm_private_state_funcs vc4_ctm_state_funcs = { 112766cc6b1SStefan Schake .atomic_duplicate_state = vc4_ctm_duplicate_state, 113766cc6b1SStefan Schake .atomic_destroy_state = vc4_ctm_destroy_state, 114766cc6b1SStefan Schake }; 115766cc6b1SStefan Schake 116dcda7c28SMaxime Ripard static void vc4_ctm_obj_fini(struct drm_device *dev, void *unused) 117dcda7c28SMaxime Ripard { 118dcda7c28SMaxime Ripard struct vc4_dev *vc4 = to_vc4_dev(dev); 119dcda7c28SMaxime Ripard 120dcda7c28SMaxime Ripard drm_atomic_private_obj_fini(&vc4->ctm_manager); 121dcda7c28SMaxime Ripard } 122dcda7c28SMaxime Ripard 123dcda7c28SMaxime Ripard static int vc4_ctm_obj_init(struct vc4_dev *vc4) 124dcda7c28SMaxime Ripard { 125dcda7c28SMaxime Ripard struct vc4_ctm_state *ctm_state; 126dcda7c28SMaxime Ripard 127dcda7c28SMaxime Ripard drm_modeset_lock_init(&vc4->ctm_state_lock); 128dcda7c28SMaxime Ripard 129dcda7c28SMaxime Ripard ctm_state = kzalloc(sizeof(*ctm_state), GFP_KERNEL); 130dcda7c28SMaxime Ripard if (!ctm_state) 131dcda7c28SMaxime Ripard return -ENOMEM; 132dcda7c28SMaxime Ripard 133dcda7c28SMaxime Ripard drm_atomic_private_obj_init(&vc4->base, &vc4->ctm_manager, &ctm_state->base, 134dcda7c28SMaxime Ripard &vc4_ctm_state_funcs); 135dcda7c28SMaxime Ripard 1363c354ed1SMaxime Ripard return drmm_add_action_or_reset(&vc4->base, vc4_ctm_obj_fini, NULL); 137dcda7c28SMaxime Ripard } 138dcda7c28SMaxime Ripard 139766cc6b1SStefan Schake /* Converts a DRM S31.32 value to the HW S0.9 format. */ 140766cc6b1SStefan Schake static u16 vc4_ctm_s31_32_to_s0_9(u64 in) 141766cc6b1SStefan Schake { 142766cc6b1SStefan Schake u16 r; 143766cc6b1SStefan Schake 144766cc6b1SStefan Schake /* Sign bit. */ 145766cc6b1SStefan Schake r = in & BIT_ULL(63) ? BIT(9) : 0; 146766cc6b1SStefan Schake 147766cc6b1SStefan Schake if ((in & GENMASK_ULL(62, 32)) > 0) { 148766cc6b1SStefan Schake /* We have zero integer bits so we can only saturate here. */ 149766cc6b1SStefan Schake r |= GENMASK(8, 0); 150766cc6b1SStefan Schake } else { 151766cc6b1SStefan Schake /* Otherwise take the 9 most important fractional bits. */ 152766cc6b1SStefan Schake r |= (in >> 23) & GENMASK(8, 0); 153766cc6b1SStefan Schake } 154766cc6b1SStefan Schake 155766cc6b1SStefan Schake return r; 156766cc6b1SStefan Schake } 157766cc6b1SStefan Schake 158766cc6b1SStefan Schake static void 159766cc6b1SStefan Schake vc4_ctm_commit(struct vc4_dev *vc4, struct drm_atomic_state *state) 160766cc6b1SStefan Schake { 161766cc6b1SStefan Schake struct vc4_ctm_state *ctm_state = to_vc4_ctm_state(vc4->ctm_manager.state); 162766cc6b1SStefan Schake struct drm_color_ctm *ctm = ctm_state->ctm; 163766cc6b1SStefan Schake 164766cc6b1SStefan Schake if (ctm_state->fifo) { 165766cc6b1SStefan Schake HVS_WRITE(SCALER_OLEDCOEF2, 166766cc6b1SStefan Schake VC4_SET_FIELD(vc4_ctm_s31_32_to_s0_9(ctm->matrix[0]), 167766cc6b1SStefan Schake SCALER_OLEDCOEF2_R_TO_R) | 168766cc6b1SStefan Schake VC4_SET_FIELD(vc4_ctm_s31_32_to_s0_9(ctm->matrix[3]), 169766cc6b1SStefan Schake SCALER_OLEDCOEF2_R_TO_G) | 170766cc6b1SStefan Schake VC4_SET_FIELD(vc4_ctm_s31_32_to_s0_9(ctm->matrix[6]), 171766cc6b1SStefan Schake SCALER_OLEDCOEF2_R_TO_B)); 172766cc6b1SStefan Schake HVS_WRITE(SCALER_OLEDCOEF1, 173766cc6b1SStefan Schake VC4_SET_FIELD(vc4_ctm_s31_32_to_s0_9(ctm->matrix[1]), 174766cc6b1SStefan Schake SCALER_OLEDCOEF1_G_TO_R) | 175766cc6b1SStefan Schake VC4_SET_FIELD(vc4_ctm_s31_32_to_s0_9(ctm->matrix[4]), 176766cc6b1SStefan Schake SCALER_OLEDCOEF1_G_TO_G) | 177766cc6b1SStefan Schake VC4_SET_FIELD(vc4_ctm_s31_32_to_s0_9(ctm->matrix[7]), 178766cc6b1SStefan Schake SCALER_OLEDCOEF1_G_TO_B)); 179766cc6b1SStefan Schake HVS_WRITE(SCALER_OLEDCOEF0, 180766cc6b1SStefan Schake VC4_SET_FIELD(vc4_ctm_s31_32_to_s0_9(ctm->matrix[2]), 181766cc6b1SStefan Schake SCALER_OLEDCOEF0_B_TO_R) | 182766cc6b1SStefan Schake VC4_SET_FIELD(vc4_ctm_s31_32_to_s0_9(ctm->matrix[5]), 183766cc6b1SStefan Schake SCALER_OLEDCOEF0_B_TO_G) | 184766cc6b1SStefan Schake VC4_SET_FIELD(vc4_ctm_s31_32_to_s0_9(ctm->matrix[8]), 185766cc6b1SStefan Schake SCALER_OLEDCOEF0_B_TO_B)); 186766cc6b1SStefan Schake } 187766cc6b1SStefan Schake 188766cc6b1SStefan Schake HVS_WRITE(SCALER_OLEDOFFS, 189766cc6b1SStefan Schake VC4_SET_FIELD(ctm_state->fifo, SCALER_OLEDOFFS_DISPFIFO)); 190766cc6b1SStefan Schake } 191c8b75bcaSEric Anholt 192f2df84e0SMaxime Ripard static struct vc4_hvs_state * 1939ec03d7fSMaxime Ripard vc4_hvs_get_new_global_state(struct drm_atomic_state *state) 1949ec03d7fSMaxime Ripard { 1959ec03d7fSMaxime Ripard struct vc4_dev *vc4 = to_vc4_dev(state->dev); 1969ec03d7fSMaxime Ripard struct drm_private_state *priv_state; 1979ec03d7fSMaxime Ripard 1989ec03d7fSMaxime Ripard priv_state = drm_atomic_get_new_private_obj_state(state, &vc4->hvs_channels); 1999ec03d7fSMaxime Ripard if (IS_ERR(priv_state)) 2009ec03d7fSMaxime Ripard return ERR_CAST(priv_state); 2019ec03d7fSMaxime Ripard 2029ec03d7fSMaxime Ripard return to_vc4_hvs_state(priv_state); 2039ec03d7fSMaxime Ripard } 2049ec03d7fSMaxime Ripard 2059ec03d7fSMaxime Ripard static struct vc4_hvs_state * 2069ec03d7fSMaxime Ripard vc4_hvs_get_old_global_state(struct drm_atomic_state *state) 2079ec03d7fSMaxime Ripard { 2089ec03d7fSMaxime Ripard struct vc4_dev *vc4 = to_vc4_dev(state->dev); 2099ec03d7fSMaxime Ripard struct drm_private_state *priv_state; 2109ec03d7fSMaxime Ripard 2119ec03d7fSMaxime Ripard priv_state = drm_atomic_get_old_private_obj_state(state, &vc4->hvs_channels); 2129ec03d7fSMaxime Ripard if (IS_ERR(priv_state)) 2139ec03d7fSMaxime Ripard return ERR_CAST(priv_state); 2149ec03d7fSMaxime Ripard 2159ec03d7fSMaxime Ripard return to_vc4_hvs_state(priv_state); 2169ec03d7fSMaxime Ripard } 2179ec03d7fSMaxime Ripard 2189ec03d7fSMaxime Ripard static struct vc4_hvs_state * 219f2df84e0SMaxime Ripard vc4_hvs_get_global_state(struct drm_atomic_state *state) 220f2df84e0SMaxime Ripard { 221f2df84e0SMaxime Ripard struct vc4_dev *vc4 = to_vc4_dev(state->dev); 222f2df84e0SMaxime Ripard struct drm_private_state *priv_state; 223f2df84e0SMaxime Ripard 224f2df84e0SMaxime Ripard priv_state = drm_atomic_get_private_obj_state(state, &vc4->hvs_channels); 225f2df84e0SMaxime Ripard if (IS_ERR(priv_state)) 226f2df84e0SMaxime Ripard return ERR_CAST(priv_state); 227f2df84e0SMaxime Ripard 228f2df84e0SMaxime Ripard return to_vc4_hvs_state(priv_state); 229f2df84e0SMaxime Ripard } 230f2df84e0SMaxime Ripard 23187ebcd42SMaxime Ripard static void vc4_hvs_pv_muxing_commit(struct vc4_dev *vc4, 23287ebcd42SMaxime Ripard struct drm_atomic_state *state) 23387ebcd42SMaxime Ripard { 23487ebcd42SMaxime Ripard struct drm_crtc_state *crtc_state; 23587ebcd42SMaxime Ripard struct drm_crtc *crtc; 23687ebcd42SMaxime Ripard unsigned int i; 23787ebcd42SMaxime Ripard 23887ebcd42SMaxime Ripard for_each_new_crtc_in_state(state, crtc, crtc_state, i) { 239a16c6640SMaxime Ripard struct vc4_crtc *vc4_crtc = to_vc4_crtc(crtc); 24087ebcd42SMaxime Ripard struct vc4_crtc_state *vc4_state = to_vc4_crtc_state(crtc_state); 24187ebcd42SMaxime Ripard u32 dispctrl; 24287ebcd42SMaxime Ripard u32 dsp3_mux; 24387ebcd42SMaxime Ripard 24487ebcd42SMaxime Ripard if (!crtc_state->active) 24587ebcd42SMaxime Ripard continue; 24687ebcd42SMaxime Ripard 24787ebcd42SMaxime Ripard if (vc4_state->assigned_channel != 2) 24887ebcd42SMaxime Ripard continue; 24987ebcd42SMaxime Ripard 25087ebcd42SMaxime Ripard /* 25187ebcd42SMaxime Ripard * SCALER_DISPCTRL_DSP3 = X, where X < 2 means 'connect DSP3 to 25287ebcd42SMaxime Ripard * FIFO X'. 25387ebcd42SMaxime Ripard * SCALER_DISPCTRL_DSP3 = 3 means 'disable DSP 3'. 25487ebcd42SMaxime Ripard * 25587ebcd42SMaxime Ripard * DSP3 is connected to FIFO2 unless the transposer is 25687ebcd42SMaxime Ripard * enabled. In this case, FIFO 2 is directly accessed by the 25787ebcd42SMaxime Ripard * TXP IP, and we need to disable the FIFO2 -> pixelvalve1 25887ebcd42SMaxime Ripard * route. 25987ebcd42SMaxime Ripard */ 260a16c6640SMaxime Ripard if (vc4_crtc->feeds_txp) 26187ebcd42SMaxime Ripard dsp3_mux = VC4_SET_FIELD(3, SCALER_DISPCTRL_DSP3_MUX); 26287ebcd42SMaxime Ripard else 26387ebcd42SMaxime Ripard dsp3_mux = VC4_SET_FIELD(2, SCALER_DISPCTRL_DSP3_MUX); 26487ebcd42SMaxime Ripard 26587ebcd42SMaxime Ripard dispctrl = HVS_READ(SCALER_DISPCTRL) & 26687ebcd42SMaxime Ripard ~SCALER_DISPCTRL_DSP3_MUX_MASK; 26787ebcd42SMaxime Ripard HVS_WRITE(SCALER_DISPCTRL, dispctrl | dsp3_mux); 26887ebcd42SMaxime Ripard } 26987ebcd42SMaxime Ripard } 27087ebcd42SMaxime Ripard 27187ebcd42SMaxime Ripard static void vc5_hvs_pv_muxing_commit(struct vc4_dev *vc4, 27287ebcd42SMaxime Ripard struct drm_atomic_state *state) 27387ebcd42SMaxime Ripard { 27487ebcd42SMaxime Ripard struct drm_crtc_state *crtc_state; 27587ebcd42SMaxime Ripard struct drm_crtc *crtc; 2762820526dSMaxime Ripard unsigned char mux; 27787ebcd42SMaxime Ripard unsigned int i; 27887ebcd42SMaxime Ripard u32 reg; 27987ebcd42SMaxime Ripard 28087ebcd42SMaxime Ripard for_each_new_crtc_in_state(state, crtc, crtc_state, i) { 28187ebcd42SMaxime Ripard struct vc4_crtc_state *vc4_state = to_vc4_crtc_state(crtc_state); 28287ebcd42SMaxime Ripard struct vc4_crtc *vc4_crtc = to_vc4_crtc(crtc); 28387ebcd42SMaxime Ripard 2842820526dSMaxime Ripard if (!vc4_state->update_muxing) 28587ebcd42SMaxime Ripard continue; 28687ebcd42SMaxime Ripard 28787ebcd42SMaxime Ripard switch (vc4_crtc->data->hvs_output) { 28887ebcd42SMaxime Ripard case 2: 2892820526dSMaxime Ripard mux = (vc4_state->assigned_channel == 2) ? 0 : 1; 2902820526dSMaxime Ripard reg = HVS_READ(SCALER_DISPECTRL); 2912820526dSMaxime Ripard HVS_WRITE(SCALER_DISPECTRL, 2922820526dSMaxime Ripard (reg & ~SCALER_DISPECTRL_DSP2_MUX_MASK) | 2932820526dSMaxime Ripard VC4_SET_FIELD(mux, SCALER_DISPECTRL_DSP2_MUX)); 29487ebcd42SMaxime Ripard break; 29587ebcd42SMaxime Ripard 29687ebcd42SMaxime Ripard case 3: 2972820526dSMaxime Ripard if (vc4_state->assigned_channel == VC4_HVS_CHANNEL_DISABLED) 2982820526dSMaxime Ripard mux = 3; 2992820526dSMaxime Ripard else 3002820526dSMaxime Ripard mux = vc4_state->assigned_channel; 3012820526dSMaxime Ripard 3022820526dSMaxime Ripard reg = HVS_READ(SCALER_DISPCTRL); 3032820526dSMaxime Ripard HVS_WRITE(SCALER_DISPCTRL, 3042820526dSMaxime Ripard (reg & ~SCALER_DISPCTRL_DSP3_MUX_MASK) | 3052820526dSMaxime Ripard VC4_SET_FIELD(mux, SCALER_DISPCTRL_DSP3_MUX)); 30687ebcd42SMaxime Ripard break; 30787ebcd42SMaxime Ripard 30887ebcd42SMaxime Ripard case 4: 3092820526dSMaxime Ripard if (vc4_state->assigned_channel == VC4_HVS_CHANNEL_DISABLED) 3102820526dSMaxime Ripard mux = 3; 3112820526dSMaxime Ripard else 3122820526dSMaxime Ripard mux = vc4_state->assigned_channel; 3132820526dSMaxime Ripard 3142820526dSMaxime Ripard reg = HVS_READ(SCALER_DISPEOLN); 3152820526dSMaxime Ripard HVS_WRITE(SCALER_DISPEOLN, 3162820526dSMaxime Ripard (reg & ~SCALER_DISPEOLN_DSP4_MUX_MASK) | 3172820526dSMaxime Ripard VC4_SET_FIELD(mux, SCALER_DISPEOLN_DSP4_MUX)); 3182820526dSMaxime Ripard 31987ebcd42SMaxime Ripard break; 32087ebcd42SMaxime Ripard 32187ebcd42SMaxime Ripard case 5: 3222820526dSMaxime Ripard if (vc4_state->assigned_channel == VC4_HVS_CHANNEL_DISABLED) 3232820526dSMaxime Ripard mux = 3; 3242820526dSMaxime Ripard else 3252820526dSMaxime Ripard mux = vc4_state->assigned_channel; 3262820526dSMaxime Ripard 3272820526dSMaxime Ripard reg = HVS_READ(SCALER_DISPDITHER); 3282820526dSMaxime Ripard HVS_WRITE(SCALER_DISPDITHER, 3292820526dSMaxime Ripard (reg & ~SCALER_DISPDITHER_DSP5_MUX_MASK) | 3302820526dSMaxime Ripard VC4_SET_FIELD(mux, SCALER_DISPDITHER_DSP5_MUX)); 33187ebcd42SMaxime Ripard break; 33287ebcd42SMaxime Ripard 33387ebcd42SMaxime Ripard default: 33487ebcd42SMaxime Ripard break; 33587ebcd42SMaxime Ripard } 33687ebcd42SMaxime Ripard } 33787ebcd42SMaxime Ripard } 33887ebcd42SMaxime Ripard 339f3c420feSMaxime Ripard static void vc4_atomic_commit_tail(struct drm_atomic_state *state) 340b501baccSEric Anholt { 341b501baccSEric Anholt struct drm_device *dev = state->dev; 342b501baccSEric Anholt struct vc4_dev *vc4 = to_vc4_dev(dev); 343d7d96c00SMaxime Ripard struct vc4_hvs *hvs = vc4->hvs; 34459635667SMaxime Ripard struct drm_crtc_state *new_crtc_state; 34516e10105SMaxime Ripard struct vc4_hvs_state *new_hvs_state; 34659635667SMaxime Ripard struct drm_crtc *crtc; 3479ec03d7fSMaxime Ripard struct vc4_hvs_state *old_hvs_state; 3486052a311SMaxime Ripard unsigned int channel; 349531a1b62SBoris Brezillon int i; 350531a1b62SBoris Brezillon 35116e10105SMaxime Ripard old_hvs_state = vc4_hvs_get_old_global_state(state); 35299b03ca6SDaniel Vetter if (WARN_ON(IS_ERR(old_hvs_state))) 35316e10105SMaxime Ripard return; 35416e10105SMaxime Ripard 35516e10105SMaxime Ripard new_hvs_state = vc4_hvs_get_new_global_state(state); 35699b03ca6SDaniel Vetter if (WARN_ON(IS_ERR(new_hvs_state))) 35716e10105SMaxime Ripard return; 35816e10105SMaxime Ripard 35959635667SMaxime Ripard for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) { 36087ebcd42SMaxime Ripard struct vc4_crtc_state *vc4_crtc_state; 36159635667SMaxime Ripard 36259635667SMaxime Ripard if (!new_crtc_state->commit) 363531a1b62SBoris Brezillon continue; 364531a1b62SBoris Brezillon 36587ebcd42SMaxime Ripard vc4_crtc_state = to_vc4_crtc_state(new_crtc_state); 36687ebcd42SMaxime Ripard vc4_hvs_mask_underrun(dev, vc4_crtc_state->assigned_channel); 367531a1b62SBoris Brezillon } 368b501baccSEric Anholt 3696052a311SMaxime Ripard for (channel = 0; channel < HVS_NUM_CHANNELS; channel++) { 370049cfff8SMaxime Ripard struct drm_crtc_commit *commit; 371b99c2c95SMaxime Ripard int ret; 3729ec03d7fSMaxime Ripard 3739ec03d7fSMaxime Ripard if (!old_hvs_state->fifo_state[channel].in_use) 3749ec03d7fSMaxime Ripard continue; 3759ec03d7fSMaxime Ripard 376049cfff8SMaxime Ripard commit = old_hvs_state->fifo_state[channel].pending_commit; 377049cfff8SMaxime Ripard if (!commit) 378049cfff8SMaxime Ripard continue; 379049cfff8SMaxime Ripard 380049cfff8SMaxime Ripard ret = drm_crtc_commit_wait(commit); 381b99c2c95SMaxime Ripard if (ret) 382b99c2c95SMaxime Ripard drm_err(dev, "Timed out waiting for commit\n"); 383049cfff8SMaxime Ripard 384049cfff8SMaxime Ripard drm_crtc_commit_put(commit); 385d134c5ffSMaxime Ripard old_hvs_state->fifo_state[channel].pending_commit = NULL; 3869ec03d7fSMaxime Ripard } 3879ec03d7fSMaxime Ripard 388244a36e5SMaxime Ripard if (vc4->hvs->hvs5) { 389244a36e5SMaxime Ripard unsigned long core_rate = max_t(unsigned long, 390244a36e5SMaxime Ripard 500000000, 391244a36e5SMaxime Ripard new_hvs_state->core_clock_rate); 392244a36e5SMaxime Ripard 393244a36e5SMaxime Ripard clk_set_min_rate(hvs->core_clk, core_rate); 394244a36e5SMaxime Ripard } 395b501baccSEric Anholt drm_atomic_helper_commit_modeset_disables(dev, state); 396b501baccSEric Anholt 397766cc6b1SStefan Schake vc4_ctm_commit(vc4, state); 398766cc6b1SStefan Schake 39987ebcd42SMaxime Ripard if (vc4->hvs->hvs5) 40087ebcd42SMaxime Ripard vc5_hvs_pv_muxing_commit(vc4, state); 40187ebcd42SMaxime Ripard else 40287ebcd42SMaxime Ripard vc4_hvs_pv_muxing_commit(vc4, state); 40387ebcd42SMaxime Ripard 4042b58e98dSLiu Ying drm_atomic_helper_commit_planes(dev, state, 0); 405b501baccSEric Anholt 406b501baccSEric Anholt drm_atomic_helper_commit_modeset_enables(dev, state); 407b501baccSEric Anholt 4081ebe99a7SBoris Brezillon drm_atomic_helper_fake_vblank(state); 4091ebe99a7SBoris Brezillon 41034c8ea40SBoris Brezillon drm_atomic_helper_commit_hw_done(state); 41134c8ea40SBoris Brezillon 412184d3cf4SBoris Brezillon drm_atomic_helper_wait_for_flip_done(dev, state); 413b501baccSEric Anholt 414b501baccSEric Anholt drm_atomic_helper_cleanup_planes(dev, state); 415b501baccSEric Anholt 41616e10105SMaxime Ripard if (vc4->hvs->hvs5) { 41716e10105SMaxime Ripard drm_dbg(dev, "Running the core clock at %lu Hz\n", 41816e10105SMaxime Ripard new_hvs_state->core_clock_rate); 41916e10105SMaxime Ripard 42016e10105SMaxime Ripard clk_set_min_rate(hvs->core_clk, new_hvs_state->core_clock_rate); 42116e10105SMaxime Ripard } 422b501baccSEric Anholt } 423b501baccSEric Anholt 4249ec03d7fSMaxime Ripard static int vc4_atomic_commit_setup(struct drm_atomic_state *state) 4259ec03d7fSMaxime Ripard { 4269ec03d7fSMaxime Ripard struct drm_crtc_state *crtc_state; 4279ec03d7fSMaxime Ripard struct vc4_hvs_state *hvs_state; 4289ec03d7fSMaxime Ripard struct drm_crtc *crtc; 4299ec03d7fSMaxime Ripard unsigned int i; 4309ec03d7fSMaxime Ripard 4319ec03d7fSMaxime Ripard hvs_state = vc4_hvs_get_new_global_state(state); 432f9277679SMaxime Ripard if (WARN_ON(IS_ERR(hvs_state))) 433f9277679SMaxime Ripard return PTR_ERR(hvs_state); 4349ec03d7fSMaxime Ripard 4359ec03d7fSMaxime Ripard for_each_new_crtc_in_state(state, crtc, crtc_state, i) { 4369ec03d7fSMaxime Ripard struct vc4_crtc_state *vc4_crtc_state = 4379ec03d7fSMaxime Ripard to_vc4_crtc_state(crtc_state); 4389ec03d7fSMaxime Ripard unsigned int channel = 4399ec03d7fSMaxime Ripard vc4_crtc_state->assigned_channel; 4409ec03d7fSMaxime Ripard 4419ec03d7fSMaxime Ripard if (channel == VC4_HVS_CHANNEL_DISABLED) 4429ec03d7fSMaxime Ripard continue; 4439ec03d7fSMaxime Ripard 4449ec03d7fSMaxime Ripard if (!hvs_state->fifo_state[channel].in_use) 4459ec03d7fSMaxime Ripard continue; 4469ec03d7fSMaxime Ripard 4479ec03d7fSMaxime Ripard hvs_state->fifo_state[channel].pending_commit = 4489ec03d7fSMaxime Ripard drm_crtc_commit_get(crtc_state->commit); 4499ec03d7fSMaxime Ripard } 4509ec03d7fSMaxime Ripard 4519ec03d7fSMaxime Ripard return 0; 4529ec03d7fSMaxime Ripard } 4539ec03d7fSMaxime Ripard 45483753117SEric Anholt static struct drm_framebuffer *vc4_fb_create(struct drm_device *dev, 45583753117SEric Anholt struct drm_file *file_priv, 45683753117SEric Anholt const struct drm_mode_fb_cmd2 *mode_cmd) 45783753117SEric Anholt { 45883753117SEric Anholt struct drm_mode_fb_cmd2 mode_cmd_local; 45983753117SEric Anholt 46083753117SEric Anholt /* If the user didn't specify a modifier, use the 46183753117SEric Anholt * vc4_set_tiling_ioctl() state for the BO. 46283753117SEric Anholt */ 46383753117SEric Anholt if (!(mode_cmd->flags & DRM_MODE_FB_MODIFIERS)) { 46483753117SEric Anholt struct drm_gem_object *gem_obj; 46583753117SEric Anholt struct vc4_bo *bo; 46683753117SEric Anholt 46783753117SEric Anholt gem_obj = drm_gem_object_lookup(file_priv, 46883753117SEric Anholt mode_cmd->handles[0]); 46983753117SEric Anholt if (!gem_obj) { 470fb95992aSEric Anholt DRM_DEBUG("Failed to look up GEM BO %d\n", 47183753117SEric Anholt mode_cmd->handles[0]); 47283753117SEric Anholt return ERR_PTR(-ENOENT); 47383753117SEric Anholt } 47483753117SEric Anholt bo = to_vc4_bo(gem_obj); 47583753117SEric Anholt 47683753117SEric Anholt mode_cmd_local = *mode_cmd; 47783753117SEric Anholt 47883753117SEric Anholt if (bo->t_format) { 47983753117SEric Anholt mode_cmd_local.modifier[0] = 48083753117SEric Anholt DRM_FORMAT_MOD_BROADCOM_VC4_T_TILED; 48183753117SEric Anholt } else { 48283753117SEric Anholt mode_cmd_local.modifier[0] = DRM_FORMAT_MOD_NONE; 48383753117SEric Anholt } 48483753117SEric Anholt 485f7a8cd30SEmil Velikov drm_gem_object_put(gem_obj); 48683753117SEric Anholt 48783753117SEric Anholt mode_cmd = &mode_cmd_local; 48883753117SEric Anholt } 48983753117SEric Anholt 4909762477cSNoralf Trønnes return drm_gem_fb_create(dev, file_priv, mode_cmd); 49183753117SEric Anholt } 49283753117SEric Anholt 493766cc6b1SStefan Schake /* Our CTM has some peculiar limitations: we can only enable it for one CRTC 494766cc6b1SStefan Schake * at a time and the HW only supports S0.9 scalars. To account for the latter, 495766cc6b1SStefan Schake * we don't allow userland to set a CTM that we have no hope of approximating. 496766cc6b1SStefan Schake */ 497766cc6b1SStefan Schake static int 498766cc6b1SStefan Schake vc4_ctm_atomic_check(struct drm_device *dev, struct drm_atomic_state *state) 499766cc6b1SStefan Schake { 500766cc6b1SStefan Schake struct vc4_dev *vc4 = to_vc4_dev(dev); 501766cc6b1SStefan Schake struct vc4_ctm_state *ctm_state = NULL; 502766cc6b1SStefan Schake struct drm_crtc *crtc; 503766cc6b1SStefan Schake struct drm_crtc_state *old_crtc_state, *new_crtc_state; 504766cc6b1SStefan Schake struct drm_color_ctm *ctm; 505766cc6b1SStefan Schake int i; 506766cc6b1SStefan Schake 507766cc6b1SStefan Schake for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) { 508766cc6b1SStefan Schake /* CTM is being disabled. */ 509766cc6b1SStefan Schake if (!new_crtc_state->ctm && old_crtc_state->ctm) { 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 ctm_state->fifo = 0; 514766cc6b1SStefan Schake } 515766cc6b1SStefan Schake } 516766cc6b1SStefan Schake 517766cc6b1SStefan Schake for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) { 518766cc6b1SStefan Schake if (new_crtc_state->ctm == old_crtc_state->ctm) 519766cc6b1SStefan Schake continue; 520766cc6b1SStefan Schake 521766cc6b1SStefan Schake if (!ctm_state) { 522766cc6b1SStefan Schake ctm_state = vc4_get_ctm_state(state, &vc4->ctm_manager); 523766cc6b1SStefan Schake if (IS_ERR(ctm_state)) 524766cc6b1SStefan Schake return PTR_ERR(ctm_state); 525766cc6b1SStefan Schake } 526766cc6b1SStefan Schake 527766cc6b1SStefan Schake /* CTM is being enabled or the matrix changed. */ 528766cc6b1SStefan Schake if (new_crtc_state->ctm) { 52987ebcd42SMaxime Ripard struct vc4_crtc_state *vc4_crtc_state = 53087ebcd42SMaxime Ripard to_vc4_crtc_state(new_crtc_state); 53187ebcd42SMaxime Ripard 532766cc6b1SStefan Schake /* fifo is 1-based since 0 disables CTM. */ 53387ebcd42SMaxime Ripard int fifo = vc4_crtc_state->assigned_channel + 1; 534766cc6b1SStefan Schake 535766cc6b1SStefan Schake /* Check userland isn't trying to turn on CTM for more 536766cc6b1SStefan Schake * than one CRTC at a time. 537766cc6b1SStefan Schake */ 538766cc6b1SStefan Schake if (ctm_state->fifo && ctm_state->fifo != fifo) { 539766cc6b1SStefan Schake DRM_DEBUG_DRIVER("Too many CTM configured\n"); 540766cc6b1SStefan Schake return -EINVAL; 541766cc6b1SStefan Schake } 542766cc6b1SStefan Schake 543766cc6b1SStefan Schake /* Check we can approximate the specified CTM. 544766cc6b1SStefan Schake * We disallow scalars |c| > 1.0 since the HW has 545766cc6b1SStefan Schake * no integer bits. 546766cc6b1SStefan Schake */ 547766cc6b1SStefan Schake ctm = new_crtc_state->ctm->data; 548766cc6b1SStefan Schake for (i = 0; i < ARRAY_SIZE(ctm->matrix); i++) { 549766cc6b1SStefan Schake u64 val = ctm->matrix[i]; 550766cc6b1SStefan Schake 551766cc6b1SStefan Schake val &= ~BIT_ULL(63); 552766cc6b1SStefan Schake if (val > BIT_ULL(32)) 553766cc6b1SStefan Schake return -EINVAL; 554766cc6b1SStefan Schake } 555766cc6b1SStefan Schake 556766cc6b1SStefan Schake ctm_state->fifo = fifo; 557766cc6b1SStefan Schake ctm_state->ctm = ctm; 558766cc6b1SStefan Schake } 559766cc6b1SStefan Schake } 560766cc6b1SStefan Schake 561766cc6b1SStefan Schake return 0; 562766cc6b1SStefan Schake } 563766cc6b1SStefan Schake 5644686da83SBoris Brezillon static int vc4_load_tracker_atomic_check(struct drm_atomic_state *state) 5654686da83SBoris Brezillon { 5664686da83SBoris Brezillon struct drm_plane_state *old_plane_state, *new_plane_state; 5674686da83SBoris Brezillon struct vc4_dev *vc4 = to_vc4_dev(state->dev); 5684686da83SBoris Brezillon struct vc4_load_tracker_state *load_state; 5694686da83SBoris Brezillon struct drm_private_state *priv_state; 5704686da83SBoris Brezillon struct drm_plane *plane; 5714686da83SBoris Brezillon int i; 5724686da83SBoris Brezillon 5734686da83SBoris Brezillon priv_state = drm_atomic_get_private_obj_state(state, 5744686da83SBoris Brezillon &vc4->load_tracker); 5754686da83SBoris Brezillon if (IS_ERR(priv_state)) 5764686da83SBoris Brezillon return PTR_ERR(priv_state); 5774686da83SBoris Brezillon 5784686da83SBoris Brezillon load_state = to_vc4_load_tracker_state(priv_state); 5794686da83SBoris Brezillon for_each_oldnew_plane_in_state(state, plane, old_plane_state, 5804686da83SBoris Brezillon new_plane_state, i) { 5814686da83SBoris Brezillon struct vc4_plane_state *vc4_plane_state; 5824686da83SBoris Brezillon 5834686da83SBoris Brezillon if (old_plane_state->fb && old_plane_state->crtc) { 5844686da83SBoris Brezillon vc4_plane_state = to_vc4_plane_state(old_plane_state); 5854686da83SBoris Brezillon load_state->membus_load -= vc4_plane_state->membus_load; 5864686da83SBoris Brezillon load_state->hvs_load -= vc4_plane_state->hvs_load; 5874686da83SBoris Brezillon } 5884686da83SBoris Brezillon 5894686da83SBoris Brezillon if (new_plane_state->fb && new_plane_state->crtc) { 5904686da83SBoris Brezillon vc4_plane_state = to_vc4_plane_state(new_plane_state); 5914686da83SBoris Brezillon load_state->membus_load += vc4_plane_state->membus_load; 5924686da83SBoris Brezillon load_state->hvs_load += vc4_plane_state->hvs_load; 5934686da83SBoris Brezillon } 5944686da83SBoris Brezillon } 5954686da83SBoris Brezillon 5966b5c029dSPaul Kocialkowski /* Don't check the load when the tracker is disabled. */ 5976b5c029dSPaul Kocialkowski if (!vc4->load_tracker_enabled) 5986b5c029dSPaul Kocialkowski return 0; 5996b5c029dSPaul Kocialkowski 6004686da83SBoris Brezillon /* The absolute limit is 2Gbyte/sec, but let's take a margin to let 6014686da83SBoris Brezillon * the system work when other blocks are accessing the memory. 6024686da83SBoris Brezillon */ 6034686da83SBoris Brezillon if (load_state->membus_load > SZ_1G + SZ_512M) 6044686da83SBoris Brezillon return -ENOSPC; 6054686da83SBoris Brezillon 6064686da83SBoris Brezillon /* HVS clock is supposed to run @ 250Mhz, let's take a margin and 6074686da83SBoris Brezillon * consider the maximum number of cycles is 240M. 6084686da83SBoris Brezillon */ 6094686da83SBoris Brezillon if (load_state->hvs_load > 240000000ULL) 6104686da83SBoris Brezillon return -ENOSPC; 6114686da83SBoris Brezillon 6124686da83SBoris Brezillon return 0; 6134686da83SBoris Brezillon } 6144686da83SBoris Brezillon 6154686da83SBoris Brezillon static struct drm_private_state * 6164686da83SBoris Brezillon vc4_load_tracker_duplicate_state(struct drm_private_obj *obj) 6174686da83SBoris Brezillon { 6184686da83SBoris Brezillon struct vc4_load_tracker_state *state; 6194686da83SBoris Brezillon 6204686da83SBoris Brezillon state = kmemdup(obj->state, sizeof(*state), GFP_KERNEL); 6214686da83SBoris Brezillon if (!state) 6224686da83SBoris Brezillon return NULL; 6234686da83SBoris Brezillon 6244686da83SBoris Brezillon __drm_atomic_helper_private_obj_duplicate_state(obj, &state->base); 6254686da83SBoris Brezillon 6264686da83SBoris Brezillon return &state->base; 6274686da83SBoris Brezillon } 6284686da83SBoris Brezillon 6294686da83SBoris Brezillon static void vc4_load_tracker_destroy_state(struct drm_private_obj *obj, 6304686da83SBoris Brezillon struct drm_private_state *state) 6314686da83SBoris Brezillon { 6324686da83SBoris Brezillon struct vc4_load_tracker_state *load_state; 6334686da83SBoris Brezillon 6344686da83SBoris Brezillon load_state = to_vc4_load_tracker_state(state); 6354686da83SBoris Brezillon kfree(load_state); 6364686da83SBoris Brezillon } 6374686da83SBoris Brezillon 6384686da83SBoris Brezillon static const struct drm_private_state_funcs vc4_load_tracker_state_funcs = { 6394686da83SBoris Brezillon .atomic_duplicate_state = vc4_load_tracker_duplicate_state, 6404686da83SBoris Brezillon .atomic_destroy_state = vc4_load_tracker_destroy_state, 6414686da83SBoris Brezillon }; 6424686da83SBoris Brezillon 643dcda7c28SMaxime Ripard static void vc4_load_tracker_obj_fini(struct drm_device *dev, void *unused) 644dcda7c28SMaxime Ripard { 645dcda7c28SMaxime Ripard struct vc4_dev *vc4 = to_vc4_dev(dev); 646dcda7c28SMaxime Ripard 647dcda7c28SMaxime Ripard drm_atomic_private_obj_fini(&vc4->load_tracker); 648dcda7c28SMaxime Ripard } 649dcda7c28SMaxime Ripard 650dcda7c28SMaxime Ripard static int vc4_load_tracker_obj_init(struct vc4_dev *vc4) 651dcda7c28SMaxime Ripard { 652dcda7c28SMaxime Ripard struct vc4_load_tracker_state *load_state; 653dcda7c28SMaxime Ripard 654dcda7c28SMaxime Ripard load_state = kzalloc(sizeof(*load_state), GFP_KERNEL); 655dcda7c28SMaxime Ripard if (!load_state) 656dcda7c28SMaxime Ripard return -ENOMEM; 657dcda7c28SMaxime Ripard 658dcda7c28SMaxime Ripard drm_atomic_private_obj_init(&vc4->base, &vc4->load_tracker, 659dcda7c28SMaxime Ripard &load_state->base, 660dcda7c28SMaxime Ripard &vc4_load_tracker_state_funcs); 661dcda7c28SMaxime Ripard 6623c354ed1SMaxime Ripard return drmm_add_action_or_reset(&vc4->base, vc4_load_tracker_obj_fini, NULL); 663dcda7c28SMaxime Ripard } 664dcda7c28SMaxime Ripard 665f2df84e0SMaxime Ripard static struct drm_private_state * 666f2df84e0SMaxime Ripard vc4_hvs_channels_duplicate_state(struct drm_private_obj *obj) 667f2df84e0SMaxime Ripard { 668f2df84e0SMaxime Ripard struct vc4_hvs_state *old_state = to_vc4_hvs_state(obj->state); 669f2df84e0SMaxime Ripard struct vc4_hvs_state *state; 6709ec03d7fSMaxime Ripard unsigned int i; 671f2df84e0SMaxime Ripard 672f2df84e0SMaxime Ripard state = kzalloc(sizeof(*state), GFP_KERNEL); 673f2df84e0SMaxime Ripard if (!state) 674f2df84e0SMaxime Ripard return NULL; 675f2df84e0SMaxime Ripard 676f2df84e0SMaxime Ripard __drm_atomic_helper_private_obj_duplicate_state(obj, &state->base); 677f2df84e0SMaxime Ripard 6789ec03d7fSMaxime Ripard for (i = 0; i < HVS_NUM_CHANNELS; i++) { 6799ec03d7fSMaxime Ripard state->fifo_state[i].in_use = old_state->fifo_state[i].in_use; 68016e10105SMaxime Ripard state->fifo_state[i].fifo_load = old_state->fifo_state[i].fifo_load; 6819ec03d7fSMaxime Ripard } 6829ec03d7fSMaxime Ripard 68316e10105SMaxime Ripard state->core_clock_rate = old_state->core_clock_rate; 68416e10105SMaxime Ripard 685f2df84e0SMaxime Ripard return &state->base; 686f2df84e0SMaxime Ripard } 687f2df84e0SMaxime Ripard 688f2df84e0SMaxime Ripard static void vc4_hvs_channels_destroy_state(struct drm_private_obj *obj, 689f2df84e0SMaxime Ripard struct drm_private_state *state) 690f2df84e0SMaxime Ripard { 691f2df84e0SMaxime Ripard struct vc4_hvs_state *hvs_state = to_vc4_hvs_state(state); 6929ec03d7fSMaxime Ripard unsigned int i; 6939ec03d7fSMaxime Ripard 6949ec03d7fSMaxime Ripard for (i = 0; i < HVS_NUM_CHANNELS; i++) { 6959ec03d7fSMaxime Ripard if (!hvs_state->fifo_state[i].pending_commit) 6969ec03d7fSMaxime Ripard continue; 6979ec03d7fSMaxime Ripard 6989ec03d7fSMaxime Ripard drm_crtc_commit_put(hvs_state->fifo_state[i].pending_commit); 6999ec03d7fSMaxime Ripard } 700f2df84e0SMaxime Ripard 701f2df84e0SMaxime Ripard kfree(hvs_state); 702f2df84e0SMaxime Ripard } 703f2df84e0SMaxime Ripard 704*66bfe59dSMaxime Ripard static void vc4_hvs_channels_print_state(struct drm_printer *p, 705*66bfe59dSMaxime Ripard const struct drm_private_state *state) 706*66bfe59dSMaxime Ripard { 707*66bfe59dSMaxime Ripard struct vc4_hvs_state *hvs_state = to_vc4_hvs_state(state); 708*66bfe59dSMaxime Ripard unsigned int i; 709*66bfe59dSMaxime Ripard 710*66bfe59dSMaxime Ripard drm_printf(p, "HVS State\n"); 711*66bfe59dSMaxime Ripard drm_printf(p, "\tCore Clock Rate: %lu\n", hvs_state->core_clock_rate); 712*66bfe59dSMaxime Ripard 713*66bfe59dSMaxime Ripard for (i = 0; i < HVS_NUM_CHANNELS; i++) { 714*66bfe59dSMaxime Ripard drm_printf(p, "\tChannel %d\n", i); 715*66bfe59dSMaxime Ripard drm_printf(p, "\t\tin use=%d\n", hvs_state->fifo_state[i].in_use); 716*66bfe59dSMaxime Ripard drm_printf(p, "\t\tload=%lu\n", hvs_state->fifo_state[i].fifo_load); 717*66bfe59dSMaxime Ripard } 718*66bfe59dSMaxime Ripard } 719*66bfe59dSMaxime Ripard 720f2df84e0SMaxime Ripard static const struct drm_private_state_funcs vc4_hvs_state_funcs = { 721f2df84e0SMaxime Ripard .atomic_duplicate_state = vc4_hvs_channels_duplicate_state, 722f2df84e0SMaxime Ripard .atomic_destroy_state = vc4_hvs_channels_destroy_state, 723*66bfe59dSMaxime Ripard .atomic_print_state = vc4_hvs_channels_print_state, 724f2df84e0SMaxime Ripard }; 725f2df84e0SMaxime Ripard 726f2df84e0SMaxime Ripard static void vc4_hvs_channels_obj_fini(struct drm_device *dev, void *unused) 727f2df84e0SMaxime Ripard { 728f2df84e0SMaxime Ripard struct vc4_dev *vc4 = to_vc4_dev(dev); 729f2df84e0SMaxime Ripard 730f2df84e0SMaxime Ripard drm_atomic_private_obj_fini(&vc4->hvs_channels); 731f2df84e0SMaxime Ripard } 732f2df84e0SMaxime Ripard 733f2df84e0SMaxime Ripard static int vc4_hvs_channels_obj_init(struct vc4_dev *vc4) 734f2df84e0SMaxime Ripard { 735f2df84e0SMaxime Ripard struct vc4_hvs_state *state; 736f2df84e0SMaxime Ripard 737f2df84e0SMaxime Ripard state = kzalloc(sizeof(*state), GFP_KERNEL); 738f2df84e0SMaxime Ripard if (!state) 739f2df84e0SMaxime Ripard return -ENOMEM; 740f2df84e0SMaxime Ripard 741f2df84e0SMaxime Ripard drm_atomic_private_obj_init(&vc4->base, &vc4->hvs_channels, 742f2df84e0SMaxime Ripard &state->base, 743f2df84e0SMaxime Ripard &vc4_hvs_state_funcs); 744f2df84e0SMaxime Ripard 745f2df84e0SMaxime Ripard return drmm_add_action_or_reset(&vc4->base, vc4_hvs_channels_obj_fini, NULL); 746f2df84e0SMaxime Ripard } 747f2df84e0SMaxime Ripard 748b5dbc4d3SMaxime Ripard /* 749b5dbc4d3SMaxime Ripard * The BCM2711 HVS has up to 7 outputs connected to the pixelvalves and 750b5dbc4d3SMaxime Ripard * the TXP (and therefore all the CRTCs found on that platform). 751b5dbc4d3SMaxime Ripard * 752b5dbc4d3SMaxime Ripard * The naive (and our initial) implementation would just iterate over 753b5dbc4d3SMaxime Ripard * all the active CRTCs, try to find a suitable FIFO, and then remove it 754b5dbc4d3SMaxime Ripard * from the pool of available FIFOs. However, there are a few corner 755b5dbc4d3SMaxime Ripard * cases that need to be considered: 756b5dbc4d3SMaxime Ripard * 757b5dbc4d3SMaxime Ripard * - When running in a dual-display setup (so with two CRTCs involved), 758b5dbc4d3SMaxime Ripard * we can update the state of a single CRTC (for example by changing 759b5dbc4d3SMaxime Ripard * its mode using xrandr under X11) without affecting the other. In 760b5dbc4d3SMaxime Ripard * this case, the other CRTC wouldn't be in the state at all, so we 761b5dbc4d3SMaxime Ripard * need to consider all the running CRTCs in the DRM device to assign 762b5dbc4d3SMaxime Ripard * a FIFO, not just the one in the state. 763b5dbc4d3SMaxime Ripard * 764f2df84e0SMaxime Ripard * - To fix the above, we can't use drm_atomic_get_crtc_state on all 765f2df84e0SMaxime Ripard * enabled CRTCs to pull their CRTC state into the global state, since 766f2df84e0SMaxime Ripard * a page flip would start considering their vblank to complete. Since 767f2df84e0SMaxime Ripard * we don't have a guarantee that they are actually active, that 768f2df84e0SMaxime Ripard * vblank might never happen, and shouldn't even be considered if we 769f2df84e0SMaxime Ripard * want to do a page flip on a single CRTC. That can be tested by 770f2df84e0SMaxime Ripard * doing a modetest -v first on HDMI1 and then on HDMI0. 771f2df84e0SMaxime Ripard * 772b5dbc4d3SMaxime Ripard * - Since we need the pixelvalve to be disabled and enabled back when 773b5dbc4d3SMaxime Ripard * the FIFO is changed, we should keep the FIFO assigned for as long 774b5dbc4d3SMaxime Ripard * as the CRTC is enabled, only considering it free again once that 775b5dbc4d3SMaxime Ripard * CRTC has been disabled. This can be tested by booting X11 on a 776b5dbc4d3SMaxime Ripard * single display, and changing the resolution down and then back up. 777b5dbc4d3SMaxime Ripard */ 778a72b0458SMaxime Ripard static int vc4_pv_muxing_atomic_check(struct drm_device *dev, 779a72b0458SMaxime Ripard struct drm_atomic_state *state) 780766cc6b1SStefan Schake { 781f2df84e0SMaxime Ripard struct vc4_hvs_state *hvs_new_state; 7828ba0b6d1SMaxime Ripard struct drm_crtc_state *old_crtc_state, *new_crtc_state; 78387ebcd42SMaxime Ripard struct drm_crtc *crtc; 78403b03efeSMaxime Ripard unsigned int unassigned_channels = 0; 785a72b0458SMaxime Ripard unsigned int i; 78687ebcd42SMaxime Ripard 787f2df84e0SMaxime Ripard hvs_new_state = vc4_hvs_get_global_state(state); 788f9277679SMaxime Ripard if (IS_ERR(hvs_new_state)) 789f9277679SMaxime Ripard return PTR_ERR(hvs_new_state); 790089d8341SMaxime Ripard 79103b03efeSMaxime Ripard for (i = 0; i < ARRAY_SIZE(hvs_new_state->fifo_state); i++) 79203b03efeSMaxime Ripard if (!hvs_new_state->fifo_state[i].in_use) 79303b03efeSMaxime Ripard unassigned_channels |= BIT(i); 79403b03efeSMaxime Ripard 7958ba0b6d1SMaxime Ripard for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) { 796f2df84e0SMaxime Ripard struct vc4_crtc_state *old_vc4_crtc_state = 797f2df84e0SMaxime Ripard to_vc4_crtc_state(old_crtc_state); 7988ba0b6d1SMaxime Ripard struct vc4_crtc_state *new_vc4_crtc_state = 7998ba0b6d1SMaxime Ripard to_vc4_crtc_state(new_crtc_state); 80087ebcd42SMaxime Ripard struct vc4_crtc *vc4_crtc = to_vc4_crtc(crtc); 80187ebcd42SMaxime Ripard unsigned int matching_channels; 802d62a8ed7SMaxime Ripard unsigned int channel; 80387ebcd42SMaxime Ripard 8042820526dSMaxime Ripard /* Nothing to do here, let's skip it */ 8052820526dSMaxime Ripard if (old_crtc_state->enable == new_crtc_state->enable) 8062820526dSMaxime Ripard continue; 8072820526dSMaxime Ripard 8082820526dSMaxime Ripard /* Muxing will need to be modified, mark it as such */ 8092820526dSMaxime Ripard new_vc4_crtc_state->update_muxing = true; 8102820526dSMaxime Ripard 8112820526dSMaxime Ripard /* If we're disabling our CRTC, we put back our channel */ 8122820526dSMaxime Ripard if (!new_crtc_state->enable) { 8139ec03d7fSMaxime Ripard channel = old_vc4_crtc_state->assigned_channel; 8149ec03d7fSMaxime Ripard hvs_new_state->fifo_state[channel].in_use = false; 8158ba0b6d1SMaxime Ripard new_vc4_crtc_state->assigned_channel = VC4_HVS_CHANNEL_DISABLED; 8162820526dSMaxime Ripard continue; 817f2df84e0SMaxime Ripard } 8188ba0b6d1SMaxime Ripard 81987ebcd42SMaxime Ripard /* 82087ebcd42SMaxime Ripard * The problem we have to solve here is that we have 82187ebcd42SMaxime Ripard * up to 7 encoders, connected to up to 6 CRTCs. 82287ebcd42SMaxime Ripard * 82387ebcd42SMaxime Ripard * Those CRTCs, depending on the instance, can be 82487ebcd42SMaxime Ripard * routed to 1, 2 or 3 HVS FIFOs, and we need to set 82587ebcd42SMaxime Ripard * the change the muxing between FIFOs and outputs in 82687ebcd42SMaxime Ripard * the HVS accordingly. 82787ebcd42SMaxime Ripard * 82887ebcd42SMaxime Ripard * It would be pretty hard to come up with an 82987ebcd42SMaxime Ripard * algorithm that would generically solve 83087ebcd42SMaxime Ripard * this. However, the current routing trees we support 83187ebcd42SMaxime Ripard * allow us to simplify a bit the problem. 83287ebcd42SMaxime Ripard * 83387ebcd42SMaxime Ripard * Indeed, with the current supported layouts, if we 83487ebcd42SMaxime Ripard * try to assign in the ascending crtc index order the 83587ebcd42SMaxime Ripard * FIFOs, we can't fall into the situation where an 83687ebcd42SMaxime Ripard * earlier CRTC that had multiple routes is assigned 83787ebcd42SMaxime Ripard * one that was the only option for a later CRTC. 83887ebcd42SMaxime Ripard * 83987ebcd42SMaxime Ripard * If the layout changes and doesn't give us that in 84087ebcd42SMaxime Ripard * the future, we will need to have something smarter, 84187ebcd42SMaxime Ripard * but it works so far. 84287ebcd42SMaxime Ripard */ 84303b03efeSMaxime Ripard matching_channels = unassigned_channels & vc4_crtc->data->hvs_available_channels; 844d62a8ed7SMaxime Ripard if (!matching_channels) 845d62a8ed7SMaxime Ripard return -EINVAL; 84687ebcd42SMaxime Ripard 847d62a8ed7SMaxime Ripard channel = ffs(matching_channels) - 1; 8488ba0b6d1SMaxime Ripard new_vc4_crtc_state->assigned_channel = channel; 84903b03efeSMaxime Ripard unassigned_channels &= ~BIT(channel); 8509ec03d7fSMaxime Ripard hvs_new_state->fifo_state[channel].in_use = true; 85187ebcd42SMaxime Ripard } 852766cc6b1SStefan Schake 853a72b0458SMaxime Ripard return 0; 854a72b0458SMaxime Ripard } 855a72b0458SMaxime Ripard 856a72b0458SMaxime Ripard static int 85716e10105SMaxime Ripard vc4_core_clock_atomic_check(struct drm_atomic_state *state) 85816e10105SMaxime Ripard { 85916e10105SMaxime Ripard struct vc4_dev *vc4 = to_vc4_dev(state->dev); 86016e10105SMaxime Ripard struct drm_private_state *priv_state; 86116e10105SMaxime Ripard struct vc4_hvs_state *hvs_new_state; 86216e10105SMaxime Ripard struct vc4_load_tracker_state *load_state; 86316e10105SMaxime Ripard struct drm_crtc_state *old_crtc_state, *new_crtc_state; 86416e10105SMaxime Ripard struct drm_crtc *crtc; 86516e10105SMaxime Ripard unsigned int num_outputs; 86616e10105SMaxime Ripard unsigned long pixel_rate; 86716e10105SMaxime Ripard unsigned long cob_rate; 86816e10105SMaxime Ripard unsigned int i; 86916e10105SMaxime Ripard 87016e10105SMaxime Ripard priv_state = drm_atomic_get_private_obj_state(state, 87116e10105SMaxime Ripard &vc4->load_tracker); 87216e10105SMaxime Ripard if (IS_ERR(priv_state)) 87316e10105SMaxime Ripard return PTR_ERR(priv_state); 87416e10105SMaxime Ripard 87516e10105SMaxime Ripard load_state = to_vc4_load_tracker_state(priv_state); 87616e10105SMaxime Ripard 87716e10105SMaxime Ripard hvs_new_state = vc4_hvs_get_global_state(state); 87899b03ca6SDaniel Vetter if (IS_ERR(hvs_new_state)) 87999b03ca6SDaniel Vetter return PTR_ERR(hvs_new_state); 88016e10105SMaxime Ripard 88116e10105SMaxime Ripard for_each_oldnew_crtc_in_state(state, crtc, 88216e10105SMaxime Ripard old_crtc_state, 88316e10105SMaxime Ripard new_crtc_state, 88416e10105SMaxime Ripard i) { 88516e10105SMaxime Ripard if (old_crtc_state->active) { 88616e10105SMaxime Ripard struct vc4_crtc_state *old_vc4_state = 88716e10105SMaxime Ripard to_vc4_crtc_state(old_crtc_state); 88816e10105SMaxime Ripard unsigned int channel = old_vc4_state->assigned_channel; 88916e10105SMaxime Ripard 89016e10105SMaxime Ripard hvs_new_state->fifo_state[channel].fifo_load = 0; 89116e10105SMaxime Ripard } 89216e10105SMaxime Ripard 89316e10105SMaxime Ripard if (new_crtc_state->active) { 89416e10105SMaxime Ripard struct vc4_crtc_state *new_vc4_state = 89516e10105SMaxime Ripard to_vc4_crtc_state(new_crtc_state); 89616e10105SMaxime Ripard unsigned int channel = new_vc4_state->assigned_channel; 89716e10105SMaxime Ripard 89816e10105SMaxime Ripard hvs_new_state->fifo_state[channel].fifo_load = 89916e10105SMaxime Ripard new_vc4_state->hvs_load; 90016e10105SMaxime Ripard } 90116e10105SMaxime Ripard } 90216e10105SMaxime Ripard 90316e10105SMaxime Ripard cob_rate = 0; 90416e10105SMaxime Ripard num_outputs = 0; 90516e10105SMaxime Ripard for (i = 0; i < HVS_NUM_CHANNELS; i++) { 90616e10105SMaxime Ripard if (!hvs_new_state->fifo_state[i].in_use) 90716e10105SMaxime Ripard continue; 90816e10105SMaxime Ripard 90916e10105SMaxime Ripard num_outputs++; 91016e10105SMaxime Ripard cob_rate += hvs_new_state->fifo_state[i].fifo_load; 91116e10105SMaxime Ripard } 91216e10105SMaxime Ripard 91316e10105SMaxime Ripard pixel_rate = load_state->hvs_load; 91416e10105SMaxime Ripard if (num_outputs > 1) { 91516e10105SMaxime Ripard pixel_rate = (pixel_rate * 40) / 100; 91616e10105SMaxime Ripard } else { 91716e10105SMaxime Ripard pixel_rate = (pixel_rate * 60) / 100; 91816e10105SMaxime Ripard } 91916e10105SMaxime Ripard 92016e10105SMaxime Ripard hvs_new_state->core_clock_rate = max(cob_rate, pixel_rate); 92116e10105SMaxime Ripard 92216e10105SMaxime Ripard return 0; 92316e10105SMaxime Ripard } 92416e10105SMaxime Ripard 92516e10105SMaxime Ripard 92616e10105SMaxime Ripard static int 927a72b0458SMaxime Ripard vc4_atomic_check(struct drm_device *dev, struct drm_atomic_state *state) 928a72b0458SMaxime Ripard { 929a72b0458SMaxime Ripard int ret; 930a72b0458SMaxime Ripard 931a72b0458SMaxime Ripard ret = vc4_pv_muxing_atomic_check(dev, state); 932a72b0458SMaxime Ripard if (ret) 933a72b0458SMaxime Ripard return ret; 934a72b0458SMaxime Ripard 935766cc6b1SStefan Schake ret = vc4_ctm_atomic_check(dev, state); 936766cc6b1SStefan Schake if (ret < 0) 937766cc6b1SStefan Schake return ret; 938766cc6b1SStefan Schake 9394686da83SBoris Brezillon ret = drm_atomic_helper_check(dev, state); 9404686da83SBoris Brezillon if (ret) 9414686da83SBoris Brezillon return ret; 9424686da83SBoris Brezillon 94316e10105SMaxime Ripard ret = vc4_load_tracker_atomic_check(state); 94416e10105SMaxime Ripard if (ret) 94516e10105SMaxime Ripard return ret; 94616e10105SMaxime Ripard 94716e10105SMaxime Ripard return vc4_core_clock_atomic_check(state); 948766cc6b1SStefan Schake } 949766cc6b1SStefan Schake 9509ec03d7fSMaxime Ripard static struct drm_mode_config_helper_funcs vc4_mode_config_helpers = { 9519ec03d7fSMaxime Ripard .atomic_commit_setup = vc4_atomic_commit_setup, 952f3c420feSMaxime Ripard .atomic_commit_tail = vc4_atomic_commit_tail, 9539ec03d7fSMaxime Ripard }; 9549ec03d7fSMaxime Ripard 955c8b75bcaSEric Anholt static const struct drm_mode_config_funcs vc4_mode_funcs = { 956766cc6b1SStefan Schake .atomic_check = vc4_atomic_check, 957f3c420feSMaxime Ripard .atomic_commit = drm_atomic_helper_commit, 95883753117SEric Anholt .fb_create = vc4_fb_create, 959c8b75bcaSEric Anholt }; 960c8b75bcaSEric Anholt 961c8b75bcaSEric Anholt int vc4_kms_load(struct drm_device *dev) 962c8b75bcaSEric Anholt { 96348666d56SDerek Foreman struct vc4_dev *vc4 = to_vc4_dev(dev); 964f437bc1eSMaxime Ripard bool is_vc5 = of_device_is_compatible(dev->dev->of_node, 965f437bc1eSMaxime Ripard "brcm,bcm2711-vc5"); 966c8b75bcaSEric Anholt int ret; 967c8b75bcaSEric Anholt 9687f817159SMaxime Ripard /* 9697f817159SMaxime Ripard * The limits enforced by the load tracker aren't relevant for 9707f817159SMaxime Ripard * the BCM2711, but the load tracker computations are used for 9717f817159SMaxime Ripard * the core clock rate calculation. 9727f817159SMaxime Ripard */ 973f437bc1eSMaxime Ripard if (!is_vc5) { 974f437bc1eSMaxime Ripard /* Start with the load tracker enabled. Can be 975f437bc1eSMaxime Ripard * disabled through the debugfs load_tracker file. 9766b5c029dSPaul Kocialkowski */ 9776b5c029dSPaul Kocialkowski vc4->load_tracker_enabled = true; 978f437bc1eSMaxime Ripard } 9796b5c029dSPaul Kocialkowski 9807d2818f5SMario Kleiner /* Set support for vblank irq fast disable, before drm_vblank_init() */ 9817d2818f5SMario Kleiner dev->vblank_disable_immediate = true; 9827d2818f5SMario Kleiner 983c8b75bcaSEric Anholt ret = drm_vblank_init(dev, dev->mode_config.num_crtc); 984c8b75bcaSEric Anholt if (ret < 0) { 985c8b75bcaSEric Anholt dev_err(dev->dev, "failed to initialize vblank\n"); 986c8b75bcaSEric Anholt return ret; 987c8b75bcaSEric Anholt } 988c8b75bcaSEric Anholt 989f437bc1eSMaxime Ripard if (is_vc5) { 990f437bc1eSMaxime Ripard dev->mode_config.max_width = 7680; 991f437bc1eSMaxime Ripard dev->mode_config.max_height = 7680; 992f437bc1eSMaxime Ripard } else { 993c8b75bcaSEric Anholt dev->mode_config.max_width = 2048; 994c8b75bcaSEric Anholt dev->mode_config.max_height = 2048; 995f437bc1eSMaxime Ripard } 996f437bc1eSMaxime Ripard 997c8b75bcaSEric Anholt dev->mode_config.funcs = &vc4_mode_funcs; 9989ec03d7fSMaxime Ripard dev->mode_config.helper_private = &vc4_mode_config_helpers; 999c8b75bcaSEric Anholt dev->mode_config.preferred_depth = 24; 1000b501baccSEric Anholt dev->mode_config.async_page_flip = true; 1001b501baccSEric Anholt 1002dcda7c28SMaxime Ripard ret = vc4_ctm_obj_init(vc4); 1003dcda7c28SMaxime Ripard if (ret) 1004dcda7c28SMaxime Ripard return ret; 1005766cc6b1SStefan Schake 1006dcda7c28SMaxime Ripard ret = vc4_load_tracker_obj_init(vc4); 1007dcda7c28SMaxime Ripard if (ret) 1008dcda7c28SMaxime Ripard return ret; 10094686da83SBoris Brezillon 1010f2df84e0SMaxime Ripard ret = vc4_hvs_channels_obj_init(vc4); 1011f2df84e0SMaxime Ripard if (ret) 1012f2df84e0SMaxime Ripard return ret; 1013f2df84e0SMaxime Ripard 1014c8b75bcaSEric Anholt drm_mode_config_reset(dev); 1015c8b75bcaSEric Anholt 1016c8b75bcaSEric Anholt drm_kms_helper_poll_init(dev); 1017c8b75bcaSEric Anholt 1018c8b75bcaSEric Anholt return 0; 1019c8b75bcaSEric Anholt } 1020