xref: /linux/drivers/gpu/drm/vc4/vc4_kms.c (revision cdd5b5a9761fd66d17586e4f4ba6588c70e640ea)
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>
15e3479398SMaxime Ripard #include <linux/sort.h>
16d7d96c00SMaxime Ripard 
17b7e8e25bSMasahiro Yamada #include <drm/drm_atomic.h>
18b7e8e25bSMasahiro Yamada #include <drm/drm_atomic_helper.h>
19fd6d6d80SSam Ravnborg #include <drm/drm_crtc.h>
20720cf96dSVille Syrjälä #include <drm/drm_fourcc.h>
219762477cSNoralf Trønnes #include <drm/drm_gem_framebuffer_helper.h>
22fcd70cd3SDaniel Vetter #include <drm/drm_probe_helper.h>
23fd6d6d80SSam Ravnborg #include <drm/drm_vblank.h>
24fd6d6d80SSam Ravnborg 
25c8b75bcaSEric Anholt #include "vc4_drv.h"
26766cc6b1SStefan Schake #include "vc4_regs.h"
27766cc6b1SStefan Schake 
28766cc6b1SStefan Schake struct vc4_ctm_state {
29766cc6b1SStefan Schake 	struct drm_private_state base;
30766cc6b1SStefan Schake 	struct drm_color_ctm *ctm;
31766cc6b1SStefan Schake 	int fifo;
32766cc6b1SStefan Schake };
33766cc6b1SStefan Schake 
34*5a46e490SMaxime Ripard #define to_vc4_ctm_state(_state)				\
35*5a46e490SMaxime Ripard 	container_of_const(_state, struct vc4_ctm_state, base)
36766cc6b1SStefan Schake 
374686da83SBoris Brezillon struct vc4_load_tracker_state {
384686da83SBoris Brezillon 	struct drm_private_state base;
394686da83SBoris Brezillon 	u64 hvs_load;
404686da83SBoris Brezillon 	u64 membus_load;
414686da83SBoris Brezillon };
424686da83SBoris Brezillon 
43*5a46e490SMaxime Ripard #define to_vc4_load_tracker_state(_state)				\
44*5a46e490SMaxime Ripard 	container_of_const(_state, struct vc4_load_tracker_state, base)
454686da83SBoris Brezillon 
vc4_get_ctm_state(struct drm_atomic_state * state,struct drm_private_obj * manager)46766cc6b1SStefan Schake static struct vc4_ctm_state *vc4_get_ctm_state(struct drm_atomic_state *state,
47766cc6b1SStefan Schake 					       struct drm_private_obj *manager)
48766cc6b1SStefan Schake {
49766cc6b1SStefan Schake 	struct drm_device *dev = state->dev;
5088e08589SMaxime Ripard 	struct vc4_dev *vc4 = to_vc4_dev(dev);
51766cc6b1SStefan Schake 	struct drm_private_state *priv_state;
52766cc6b1SStefan Schake 	int ret;
53766cc6b1SStefan Schake 
54766cc6b1SStefan Schake 	ret = drm_modeset_lock(&vc4->ctm_state_lock, state->acquire_ctx);
55766cc6b1SStefan Schake 	if (ret)
56766cc6b1SStefan Schake 		return ERR_PTR(ret);
57766cc6b1SStefan Schake 
58766cc6b1SStefan Schake 	priv_state = drm_atomic_get_private_obj_state(state, manager);
59766cc6b1SStefan Schake 	if (IS_ERR(priv_state))
60766cc6b1SStefan Schake 		return ERR_CAST(priv_state);
61766cc6b1SStefan Schake 
62766cc6b1SStefan Schake 	return to_vc4_ctm_state(priv_state);
63766cc6b1SStefan Schake }
64766cc6b1SStefan Schake 
65766cc6b1SStefan Schake static struct drm_private_state *
vc4_ctm_duplicate_state(struct drm_private_obj * obj)66766cc6b1SStefan Schake vc4_ctm_duplicate_state(struct drm_private_obj *obj)
67766cc6b1SStefan Schake {
68766cc6b1SStefan Schake 	struct vc4_ctm_state *state;
69766cc6b1SStefan Schake 
70766cc6b1SStefan Schake 	state = kmemdup(obj->state, sizeof(*state), GFP_KERNEL);
71766cc6b1SStefan Schake 	if (!state)
72766cc6b1SStefan Schake 		return NULL;
73766cc6b1SStefan Schake 
74766cc6b1SStefan Schake 	__drm_atomic_helper_private_obj_duplicate_state(obj, &state->base);
75766cc6b1SStefan Schake 
76766cc6b1SStefan Schake 	return &state->base;
77766cc6b1SStefan Schake }
78766cc6b1SStefan Schake 
vc4_ctm_destroy_state(struct drm_private_obj * obj,struct drm_private_state * state)79766cc6b1SStefan Schake static void vc4_ctm_destroy_state(struct drm_private_obj *obj,
80766cc6b1SStefan Schake 				  struct drm_private_state *state)
81766cc6b1SStefan Schake {
82766cc6b1SStefan Schake 	struct vc4_ctm_state *ctm_state = to_vc4_ctm_state(state);
83766cc6b1SStefan Schake 
84766cc6b1SStefan Schake 	kfree(ctm_state);
85766cc6b1SStefan Schake }
86766cc6b1SStefan Schake 
87766cc6b1SStefan Schake static const struct drm_private_state_funcs vc4_ctm_state_funcs = {
88766cc6b1SStefan Schake 	.atomic_duplicate_state = vc4_ctm_duplicate_state,
89766cc6b1SStefan Schake 	.atomic_destroy_state = vc4_ctm_destroy_state,
90766cc6b1SStefan Schake };
91766cc6b1SStefan Schake 
vc4_ctm_obj_fini(struct drm_device * dev,void * unused)92dcda7c28SMaxime Ripard static void vc4_ctm_obj_fini(struct drm_device *dev, void *unused)
93dcda7c28SMaxime Ripard {
94dcda7c28SMaxime Ripard 	struct vc4_dev *vc4 = to_vc4_dev(dev);
95dcda7c28SMaxime Ripard 
96dcda7c28SMaxime Ripard 	drm_atomic_private_obj_fini(&vc4->ctm_manager);
97dcda7c28SMaxime Ripard }
98dcda7c28SMaxime Ripard 
vc4_ctm_obj_init(struct vc4_dev * vc4)99dcda7c28SMaxime Ripard static int vc4_ctm_obj_init(struct vc4_dev *vc4)
100dcda7c28SMaxime Ripard {
101dcda7c28SMaxime Ripard 	struct vc4_ctm_state *ctm_state;
102dcda7c28SMaxime Ripard 
103dcda7c28SMaxime Ripard 	drm_modeset_lock_init(&vc4->ctm_state_lock);
104dcda7c28SMaxime Ripard 
105dcda7c28SMaxime Ripard 	ctm_state = kzalloc(sizeof(*ctm_state), GFP_KERNEL);
106dcda7c28SMaxime Ripard 	if (!ctm_state)
107dcda7c28SMaxime Ripard 		return -ENOMEM;
108dcda7c28SMaxime Ripard 
109dcda7c28SMaxime Ripard 	drm_atomic_private_obj_init(&vc4->base, &vc4->ctm_manager, &ctm_state->base,
110dcda7c28SMaxime Ripard 				    &vc4_ctm_state_funcs);
111dcda7c28SMaxime Ripard 
1123c354ed1SMaxime Ripard 	return drmm_add_action_or_reset(&vc4->base, vc4_ctm_obj_fini, NULL);
113dcda7c28SMaxime Ripard }
114dcda7c28SMaxime Ripard 
115766cc6b1SStefan Schake /* Converts a DRM S31.32 value to the HW S0.9 format. */
vc4_ctm_s31_32_to_s0_9(u64 in)116766cc6b1SStefan Schake static u16 vc4_ctm_s31_32_to_s0_9(u64 in)
117766cc6b1SStefan Schake {
118766cc6b1SStefan Schake 	u16 r;
119766cc6b1SStefan Schake 
120766cc6b1SStefan Schake 	/* Sign bit. */
121766cc6b1SStefan Schake 	r = in & BIT_ULL(63) ? BIT(9) : 0;
122766cc6b1SStefan Schake 
123766cc6b1SStefan Schake 	if ((in & GENMASK_ULL(62, 32)) > 0) {
124766cc6b1SStefan Schake 		/* We have zero integer bits so we can only saturate here. */
125766cc6b1SStefan Schake 		r |= GENMASK(8, 0);
126766cc6b1SStefan Schake 	} else {
127766cc6b1SStefan Schake 		/* Otherwise take the 9 most important fractional bits. */
128766cc6b1SStefan Schake 		r |= (in >> 23) & GENMASK(8, 0);
129766cc6b1SStefan Schake 	}
130766cc6b1SStefan Schake 
131766cc6b1SStefan Schake 	return r;
132766cc6b1SStefan Schake }
133766cc6b1SStefan Schake 
134766cc6b1SStefan Schake static void
vc4_ctm_commit(struct vc4_dev * vc4,struct drm_atomic_state * state)135766cc6b1SStefan Schake vc4_ctm_commit(struct vc4_dev *vc4, struct drm_atomic_state *state)
136766cc6b1SStefan Schake {
1373454f01aSMaxime Ripard 	struct vc4_hvs *hvs = vc4->hvs;
138766cc6b1SStefan Schake 	struct vc4_ctm_state *ctm_state = to_vc4_ctm_state(vc4->ctm_manager.state);
139766cc6b1SStefan Schake 	struct drm_color_ctm *ctm = ctm_state->ctm;
140766cc6b1SStefan Schake 
141766cc6b1SStefan Schake 	if (ctm_state->fifo) {
142766cc6b1SStefan Schake 		HVS_WRITE(SCALER_OLEDCOEF2,
143766cc6b1SStefan Schake 			  VC4_SET_FIELD(vc4_ctm_s31_32_to_s0_9(ctm->matrix[0]),
144766cc6b1SStefan Schake 					SCALER_OLEDCOEF2_R_TO_R) |
145766cc6b1SStefan Schake 			  VC4_SET_FIELD(vc4_ctm_s31_32_to_s0_9(ctm->matrix[3]),
146766cc6b1SStefan Schake 					SCALER_OLEDCOEF2_R_TO_G) |
147766cc6b1SStefan Schake 			  VC4_SET_FIELD(vc4_ctm_s31_32_to_s0_9(ctm->matrix[6]),
148766cc6b1SStefan Schake 					SCALER_OLEDCOEF2_R_TO_B));
149766cc6b1SStefan Schake 		HVS_WRITE(SCALER_OLEDCOEF1,
150766cc6b1SStefan Schake 			  VC4_SET_FIELD(vc4_ctm_s31_32_to_s0_9(ctm->matrix[1]),
151766cc6b1SStefan Schake 					SCALER_OLEDCOEF1_G_TO_R) |
152766cc6b1SStefan Schake 			  VC4_SET_FIELD(vc4_ctm_s31_32_to_s0_9(ctm->matrix[4]),
153766cc6b1SStefan Schake 					SCALER_OLEDCOEF1_G_TO_G) |
154766cc6b1SStefan Schake 			  VC4_SET_FIELD(vc4_ctm_s31_32_to_s0_9(ctm->matrix[7]),
155766cc6b1SStefan Schake 					SCALER_OLEDCOEF1_G_TO_B));
156766cc6b1SStefan Schake 		HVS_WRITE(SCALER_OLEDCOEF0,
157766cc6b1SStefan Schake 			  VC4_SET_FIELD(vc4_ctm_s31_32_to_s0_9(ctm->matrix[2]),
158766cc6b1SStefan Schake 					SCALER_OLEDCOEF0_B_TO_R) |
159766cc6b1SStefan Schake 			  VC4_SET_FIELD(vc4_ctm_s31_32_to_s0_9(ctm->matrix[5]),
160766cc6b1SStefan Schake 					SCALER_OLEDCOEF0_B_TO_G) |
161766cc6b1SStefan Schake 			  VC4_SET_FIELD(vc4_ctm_s31_32_to_s0_9(ctm->matrix[8]),
162766cc6b1SStefan Schake 					SCALER_OLEDCOEF0_B_TO_B));
163766cc6b1SStefan Schake 	}
164766cc6b1SStefan Schake 
165766cc6b1SStefan Schake 	HVS_WRITE(SCALER_OLEDOFFS,
166766cc6b1SStefan Schake 		  VC4_SET_FIELD(ctm_state->fifo, SCALER_OLEDOFFS_DISPFIFO));
167766cc6b1SStefan Schake }
168c8b75bcaSEric Anholt 
1693c5cb5ecSMaxime Ripard struct vc4_hvs_state *
vc4_hvs_get_new_global_state(const struct drm_atomic_state * state)170e818ee68SMaxime Ripard vc4_hvs_get_new_global_state(const struct drm_atomic_state *state)
1719ec03d7fSMaxime Ripard {
1729ec03d7fSMaxime Ripard 	struct vc4_dev *vc4 = to_vc4_dev(state->dev);
1739ec03d7fSMaxime Ripard 	struct drm_private_state *priv_state;
1749ec03d7fSMaxime Ripard 
1759ec03d7fSMaxime Ripard 	priv_state = drm_atomic_get_new_private_obj_state(state, &vc4->hvs_channels);
176dba9e346SGaosheng Cui 	if (!priv_state)
177dba9e346SGaosheng Cui 		return ERR_PTR(-EINVAL);
1789ec03d7fSMaxime Ripard 
1799ec03d7fSMaxime Ripard 	return to_vc4_hvs_state(priv_state);
1809ec03d7fSMaxime Ripard }
1819ec03d7fSMaxime Ripard 
1823c5cb5ecSMaxime Ripard struct vc4_hvs_state *
vc4_hvs_get_old_global_state(const struct drm_atomic_state * state)183e818ee68SMaxime Ripard vc4_hvs_get_old_global_state(const struct drm_atomic_state *state)
1849ec03d7fSMaxime Ripard {
1859ec03d7fSMaxime Ripard 	struct vc4_dev *vc4 = to_vc4_dev(state->dev);
1869ec03d7fSMaxime Ripard 	struct drm_private_state *priv_state;
1879ec03d7fSMaxime Ripard 
1889ec03d7fSMaxime Ripard 	priv_state = drm_atomic_get_old_private_obj_state(state, &vc4->hvs_channels);
189dba9e346SGaosheng Cui 	if (!priv_state)
190dba9e346SGaosheng Cui 		return ERR_PTR(-EINVAL);
1919ec03d7fSMaxime Ripard 
1929ec03d7fSMaxime Ripard 	return to_vc4_hvs_state(priv_state);
1939ec03d7fSMaxime Ripard }
1949ec03d7fSMaxime Ripard 
1953c5cb5ecSMaxime Ripard struct vc4_hvs_state *
vc4_hvs_get_global_state(struct drm_atomic_state * state)196f2df84e0SMaxime Ripard vc4_hvs_get_global_state(struct drm_atomic_state *state)
197f2df84e0SMaxime Ripard {
198f2df84e0SMaxime Ripard 	struct vc4_dev *vc4 = to_vc4_dev(state->dev);
199f2df84e0SMaxime Ripard 	struct drm_private_state *priv_state;
200f2df84e0SMaxime Ripard 
201f2df84e0SMaxime Ripard 	priv_state = drm_atomic_get_private_obj_state(state, &vc4->hvs_channels);
202f2df84e0SMaxime Ripard 	if (IS_ERR(priv_state))
203f2df84e0SMaxime Ripard 		return ERR_CAST(priv_state);
204f2df84e0SMaxime Ripard 
205f2df84e0SMaxime Ripard 	return to_vc4_hvs_state(priv_state);
206f2df84e0SMaxime Ripard }
207f2df84e0SMaxime Ripard 
vc4_hvs_pv_muxing_commit(struct vc4_dev * vc4,struct drm_atomic_state * state)20887ebcd42SMaxime Ripard static void vc4_hvs_pv_muxing_commit(struct vc4_dev *vc4,
20987ebcd42SMaxime Ripard 				     struct drm_atomic_state *state)
21087ebcd42SMaxime Ripard {
2113454f01aSMaxime Ripard 	struct vc4_hvs *hvs = vc4->hvs;
21287ebcd42SMaxime Ripard 	struct drm_crtc_state *crtc_state;
21387ebcd42SMaxime Ripard 	struct drm_crtc *crtc;
21487ebcd42SMaxime Ripard 	unsigned int i;
21587ebcd42SMaxime Ripard 
21687ebcd42SMaxime Ripard 	for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
217a16c6640SMaxime Ripard 		struct vc4_crtc *vc4_crtc = to_vc4_crtc(crtc);
21887ebcd42SMaxime Ripard 		struct vc4_crtc_state *vc4_state = to_vc4_crtc_state(crtc_state);
21987ebcd42SMaxime Ripard 		u32 dispctrl;
22087ebcd42SMaxime Ripard 		u32 dsp3_mux;
22187ebcd42SMaxime Ripard 
22287ebcd42SMaxime Ripard 		if (!crtc_state->active)
22387ebcd42SMaxime Ripard 			continue;
22487ebcd42SMaxime Ripard 
22587ebcd42SMaxime Ripard 		if (vc4_state->assigned_channel != 2)
22687ebcd42SMaxime Ripard 			continue;
22787ebcd42SMaxime Ripard 
22887ebcd42SMaxime Ripard 		/*
22987ebcd42SMaxime Ripard 		 * SCALER_DISPCTRL_DSP3 = X, where X < 2 means 'connect DSP3 to
23087ebcd42SMaxime Ripard 		 * FIFO X'.
23187ebcd42SMaxime Ripard 		 * SCALER_DISPCTRL_DSP3 = 3 means 'disable DSP 3'.
23287ebcd42SMaxime Ripard 		 *
23387ebcd42SMaxime Ripard 		 * DSP3 is connected to FIFO2 unless the transposer is
23487ebcd42SMaxime Ripard 		 * enabled. In this case, FIFO 2 is directly accessed by the
23587ebcd42SMaxime Ripard 		 * TXP IP, and we need to disable the FIFO2 -> pixelvalve1
23687ebcd42SMaxime Ripard 		 * route.
23787ebcd42SMaxime Ripard 		 */
238a16c6640SMaxime Ripard 		if (vc4_crtc->feeds_txp)
23987ebcd42SMaxime Ripard 			dsp3_mux = VC4_SET_FIELD(3, SCALER_DISPCTRL_DSP3_MUX);
24087ebcd42SMaxime Ripard 		else
24187ebcd42SMaxime Ripard 			dsp3_mux = VC4_SET_FIELD(2, SCALER_DISPCTRL_DSP3_MUX);
24287ebcd42SMaxime Ripard 
24387ebcd42SMaxime Ripard 		dispctrl = HVS_READ(SCALER_DISPCTRL) &
24487ebcd42SMaxime Ripard 			   ~SCALER_DISPCTRL_DSP3_MUX_MASK;
24587ebcd42SMaxime Ripard 		HVS_WRITE(SCALER_DISPCTRL, dispctrl | dsp3_mux);
24687ebcd42SMaxime Ripard 	}
24787ebcd42SMaxime Ripard }
24887ebcd42SMaxime Ripard 
vc5_hvs_pv_muxing_commit(struct vc4_dev * vc4,struct drm_atomic_state * state)24987ebcd42SMaxime Ripard static void vc5_hvs_pv_muxing_commit(struct vc4_dev *vc4,
25087ebcd42SMaxime Ripard 				     struct drm_atomic_state *state)
25187ebcd42SMaxime Ripard {
2523454f01aSMaxime Ripard 	struct vc4_hvs *hvs = vc4->hvs;
25387ebcd42SMaxime Ripard 	struct drm_crtc_state *crtc_state;
25487ebcd42SMaxime Ripard 	struct drm_crtc *crtc;
2552820526dSMaxime Ripard 	unsigned char mux;
25687ebcd42SMaxime Ripard 	unsigned int i;
25787ebcd42SMaxime Ripard 	u32 reg;
25887ebcd42SMaxime Ripard 
25987ebcd42SMaxime Ripard 	for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
26087ebcd42SMaxime Ripard 		struct vc4_crtc_state *vc4_state = to_vc4_crtc_state(crtc_state);
26187ebcd42SMaxime Ripard 		struct vc4_crtc *vc4_crtc = to_vc4_crtc(crtc);
262f47d37a9SMaxime Ripard 		unsigned int channel = vc4_state->assigned_channel;
26387ebcd42SMaxime Ripard 
2642820526dSMaxime Ripard 		if (!vc4_state->update_muxing)
26587ebcd42SMaxime Ripard 			continue;
26687ebcd42SMaxime Ripard 
26787ebcd42SMaxime Ripard 		switch (vc4_crtc->data->hvs_output) {
26887ebcd42SMaxime Ripard 		case 2:
269457e5184SMaxime Ripard 			drm_WARN_ON(&vc4->base,
270457e5184SMaxime Ripard 				    VC4_GET_FIELD(HVS_READ(SCALER_DISPCTRL),
271457e5184SMaxime Ripard 						  SCALER_DISPCTRL_DSP3_MUX) == channel);
272457e5184SMaxime Ripard 
273f47d37a9SMaxime Ripard 			mux = (channel == 2) ? 0 : 1;
2742820526dSMaxime Ripard 			reg = HVS_READ(SCALER_DISPECTRL);
2752820526dSMaxime Ripard 			HVS_WRITE(SCALER_DISPECTRL,
2762820526dSMaxime Ripard 				  (reg & ~SCALER_DISPECTRL_DSP2_MUX_MASK) |
2772820526dSMaxime Ripard 				  VC4_SET_FIELD(mux, SCALER_DISPECTRL_DSP2_MUX));
27887ebcd42SMaxime Ripard 			break;
27987ebcd42SMaxime Ripard 
28087ebcd42SMaxime Ripard 		case 3:
281f47d37a9SMaxime Ripard 			if (channel == VC4_HVS_CHANNEL_DISABLED)
2822820526dSMaxime Ripard 				mux = 3;
2832820526dSMaxime Ripard 			else
284f47d37a9SMaxime Ripard 				mux = channel;
2852820526dSMaxime Ripard 
2862820526dSMaxime Ripard 			reg = HVS_READ(SCALER_DISPCTRL);
2872820526dSMaxime Ripard 			HVS_WRITE(SCALER_DISPCTRL,
2882820526dSMaxime Ripard 				  (reg & ~SCALER_DISPCTRL_DSP3_MUX_MASK) |
2892820526dSMaxime Ripard 				  VC4_SET_FIELD(mux, SCALER_DISPCTRL_DSP3_MUX));
29087ebcd42SMaxime Ripard 			break;
29187ebcd42SMaxime Ripard 
29287ebcd42SMaxime Ripard 		case 4:
293f47d37a9SMaxime Ripard 			if (channel == VC4_HVS_CHANNEL_DISABLED)
2942820526dSMaxime Ripard 				mux = 3;
2952820526dSMaxime Ripard 			else
296f47d37a9SMaxime Ripard 				mux = channel;
2972820526dSMaxime Ripard 
2982820526dSMaxime Ripard 			reg = HVS_READ(SCALER_DISPEOLN);
2992820526dSMaxime Ripard 			HVS_WRITE(SCALER_DISPEOLN,
3002820526dSMaxime Ripard 				  (reg & ~SCALER_DISPEOLN_DSP4_MUX_MASK) |
3012820526dSMaxime Ripard 				  VC4_SET_FIELD(mux, SCALER_DISPEOLN_DSP4_MUX));
3022820526dSMaxime Ripard 
30387ebcd42SMaxime Ripard 			break;
30487ebcd42SMaxime Ripard 
30587ebcd42SMaxime Ripard 		case 5:
306f47d37a9SMaxime Ripard 			if (channel == VC4_HVS_CHANNEL_DISABLED)
3072820526dSMaxime Ripard 				mux = 3;
3082820526dSMaxime Ripard 			else
309f47d37a9SMaxime Ripard 				mux = channel;
3102820526dSMaxime Ripard 
3112820526dSMaxime Ripard 			reg = HVS_READ(SCALER_DISPDITHER);
3122820526dSMaxime Ripard 			HVS_WRITE(SCALER_DISPDITHER,
3132820526dSMaxime Ripard 				  (reg & ~SCALER_DISPDITHER_DSP5_MUX_MASK) |
3142820526dSMaxime Ripard 				  VC4_SET_FIELD(mux, SCALER_DISPDITHER_DSP5_MUX));
31587ebcd42SMaxime Ripard 			break;
31687ebcd42SMaxime Ripard 
31787ebcd42SMaxime Ripard 		default:
31887ebcd42SMaxime Ripard 			break;
31987ebcd42SMaxime Ripard 		}
32087ebcd42SMaxime Ripard 	}
32187ebcd42SMaxime Ripard }
32287ebcd42SMaxime Ripard 
vc4_atomic_commit_tail(struct drm_atomic_state * state)323f3c420feSMaxime Ripard static void vc4_atomic_commit_tail(struct drm_atomic_state *state)
324b501baccSEric Anholt {
325b501baccSEric Anholt 	struct drm_device *dev = state->dev;
326b501baccSEric Anholt 	struct vc4_dev *vc4 = to_vc4_dev(dev);
327d7d96c00SMaxime Ripard 	struct vc4_hvs *hvs = vc4->hvs;
32859635667SMaxime Ripard 	struct drm_crtc_state *new_crtc_state;
32916e10105SMaxime Ripard 	struct vc4_hvs_state *new_hvs_state;
33059635667SMaxime Ripard 	struct drm_crtc *crtc;
3319ec03d7fSMaxime Ripard 	struct vc4_hvs_state *old_hvs_state;
3326052a311SMaxime Ripard 	unsigned int channel;
333531a1b62SBoris Brezillon 	int i;
334531a1b62SBoris Brezillon 
33516e10105SMaxime Ripard 	old_hvs_state = vc4_hvs_get_old_global_state(state);
33699b03ca6SDaniel Vetter 	if (WARN_ON(IS_ERR(old_hvs_state)))
33716e10105SMaxime Ripard 		return;
33816e10105SMaxime Ripard 
33916e10105SMaxime Ripard 	new_hvs_state = vc4_hvs_get_new_global_state(state);
34099b03ca6SDaniel Vetter 	if (WARN_ON(IS_ERR(new_hvs_state)))
34116e10105SMaxime Ripard 		return;
34216e10105SMaxime Ripard 
34359635667SMaxime Ripard 	for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
34487ebcd42SMaxime Ripard 		struct vc4_crtc_state *vc4_crtc_state;
34559635667SMaxime Ripard 
34659635667SMaxime Ripard 		if (!new_crtc_state->commit)
347531a1b62SBoris Brezillon 			continue;
348531a1b62SBoris Brezillon 
34987ebcd42SMaxime Ripard 		vc4_crtc_state = to_vc4_crtc_state(new_crtc_state);
3503454f01aSMaxime Ripard 		vc4_hvs_mask_underrun(hvs, vc4_crtc_state->assigned_channel);
351531a1b62SBoris Brezillon 	}
352b501baccSEric Anholt 
3536052a311SMaxime Ripard 	for (channel = 0; channel < HVS_NUM_CHANNELS; channel++) {
354049cfff8SMaxime Ripard 		struct drm_crtc_commit *commit;
355b99c2c95SMaxime Ripard 		int ret;
3569ec03d7fSMaxime Ripard 
3579ec03d7fSMaxime Ripard 		if (!old_hvs_state->fifo_state[channel].in_use)
3589ec03d7fSMaxime Ripard 			continue;
3599ec03d7fSMaxime Ripard 
360049cfff8SMaxime Ripard 		commit = old_hvs_state->fifo_state[channel].pending_commit;
361049cfff8SMaxime Ripard 		if (!commit)
362049cfff8SMaxime Ripard 			continue;
363049cfff8SMaxime Ripard 
364049cfff8SMaxime Ripard 		ret = drm_crtc_commit_wait(commit);
365b99c2c95SMaxime Ripard 		if (ret)
366b99c2c95SMaxime Ripard 			drm_err(dev, "Timed out waiting for commit\n");
367049cfff8SMaxime Ripard 
368049cfff8SMaxime Ripard 		drm_crtc_commit_put(commit);
369d134c5ffSMaxime Ripard 		old_hvs_state->fifo_state[channel].pending_commit = NULL;
3709ec03d7fSMaxime Ripard 	}
3719ec03d7fSMaxime Ripard 
3721cbc91ebSMaxime Ripard 	if (vc4->is_vc5) {
373748acfc9SMaxime Ripard 		unsigned long state_rate = max(old_hvs_state->core_clock_rate,
374244a36e5SMaxime Ripard 					       new_hvs_state->core_clock_rate);
375fc041428SMaxime Ripard 		unsigned long core_rate = clamp_t(unsigned long, state_rate,
376fc041428SMaxime Ripard 						  500000000, hvs->max_core_rate);
377244a36e5SMaxime Ripard 
3785b6ef06eSMaxime Ripard 		drm_dbg(dev, "Raising the core clock at %lu Hz\n", core_rate);
3795b6ef06eSMaxime Ripard 
3805b6ef06eSMaxime Ripard 		/*
3815b6ef06eSMaxime Ripard 		 * Do a temporary request on the core clock during the
3825b6ef06eSMaxime Ripard 		 * modeset.
3835b6ef06eSMaxime Ripard 		 */
3847d0648c8SMaxime Ripard 		WARN_ON(clk_set_min_rate(hvs->core_clk, core_rate));
385244a36e5SMaxime Ripard 	}
3865b6ef06eSMaxime Ripard 
387b501baccSEric Anholt 	drm_atomic_helper_commit_modeset_disables(dev, state);
388b501baccSEric Anholt 
389766cc6b1SStefan Schake 	vc4_ctm_commit(vc4, state);
390766cc6b1SStefan Schake 
3911cbc91ebSMaxime Ripard 	if (vc4->is_vc5)
39287ebcd42SMaxime Ripard 		vc5_hvs_pv_muxing_commit(vc4, state);
39387ebcd42SMaxime Ripard 	else
39487ebcd42SMaxime Ripard 		vc4_hvs_pv_muxing_commit(vc4, state);
39587ebcd42SMaxime Ripard 
396d65661acSMaxime Ripard 	drm_atomic_helper_commit_planes(dev, state,
397d65661acSMaxime Ripard 					DRM_PLANE_COMMIT_ACTIVE_ONLY);
398b501baccSEric Anholt 
399b501baccSEric Anholt 	drm_atomic_helper_commit_modeset_enables(dev, state);
400b501baccSEric Anholt 
4011ebe99a7SBoris Brezillon 	drm_atomic_helper_fake_vblank(state);
4021ebe99a7SBoris Brezillon 
40334c8ea40SBoris Brezillon 	drm_atomic_helper_commit_hw_done(state);
40434c8ea40SBoris Brezillon 
405184d3cf4SBoris Brezillon 	drm_atomic_helper_wait_for_flip_done(dev, state);
406b501baccSEric Anholt 
407b501baccSEric Anholt 	drm_atomic_helper_cleanup_planes(dev, state);
408b501baccSEric Anholt 
4091cbc91ebSMaxime Ripard 	if (vc4->is_vc5) {
410fc041428SMaxime Ripard 		unsigned long core_rate = min_t(unsigned long,
411fc041428SMaxime Ripard 						hvs->max_core_rate,
41216e10105SMaxime Ripard 						new_hvs_state->core_clock_rate);
41316e10105SMaxime Ripard 
414fc041428SMaxime Ripard 		drm_dbg(dev, "Running the core clock at %lu Hz\n", core_rate);
415fc041428SMaxime Ripard 
4165b6ef06eSMaxime Ripard 		/*
4175b6ef06eSMaxime Ripard 		 * Request a clock rate based on the current HVS
4185b6ef06eSMaxime Ripard 		 * requirements.
4195b6ef06eSMaxime Ripard 		 */
420fc041428SMaxime Ripard 		WARN_ON(clk_set_min_rate(hvs->core_clk, core_rate));
4213870b54eSMaxime Ripard 
4223870b54eSMaxime Ripard 		drm_dbg(dev, "Core clock actual rate: %lu Hz\n",
4233870b54eSMaxime Ripard 			clk_get_rate(hvs->core_clk));
42416e10105SMaxime Ripard 	}
425b501baccSEric Anholt }
426b501baccSEric Anholt 
vc4_atomic_commit_setup(struct drm_atomic_state * state)4279ec03d7fSMaxime Ripard static int vc4_atomic_commit_setup(struct drm_atomic_state *state)
4289ec03d7fSMaxime Ripard {
4299ec03d7fSMaxime Ripard 	struct drm_crtc_state *crtc_state;
4309ec03d7fSMaxime Ripard 	struct vc4_hvs_state *hvs_state;
4319ec03d7fSMaxime Ripard 	struct drm_crtc *crtc;
4329ec03d7fSMaxime Ripard 	unsigned int i;
4339ec03d7fSMaxime Ripard 
4349ec03d7fSMaxime Ripard 	hvs_state = vc4_hvs_get_new_global_state(state);
435f9277679SMaxime Ripard 	if (WARN_ON(IS_ERR(hvs_state)))
436f9277679SMaxime Ripard 		return PTR_ERR(hvs_state);
4379ec03d7fSMaxime Ripard 
4389ec03d7fSMaxime Ripard 	for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
4399ec03d7fSMaxime Ripard 		struct vc4_crtc_state *vc4_crtc_state =
4409ec03d7fSMaxime Ripard 			to_vc4_crtc_state(crtc_state);
4419ec03d7fSMaxime Ripard 		unsigned int channel =
4429ec03d7fSMaxime Ripard 			vc4_crtc_state->assigned_channel;
4439ec03d7fSMaxime Ripard 
4449ec03d7fSMaxime Ripard 		if (channel == VC4_HVS_CHANNEL_DISABLED)
4459ec03d7fSMaxime Ripard 			continue;
4469ec03d7fSMaxime Ripard 
4479ec03d7fSMaxime Ripard 		if (!hvs_state->fifo_state[channel].in_use)
4489ec03d7fSMaxime Ripard 			continue;
4499ec03d7fSMaxime Ripard 
4509ec03d7fSMaxime Ripard 		hvs_state->fifo_state[channel].pending_commit =
4519ec03d7fSMaxime Ripard 			drm_crtc_commit_get(crtc_state->commit);
4529ec03d7fSMaxime Ripard 	}
4539ec03d7fSMaxime Ripard 
4549ec03d7fSMaxime Ripard 	return 0;
4559ec03d7fSMaxime Ripard }
4569ec03d7fSMaxime Ripard 
vc4_fb_create(struct drm_device * dev,struct drm_file * file_priv,const struct drm_mode_fb_cmd2 * mode_cmd)45783753117SEric Anholt static struct drm_framebuffer *vc4_fb_create(struct drm_device *dev,
45883753117SEric Anholt 					     struct drm_file *file_priv,
45983753117SEric Anholt 					     const struct drm_mode_fb_cmd2 *mode_cmd)
46083753117SEric Anholt {
46130f8c74cSMaxime Ripard 	struct vc4_dev *vc4 = to_vc4_dev(dev);
46283753117SEric Anholt 	struct drm_mode_fb_cmd2 mode_cmd_local;
46383753117SEric Anholt 
46430f8c74cSMaxime Ripard 	if (WARN_ON_ONCE(vc4->is_vc5))
46530f8c74cSMaxime Ripard 		return ERR_PTR(-ENODEV);
46630f8c74cSMaxime Ripard 
46783753117SEric Anholt 	/* If the user didn't specify a modifier, use the
46883753117SEric Anholt 	 * vc4_set_tiling_ioctl() state for the BO.
46983753117SEric Anholt 	 */
47083753117SEric Anholt 	if (!(mode_cmd->flags & DRM_MODE_FB_MODIFIERS)) {
47183753117SEric Anholt 		struct drm_gem_object *gem_obj;
47283753117SEric Anholt 		struct vc4_bo *bo;
47383753117SEric Anholt 
47483753117SEric Anholt 		gem_obj = drm_gem_object_lookup(file_priv,
47583753117SEric Anholt 						mode_cmd->handles[0]);
47683753117SEric Anholt 		if (!gem_obj) {
477fb95992aSEric Anholt 			DRM_DEBUG("Failed to look up GEM BO %d\n",
47883753117SEric Anholt 				  mode_cmd->handles[0]);
47983753117SEric Anholt 			return ERR_PTR(-ENOENT);
48083753117SEric Anholt 		}
48183753117SEric Anholt 		bo = to_vc4_bo(gem_obj);
48283753117SEric Anholt 
48383753117SEric Anholt 		mode_cmd_local = *mode_cmd;
48483753117SEric Anholt 
48583753117SEric Anholt 		if (bo->t_format) {
48683753117SEric Anholt 			mode_cmd_local.modifier[0] =
48783753117SEric Anholt 				DRM_FORMAT_MOD_BROADCOM_VC4_T_TILED;
48883753117SEric Anholt 		} else {
48983753117SEric Anholt 			mode_cmd_local.modifier[0] = DRM_FORMAT_MOD_NONE;
49083753117SEric Anholt 		}
49183753117SEric Anholt 
492f7a8cd30SEmil Velikov 		drm_gem_object_put(gem_obj);
49383753117SEric Anholt 
49483753117SEric Anholt 		mode_cmd = &mode_cmd_local;
49583753117SEric Anholt 	}
49683753117SEric Anholt 
4979762477cSNoralf Trønnes 	return drm_gem_fb_create(dev, file_priv, mode_cmd);
49883753117SEric Anholt }
49983753117SEric Anholt 
500766cc6b1SStefan Schake /* Our CTM has some peculiar limitations: we can only enable it for one CRTC
501766cc6b1SStefan Schake  * at a time and the HW only supports S0.9 scalars. To account for the latter,
502766cc6b1SStefan Schake  * we don't allow userland to set a CTM that we have no hope of approximating.
503766cc6b1SStefan Schake  */
504766cc6b1SStefan Schake static int
vc4_ctm_atomic_check(struct drm_device * dev,struct drm_atomic_state * state)505766cc6b1SStefan Schake vc4_ctm_atomic_check(struct drm_device *dev, struct drm_atomic_state *state)
506766cc6b1SStefan Schake {
507766cc6b1SStefan Schake 	struct vc4_dev *vc4 = to_vc4_dev(dev);
508766cc6b1SStefan Schake 	struct vc4_ctm_state *ctm_state = NULL;
509766cc6b1SStefan Schake 	struct drm_crtc *crtc;
510766cc6b1SStefan Schake 	struct drm_crtc_state *old_crtc_state, *new_crtc_state;
511766cc6b1SStefan Schake 	struct drm_color_ctm *ctm;
512766cc6b1SStefan Schake 	int i;
513766cc6b1SStefan Schake 
514766cc6b1SStefan Schake 	for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
515766cc6b1SStefan Schake 		/* CTM is being disabled. */
516766cc6b1SStefan Schake 		if (!new_crtc_state->ctm && old_crtc_state->ctm) {
517766cc6b1SStefan Schake 			ctm_state = vc4_get_ctm_state(state, &vc4->ctm_manager);
518766cc6b1SStefan Schake 			if (IS_ERR(ctm_state))
519766cc6b1SStefan Schake 				return PTR_ERR(ctm_state);
520766cc6b1SStefan Schake 			ctm_state->fifo = 0;
521766cc6b1SStefan Schake 		}
522766cc6b1SStefan Schake 	}
523766cc6b1SStefan Schake 
524766cc6b1SStefan Schake 	for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
525766cc6b1SStefan Schake 		if (new_crtc_state->ctm == old_crtc_state->ctm)
526766cc6b1SStefan Schake 			continue;
527766cc6b1SStefan Schake 
528766cc6b1SStefan Schake 		if (!ctm_state) {
529766cc6b1SStefan Schake 			ctm_state = vc4_get_ctm_state(state, &vc4->ctm_manager);
530766cc6b1SStefan Schake 			if (IS_ERR(ctm_state))
531766cc6b1SStefan Schake 				return PTR_ERR(ctm_state);
532766cc6b1SStefan Schake 		}
533766cc6b1SStefan Schake 
534766cc6b1SStefan Schake 		/* CTM is being enabled or the matrix changed. */
535766cc6b1SStefan Schake 		if (new_crtc_state->ctm) {
53687ebcd42SMaxime Ripard 			struct vc4_crtc_state *vc4_crtc_state =
53787ebcd42SMaxime Ripard 				to_vc4_crtc_state(new_crtc_state);
53887ebcd42SMaxime Ripard 
539766cc6b1SStefan Schake 			/* fifo is 1-based since 0 disables CTM. */
54087ebcd42SMaxime Ripard 			int fifo = vc4_crtc_state->assigned_channel + 1;
541766cc6b1SStefan Schake 
542766cc6b1SStefan Schake 			/* Check userland isn't trying to turn on CTM for more
543766cc6b1SStefan Schake 			 * than one CRTC at a time.
544766cc6b1SStefan Schake 			 */
545766cc6b1SStefan Schake 			if (ctm_state->fifo && ctm_state->fifo != fifo) {
546766cc6b1SStefan Schake 				DRM_DEBUG_DRIVER("Too many CTM configured\n");
547766cc6b1SStefan Schake 				return -EINVAL;
548766cc6b1SStefan Schake 			}
549766cc6b1SStefan Schake 
550766cc6b1SStefan Schake 			/* Check we can approximate the specified CTM.
551766cc6b1SStefan Schake 			 * We disallow scalars |c| > 1.0 since the HW has
552766cc6b1SStefan Schake 			 * no integer bits.
553766cc6b1SStefan Schake 			 */
554766cc6b1SStefan Schake 			ctm = new_crtc_state->ctm->data;
555766cc6b1SStefan Schake 			for (i = 0; i < ARRAY_SIZE(ctm->matrix); i++) {
556766cc6b1SStefan Schake 				u64 val = ctm->matrix[i];
557766cc6b1SStefan Schake 
558766cc6b1SStefan Schake 				val &= ~BIT_ULL(63);
559766cc6b1SStefan Schake 				if (val > BIT_ULL(32))
560766cc6b1SStefan Schake 					return -EINVAL;
561766cc6b1SStefan Schake 			}
562766cc6b1SStefan Schake 
563766cc6b1SStefan Schake 			ctm_state->fifo = fifo;
564766cc6b1SStefan Schake 			ctm_state->ctm = ctm;
565766cc6b1SStefan Schake 		}
566766cc6b1SStefan Schake 	}
567766cc6b1SStefan Schake 
568766cc6b1SStefan Schake 	return 0;
569766cc6b1SStefan Schake }
570766cc6b1SStefan Schake 
vc4_load_tracker_atomic_check(struct drm_atomic_state * state)5714686da83SBoris Brezillon static int vc4_load_tracker_atomic_check(struct drm_atomic_state *state)
5724686da83SBoris Brezillon {
5734686da83SBoris Brezillon 	struct drm_plane_state *old_plane_state, *new_plane_state;
5744686da83SBoris Brezillon 	struct vc4_dev *vc4 = to_vc4_dev(state->dev);
5754686da83SBoris Brezillon 	struct vc4_load_tracker_state *load_state;
5764686da83SBoris Brezillon 	struct drm_private_state *priv_state;
5774686da83SBoris Brezillon 	struct drm_plane *plane;
5784686da83SBoris Brezillon 	int i;
5794686da83SBoris Brezillon 
5804686da83SBoris Brezillon 	priv_state = drm_atomic_get_private_obj_state(state,
5814686da83SBoris Brezillon 						      &vc4->load_tracker);
5824686da83SBoris Brezillon 	if (IS_ERR(priv_state))
5834686da83SBoris Brezillon 		return PTR_ERR(priv_state);
5844686da83SBoris Brezillon 
5854686da83SBoris Brezillon 	load_state = to_vc4_load_tracker_state(priv_state);
5864686da83SBoris Brezillon 	for_each_oldnew_plane_in_state(state, plane, old_plane_state,
5874686da83SBoris Brezillon 				       new_plane_state, i) {
5884686da83SBoris Brezillon 		struct vc4_plane_state *vc4_plane_state;
5894686da83SBoris Brezillon 
5904686da83SBoris Brezillon 		if (old_plane_state->fb && old_plane_state->crtc) {
5914686da83SBoris Brezillon 			vc4_plane_state = to_vc4_plane_state(old_plane_state);
5924686da83SBoris Brezillon 			load_state->membus_load -= vc4_plane_state->membus_load;
5934686da83SBoris Brezillon 			load_state->hvs_load -= vc4_plane_state->hvs_load;
5944686da83SBoris Brezillon 		}
5954686da83SBoris Brezillon 
5964686da83SBoris Brezillon 		if (new_plane_state->fb && new_plane_state->crtc) {
5974686da83SBoris Brezillon 			vc4_plane_state = to_vc4_plane_state(new_plane_state);
5984686da83SBoris Brezillon 			load_state->membus_load += vc4_plane_state->membus_load;
5994686da83SBoris Brezillon 			load_state->hvs_load += vc4_plane_state->hvs_load;
6004686da83SBoris Brezillon 		}
6014686da83SBoris Brezillon 	}
6024686da83SBoris Brezillon 
6036b5c029dSPaul Kocialkowski 	/* Don't check the load when the tracker is disabled. */
6046b5c029dSPaul Kocialkowski 	if (!vc4->load_tracker_enabled)
6056b5c029dSPaul Kocialkowski 		return 0;
6066b5c029dSPaul Kocialkowski 
6074686da83SBoris Brezillon 	/* The absolute limit is 2Gbyte/sec, but let's take a margin to let
6084686da83SBoris Brezillon 	 * the system work when other blocks are accessing the memory.
6094686da83SBoris Brezillon 	 */
6104686da83SBoris Brezillon 	if (load_state->membus_load > SZ_1G + SZ_512M)
6114686da83SBoris Brezillon 		return -ENOSPC;
6124686da83SBoris Brezillon 
6134686da83SBoris Brezillon 	/* HVS clock is supposed to run @ 250Mhz, let's take a margin and
6144686da83SBoris Brezillon 	 * consider the maximum number of cycles is 240M.
6154686da83SBoris Brezillon 	 */
6164686da83SBoris Brezillon 	if (load_state->hvs_load > 240000000ULL)
6174686da83SBoris Brezillon 		return -ENOSPC;
6184686da83SBoris Brezillon 
6194686da83SBoris Brezillon 	return 0;
6204686da83SBoris Brezillon }
6214686da83SBoris Brezillon 
6224686da83SBoris Brezillon static struct drm_private_state *
vc4_load_tracker_duplicate_state(struct drm_private_obj * obj)6234686da83SBoris Brezillon vc4_load_tracker_duplicate_state(struct drm_private_obj *obj)
6244686da83SBoris Brezillon {
6254686da83SBoris Brezillon 	struct vc4_load_tracker_state *state;
6264686da83SBoris Brezillon 
6274686da83SBoris Brezillon 	state = kmemdup(obj->state, sizeof(*state), GFP_KERNEL);
6284686da83SBoris Brezillon 	if (!state)
6294686da83SBoris Brezillon 		return NULL;
6304686da83SBoris Brezillon 
6314686da83SBoris Brezillon 	__drm_atomic_helper_private_obj_duplicate_state(obj, &state->base);
6324686da83SBoris Brezillon 
6334686da83SBoris Brezillon 	return &state->base;
6344686da83SBoris Brezillon }
6354686da83SBoris Brezillon 
vc4_load_tracker_destroy_state(struct drm_private_obj * obj,struct drm_private_state * state)6364686da83SBoris Brezillon static void vc4_load_tracker_destroy_state(struct drm_private_obj *obj,
6374686da83SBoris Brezillon 					   struct drm_private_state *state)
6384686da83SBoris Brezillon {
6394686da83SBoris Brezillon 	struct vc4_load_tracker_state *load_state;
6404686da83SBoris Brezillon 
6414686da83SBoris Brezillon 	load_state = to_vc4_load_tracker_state(state);
6424686da83SBoris Brezillon 	kfree(load_state);
6434686da83SBoris Brezillon }
6444686da83SBoris Brezillon 
6454686da83SBoris Brezillon static const struct drm_private_state_funcs vc4_load_tracker_state_funcs = {
6464686da83SBoris Brezillon 	.atomic_duplicate_state = vc4_load_tracker_duplicate_state,
6474686da83SBoris Brezillon 	.atomic_destroy_state = vc4_load_tracker_destroy_state,
6484686da83SBoris Brezillon };
6494686da83SBoris Brezillon 
vc4_load_tracker_obj_fini(struct drm_device * dev,void * unused)650dcda7c28SMaxime Ripard static void vc4_load_tracker_obj_fini(struct drm_device *dev, void *unused)
651dcda7c28SMaxime Ripard {
652dcda7c28SMaxime Ripard 	struct vc4_dev *vc4 = to_vc4_dev(dev);
653dcda7c28SMaxime Ripard 
654dcda7c28SMaxime Ripard 	drm_atomic_private_obj_fini(&vc4->load_tracker);
655dcda7c28SMaxime Ripard }
656dcda7c28SMaxime Ripard 
vc4_load_tracker_obj_init(struct vc4_dev * vc4)657dcda7c28SMaxime Ripard static int vc4_load_tracker_obj_init(struct vc4_dev *vc4)
658dcda7c28SMaxime Ripard {
659dcda7c28SMaxime Ripard 	struct vc4_load_tracker_state *load_state;
660dcda7c28SMaxime Ripard 
661dcda7c28SMaxime Ripard 	load_state = kzalloc(sizeof(*load_state), GFP_KERNEL);
662dcda7c28SMaxime Ripard 	if (!load_state)
663dcda7c28SMaxime Ripard 		return -ENOMEM;
664dcda7c28SMaxime Ripard 
665dcda7c28SMaxime Ripard 	drm_atomic_private_obj_init(&vc4->base, &vc4->load_tracker,
666dcda7c28SMaxime Ripard 				    &load_state->base,
667dcda7c28SMaxime Ripard 				    &vc4_load_tracker_state_funcs);
668dcda7c28SMaxime Ripard 
6693c354ed1SMaxime Ripard 	return drmm_add_action_or_reset(&vc4->base, vc4_load_tracker_obj_fini, NULL);
670dcda7c28SMaxime Ripard }
671dcda7c28SMaxime Ripard 
672f2df84e0SMaxime Ripard static struct drm_private_state *
vc4_hvs_channels_duplicate_state(struct drm_private_obj * obj)673f2df84e0SMaxime Ripard vc4_hvs_channels_duplicate_state(struct drm_private_obj *obj)
674f2df84e0SMaxime Ripard {
675f2df84e0SMaxime Ripard 	struct vc4_hvs_state *old_state = to_vc4_hvs_state(obj->state);
676f2df84e0SMaxime Ripard 	struct vc4_hvs_state *state;
6779ec03d7fSMaxime Ripard 	unsigned int i;
678f2df84e0SMaxime Ripard 
679f2df84e0SMaxime Ripard 	state = kzalloc(sizeof(*state), GFP_KERNEL);
680f2df84e0SMaxime Ripard 	if (!state)
681f2df84e0SMaxime Ripard 		return NULL;
682f2df84e0SMaxime Ripard 
683f2df84e0SMaxime Ripard 	__drm_atomic_helper_private_obj_duplicate_state(obj, &state->base);
684f2df84e0SMaxime Ripard 
6859ec03d7fSMaxime Ripard 	for (i = 0; i < HVS_NUM_CHANNELS; i++) {
6869ec03d7fSMaxime Ripard 		state->fifo_state[i].in_use = old_state->fifo_state[i].in_use;
68716e10105SMaxime Ripard 		state->fifo_state[i].fifo_load = old_state->fifo_state[i].fifo_load;
6889ec03d7fSMaxime Ripard 	}
6899ec03d7fSMaxime Ripard 
69016e10105SMaxime Ripard 	state->core_clock_rate = old_state->core_clock_rate;
69116e10105SMaxime Ripard 
692f2df84e0SMaxime Ripard 	return &state->base;
693f2df84e0SMaxime Ripard }
694f2df84e0SMaxime Ripard 
vc4_hvs_channels_destroy_state(struct drm_private_obj * obj,struct drm_private_state * state)695f2df84e0SMaxime Ripard static void vc4_hvs_channels_destroy_state(struct drm_private_obj *obj,
696f2df84e0SMaxime Ripard 					   struct drm_private_state *state)
697f2df84e0SMaxime Ripard {
698f2df84e0SMaxime Ripard 	struct vc4_hvs_state *hvs_state = to_vc4_hvs_state(state);
6999ec03d7fSMaxime Ripard 	unsigned int i;
7009ec03d7fSMaxime Ripard 
7019ec03d7fSMaxime Ripard 	for (i = 0; i < HVS_NUM_CHANNELS; i++) {
7029ec03d7fSMaxime Ripard 		if (!hvs_state->fifo_state[i].pending_commit)
7039ec03d7fSMaxime Ripard 			continue;
7049ec03d7fSMaxime Ripard 
7059ec03d7fSMaxime Ripard 		drm_crtc_commit_put(hvs_state->fifo_state[i].pending_commit);
7069ec03d7fSMaxime Ripard 	}
707f2df84e0SMaxime Ripard 
708f2df84e0SMaxime Ripard 	kfree(hvs_state);
709f2df84e0SMaxime Ripard }
710f2df84e0SMaxime Ripard 
vc4_hvs_channels_print_state(struct drm_printer * p,const struct drm_private_state * state)71166bfe59dSMaxime Ripard static void vc4_hvs_channels_print_state(struct drm_printer *p,
71266bfe59dSMaxime Ripard 					 const struct drm_private_state *state)
71366bfe59dSMaxime Ripard {
714*5a46e490SMaxime Ripard 	const struct vc4_hvs_state *hvs_state = to_vc4_hvs_state(state);
71566bfe59dSMaxime Ripard 	unsigned int i;
71666bfe59dSMaxime Ripard 
71766bfe59dSMaxime Ripard 	drm_printf(p, "HVS State\n");
71866bfe59dSMaxime Ripard 	drm_printf(p, "\tCore Clock Rate: %lu\n", hvs_state->core_clock_rate);
71966bfe59dSMaxime Ripard 
72066bfe59dSMaxime Ripard 	for (i = 0; i < HVS_NUM_CHANNELS; i++) {
72166bfe59dSMaxime Ripard 		drm_printf(p, "\tChannel %d\n", i);
72266bfe59dSMaxime Ripard 		drm_printf(p, "\t\tin use=%d\n", hvs_state->fifo_state[i].in_use);
72366bfe59dSMaxime Ripard 		drm_printf(p, "\t\tload=%lu\n", hvs_state->fifo_state[i].fifo_load);
72466bfe59dSMaxime Ripard 	}
72566bfe59dSMaxime Ripard }
72666bfe59dSMaxime Ripard 
727f2df84e0SMaxime Ripard static const struct drm_private_state_funcs vc4_hvs_state_funcs = {
728f2df84e0SMaxime Ripard 	.atomic_duplicate_state = vc4_hvs_channels_duplicate_state,
729f2df84e0SMaxime Ripard 	.atomic_destroy_state = vc4_hvs_channels_destroy_state,
73066bfe59dSMaxime Ripard 	.atomic_print_state = vc4_hvs_channels_print_state,
731f2df84e0SMaxime Ripard };
732f2df84e0SMaxime Ripard 
vc4_hvs_channels_obj_fini(struct drm_device * dev,void * unused)733f2df84e0SMaxime Ripard static void vc4_hvs_channels_obj_fini(struct drm_device *dev, void *unused)
734f2df84e0SMaxime Ripard {
735f2df84e0SMaxime Ripard 	struct vc4_dev *vc4 = to_vc4_dev(dev);
736f2df84e0SMaxime Ripard 
737f2df84e0SMaxime Ripard 	drm_atomic_private_obj_fini(&vc4->hvs_channels);
738f2df84e0SMaxime Ripard }
739f2df84e0SMaxime Ripard 
vc4_hvs_channels_obj_init(struct vc4_dev * vc4)740f2df84e0SMaxime Ripard static int vc4_hvs_channels_obj_init(struct vc4_dev *vc4)
741f2df84e0SMaxime Ripard {
742f2df84e0SMaxime Ripard 	struct vc4_hvs_state *state;
743f2df84e0SMaxime Ripard 
744f2df84e0SMaxime Ripard 	state = kzalloc(sizeof(*state), GFP_KERNEL);
745f2df84e0SMaxime Ripard 	if (!state)
746f2df84e0SMaxime Ripard 		return -ENOMEM;
747f2df84e0SMaxime Ripard 
748f2df84e0SMaxime Ripard 	drm_atomic_private_obj_init(&vc4->base, &vc4->hvs_channels,
749f2df84e0SMaxime Ripard 				    &state->base,
750f2df84e0SMaxime Ripard 				    &vc4_hvs_state_funcs);
751f2df84e0SMaxime Ripard 
752f2df84e0SMaxime Ripard 	return drmm_add_action_or_reset(&vc4->base, vc4_hvs_channels_obj_fini, NULL);
753f2df84e0SMaxime Ripard }
754f2df84e0SMaxime Ripard 
cmp_vc4_crtc_hvs_output(const void * a,const void * b)755e3479398SMaxime Ripard static int cmp_vc4_crtc_hvs_output(const void *a, const void *b)
756e3479398SMaxime Ripard {
757e3479398SMaxime Ripard 	const struct vc4_crtc *crtc_a =
758e3479398SMaxime Ripard 		to_vc4_crtc(*(const struct drm_crtc **)a);
759e3479398SMaxime Ripard 	const struct vc4_crtc_data *data_a =
760e3479398SMaxime Ripard 		vc4_crtc_to_vc4_crtc_data(crtc_a);
761e3479398SMaxime Ripard 	const struct vc4_crtc *crtc_b =
762e3479398SMaxime Ripard 		to_vc4_crtc(*(const struct drm_crtc **)b);
763e3479398SMaxime Ripard 	const struct vc4_crtc_data *data_b =
764e3479398SMaxime Ripard 		vc4_crtc_to_vc4_crtc_data(crtc_b);
765e3479398SMaxime Ripard 
766e3479398SMaxime Ripard 	return data_a->hvs_output - data_b->hvs_output;
767e3479398SMaxime Ripard }
768e3479398SMaxime Ripard 
769b5dbc4d3SMaxime Ripard /*
770b5dbc4d3SMaxime Ripard  * The BCM2711 HVS has up to 7 outputs connected to the pixelvalves and
771b5dbc4d3SMaxime Ripard  * the TXP (and therefore all the CRTCs found on that platform).
772b5dbc4d3SMaxime Ripard  *
773b5dbc4d3SMaxime Ripard  * The naive (and our initial) implementation would just iterate over
774b5dbc4d3SMaxime Ripard  * all the active CRTCs, try to find a suitable FIFO, and then remove it
775b5dbc4d3SMaxime Ripard  * from the pool of available FIFOs. However, there are a few corner
776b5dbc4d3SMaxime Ripard  * cases that need to be considered:
777b5dbc4d3SMaxime Ripard  *
778b5dbc4d3SMaxime Ripard  * - When running in a dual-display setup (so with two CRTCs involved),
779b5dbc4d3SMaxime Ripard  *   we can update the state of a single CRTC (for example by changing
780b5dbc4d3SMaxime Ripard  *   its mode using xrandr under X11) without affecting the other. In
781b5dbc4d3SMaxime Ripard  *   this case, the other CRTC wouldn't be in the state at all, so we
782b5dbc4d3SMaxime Ripard  *   need to consider all the running CRTCs in the DRM device to assign
783b5dbc4d3SMaxime Ripard  *   a FIFO, not just the one in the state.
784b5dbc4d3SMaxime Ripard  *
785f2df84e0SMaxime Ripard  * - To fix the above, we can't use drm_atomic_get_crtc_state on all
786f2df84e0SMaxime Ripard  *   enabled CRTCs to pull their CRTC state into the global state, since
787f2df84e0SMaxime Ripard  *   a page flip would start considering their vblank to complete. Since
788f2df84e0SMaxime Ripard  *   we don't have a guarantee that they are actually active, that
789f2df84e0SMaxime Ripard  *   vblank might never happen, and shouldn't even be considered if we
790f2df84e0SMaxime Ripard  *   want to do a page flip on a single CRTC. That can be tested by
791f2df84e0SMaxime Ripard  *   doing a modetest -v first on HDMI1 and then on HDMI0.
792f2df84e0SMaxime Ripard  *
793b5dbc4d3SMaxime Ripard  * - Since we need the pixelvalve to be disabled and enabled back when
794b5dbc4d3SMaxime Ripard  *   the FIFO is changed, we should keep the FIFO assigned for as long
795b5dbc4d3SMaxime Ripard  *   as the CRTC is enabled, only considering it free again once that
796b5dbc4d3SMaxime Ripard  *   CRTC has been disabled. This can be tested by booting X11 on a
797b5dbc4d3SMaxime Ripard  *   single display, and changing the resolution down and then back up.
798b5dbc4d3SMaxime Ripard  */
vc4_pv_muxing_atomic_check(struct drm_device * dev,struct drm_atomic_state * state)799a72b0458SMaxime Ripard static int vc4_pv_muxing_atomic_check(struct drm_device *dev,
800a72b0458SMaxime Ripard 				      struct drm_atomic_state *state)
801766cc6b1SStefan Schake {
802f2df84e0SMaxime Ripard 	struct vc4_hvs_state *hvs_new_state;
803e3479398SMaxime Ripard 	struct drm_crtc **sorted_crtcs;
80487ebcd42SMaxime Ripard 	struct drm_crtc *crtc;
80503b03efeSMaxime Ripard 	unsigned int unassigned_channels = 0;
806a72b0458SMaxime Ripard 	unsigned int i;
807e3479398SMaxime Ripard 	int ret;
80887ebcd42SMaxime Ripard 
809f2df84e0SMaxime Ripard 	hvs_new_state = vc4_hvs_get_global_state(state);
810f9277679SMaxime Ripard 	if (IS_ERR(hvs_new_state))
811f9277679SMaxime Ripard 		return PTR_ERR(hvs_new_state);
812089d8341SMaxime Ripard 
81303b03efeSMaxime Ripard 	for (i = 0; i < ARRAY_SIZE(hvs_new_state->fifo_state); i++)
81403b03efeSMaxime Ripard 		if (!hvs_new_state->fifo_state[i].in_use)
81503b03efeSMaxime Ripard 			unassigned_channels |= BIT(i);
81603b03efeSMaxime Ripard 
817e3479398SMaxime Ripard 	/*
818e3479398SMaxime Ripard 	 * The problem we have to solve here is that we have up to 7
819e3479398SMaxime Ripard 	 * encoders, connected to up to 6 CRTCs.
820e3479398SMaxime Ripard 	 *
821e3479398SMaxime Ripard 	 * Those CRTCs, depending on the instance, can be routed to 1, 2
822e3479398SMaxime Ripard 	 * or 3 HVS FIFOs, and we need to set the muxing between FIFOs and
823e3479398SMaxime Ripard 	 * outputs in the HVS accordingly.
824e3479398SMaxime Ripard 	 *
825e3479398SMaxime Ripard 	 * It would be pretty hard to come up with an algorithm that
826e3479398SMaxime Ripard 	 * would generically solve this. However, the current routing
827e3479398SMaxime Ripard 	 * trees we support allow us to simplify a bit the problem.
828e3479398SMaxime Ripard 	 *
829e3479398SMaxime Ripard 	 * Indeed, with the current supported layouts, if we try to
830e3479398SMaxime Ripard 	 * assign in the ascending crtc index order the FIFOs, we can't
831e3479398SMaxime Ripard 	 * fall into the situation where an earlier CRTC that had
832e3479398SMaxime Ripard 	 * multiple routes is assigned one that was the only option for
833e3479398SMaxime Ripard 	 * a later CRTC.
834e3479398SMaxime Ripard 	 *
835e3479398SMaxime Ripard 	 * If the layout changes and doesn't give us that in the future,
836e3479398SMaxime Ripard 	 * we will need to have something smarter, but it works so far.
837e3479398SMaxime Ripard 	 */
838e3479398SMaxime Ripard 	sorted_crtcs = kmalloc_array(dev->num_crtcs, sizeof(*sorted_crtcs), GFP_KERNEL);
839e3479398SMaxime Ripard 	if (!sorted_crtcs)
840e3479398SMaxime Ripard 		return -ENOMEM;
841e3479398SMaxime Ripard 
842e3479398SMaxime Ripard 	i = 0;
843e3479398SMaxime Ripard 	drm_for_each_crtc(crtc, dev)
844e3479398SMaxime Ripard 		sorted_crtcs[i++] = crtc;
845e3479398SMaxime Ripard 
846e3479398SMaxime Ripard 	sort(sorted_crtcs, i, sizeof(*sorted_crtcs), cmp_vc4_crtc_hvs_output, NULL);
847e3479398SMaxime Ripard 
848e3479398SMaxime Ripard 	for (i = 0; i < dev->num_crtcs; i++) {
849e3479398SMaxime Ripard 		struct vc4_crtc_state *old_vc4_crtc_state, *new_vc4_crtc_state;
850e3479398SMaxime Ripard 		struct drm_crtc_state *old_crtc_state, *new_crtc_state;
851e3479398SMaxime Ripard 		struct vc4_crtc *vc4_crtc;
85287ebcd42SMaxime Ripard 		unsigned int matching_channels;
853d62a8ed7SMaxime Ripard 		unsigned int channel;
85487ebcd42SMaxime Ripard 
855e3479398SMaxime Ripard 		crtc = sorted_crtcs[i];
856e3479398SMaxime Ripard 		if (!crtc)
857e3479398SMaxime Ripard 			continue;
858e3479398SMaxime Ripard 		vc4_crtc = to_vc4_crtc(crtc);
859e3479398SMaxime Ripard 
860e3479398SMaxime Ripard 		old_crtc_state = drm_atomic_get_old_crtc_state(state, crtc);
861e3479398SMaxime Ripard 		if (!old_crtc_state)
862e3479398SMaxime Ripard 			continue;
863e3479398SMaxime Ripard 		old_vc4_crtc_state = to_vc4_crtc_state(old_crtc_state);
864e3479398SMaxime Ripard 
865e3479398SMaxime Ripard 		new_crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
866e3479398SMaxime Ripard 		if (!new_crtc_state)
867e3479398SMaxime Ripard 			continue;
868e3479398SMaxime Ripard 		new_vc4_crtc_state = to_vc4_crtc_state(new_crtc_state);
869e3479398SMaxime Ripard 
8703870b54eSMaxime Ripard 		drm_dbg(dev, "%s: Trying to find a channel.\n", crtc->name);
8713870b54eSMaxime Ripard 
8722820526dSMaxime Ripard 		/* Nothing to do here, let's skip it */
8733870b54eSMaxime Ripard 		if (old_crtc_state->enable == new_crtc_state->enable) {
8743870b54eSMaxime Ripard 			if (new_crtc_state->enable)
8753870b54eSMaxime Ripard 				drm_dbg(dev, "%s: Already enabled, reusing channel %d.\n",
8763870b54eSMaxime Ripard 					crtc->name, new_vc4_crtc_state->assigned_channel);
8773870b54eSMaxime Ripard 			else
8783870b54eSMaxime Ripard 				drm_dbg(dev, "%s: Disabled, ignoring.\n", crtc->name);
8793870b54eSMaxime Ripard 
8802820526dSMaxime Ripard 			continue;
8813870b54eSMaxime Ripard 		}
8822820526dSMaxime Ripard 
8832820526dSMaxime Ripard 		/* Muxing will need to be modified, mark it as such */
8842820526dSMaxime Ripard 		new_vc4_crtc_state->update_muxing = true;
8852820526dSMaxime Ripard 
8862820526dSMaxime Ripard 		/* If we're disabling our CRTC, we put back our channel */
8872820526dSMaxime Ripard 		if (!new_crtc_state->enable) {
8889ec03d7fSMaxime Ripard 			channel = old_vc4_crtc_state->assigned_channel;
8893870b54eSMaxime Ripard 
8903870b54eSMaxime Ripard 			drm_dbg(dev, "%s: Disabling, Freeing channel %d\n",
8913870b54eSMaxime Ripard 				crtc->name, channel);
8923870b54eSMaxime Ripard 
8939ec03d7fSMaxime Ripard 			hvs_new_state->fifo_state[channel].in_use = false;
8948ba0b6d1SMaxime Ripard 			new_vc4_crtc_state->assigned_channel = VC4_HVS_CHANNEL_DISABLED;
8952820526dSMaxime Ripard 			continue;
896f2df84e0SMaxime Ripard 		}
8978ba0b6d1SMaxime Ripard 
89803b03efeSMaxime Ripard 		matching_channels = unassigned_channels & vc4_crtc->data->hvs_available_channels;
899e3479398SMaxime Ripard 		if (!matching_channels) {
900e3479398SMaxime Ripard 			ret = -EINVAL;
901e3479398SMaxime Ripard 			goto err_free_crtc_array;
902e3479398SMaxime Ripard 		}
90387ebcd42SMaxime Ripard 
904d62a8ed7SMaxime Ripard 		channel = ffs(matching_channels) - 1;
9053870b54eSMaxime Ripard 
9063870b54eSMaxime Ripard 		drm_dbg(dev, "Assigned HVS channel %d to CRTC %s\n", channel, crtc->name);
9078ba0b6d1SMaxime Ripard 		new_vc4_crtc_state->assigned_channel = channel;
90803b03efeSMaxime Ripard 		unassigned_channels &= ~BIT(channel);
9099ec03d7fSMaxime Ripard 		hvs_new_state->fifo_state[channel].in_use = true;
91087ebcd42SMaxime Ripard 	}
911766cc6b1SStefan Schake 
912e3479398SMaxime Ripard 	kfree(sorted_crtcs);
913a72b0458SMaxime Ripard 	return 0;
914e3479398SMaxime Ripard 
915e3479398SMaxime Ripard err_free_crtc_array:
916e3479398SMaxime Ripard 	kfree(sorted_crtcs);
917e3479398SMaxime Ripard 	return ret;
918a72b0458SMaxime Ripard }
919a72b0458SMaxime Ripard 
920a72b0458SMaxime Ripard static int
vc4_core_clock_atomic_check(struct drm_atomic_state * state)92116e10105SMaxime Ripard vc4_core_clock_atomic_check(struct drm_atomic_state *state)
92216e10105SMaxime Ripard {
92316e10105SMaxime Ripard 	struct vc4_dev *vc4 = to_vc4_dev(state->dev);
92416e10105SMaxime Ripard 	struct drm_private_state *priv_state;
92516e10105SMaxime Ripard 	struct vc4_hvs_state *hvs_new_state;
92616e10105SMaxime Ripard 	struct vc4_load_tracker_state *load_state;
92716e10105SMaxime Ripard 	struct drm_crtc_state *old_crtc_state, *new_crtc_state;
92816e10105SMaxime Ripard 	struct drm_crtc *crtc;
92916e10105SMaxime Ripard 	unsigned int num_outputs;
93016e10105SMaxime Ripard 	unsigned long pixel_rate;
93116e10105SMaxime Ripard 	unsigned long cob_rate;
93216e10105SMaxime Ripard 	unsigned int i;
93316e10105SMaxime Ripard 
93416e10105SMaxime Ripard 	priv_state = drm_atomic_get_private_obj_state(state,
93516e10105SMaxime Ripard 						      &vc4->load_tracker);
93616e10105SMaxime Ripard 	if (IS_ERR(priv_state))
93716e10105SMaxime Ripard 		return PTR_ERR(priv_state);
93816e10105SMaxime Ripard 
93916e10105SMaxime Ripard 	load_state = to_vc4_load_tracker_state(priv_state);
94016e10105SMaxime Ripard 
94116e10105SMaxime Ripard 	hvs_new_state = vc4_hvs_get_global_state(state);
94299b03ca6SDaniel Vetter 	if (IS_ERR(hvs_new_state))
94399b03ca6SDaniel Vetter 		return PTR_ERR(hvs_new_state);
94416e10105SMaxime Ripard 
94516e10105SMaxime Ripard 	for_each_oldnew_crtc_in_state(state, crtc,
94616e10105SMaxime Ripard 				      old_crtc_state,
94716e10105SMaxime Ripard 				      new_crtc_state,
94816e10105SMaxime Ripard 				      i) {
94916e10105SMaxime Ripard 		if (old_crtc_state->active) {
95016e10105SMaxime Ripard 			struct vc4_crtc_state *old_vc4_state =
95116e10105SMaxime Ripard 				to_vc4_crtc_state(old_crtc_state);
95216e10105SMaxime Ripard 			unsigned int channel = old_vc4_state->assigned_channel;
95316e10105SMaxime Ripard 
95416e10105SMaxime Ripard 			hvs_new_state->fifo_state[channel].fifo_load = 0;
95516e10105SMaxime Ripard 		}
95616e10105SMaxime Ripard 
95716e10105SMaxime Ripard 		if (new_crtc_state->active) {
95816e10105SMaxime Ripard 			struct vc4_crtc_state *new_vc4_state =
95916e10105SMaxime Ripard 				to_vc4_crtc_state(new_crtc_state);
96016e10105SMaxime Ripard 			unsigned int channel = new_vc4_state->assigned_channel;
96116e10105SMaxime Ripard 
96216e10105SMaxime Ripard 			hvs_new_state->fifo_state[channel].fifo_load =
96316e10105SMaxime Ripard 				new_vc4_state->hvs_load;
96416e10105SMaxime Ripard 		}
96516e10105SMaxime Ripard 	}
96616e10105SMaxime Ripard 
96716e10105SMaxime Ripard 	cob_rate = 0;
96816e10105SMaxime Ripard 	num_outputs = 0;
96916e10105SMaxime Ripard 	for (i = 0; i < HVS_NUM_CHANNELS; i++) {
97016e10105SMaxime Ripard 		if (!hvs_new_state->fifo_state[i].in_use)
97116e10105SMaxime Ripard 			continue;
97216e10105SMaxime Ripard 
97316e10105SMaxime Ripard 		num_outputs++;
9741701a23aSMaxime Ripard 		cob_rate = max_t(unsigned long,
9751701a23aSMaxime Ripard 				 hvs_new_state->fifo_state[i].fifo_load,
9761701a23aSMaxime Ripard 				 cob_rate);
97716e10105SMaxime Ripard 	}
97816e10105SMaxime Ripard 
97916e10105SMaxime Ripard 	pixel_rate = load_state->hvs_load;
98016e10105SMaxime Ripard 	if (num_outputs > 1) {
98116e10105SMaxime Ripard 		pixel_rate = (pixel_rate * 40) / 100;
98216e10105SMaxime Ripard 	} else {
98316e10105SMaxime Ripard 		pixel_rate = (pixel_rate * 60) / 100;
98416e10105SMaxime Ripard 	}
98516e10105SMaxime Ripard 
98616e10105SMaxime Ripard 	hvs_new_state->core_clock_rate = max(cob_rate, pixel_rate);
98716e10105SMaxime Ripard 
98816e10105SMaxime Ripard 	return 0;
98916e10105SMaxime Ripard }
99016e10105SMaxime Ripard 
99116e10105SMaxime Ripard 
99216e10105SMaxime Ripard static int
vc4_atomic_check(struct drm_device * dev,struct drm_atomic_state * state)993a72b0458SMaxime Ripard vc4_atomic_check(struct drm_device *dev, struct drm_atomic_state *state)
994a72b0458SMaxime Ripard {
995a72b0458SMaxime Ripard 	int ret;
996a72b0458SMaxime Ripard 
997a72b0458SMaxime Ripard 	ret = vc4_pv_muxing_atomic_check(dev, state);
998a72b0458SMaxime Ripard 	if (ret)
999a72b0458SMaxime Ripard 		return ret;
1000a72b0458SMaxime Ripard 
1001766cc6b1SStefan Schake 	ret = vc4_ctm_atomic_check(dev, state);
1002766cc6b1SStefan Schake 	if (ret < 0)
1003766cc6b1SStefan Schake 		return ret;
1004766cc6b1SStefan Schake 
10054686da83SBoris Brezillon 	ret = drm_atomic_helper_check(dev, state);
10064686da83SBoris Brezillon 	if (ret)
10074686da83SBoris Brezillon 		return ret;
10084686da83SBoris Brezillon 
100916e10105SMaxime Ripard 	ret = vc4_load_tracker_atomic_check(state);
101016e10105SMaxime Ripard 	if (ret)
101116e10105SMaxime Ripard 		return ret;
101216e10105SMaxime Ripard 
101316e10105SMaxime Ripard 	return vc4_core_clock_atomic_check(state);
1014766cc6b1SStefan Schake }
1015766cc6b1SStefan Schake 
10169ec03d7fSMaxime Ripard static struct drm_mode_config_helper_funcs vc4_mode_config_helpers = {
10179ec03d7fSMaxime Ripard 	.atomic_commit_setup	= vc4_atomic_commit_setup,
1018f3c420feSMaxime Ripard 	.atomic_commit_tail	= vc4_atomic_commit_tail,
10199ec03d7fSMaxime Ripard };
10209ec03d7fSMaxime Ripard 
1021c8b75bcaSEric Anholt static const struct drm_mode_config_funcs vc4_mode_funcs = {
1022766cc6b1SStefan Schake 	.atomic_check = vc4_atomic_check,
1023f3c420feSMaxime Ripard 	.atomic_commit = drm_atomic_helper_commit,
102483753117SEric Anholt 	.fb_create = vc4_fb_create,
1025c8b75bcaSEric Anholt };
1026c8b75bcaSEric Anholt 
102739a30ec6SMaxime Ripard static const struct drm_mode_config_funcs vc5_mode_funcs = {
102839a30ec6SMaxime Ripard 	.atomic_check = vc4_atomic_check,
102939a30ec6SMaxime Ripard 	.atomic_commit = drm_atomic_helper_commit,
103039a30ec6SMaxime Ripard 	.fb_create = drm_gem_fb_create,
103139a30ec6SMaxime Ripard };
103239a30ec6SMaxime Ripard 
vc4_kms_load(struct drm_device * dev)1033c8b75bcaSEric Anholt int vc4_kms_load(struct drm_device *dev)
1034c8b75bcaSEric Anholt {
103548666d56SDerek Foreman 	struct vc4_dev *vc4 = to_vc4_dev(dev);
1036c8b75bcaSEric Anholt 	int ret;
1037c8b75bcaSEric Anholt 
10387f817159SMaxime Ripard 	/*
10397f817159SMaxime Ripard 	 * The limits enforced by the load tracker aren't relevant for
10407f817159SMaxime Ripard 	 * the BCM2711, but the load tracker computations are used for
10417f817159SMaxime Ripard 	 * the core clock rate calculation.
10427f817159SMaxime Ripard 	 */
10431cbc91ebSMaxime Ripard 	if (!vc4->is_vc5) {
1044f437bc1eSMaxime Ripard 		/* Start with the load tracker enabled. Can be
1045f437bc1eSMaxime Ripard 		 * disabled through the debugfs load_tracker file.
10466b5c029dSPaul Kocialkowski 		 */
10476b5c029dSPaul Kocialkowski 		vc4->load_tracker_enabled = true;
1048f437bc1eSMaxime Ripard 	}
10496b5c029dSPaul Kocialkowski 
10507d2818f5SMario Kleiner 	/* Set support for vblank irq fast disable, before drm_vblank_init() */
10517d2818f5SMario Kleiner 	dev->vblank_disable_immediate = true;
10527d2818f5SMario Kleiner 
1053c8b75bcaSEric Anholt 	ret = drm_vblank_init(dev, dev->mode_config.num_crtc);
1054c8b75bcaSEric Anholt 	if (ret < 0) {
1055c8b75bcaSEric Anholt 		dev_err(dev->dev, "failed to initialize vblank\n");
1056c8b75bcaSEric Anholt 		return ret;
1057c8b75bcaSEric Anholt 	}
1058c8b75bcaSEric Anholt 
10591cbc91ebSMaxime Ripard 	if (vc4->is_vc5) {
1060f437bc1eSMaxime Ripard 		dev->mode_config.max_width = 7680;
1061f437bc1eSMaxime Ripard 		dev->mode_config.max_height = 7680;
1062f437bc1eSMaxime Ripard 	} else {
1063c8b75bcaSEric Anholt 		dev->mode_config.max_width = 2048;
1064c8b75bcaSEric Anholt 		dev->mode_config.max_height = 2048;
1065f437bc1eSMaxime Ripard 	}
1066f437bc1eSMaxime Ripard 
106739a30ec6SMaxime Ripard 	dev->mode_config.funcs = vc4->is_vc5 ? &vc5_mode_funcs : &vc4_mode_funcs;
10689ec03d7fSMaxime Ripard 	dev->mode_config.helper_private = &vc4_mode_config_helpers;
1069c8b75bcaSEric Anholt 	dev->mode_config.preferred_depth = 24;
1070b501baccSEric Anholt 	dev->mode_config.async_page_flip = true;
1071868bc999SDave Stevenson 	dev->mode_config.normalize_zpos = true;
1072b501baccSEric Anholt 
1073dcda7c28SMaxime Ripard 	ret = vc4_ctm_obj_init(vc4);
1074dcda7c28SMaxime Ripard 	if (ret)
1075dcda7c28SMaxime Ripard 		return ret;
1076766cc6b1SStefan Schake 
1077dcda7c28SMaxime Ripard 	ret = vc4_load_tracker_obj_init(vc4);
1078dcda7c28SMaxime Ripard 	if (ret)
1079dcda7c28SMaxime Ripard 		return ret;
10804686da83SBoris Brezillon 
1081f2df84e0SMaxime Ripard 	ret = vc4_hvs_channels_obj_init(vc4);
1082f2df84e0SMaxime Ripard 	if (ret)
1083f2df84e0SMaxime Ripard 		return ret;
1084f2df84e0SMaxime Ripard 
1085c8b75bcaSEric Anholt 	drm_mode_config_reset(dev);
1086c8b75bcaSEric Anholt 
1087c8b75bcaSEric Anholt 	drm_kms_helper_poll_init(dev);
1088c8b75bcaSEric Anholt 
1089c8b75bcaSEric Anholt 	return 0;
1090c8b75bcaSEric Anholt }
1091