xref: /openbmc/linux/drivers/gpu/drm/vc4/vc4_crtc.c (revision c900529f3d9161bfde5cca0754f83b4d3c3e0220)
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 CRTC module
8c8b75bcaSEric Anholt  *
9c8b75bcaSEric Anholt  * In VC4, the Pixel Valve is what most closely corresponds to the
10c8b75bcaSEric Anholt  * DRM's concept of a CRTC.  The PV generates video timings from the
11f6c01530SEric Anholt  * encoder's clock plus its configuration.  It pulls scaled pixels from
12c8b75bcaSEric Anholt  * the HVS at that timing, and feeds it to the encoder.
13c8b75bcaSEric Anholt  *
14c8b75bcaSEric Anholt  * However, the DRM CRTC also collects the configuration of all the
15f6c01530SEric Anholt  * DRM planes attached to it.  As a result, the CRTC is also
16f6c01530SEric Anholt  * responsible for writing the display list for the HVS channel that
17f6c01530SEric Anholt  * the CRTC will use.
18c8b75bcaSEric Anholt  *
19c8b75bcaSEric Anholt  * The 2835 has 3 different pixel valves.  pv0 in the audio power
20c8b75bcaSEric Anholt  * domain feeds DSI0 or DPI, while pv1 feeds DS1 or SMI.  pv2 in the
21c8b75bcaSEric Anholt  * image domain can feed either HDMI or the SDTV controller.  The
22c8b75bcaSEric Anholt  * pixel valve chooses from the CPRMAN clocks (HSM for HDMI, VEC for
23c8b75bcaSEric Anholt  * SDTV, etc.) according to which output type is chosen in the mux.
24c8b75bcaSEric Anholt  *
25c8b75bcaSEric Anholt  * For power management, the pixel valve's registers are all clocked
26c8b75bcaSEric Anholt  * by the AXI clock, while the timings and FIFOs make use of the
27c8b75bcaSEric Anholt  * output-specific clock.  Since the encoders also directly consume
28c8b75bcaSEric Anholt  * the CPRMAN clocks, and know what timings they need, they are the
29c8b75bcaSEric Anholt  * ones that set the clock.
30c8b75bcaSEric Anholt  */
31c8b75bcaSEric Anholt 
32fd6d6d80SSam Ravnborg #include <linux/clk.h>
33fd6d6d80SSam Ravnborg #include <linux/component.h>
34*722d4f06SRob Herring #include <linux/of.h>
35*722d4f06SRob Herring #include <linux/platform_device.h>
36bca10db6SMaxime Ripard #include <linux/pm_runtime.h>
37fd6d6d80SSam Ravnborg 
38b7e8e25bSMasahiro Yamada #include <drm/drm_atomic.h>
39b7e8e25bSMasahiro Yamada #include <drm/drm_atomic_helper.h>
4072fdb40cSDaniel Vetter #include <drm/drm_atomic_uapi.h>
416bcfe8eaSDanilo Krummrich #include <drm/drm_fb_dma_helper.h>
42720cf96dSVille Syrjälä #include <drm/drm_framebuffer.h>
43e0c95303SDanilo Krummrich #include <drm/drm_drv.h>
443051719aSEric Anholt #include <drm/drm_print.h>
45fcd70cd3SDaniel Vetter #include <drm/drm_probe_helper.h>
46fd6d6d80SSam Ravnborg #include <drm/drm_vblank.h>
47fd6d6d80SSam Ravnborg 
48c8b75bcaSEric Anholt #include "vc4_drv.h"
49bca10db6SMaxime Ripard #include "vc4_hdmi.h"
50c8b75bcaSEric Anholt #include "vc4_regs.h"
51c8b75bcaSEric Anholt 
52e58a5e6fSMaxime Ripard #define HVS_FIFO_LATENCY_PIX	6
53e58a5e6fSMaxime Ripard 
54da43ff04SMaxime Ripard #define CRTC_WRITE(offset, val)								\
55da43ff04SMaxime Ripard 	do {										\
56da43ff04SMaxime Ripard 		kunit_fail_current_test("Accessing a register in a unit test!\n");	\
57da43ff04SMaxime Ripard 		writel(val, vc4_crtc->regs + (offset));					\
58da43ff04SMaxime Ripard 	} while (0)
59da43ff04SMaxime Ripard 
60da43ff04SMaxime Ripard #define CRTC_READ(offset)								\
61da43ff04SMaxime Ripard 	({										\
62da43ff04SMaxime Ripard 		kunit_fail_current_test("Accessing a register in a unit test!\n");	\
63da43ff04SMaxime Ripard 		readl(vc4_crtc->regs + (offset));					\
64da43ff04SMaxime Ripard 	})
65c8b75bcaSEric Anholt 
663051719aSEric Anholt static const struct debugfs_reg32 crtc_regs[] = {
673051719aSEric Anholt 	VC4_REG32(PV_CONTROL),
683051719aSEric Anholt 	VC4_REG32(PV_V_CONTROL),
693051719aSEric Anholt 	VC4_REG32(PV_VSYNCD_EVEN),
703051719aSEric Anholt 	VC4_REG32(PV_HORZA),
713051719aSEric Anholt 	VC4_REG32(PV_HORZB),
723051719aSEric Anholt 	VC4_REG32(PV_VERTA),
733051719aSEric Anholt 	VC4_REG32(PV_VERTB),
743051719aSEric Anholt 	VC4_REG32(PV_VERTA_EVEN),
753051719aSEric Anholt 	VC4_REG32(PV_VERTB_EVEN),
763051719aSEric Anholt 	VC4_REG32(PV_INTEN),
773051719aSEric Anholt 	VC4_REG32(PV_INTSTAT),
783051719aSEric Anholt 	VC4_REG32(PV_STAT),
793051719aSEric Anholt 	VC4_REG32(PV_HACT_ACT),
80c8b75bcaSEric Anholt };
81c8b75bcaSEric Anholt 
8278cbcc38SMaxime Ripard static unsigned int
vc4_crtc_get_cob_allocation(struct vc4_dev * vc4,unsigned int channel)8378cbcc38SMaxime Ripard vc4_crtc_get_cob_allocation(struct vc4_dev *vc4, unsigned int channel)
8478cbcc38SMaxime Ripard {
853454f01aSMaxime Ripard 	struct vc4_hvs *hvs = vc4->hvs;
8678cbcc38SMaxime Ripard 	u32 dispbase = HVS_READ(SCALER_DISPBASEX(channel));
8778cbcc38SMaxime Ripard 	/* Top/base are supposed to be 4-pixel aligned, but the
8878cbcc38SMaxime Ripard 	 * Raspberry Pi firmware fills the low bits (which are
8978cbcc38SMaxime Ripard 	 * presumably ignored).
9078cbcc38SMaxime Ripard 	 */
9178cbcc38SMaxime Ripard 	u32 top = VC4_GET_FIELD(dispbase, SCALER_DISPBASEX_TOP) & ~3;
9278cbcc38SMaxime Ripard 	u32 base = VC4_GET_FIELD(dispbase, SCALER_DISPBASEX_BASE) & ~3;
9378cbcc38SMaxime Ripard 
9478cbcc38SMaxime Ripard 	return top - base + 4;
9578cbcc38SMaxime Ripard }
9678cbcc38SMaxime Ripard 
vc4_crtc_get_scanout_position(struct drm_crtc * crtc,bool in_vblank_irq,int * vpos,int * hpos,ktime_t * stime,ktime_t * etime,const struct drm_display_mode * mode)973c8639ceSThomas Zimmermann static bool vc4_crtc_get_scanout_position(struct drm_crtc *crtc,
983c8639ceSThomas Zimmermann 					  bool in_vblank_irq,
993c8639ceSThomas Zimmermann 					  int *vpos, int *hpos,
1001bf59f1dSMario Kleiner 					  ktime_t *stime, ktime_t *etime,
1011bf59f1dSMario Kleiner 					  const struct drm_display_mode *mode)
1021bf59f1dSMario Kleiner {
1033c8639ceSThomas Zimmermann 	struct drm_device *dev = crtc->dev;
1041bf59f1dSMario Kleiner 	struct vc4_dev *vc4 = to_vc4_dev(dev);
1053454f01aSMaxime Ripard 	struct vc4_hvs *hvs = vc4->hvs;
106c77b9abfSShawn Guo 	struct vc4_crtc *vc4_crtc = to_vc4_crtc(crtc);
10787ebcd42SMaxime Ripard 	struct vc4_crtc_state *vc4_crtc_state = to_vc4_crtc_state(crtc->state);
10878cbcc38SMaxime Ripard 	unsigned int cob_size;
1091bf59f1dSMario Kleiner 	u32 val;
1101bf59f1dSMario Kleiner 	int fifo_lines;
1111bf59f1dSMario Kleiner 	int vblank_lines;
1121bf6ad62SDaniel Vetter 	bool ret = false;
1131bf59f1dSMario Kleiner 
1141bf59f1dSMario Kleiner 	/* preempt_disable_rt() should go right here in PREEMPT_RT patchset. */
1151bf59f1dSMario Kleiner 
1161bf59f1dSMario Kleiner 	/* Get optional system timestamp before query. */
1171bf59f1dSMario Kleiner 	if (stime)
1181bf59f1dSMario Kleiner 		*stime = ktime_get();
1191bf59f1dSMario Kleiner 
1201bf59f1dSMario Kleiner 	/*
1211bf59f1dSMario Kleiner 	 * Read vertical scanline which is currently composed for our
1221bf59f1dSMario Kleiner 	 * pixelvalve by the HVS, and also the scaler status.
1231bf59f1dSMario Kleiner 	 */
12487ebcd42SMaxime Ripard 	val = HVS_READ(SCALER_DISPSTATX(vc4_crtc_state->assigned_channel));
1251bf59f1dSMario Kleiner 
1261bf59f1dSMario Kleiner 	/* Get optional system timestamp after query. */
1271bf59f1dSMario Kleiner 	if (etime)
1281bf59f1dSMario Kleiner 		*etime = ktime_get();
1291bf59f1dSMario Kleiner 
1301bf59f1dSMario Kleiner 	/* preempt_enable_rt() should go right here in PREEMPT_RT patchset. */
1311bf59f1dSMario Kleiner 
1321bf59f1dSMario Kleiner 	/* Vertical position of hvs composed scanline. */
1331bf59f1dSMario Kleiner 	*vpos = VC4_GET_FIELD(val, SCALER_DISPSTATX_LINE);
1341bf59f1dSMario Kleiner 	*hpos = 0;
1351bf59f1dSMario Kleiner 
136e538092cSMario Kleiner 	if (mode->flags & DRM_MODE_FLAG_INTERLACE) {
137e538092cSMario Kleiner 		*vpos /= 2;
138e538092cSMario Kleiner 
139e538092cSMario Kleiner 		/* Use hpos to correct for field offset in interlaced mode. */
1403454f01aSMaxime Ripard 		if (vc4_hvs_get_fifo_frame_count(hvs, vc4_crtc_state->assigned_channel) % 2)
141e538092cSMario Kleiner 			*hpos += mode->crtc_htotal / 2;
142e538092cSMario Kleiner 	}
143e538092cSMario Kleiner 
14487ebcd42SMaxime Ripard 	cob_size = vc4_crtc_get_cob_allocation(vc4, vc4_crtc_state->assigned_channel);
1451bf59f1dSMario Kleiner 	/* This is the offset we need for translating hvs -> pv scanout pos. */
14678cbcc38SMaxime Ripard 	fifo_lines = cob_size / mode->crtc_hdisplay;
1471bf59f1dSMario Kleiner 
1481bf59f1dSMario Kleiner 	if (fifo_lines > 0)
1491bf6ad62SDaniel Vetter 		ret = true;
1501bf59f1dSMario Kleiner 
1511bf59f1dSMario Kleiner 	/* HVS more than fifo_lines into frame for compositing? */
1521bf59f1dSMario Kleiner 	if (*vpos > fifo_lines) {
1531bf59f1dSMario Kleiner 		/*
1541bf59f1dSMario Kleiner 		 * We are in active scanout and can get some meaningful results
1551bf59f1dSMario Kleiner 		 * from HVS. The actual PV scanout can not trail behind more
1561bf59f1dSMario Kleiner 		 * than fifo_lines as that is the fifo's capacity. Assume that
1571bf59f1dSMario Kleiner 		 * in active scanout the HVS and PV work in lockstep wrt. HVS
1581bf59f1dSMario Kleiner 		 * refilling the fifo and PV consuming from the fifo, ie.
1591bf59f1dSMario Kleiner 		 * whenever the PV consumes and frees up a scanline in the
1601bf59f1dSMario Kleiner 		 * fifo, the HVS will immediately refill it, therefore
1611bf59f1dSMario Kleiner 		 * incrementing vpos. Therefore we choose HVS read position -
1621bf59f1dSMario Kleiner 		 * fifo size in scanlines as a estimate of the real scanout
1631bf59f1dSMario Kleiner 		 * position of the PV.
1641bf59f1dSMario Kleiner 		 */
1651bf59f1dSMario Kleiner 		*vpos -= fifo_lines + 1;
1661bf59f1dSMario Kleiner 
1671bf59f1dSMario Kleiner 		return ret;
1681bf59f1dSMario Kleiner 	}
1691bf59f1dSMario Kleiner 
1701bf59f1dSMario Kleiner 	/*
1711bf59f1dSMario Kleiner 	 * Less: This happens when we are in vblank and the HVS, after getting
1721bf59f1dSMario Kleiner 	 * the VSTART restart signal from the PV, just started refilling its
1731bf59f1dSMario Kleiner 	 * fifo with new lines from the top-most lines of the new framebuffers.
1741bf59f1dSMario Kleiner 	 * The PV does not scan out in vblank, so does not remove lines from
1751bf59f1dSMario Kleiner 	 * the fifo, so the fifo will be full quickly and the HVS has to pause.
1761bf59f1dSMario Kleiner 	 * We can't get meaningful readings wrt. scanline position of the PV
1771bf59f1dSMario Kleiner 	 * and need to make things up in a approximative but consistent way.
1781bf59f1dSMario Kleiner 	 */
179682e62c4SEric Anholt 	vblank_lines = mode->vtotal - mode->vdisplay;
1801bf59f1dSMario Kleiner 
1811bf6ad62SDaniel Vetter 	if (in_vblank_irq) {
1821bf59f1dSMario Kleiner 		/*
1831bf59f1dSMario Kleiner 		 * Assume the irq handler got called close to first
1841bf59f1dSMario Kleiner 		 * line of vblank, so PV has about a full vblank
1851bf59f1dSMario Kleiner 		 * scanlines to go, and as a base timestamp use the
1861bf59f1dSMario Kleiner 		 * one taken at entry into vblank irq handler, so it
1871bf59f1dSMario Kleiner 		 * is not affected by random delays due to lock
1881bf59f1dSMario Kleiner 		 * contention on event_lock or vblank_time lock in
1891bf59f1dSMario Kleiner 		 * the core.
1901bf59f1dSMario Kleiner 		 */
1911bf59f1dSMario Kleiner 		*vpos = -vblank_lines;
1921bf59f1dSMario Kleiner 
1931bf59f1dSMario Kleiner 		if (stime)
1941bf59f1dSMario Kleiner 			*stime = vc4_crtc->t_vblank;
1951bf59f1dSMario Kleiner 		if (etime)
1961bf59f1dSMario Kleiner 			*etime = vc4_crtc->t_vblank;
1971bf59f1dSMario Kleiner 
1981bf59f1dSMario Kleiner 		/*
1991bf59f1dSMario Kleiner 		 * If the HVS fifo is not yet full then we know for certain
2001bf59f1dSMario Kleiner 		 * we are at the very beginning of vblank, as the hvs just
2011bf59f1dSMario Kleiner 		 * started refilling, and the stime and etime timestamps
2021bf59f1dSMario Kleiner 		 * truly correspond to start of vblank.
2031bf6ad62SDaniel Vetter 		 *
2041bf6ad62SDaniel Vetter 		 * Unfortunately there's no way to report this to upper levels
2051bf6ad62SDaniel Vetter 		 * and make it more useful.
2061bf59f1dSMario Kleiner 		 */
2071bf59f1dSMario Kleiner 	} else {
2081bf59f1dSMario Kleiner 		/*
2091bf59f1dSMario Kleiner 		 * No clue where we are inside vblank. Return a vpos of zero,
2101bf59f1dSMario Kleiner 		 * which will cause calling code to just return the etime
2111bf59f1dSMario Kleiner 		 * timestamp uncorrected. At least this is no worse than the
2121bf59f1dSMario Kleiner 		 * standard fallback.
2131bf59f1dSMario Kleiner 		 */
2141bf59f1dSMario Kleiner 		*vpos = 0;
2151bf59f1dSMario Kleiner 	}
2161bf59f1dSMario Kleiner 
2171bf59f1dSMario Kleiner 	return ret;
2181bf59f1dSMario Kleiner }
2191bf59f1dSMario Kleiner 
vc4_get_fifo_full_level(struct vc4_crtc * vc4_crtc,u32 format)220649abf2fSMaxime Ripard static u32 vc4_get_fifo_full_level(struct vc4_crtc *vc4_crtc, u32 format)
221c8b75bcaSEric Anholt {
222658a731cSMaxime Ripard 	const struct vc4_crtc_data *crtc_data = vc4_crtc_to_vc4_crtc_data(vc4_crtc);
223649abf2fSMaxime Ripard 	const struct vc4_pv_data *pv_data = vc4_crtc_to_vc4_pv_data(vc4_crtc);
224eb9dfdd1SDom Cobley 	struct vc4_dev *vc4 = to_vc4_dev(vc4_crtc->base.dev);
225649abf2fSMaxime Ripard 	u32 fifo_len_bytes = pv_data->fifo_depth;
226c8b75bcaSEric Anholt 
227649abf2fSMaxime Ripard 	/*
228649abf2fSMaxime Ripard 	 * Pixels are pulled from the HVS if the number of bytes is
229649abf2fSMaxime Ripard 	 * lower than the FIFO full level.
230649abf2fSMaxime Ripard 	 *
231649abf2fSMaxime Ripard 	 * The latency of the pixel fetch mechanism is 6 pixels, so we
232649abf2fSMaxime Ripard 	 * need to convert those 6 pixels in bytes, depending on the
233649abf2fSMaxime Ripard 	 * format, and then subtract that from the length of the FIFO
234649abf2fSMaxime Ripard 	 * to make sure we never end up in a situation where the FIFO
235649abf2fSMaxime Ripard 	 * is full.
236649abf2fSMaxime Ripard 	 */
237c8b75bcaSEric Anholt 	switch (format) {
238c8b75bcaSEric Anholt 	case PV_CONTROL_FORMAT_DSIV_16:
239c8b75bcaSEric Anholt 	case PV_CONTROL_FORMAT_DSIC_16:
240e58a5e6fSMaxime Ripard 		return fifo_len_bytes - 2 * HVS_FIFO_LATENCY_PIX;
241c8b75bcaSEric Anholt 	case PV_CONTROL_FORMAT_DSIV_18:
242c8b75bcaSEric Anholt 		return fifo_len_bytes - 14;
243c8b75bcaSEric Anholt 	case PV_CONTROL_FORMAT_24:
244c8b75bcaSEric Anholt 	case PV_CONTROL_FORMAT_DSIV_24:
245c8b75bcaSEric Anholt 	default:
246658a731cSMaxime Ripard 		/*
247658a731cSMaxime Ripard 		 * For some reason, the pixelvalve4 doesn't work with
248658a731cSMaxime Ripard 		 * the usual formula and will only work with 32.
249658a731cSMaxime Ripard 		 */
250658a731cSMaxime Ripard 		if (crtc_data->hvs_output == 5)
251658a731cSMaxime Ripard 			return 32;
252658a731cSMaxime Ripard 
253eb9dfdd1SDom Cobley 		/*
254eb9dfdd1SDom Cobley 		 * It looks like in some situations, we will overflow
255eb9dfdd1SDom Cobley 		 * the PixelValve FIFO (with the bit 10 of PV stat being
256eb9dfdd1SDom Cobley 		 * set) and stall the HVS / PV, eventually resulting in
257eb9dfdd1SDom Cobley 		 * a page flip timeout.
258eb9dfdd1SDom Cobley 		 *
259eb9dfdd1SDom Cobley 		 * Displaying the video overlay during a playback with
260eb9dfdd1SDom Cobley 		 * Kodi on an RPi3 seems to be a great solution with a
261eb9dfdd1SDom Cobley 		 * failure rate around 50%.
262eb9dfdd1SDom Cobley 		 *
263eb9dfdd1SDom Cobley 		 * Removing 1 from the FIFO full level however
264eb9dfdd1SDom Cobley 		 * seems to completely remove that issue.
265eb9dfdd1SDom Cobley 		 */
2661cbc91ebSMaxime Ripard 		if (!vc4->is_vc5)
267eb9dfdd1SDom Cobley 			return fifo_len_bytes - 3 * HVS_FIFO_LATENCY_PIX - 1;
268eb9dfdd1SDom Cobley 
269e58a5e6fSMaxime Ripard 		return fifo_len_bytes - 3 * HVS_FIFO_LATENCY_PIX;
270c8b75bcaSEric Anholt 	}
271c8b75bcaSEric Anholt }
272c8b75bcaSEric Anholt 
vc4_crtc_get_fifo_full_level_bits(struct vc4_crtc * vc4_crtc,u32 format)27362c5d55eSMaxime Ripard static u32 vc4_crtc_get_fifo_full_level_bits(struct vc4_crtc *vc4_crtc,
27462c5d55eSMaxime Ripard 					     u32 format)
27562c5d55eSMaxime Ripard {
27662c5d55eSMaxime Ripard 	u32 level = vc4_get_fifo_full_level(vc4_crtc, format);
277658a731cSMaxime Ripard 	u32 ret = 0;
27862c5d55eSMaxime Ripard 
279658a731cSMaxime Ripard 	ret |= VC4_SET_FIELD((level >> 6),
280658a731cSMaxime Ripard 			     PV5_CONTROL_FIFO_LEVEL_HIGH);
281658a731cSMaxime Ripard 
282658a731cSMaxime Ripard 	return ret | VC4_SET_FIELD(level & 0x3f,
28362c5d55eSMaxime Ripard 				   PV_CONTROL_FIFO_LEVEL);
28462c5d55eSMaxime Ripard }
28562c5d55eSMaxime Ripard 
286c8b75bcaSEric Anholt /*
287a86773d1SEric Anholt  * Returns the encoder attached to the CRTC.
288a86773d1SEric Anholt  *
289a86773d1SEric Anholt  * VC4 can only scan out to one encoder at a time, while the DRM core
290a86773d1SEric Anholt  * allows drivers to push pixels to more than one encoder from the
291a86773d1SEric Anholt  * same CRTC.
292c8b75bcaSEric Anholt  */
vc4_get_crtc_encoder(struct drm_crtc * crtc,struct drm_crtc_state * state)293d0229c36SMaxime Ripard struct drm_encoder *vc4_get_crtc_encoder(struct drm_crtc *crtc,
29494c1adc4SMaxime Ripard 					 struct drm_crtc_state *state)
295c8b75bcaSEric Anholt {
29694c1adc4SMaxime Ripard 	struct drm_encoder *encoder;
297c8b75bcaSEric Anholt 
29894c1adc4SMaxime Ripard 	WARN_ON(hweight32(state->encoder_mask) > 1);
2995a184d95SMaxime Ripard 
30094c1adc4SMaxime Ripard 	drm_for_each_encoder_mask(encoder, crtc->dev, state->encoder_mask)
30194c1adc4SMaxime Ripard 		return encoder;
302c8b75bcaSEric Anholt 
303a86773d1SEric Anholt 	return NULL;
304c8b75bcaSEric Anholt }
305c8b75bcaSEric Anholt 
vc4_crtc_pixelvalve_reset(struct drm_crtc * crtc)3065ffabf50SMaxime Ripard static void vc4_crtc_pixelvalve_reset(struct drm_crtc *crtc)
3075ffabf50SMaxime Ripard {
3085ffabf50SMaxime Ripard 	struct vc4_crtc *vc4_crtc = to_vc4_crtc(crtc);
309e0c95303SDanilo Krummrich 	struct drm_device *dev = crtc->dev;
310e0c95303SDanilo Krummrich 	int idx;
311e0c95303SDanilo Krummrich 
312e0c95303SDanilo Krummrich 	if (!drm_dev_enter(dev, &idx))
313e0c95303SDanilo Krummrich 		return;
3145ffabf50SMaxime Ripard 
3155ffabf50SMaxime Ripard 	/* The PV needs to be disabled before it can be flushed */
3165ffabf50SMaxime Ripard 	CRTC_WRITE(PV_CONTROL, CRTC_READ(PV_CONTROL) & ~PV_CONTROL_EN);
3175ffabf50SMaxime Ripard 	CRTC_WRITE(PV_CONTROL, CRTC_READ(PV_CONTROL) | PV_CONTROL_FIFO_CLR);
318e0c95303SDanilo Krummrich 
319e0c95303SDanilo Krummrich 	drm_dev_exit(idx);
3205ffabf50SMaxime Ripard }
3215ffabf50SMaxime Ripard 
vc4_crtc_config_pv(struct drm_crtc * crtc,struct drm_encoder * encoder,struct drm_atomic_state * state)322d6faf94aSMaxime Ripard static void vc4_crtc_config_pv(struct drm_crtc *crtc, struct drm_encoder *encoder,
323d6faf94aSMaxime Ripard 			       struct drm_atomic_state *state)
324c8b75bcaSEric Anholt {
325658a731cSMaxime Ripard 	struct drm_device *dev = crtc->dev;
326658a731cSMaxime Ripard 	struct vc4_dev *vc4 = to_vc4_dev(dev);
327a86773d1SEric Anholt 	struct vc4_encoder *vc4_encoder = to_vc4_encoder(encoder);
328c8b75bcaSEric Anholt 	struct vc4_crtc *vc4_crtc = to_vc4_crtc(crtc);
329644df22fSMaxime Ripard 	const struct vc4_pv_data *pv_data = vc4_crtc_to_vc4_pv_data(vc4_crtc);
330c6883985SMaxime Ripard 	struct drm_crtc_state *crtc_state = crtc->state;
331c6883985SMaxime Ripard 	struct drm_display_mode *mode = &crtc_state->adjusted_mode;
332c8b75bcaSEric Anholt 	bool interlace = mode->flags & DRM_MODE_FLAG_INTERLACE;
3333650062eSDave Stevenson 	bool is_hdmi = vc4_encoder->type == VC4_ENCODER_TYPE_HDMI0 ||
3343650062eSDave Stevenson 		       vc4_encoder->type == VC4_ENCODER_TYPE_HDMI1;
3353650062eSDave Stevenson 	u32 pixel_rep = ((mode->flags & DRM_MODE_FLAG_DBLCLK) && !is_hdmi) ? 2 : 1;
336a86773d1SEric Anholt 	bool is_dsi = (vc4_encoder->type == VC4_ENCODER_TYPE_DSI0 ||
337a86773d1SEric Anholt 		       vc4_encoder->type == VC4_ENCODER_TYPE_DSI1);
338edfe84aeSDave Stevenson 	bool is_dsi1 = vc4_encoder->type == VC4_ENCODER_TYPE_DSI1;
339273c4176SMateusz Kwiatkowski 	bool is_vec = vc4_encoder->type == VC4_ENCODER_TYPE_VEC;
340edfe84aeSDave Stevenson 	u32 format = is_dsi1 ? PV_CONTROL_FORMAT_DSIV_24 : PV_CONTROL_FORMAT_24;
341644df22fSMaxime Ripard 	u8 ppc = pv_data->pixels_per_clock;
342273c4176SMateusz Kwiatkowski 
343273c4176SMateusz Kwiatkowski 	u16 vert_bp = mode->crtc_vtotal - mode->crtc_vsync_end;
344273c4176SMateusz Kwiatkowski 	u16 vert_sync = mode->crtc_vsync_end - mode->crtc_vsync_start;
345273c4176SMateusz Kwiatkowski 	u16 vert_fp = mode->crtc_vsync_start - mode->crtc_vdisplay;
346273c4176SMateusz Kwiatkowski 
347be26296aSMaxime Ripard 	bool debug_dump_regs = false;
348e0c95303SDanilo Krummrich 	int idx;
349e0c95303SDanilo Krummrich 
350e0c95303SDanilo Krummrich 	if (!drm_dev_enter(dev, &idx))
351e0c95303SDanilo Krummrich 		return;
352be26296aSMaxime Ripard 
353be26296aSMaxime Ripard 	if (debug_dump_regs) {
354be26296aSMaxime Ripard 		struct drm_printer p = drm_info_printer(&vc4_crtc->pdev->dev);
355be26296aSMaxime Ripard 		dev_info(&vc4_crtc->pdev->dev, "CRTC %d regs before:\n",
356be26296aSMaxime Ripard 			 drm_crtc_index(crtc));
357be26296aSMaxime Ripard 		drm_print_regset32(&p, &vc4_crtc->regset);
358be26296aSMaxime Ripard 	}
359c8b75bcaSEric Anholt 
3605ffabf50SMaxime Ripard 	vc4_crtc_pixelvalve_reset(crtc);
361c8b75bcaSEric Anholt 
362c8b75bcaSEric Anholt 	CRTC_WRITE(PV_HORZA,
363644df22fSMaxime Ripard 		   VC4_SET_FIELD((mode->htotal - mode->hsync_end) * pixel_rep / ppc,
364c8b75bcaSEric Anholt 				 PV_HORZA_HBP) |
365644df22fSMaxime Ripard 		   VC4_SET_FIELD((mode->hsync_end - mode->hsync_start) * pixel_rep / ppc,
366c8b75bcaSEric Anholt 				 PV_HORZA_HSYNC));
367644df22fSMaxime Ripard 
368c8b75bcaSEric Anholt 	CRTC_WRITE(PV_HORZB,
369644df22fSMaxime Ripard 		   VC4_SET_FIELD((mode->hsync_start - mode->hdisplay) * pixel_rep / ppc,
370c8b75bcaSEric Anholt 				 PV_HORZB_HFP) |
371644df22fSMaxime Ripard 		   VC4_SET_FIELD(mode->hdisplay * pixel_rep / ppc,
372644df22fSMaxime Ripard 				 PV_HORZB_HACTIVE));
373c8b75bcaSEric Anholt 
374c8b75bcaSEric Anholt 	if (interlace) {
375273c4176SMateusz Kwiatkowski 		bool odd_field_first = false;
376273c4176SMateusz Kwiatkowski 		u32 field_delay = mode->htotal * pixel_rep / (2 * ppc);
377273c4176SMateusz Kwiatkowski 		u16 vert_bp_even = vert_bp;
378273c4176SMateusz Kwiatkowski 		u16 vert_fp_even = vert_fp;
379273c4176SMateusz Kwiatkowski 
380273c4176SMateusz Kwiatkowski 		if (is_vec) {
381273c4176SMateusz Kwiatkowski 			/* VEC (composite output) */
382273c4176SMateusz Kwiatkowski 			++field_delay;
383273c4176SMateusz Kwiatkowski 			if (mode->htotal == 858) {
384273c4176SMateusz Kwiatkowski 				/* 525-line mode (NTSC or PAL-M) */
385273c4176SMateusz Kwiatkowski 				odd_field_first = true;
386273c4176SMateusz Kwiatkowski 			}
387273c4176SMateusz Kwiatkowski 		}
388273c4176SMateusz Kwiatkowski 
389273c4176SMateusz Kwiatkowski 		if (odd_field_first)
390273c4176SMateusz Kwiatkowski 			++vert_fp_even;
391273c4176SMateusz Kwiatkowski 		else
392273c4176SMateusz Kwiatkowski 			++vert_bp;
393273c4176SMateusz Kwiatkowski 
394c8b75bcaSEric Anholt 		CRTC_WRITE(PV_VERTA_EVEN,
395273c4176SMateusz Kwiatkowski 			   VC4_SET_FIELD(vert_bp_even, PV_VERTA_VBP) |
396273c4176SMateusz Kwiatkowski 			   VC4_SET_FIELD(vert_sync, PV_VERTA_VSYNC));
397c8b75bcaSEric Anholt 		CRTC_WRITE(PV_VERTB_EVEN,
398273c4176SMateusz Kwiatkowski 			   VC4_SET_FIELD(vert_fp_even, PV_VERTB_VFP) |
399682e62c4SEric Anholt 			   VC4_SET_FIELD(mode->crtc_vdisplay, PV_VERTB_VACTIVE));
400682e62c4SEric Anholt 
401273c4176SMateusz Kwiatkowski 		/* We set up first field even mode for HDMI and VEC's PAL.
402273c4176SMateusz Kwiatkowski 		 * For NTSC, we need first field odd.
403682e62c4SEric Anholt 		 */
404682e62c4SEric Anholt 		CRTC_WRITE(PV_V_CONTROL,
405682e62c4SEric Anholt 			   PV_VCONTROL_CONTINUOUS |
406a86773d1SEric Anholt 			   (is_dsi ? PV_VCONTROL_DSI : 0) |
407682e62c4SEric Anholt 			   PV_VCONTROL_INTERLACE |
408273c4176SMateusz Kwiatkowski 			   (odd_field_first
409273c4176SMateusz Kwiatkowski 				   ? PV_VCONTROL_ODD_FIRST
410273c4176SMateusz Kwiatkowski 				   : VC4_SET_FIELD(field_delay,
411273c4176SMateusz Kwiatkowski 						   PV_VCONTROL_ODD_DELAY)));
412273c4176SMateusz Kwiatkowski 		CRTC_WRITE(PV_VSYNCD_EVEN,
413273c4176SMateusz Kwiatkowski 			   (odd_field_first ? field_delay : 0));
414682e62c4SEric Anholt 	} else {
415a86773d1SEric Anholt 		CRTC_WRITE(PV_V_CONTROL,
416a86773d1SEric Anholt 			   PV_VCONTROL_CONTINUOUS |
417a86773d1SEric Anholt 			   (is_dsi ? PV_VCONTROL_DSI : 0));
418273c4176SMateusz Kwiatkowski 		CRTC_WRITE(PV_VSYNCD_EVEN, 0);
419c8b75bcaSEric Anholt 	}
420c8b75bcaSEric Anholt 
421273c4176SMateusz Kwiatkowski 	CRTC_WRITE(PV_VERTA,
422273c4176SMateusz Kwiatkowski 		   VC4_SET_FIELD(vert_bp, PV_VERTA_VBP) |
423273c4176SMateusz Kwiatkowski 		   VC4_SET_FIELD(vert_sync, PV_VERTA_VSYNC));
424273c4176SMateusz Kwiatkowski 	CRTC_WRITE(PV_VERTB,
425273c4176SMateusz Kwiatkowski 		   VC4_SET_FIELD(vert_fp, PV_VERTB_VFP) |
426273c4176SMateusz Kwiatkowski 		   VC4_SET_FIELD(mode->crtc_vdisplay, PV_VERTB_VACTIVE));
427273c4176SMateusz Kwiatkowski 
428ebd11f70SMaxime Ripard 	if (is_dsi)
429dfccd937SEric Anholt 		CRTC_WRITE(PV_HACT_ACT, mode->hdisplay * pixel_rep);
430c8b75bcaSEric Anholt 
4311cbc91ebSMaxime Ripard 	if (vc4->is_vc5)
432658a731cSMaxime Ripard 		CRTC_WRITE(PV_MUX_CFG,
433658a731cSMaxime Ripard 			   VC4_SET_FIELD(PV_MUX_CFG_RGB_PIXEL_MUX_MODE_NO_SWAP,
434658a731cSMaxime Ripard 					 PV_MUX_CFG_RGB_PIXEL_MUX_MODE));
435658a731cSMaxime Ripard 
4369e30cfd0SMaxime Ripard 	CRTC_WRITE(PV_CONTROL, PV_CONTROL_FIFO_CLR |
43762c5d55eSMaxime Ripard 		   vc4_crtc_get_fifo_full_level_bits(vc4_crtc, format) |
438c8b75bcaSEric Anholt 		   VC4_SET_FIELD(format, PV_CONTROL_FORMAT) |
439dfccd937SEric Anholt 		   VC4_SET_FIELD(pixel_rep - 1, PV_CONTROL_PIXEL_REP) |
440c8b75bcaSEric Anholt 		   PV_CONTROL_CLR_AT_START |
441c8b75bcaSEric Anholt 		   PV_CONTROL_TRIGGER_UNDERFLOW |
442c8b75bcaSEric Anholt 		   PV_CONTROL_WAIT_HSTART |
443a86773d1SEric Anholt 		   VC4_SET_FIELD(vc4_encoder->clock_select,
444a5c4b75fSMaxime Ripard 				 PV_CONTROL_CLK_SELECT));
445e582b6c7SEric Anholt 
446c8b75bcaSEric Anholt 	if (debug_dump_regs) {
4473051719aSEric Anholt 		struct drm_printer p = drm_info_printer(&vc4_crtc->pdev->dev);
4483051719aSEric Anholt 		dev_info(&vc4_crtc->pdev->dev, "CRTC %d regs after:\n",
4493051719aSEric Anholt 			 drm_crtc_index(crtc));
4503051719aSEric Anholt 		drm_print_regset32(&p, &vc4_crtc->regset);
451c8b75bcaSEric Anholt 	}
452e0c95303SDanilo Krummrich 
453e0c95303SDanilo Krummrich 	drm_dev_exit(idx);
454c8b75bcaSEric Anholt }
455c8b75bcaSEric Anholt 
require_hvs_enabled(struct drm_device * dev)456c8b75bcaSEric Anholt static void require_hvs_enabled(struct drm_device *dev)
457c8b75bcaSEric Anholt {
458c8b75bcaSEric Anholt 	struct vc4_dev *vc4 = to_vc4_dev(dev);
4593454f01aSMaxime Ripard 	struct vc4_hvs *hvs = vc4->hvs;
460c8b75bcaSEric Anholt 
461c8b75bcaSEric Anholt 	WARN_ON_ONCE((HVS_READ(SCALER_DISPCTRL) & SCALER_DISPCTRL_ENABLE) !=
462c8b75bcaSEric Anholt 		     SCALER_DISPCTRL_ENABLE);
463c8b75bcaSEric Anholt }
464c8b75bcaSEric Anholt 
vc4_crtc_disable(struct drm_crtc * crtc,struct drm_encoder * encoder,struct drm_atomic_state * state,unsigned int channel)4658d914746SMaxime Ripard static int vc4_crtc_disable(struct drm_crtc *crtc,
466b601c16bSMaxime Ripard 			    struct drm_encoder *encoder,
4678d914746SMaxime Ripard 			    struct drm_atomic_state *state,
4688d914746SMaxime Ripard 			    unsigned int channel)
469c8b75bcaSEric Anholt {
470792c3132SMaxime Ripard 	struct vc4_encoder *vc4_encoder = to_vc4_encoder(encoder);
4712d14ffe2SMaxime Ripard 	struct vc4_crtc *vc4_crtc = to_vc4_crtc(crtc);
4722d14ffe2SMaxime Ripard 	struct drm_device *dev = crtc->dev;
4733454f01aSMaxime Ripard 	struct vc4_dev *vc4 = to_vc4_dev(dev);
474e0c95303SDanilo Krummrich 	int idx, ret;
475e0c95303SDanilo Krummrich 
476e0c95303SDanilo Krummrich 	if (!drm_dev_enter(dev, &idx))
477e0c95303SDanilo Krummrich 		return -ENODEV;
4788175287bSMaxime Ripard 
479c8b75bcaSEric Anholt 	CRTC_WRITE(PV_V_CONTROL,
480c8b75bcaSEric Anholt 		   CRTC_READ(PV_V_CONTROL) & ~PV_VCONTROL_VIDEN);
481c8b75bcaSEric Anholt 	ret = wait_for(!(CRTC_READ(PV_V_CONTROL) & PV_VCONTROL_VIDEN), 1);
482c8b75bcaSEric Anholt 	WARN_ONCE(ret, "Timeout waiting for !PV_VCONTROL_VIDEN\n");
483c8b75bcaSEric Anholt 
484b7cb67a6SMaxime Ripard 	/*
485b7cb67a6SMaxime Ripard 	 * This delay is needed to avoid to get a pixel stuck in an
486b7cb67a6SMaxime Ripard 	 * unflushable FIFO between the pixelvalve and the HDMI
487b7cb67a6SMaxime Ripard 	 * controllers on the BCM2711.
488b7cb67a6SMaxime Ripard 	 *
489b7cb67a6SMaxime Ripard 	 * Timing is fairly sensitive here, so mdelay is the safest
490b7cb67a6SMaxime Ripard 	 * approach.
491b7cb67a6SMaxime Ripard 	 *
492b7cb67a6SMaxime Ripard 	 * If it was to be reworked, the stuck pixel happens on a
493b7cb67a6SMaxime Ripard 	 * BCM2711 when changing mode with a good probability, so a
494b7cb67a6SMaxime Ripard 	 * script that changes mode on a regular basis should trigger
495b7cb67a6SMaxime Ripard 	 * the bug after less than 10 attempts. It manifests itself with
496b7cb67a6SMaxime Ripard 	 * every pixels being shifted by one to the right, and thus the
497b7cb67a6SMaxime Ripard 	 * last pixel of a line actually being displayed as the first
498b7cb67a6SMaxime Ripard 	 * pixel on the next line.
499b7cb67a6SMaxime Ripard 	 */
500b7cb67a6SMaxime Ripard 	mdelay(20);
501b7cb67a6SMaxime Ripard 
5022d14ffe2SMaxime Ripard 	if (vc4_encoder && vc4_encoder->post_crtc_disable)
5038d914746SMaxime Ripard 		vc4_encoder->post_crtc_disable(encoder, state);
504792c3132SMaxime Ripard 
5050d2b96afSMaxime Ripard 	vc4_crtc_pixelvalve_reset(crtc);
5063454f01aSMaxime Ripard 	vc4_hvs_stop_channel(vc4->hvs, channel);
507edeb729fSBoris Brezillon 
5082d14ffe2SMaxime Ripard 	if (vc4_encoder && vc4_encoder->post_crtc_powerdown)
5098d914746SMaxime Ripard 		vc4_encoder->post_crtc_powerdown(encoder, state);
510792c3132SMaxime Ripard 
511e0c95303SDanilo Krummrich 	drm_dev_exit(idx);
512e0c95303SDanilo Krummrich 
5132d14ffe2SMaxime Ripard 	return 0;
5142d14ffe2SMaxime Ripard }
5152d14ffe2SMaxime Ripard 
vc4_crtc_disable_at_boot(struct drm_crtc * crtc)516875a4d53SMaxime Ripard int vc4_crtc_disable_at_boot(struct drm_crtc *crtc)
517875a4d53SMaxime Ripard {
518875a4d53SMaxime Ripard 	struct drm_device *drm = crtc->dev;
5193454f01aSMaxime Ripard 	struct vc4_dev *vc4 = to_vc4_dev(drm);
520875a4d53SMaxime Ripard 	struct vc4_crtc *vc4_crtc = to_vc4_crtc(crtc);
521b601c16bSMaxime Ripard 	enum vc4_encoder_type encoder_type;
522b601c16bSMaxime Ripard 	const struct vc4_pv_data *pv_data;
523b601c16bSMaxime Ripard 	struct drm_encoder *encoder;
524bca10db6SMaxime Ripard 	struct vc4_hdmi *vc4_hdmi;
525b601c16bSMaxime Ripard 	unsigned encoder_sel;
526875a4d53SMaxime Ripard 	int channel;
527bca10db6SMaxime Ripard 	int ret;
528875a4d53SMaxime Ripard 
529875a4d53SMaxime Ripard 	if (!(of_device_is_compatible(vc4_crtc->pdev->dev.of_node,
530875a4d53SMaxime Ripard 				      "brcm,bcm2711-pixelvalve2") ||
531875a4d53SMaxime Ripard 	      of_device_is_compatible(vc4_crtc->pdev->dev.of_node,
532875a4d53SMaxime Ripard 				      "brcm,bcm2711-pixelvalve4")))
533875a4d53SMaxime Ripard 		return 0;
534875a4d53SMaxime Ripard 
535875a4d53SMaxime Ripard 	if (!(CRTC_READ(PV_CONTROL) & PV_CONTROL_EN))
536875a4d53SMaxime Ripard 		return 0;
537875a4d53SMaxime Ripard 
538875a4d53SMaxime Ripard 	if (!(CRTC_READ(PV_V_CONTROL) & PV_VCONTROL_VIDEN))
539875a4d53SMaxime Ripard 		return 0;
540875a4d53SMaxime Ripard 
5413454f01aSMaxime Ripard 	channel = vc4_hvs_get_fifo_from_output(vc4->hvs, vc4_crtc->data->hvs_output);
542875a4d53SMaxime Ripard 	if (channel < 0)
543875a4d53SMaxime Ripard 		return 0;
544875a4d53SMaxime Ripard 
545b601c16bSMaxime Ripard 	encoder_sel = VC4_GET_FIELD(CRTC_READ(PV_CONTROL), PV_CONTROL_CLK_SELECT);
546b601c16bSMaxime Ripard 	if (WARN_ON(encoder_sel != 0))
547b601c16bSMaxime Ripard 		return 0;
548b601c16bSMaxime Ripard 
549b601c16bSMaxime Ripard 	pv_data = vc4_crtc_to_vc4_pv_data(vc4_crtc);
550b601c16bSMaxime Ripard 	encoder_type = pv_data->encoder_types[encoder_sel];
5510656ce12SMaxime Ripard 	encoder = vc4_find_encoder_by_type(drm, encoder_type);
552b601c16bSMaxime Ripard 	if (WARN_ON(!encoder))
553b601c16bSMaxime Ripard 		return 0;
554b601c16bSMaxime Ripard 
555bca10db6SMaxime Ripard 	vc4_hdmi = encoder_to_vc4_hdmi(encoder);
556bca10db6SMaxime Ripard 	ret = pm_runtime_resume_and_get(&vc4_hdmi->pdev->dev);
557bca10db6SMaxime Ripard 	if (ret)
558bca10db6SMaxime Ripard 		return ret;
559bca10db6SMaxime Ripard 
560bca10db6SMaxime Ripard 	ret = vc4_crtc_disable(crtc, encoder, NULL, channel);
561bca10db6SMaxime Ripard 	if (ret)
562bca10db6SMaxime Ripard 		return ret;
563bca10db6SMaxime Ripard 
5646764eb69SMaxime Ripard 	/*
5656764eb69SMaxime Ripard 	 * post_crtc_powerdown will have called pm_runtime_put, so we
5666764eb69SMaxime Ripard 	 * don't need it here otherwise we'll get the reference counting
5676764eb69SMaxime Ripard 	 * wrong.
5686764eb69SMaxime Ripard 	 */
569bca10db6SMaxime Ripard 
570bca10db6SMaxime Ripard 	return 0;
571875a4d53SMaxime Ripard }
572875a4d53SMaxime Ripard 
vc4_crtc_send_vblank(struct drm_crtc * crtc)57368e4a69aSMaxime Ripard void vc4_crtc_send_vblank(struct drm_crtc *crtc)
57468e4a69aSMaxime Ripard {
57568e4a69aSMaxime Ripard 	struct drm_device *dev = crtc->dev;
57668e4a69aSMaxime Ripard 	unsigned long flags;
57768e4a69aSMaxime Ripard 
57868e4a69aSMaxime Ripard 	if (!crtc->state || !crtc->state->event)
57968e4a69aSMaxime Ripard 		return;
58068e4a69aSMaxime Ripard 
58168e4a69aSMaxime Ripard 	spin_lock_irqsave(&dev->event_lock, flags);
58268e4a69aSMaxime Ripard 	drm_crtc_send_vblank_event(crtc, crtc->state->event);
58368e4a69aSMaxime Ripard 	crtc->state->event = NULL;
58468e4a69aSMaxime Ripard 	spin_unlock_irqrestore(&dev->event_lock, flags);
58568e4a69aSMaxime Ripard }
58668e4a69aSMaxime Ripard 
vc4_crtc_atomic_disable(struct drm_crtc * crtc,struct drm_atomic_state * state)5872d14ffe2SMaxime Ripard static void vc4_crtc_atomic_disable(struct drm_crtc *crtc,
588351f950dSMaxime Ripard 				    struct drm_atomic_state *state)
5892d14ffe2SMaxime Ripard {
590351f950dSMaxime Ripard 	struct drm_crtc_state *old_state = drm_atomic_get_old_crtc_state(state,
591351f950dSMaxime Ripard 									 crtc);
5922d14ffe2SMaxime Ripard 	struct vc4_crtc_state *old_vc4_state = to_vc4_crtc_state(old_state);
59394c1adc4SMaxime Ripard 	struct drm_encoder *encoder = vc4_get_crtc_encoder(crtc, old_state);
5942d14ffe2SMaxime Ripard 	struct drm_device *dev = crtc->dev;
5952d14ffe2SMaxime Ripard 
596e1a7094bSMaxime Ripard 	drm_dbg(dev, "Disabling CRTC %s (%u) connected to Encoder %s (%u)",
597e1a7094bSMaxime Ripard 		crtc->name, crtc->base.id, encoder->name, encoder->base.id);
598e1a7094bSMaxime Ripard 
5992d14ffe2SMaxime Ripard 	require_hvs_enabled(dev);
6002d14ffe2SMaxime Ripard 
6012d14ffe2SMaxime Ripard 	/* Disable vblank irq handling before crtc is disabled. */
6022d14ffe2SMaxime Ripard 	drm_crtc_vblank_off(crtc);
6032d14ffe2SMaxime Ripard 
604b601c16bSMaxime Ripard 	vc4_crtc_disable(crtc, encoder, state, old_vc4_state->assigned_channel);
6052d14ffe2SMaxime Ripard 
606edeb729fSBoris Brezillon 	/*
607edeb729fSBoris Brezillon 	 * Make sure we issue a vblank event after disabling the CRTC if
608edeb729fSBoris Brezillon 	 * someone was waiting it.
609edeb729fSBoris Brezillon 	 */
61068e4a69aSMaxime Ripard 	vc4_crtc_send_vblank(crtc);
611c8b75bcaSEric Anholt }
612c8b75bcaSEric Anholt 
vc4_crtc_atomic_enable(struct drm_crtc * crtc,struct drm_atomic_state * state)6130b20a0f8SLaurent Pinchart static void vc4_crtc_atomic_enable(struct drm_crtc *crtc,
614351f950dSMaxime Ripard 				   struct drm_atomic_state *state)
615c8b75bcaSEric Anholt {
61694c1adc4SMaxime Ripard 	struct drm_crtc_state *new_state = drm_atomic_get_new_crtc_state(state,
61794c1adc4SMaxime Ripard 									 crtc);
618c8b75bcaSEric Anholt 	struct drm_device *dev = crtc->dev;
619c8b75bcaSEric Anholt 	struct vc4_crtc *vc4_crtc = to_vc4_crtc(crtc);
62094c1adc4SMaxime Ripard 	struct drm_encoder *encoder = vc4_get_crtc_encoder(crtc, new_state);
621792c3132SMaxime Ripard 	struct vc4_encoder *vc4_encoder = to_vc4_encoder(encoder);
622e0c95303SDanilo Krummrich 	int idx;
623c8b75bcaSEric Anholt 
624e1a7094bSMaxime Ripard 	drm_dbg(dev, "Enabling CRTC %s (%u) connected to Encoder %s (%u)",
625e1a7094bSMaxime Ripard 		crtc->name, crtc->base.id, encoder->name, encoder->base.id);
626e1a7094bSMaxime Ripard 
627e0c95303SDanilo Krummrich 	if (!drm_dev_enter(dev, &idx))
628e0c95303SDanilo Krummrich 		return;
629e0c95303SDanilo Krummrich 
630c8b75bcaSEric Anholt 	require_hvs_enabled(dev);
631c8b75bcaSEric Anholt 
6321ed134e6SBoris Brezillon 	/* Enable vblank irq handling before crtc is started otherwise
6331ed134e6SBoris Brezillon 	 * drm_crtc_get_vblank() fails in vc4_crtc_update_dlist().
6341ed134e6SBoris Brezillon 	 */
6351ed134e6SBoris Brezillon 	drm_crtc_vblank_on(crtc);
6361ed134e6SBoris Brezillon 
637ee6965c8SMaxime Ripard 	vc4_hvs_atomic_enable(crtc, state);
638c8b75bcaSEric Anholt 
639792c3132SMaxime Ripard 	if (vc4_encoder->pre_crtc_configure)
6408d914746SMaxime Ripard 		vc4_encoder->pre_crtc_configure(encoder, state);
641792c3132SMaxime Ripard 
642d6faf94aSMaxime Ripard 	vc4_crtc_config_pv(crtc, encoder, state);
6434b72b10aSMaxime Ripard 
6444b72b10aSMaxime Ripard 	CRTC_WRITE(PV_CONTROL, CRTC_READ(PV_CONTROL) | PV_CONTROL_EN);
6454b72b10aSMaxime Ripard 
646792c3132SMaxime Ripard 	if (vc4_encoder->pre_crtc_enable)
6478d914746SMaxime Ripard 		vc4_encoder->pre_crtc_enable(encoder, state);
648792c3132SMaxime Ripard 
649008095e0SBoris Brezillon 	/* When feeding the transposer block the pixelvalve is unneeded and
650008095e0SBoris Brezillon 	 * should not be enabled.
651008095e0SBoris Brezillon 	 */
652c8b75bcaSEric Anholt 	CRTC_WRITE(PV_V_CONTROL,
653c8b75bcaSEric Anholt 		   CRTC_READ(PV_V_CONTROL) | PV_VCONTROL_VIDEN);
654792c3132SMaxime Ripard 
655792c3132SMaxime Ripard 	if (vc4_encoder->post_crtc_enable)
6568d914746SMaxime Ripard 		vc4_encoder->post_crtc_enable(encoder, state);
657e0c95303SDanilo Krummrich 
658e0c95303SDanilo Krummrich 	drm_dev_exit(idx);
659c8b75bcaSEric Anholt }
660c8b75bcaSEric Anholt 
vc4_crtc_mode_valid(struct drm_crtc * crtc,const struct drm_display_mode * mode)661c50a115bSJose Abreu static enum drm_mode_status vc4_crtc_mode_valid(struct drm_crtc *crtc,
662c50a115bSJose Abreu 						const struct drm_display_mode *mode)
663acc1be1dSMario Kleiner {
66436451467SMario Kleiner 	/* Do not allow doublescan modes from user space */
665c50a115bSJose Abreu 	if (mode->flags & DRM_MODE_FLAG_DBLSCAN) {
66636451467SMario Kleiner 		DRM_DEBUG_KMS("[CRTC:%d] Doublescan mode rejected.\n",
66736451467SMario Kleiner 			      crtc->base.id);
668c50a115bSJose Abreu 		return MODE_NO_DBLESCAN;
66936451467SMario Kleiner 	}
67036451467SMario Kleiner 
671c50a115bSJose Abreu 	return MODE_OK;
672acc1be1dSMario Kleiner }
673acc1be1dSMario Kleiner 
vc4_crtc_get_margins(struct drm_crtc_state * state,unsigned int * left,unsigned int * right,unsigned int * top,unsigned int * bottom)674666e7358SBoris Brezillon void vc4_crtc_get_margins(struct drm_crtc_state *state,
675666e7358SBoris Brezillon 			  unsigned int *left, unsigned int *right,
676666e7358SBoris Brezillon 			  unsigned int *top, unsigned int *bottom)
677666e7358SBoris Brezillon {
678666e7358SBoris Brezillon 	struct vc4_crtc_state *vc4_state = to_vc4_crtc_state(state);
679666e7358SBoris Brezillon 	struct drm_connector_state *conn_state;
680666e7358SBoris Brezillon 	struct drm_connector *conn;
681666e7358SBoris Brezillon 	int i;
682666e7358SBoris Brezillon 
683666e7358SBoris Brezillon 	*left = vc4_state->margins.left;
684666e7358SBoris Brezillon 	*right = vc4_state->margins.right;
685666e7358SBoris Brezillon 	*top = vc4_state->margins.top;
686666e7358SBoris Brezillon 	*bottom = vc4_state->margins.bottom;
687666e7358SBoris Brezillon 
688666e7358SBoris Brezillon 	/* We have to interate over all new connector states because
689666e7358SBoris Brezillon 	 * vc4_crtc_get_margins() might be called before
690666e7358SBoris Brezillon 	 * vc4_crtc_atomic_check() which means margins info in vc4_crtc_state
691666e7358SBoris Brezillon 	 * might be outdated.
692666e7358SBoris Brezillon 	 */
693666e7358SBoris Brezillon 	for_each_new_connector_in_state(state->state, conn, conn_state, i) {
694666e7358SBoris Brezillon 		if (conn_state->crtc != state->crtc)
695666e7358SBoris Brezillon 			continue;
696666e7358SBoris Brezillon 
697666e7358SBoris Brezillon 		*left = conn_state->tv.margins.left;
698666e7358SBoris Brezillon 		*right = conn_state->tv.margins.right;
699666e7358SBoris Brezillon 		*top = conn_state->tv.margins.top;
700666e7358SBoris Brezillon 		*bottom = conn_state->tv.margins.bottom;
701666e7358SBoris Brezillon 		break;
702666e7358SBoris Brezillon 	}
703666e7358SBoris Brezillon }
704666e7358SBoris Brezillon 
vc4_crtc_atomic_check(struct drm_crtc * crtc,struct drm_atomic_state * state)705f759f5b5SMaxime Ripard int vc4_crtc_atomic_check(struct drm_crtc *crtc,
70629b77ad7SMaxime Ripard 			  struct drm_atomic_state *state)
707c8b75bcaSEric Anholt {
70829b77ad7SMaxime Ripard 	struct drm_crtc_state *crtc_state = drm_atomic_get_new_crtc_state(state,
70929b77ad7SMaxime Ripard 									  crtc);
71029b77ad7SMaxime Ripard 	struct vc4_crtc_state *vc4_state = to_vc4_crtc_state(crtc_state);
711008095e0SBoris Brezillon 	struct drm_connector *conn;
712008095e0SBoris Brezillon 	struct drm_connector_state *conn_state;
71316e10105SMaxime Ripard 	struct drm_encoder *encoder;
714008095e0SBoris Brezillon 	int ret, i;
715c8b75bcaSEric Anholt 
716ee6965c8SMaxime Ripard 	ret = vc4_hvs_atomic_check(crtc, state);
717d8dbf44fSEric Anholt 	if (ret)
718d8dbf44fSEric Anholt 		return ret;
719c8b75bcaSEric Anholt 
72016e10105SMaxime Ripard 	encoder = vc4_get_crtc_encoder(crtc, crtc_state);
72116e10105SMaxime Ripard 	if (encoder) {
72216e10105SMaxime Ripard 		const struct drm_display_mode *mode = &crtc_state->adjusted_mode;
72316e10105SMaxime Ripard 		struct vc4_encoder *vc4_encoder = to_vc4_encoder(encoder);
72416e10105SMaxime Ripard 
72516e10105SMaxime Ripard 		if (vc4_encoder->type == VC4_ENCODER_TYPE_HDMI0) {
726247a631fSDom Cobley 			vc4_state->hvs_load = max(mode->clock * mode->hdisplay / mode->htotal + 8000,
72716e10105SMaxime Ripard 						  mode->clock * 9 / 10) * 1000;
72816e10105SMaxime Ripard 		} else {
72916e10105SMaxime Ripard 			vc4_state->hvs_load = mode->clock * 1000;
73016e10105SMaxime Ripard 		}
73116e10105SMaxime Ripard 	}
73216e10105SMaxime Ripard 
733d74252bbSMaxime Ripard 	for_each_new_connector_in_state(state, conn, conn_state,
73429b77ad7SMaxime Ripard 					i) {
735008095e0SBoris Brezillon 		if (conn_state->crtc != crtc)
736008095e0SBoris Brezillon 			continue;
737008095e0SBoris Brezillon 
738666e7358SBoris Brezillon 		vc4_state->margins.left = conn_state->tv.margins.left;
739666e7358SBoris Brezillon 		vc4_state->margins.right = conn_state->tv.margins.right;
740666e7358SBoris Brezillon 		vc4_state->margins.top = conn_state->tv.margins.top;
741666e7358SBoris Brezillon 		vc4_state->margins.bottom = conn_state->tv.margins.bottom;
742008095e0SBoris Brezillon 		break;
743008095e0SBoris Brezillon 	}
744008095e0SBoris Brezillon 
745c8b75bcaSEric Anholt 	return 0;
746c8b75bcaSEric Anholt }
747c8b75bcaSEric Anholt 
vc4_enable_vblank(struct drm_crtc * crtc)7480d5f46faSShawn Guo static int vc4_enable_vblank(struct drm_crtc *crtc)
749c8b75bcaSEric Anholt {
750c77b9abfSShawn Guo 	struct vc4_crtc *vc4_crtc = to_vc4_crtc(crtc);
751e0c95303SDanilo Krummrich 	struct drm_device *dev = crtc->dev;
752e0c95303SDanilo Krummrich 	int idx;
753e0c95303SDanilo Krummrich 
754e0c95303SDanilo Krummrich 	if (!drm_dev_enter(dev, &idx))
755e0c95303SDanilo Krummrich 		return -ENODEV;
756c8b75bcaSEric Anholt 
757c8b75bcaSEric Anholt 	CRTC_WRITE(PV_INTEN, PV_INT_VFP_START);
758c8b75bcaSEric Anholt 
759e0c95303SDanilo Krummrich 	drm_dev_exit(idx);
760e0c95303SDanilo Krummrich 
761c8b75bcaSEric Anholt 	return 0;
762c8b75bcaSEric Anholt }
763c8b75bcaSEric Anholt 
vc4_disable_vblank(struct drm_crtc * crtc)7640d5f46faSShawn Guo static void vc4_disable_vblank(struct drm_crtc *crtc)
765c8b75bcaSEric Anholt {
766c77b9abfSShawn Guo 	struct vc4_crtc *vc4_crtc = to_vc4_crtc(crtc);
767e0c95303SDanilo Krummrich 	struct drm_device *dev = crtc->dev;
768e0c95303SDanilo Krummrich 	int idx;
769e0c95303SDanilo Krummrich 
770e0c95303SDanilo Krummrich 	if (!drm_dev_enter(dev, &idx))
771e0c95303SDanilo Krummrich 		return;
772c8b75bcaSEric Anholt 
773c8b75bcaSEric Anholt 	CRTC_WRITE(PV_INTEN, 0);
774e0c95303SDanilo Krummrich 
775e0c95303SDanilo Krummrich 	drm_dev_exit(idx);
776c8b75bcaSEric Anholt }
777c8b75bcaSEric Anholt 
vc4_crtc_handle_page_flip(struct vc4_crtc * vc4_crtc)778c8b75bcaSEric Anholt static void vc4_crtc_handle_page_flip(struct vc4_crtc *vc4_crtc)
779c8b75bcaSEric Anholt {
780c8b75bcaSEric Anholt 	struct drm_crtc *crtc = &vc4_crtc->base;
781c8b75bcaSEric Anholt 	struct drm_device *dev = crtc->dev;
78256d1fe09SMario Kleiner 	struct vc4_dev *vc4 = to_vc4_dev(dev);
7833454f01aSMaxime Ripard 	struct vc4_hvs *hvs = vc4->hvs;
784eeb6ab46SMaxime Ripard 	u32 chan = vc4_crtc->current_hvs_channel;
785c8b75bcaSEric Anholt 	unsigned long flags;
786c8b75bcaSEric Anholt 
787c8b75bcaSEric Anholt 	spin_lock_irqsave(&dev->event_lock, flags);
7880c250c15SMaxime Ripard 	spin_lock(&vc4_crtc->irq_lock);
78956d1fe09SMario Kleiner 	if (vc4_crtc->event &&
7900c250c15SMaxime Ripard 	    (vc4_crtc->current_dlist == HVS_READ(SCALER_DISPLACTX(chan)) ||
791a16c6640SMaxime Ripard 	     vc4_crtc->feeds_txp)) {
792c8b75bcaSEric Anholt 		drm_crtc_send_vblank_event(crtc, vc4_crtc->event);
793c8b75bcaSEric Anholt 		vc4_crtc->event = NULL;
794ee7c10e1SMario Kleiner 		drm_crtc_vblank_put(crtc);
795531a1b62SBoris Brezillon 
796531a1b62SBoris Brezillon 		/* Wait for the page flip to unmask the underrun to ensure that
797531a1b62SBoris Brezillon 		 * the display list was updated by the hardware. Before that
798531a1b62SBoris Brezillon 		 * happens, the HVS will be using the previous display list with
799531a1b62SBoris Brezillon 		 * the CRTC and encoder already reconfigured, leading to
800531a1b62SBoris Brezillon 		 * underruns. This can be seen when reconfiguring the CRTC.
801531a1b62SBoris Brezillon 		 */
8023454f01aSMaxime Ripard 		vc4_hvs_unmask_underrun(hvs, chan);
803c8b75bcaSEric Anholt 	}
8040c250c15SMaxime Ripard 	spin_unlock(&vc4_crtc->irq_lock);
805c8b75bcaSEric Anholt 	spin_unlock_irqrestore(&dev->event_lock, flags);
806c8b75bcaSEric Anholt }
807c8b75bcaSEric Anholt 
vc4_crtc_handle_vblank(struct vc4_crtc * crtc)808008095e0SBoris Brezillon void vc4_crtc_handle_vblank(struct vc4_crtc *crtc)
809008095e0SBoris Brezillon {
810008095e0SBoris Brezillon 	crtc->t_vblank = ktime_get();
811008095e0SBoris Brezillon 	drm_crtc_handle_vblank(&crtc->base);
812008095e0SBoris Brezillon 	vc4_crtc_handle_page_flip(crtc);
813008095e0SBoris Brezillon }
814008095e0SBoris Brezillon 
vc4_crtc_irq_handler(int irq,void * data)815c8b75bcaSEric Anholt static irqreturn_t vc4_crtc_irq_handler(int irq, void *data)
816c8b75bcaSEric Anholt {
817c8b75bcaSEric Anholt 	struct vc4_crtc *vc4_crtc = data;
818c8b75bcaSEric Anholt 	u32 stat = CRTC_READ(PV_INTSTAT);
819c8b75bcaSEric Anholt 	irqreturn_t ret = IRQ_NONE;
820c8b75bcaSEric Anholt 
821c8b75bcaSEric Anholt 	if (stat & PV_INT_VFP_START) {
822c8b75bcaSEric Anholt 		CRTC_WRITE(PV_INTSTAT, PV_INT_VFP_START);
823008095e0SBoris Brezillon 		vc4_crtc_handle_vblank(vc4_crtc);
824c8b75bcaSEric Anholt 		ret = IRQ_HANDLED;
825c8b75bcaSEric Anholt 	}
826c8b75bcaSEric Anholt 
827c8b75bcaSEric Anholt 	return ret;
828c8b75bcaSEric Anholt }
829c8b75bcaSEric Anholt 
830b501baccSEric Anholt struct vc4_async_flip_state {
831b501baccSEric Anholt 	struct drm_crtc *crtc;
832b501baccSEric Anholt 	struct drm_framebuffer *fb;
833f7aef1c2SBoris Brezillon 	struct drm_framebuffer *old_fb;
834b501baccSEric Anholt 	struct drm_pending_vblank_event *event;
835b501baccSEric Anholt 
8362523e9dcSMaxime Ripard 	union {
837d19e00eeSMaxime Ripard 		struct dma_fence_cb fence;
8382523e9dcSMaxime Ripard 		struct vc4_seqno_cb seqno;
8392523e9dcSMaxime Ripard 	} cb;
840b501baccSEric Anholt };
841b501baccSEric Anholt 
842b501baccSEric Anholt /* Called when the V3D execution for the BO being flipped to is done, so that
843b501baccSEric Anholt  * we can actually update the plane's address to point to it.
844b501baccSEric Anholt  */
845b501baccSEric Anholt static void
vc4_async_page_flip_complete(struct vc4_async_flip_state * flip_state)8462523e9dcSMaxime Ripard vc4_async_page_flip_complete(struct vc4_async_flip_state *flip_state)
847b501baccSEric Anholt {
848b501baccSEric Anholt 	struct drm_crtc *crtc = flip_state->crtc;
849b501baccSEric Anholt 	struct drm_device *dev = crtc->dev;
850b501baccSEric Anholt 	struct drm_plane *plane = crtc->primary;
851b501baccSEric Anholt 
852b501baccSEric Anholt 	vc4_plane_async_set_fb(plane, flip_state->fb);
853b501baccSEric Anholt 	if (flip_state->event) {
854b501baccSEric Anholt 		unsigned long flags;
855b501baccSEric Anholt 
856b501baccSEric Anholt 		spin_lock_irqsave(&dev->event_lock, flags);
857b501baccSEric Anholt 		drm_crtc_send_vblank_event(crtc, flip_state->event);
858b501baccSEric Anholt 		spin_unlock_irqrestore(&dev->event_lock, flags);
859b501baccSEric Anholt 	}
860b501baccSEric Anholt 
861ee7c10e1SMario Kleiner 	drm_crtc_vblank_put(crtc);
8621d5494e9SCihangir Akturk 	drm_framebuffer_put(flip_state->fb);
863f7aef1c2SBoris Brezillon 
8644d12c36fSMaxime Ripard 	if (flip_state->old_fb)
865f7aef1c2SBoris Brezillon 		drm_framebuffer_put(flip_state->old_fb);
866f7aef1c2SBoris Brezillon 
867b501baccSEric Anholt 	kfree(flip_state);
868b501baccSEric Anholt }
869b501baccSEric Anholt 
vc4_async_page_flip_seqno_complete(struct vc4_seqno_cb * cb)8702523e9dcSMaxime Ripard static void vc4_async_page_flip_seqno_complete(struct vc4_seqno_cb *cb)
8712523e9dcSMaxime Ripard {
8722523e9dcSMaxime Ripard 	struct vc4_async_flip_state *flip_state =
8732523e9dcSMaxime Ripard 		container_of(cb, struct vc4_async_flip_state, cb.seqno);
8744d12c36fSMaxime Ripard 	struct vc4_bo *bo = NULL;
8754d12c36fSMaxime Ripard 
8764d12c36fSMaxime Ripard 	if (flip_state->old_fb) {
8774a83c26aSDanilo Krummrich 		struct drm_gem_dma_object *dma_bo =
8786bcfe8eaSDanilo Krummrich 			drm_fb_dma_get_gem_obj(flip_state->old_fb, 0);
8794a83c26aSDanilo Krummrich 		bo = to_vc4_bo(&dma_bo->base);
8804d12c36fSMaxime Ripard 	}
8812523e9dcSMaxime Ripard 
8822523e9dcSMaxime Ripard 	vc4_async_page_flip_complete(flip_state);
8834d12c36fSMaxime Ripard 
8844d12c36fSMaxime Ripard 	/*
8854d12c36fSMaxime Ripard 	 * Decrement the BO usecnt in order to keep the inc/dec
8864d12c36fSMaxime Ripard 	 * calls balanced when the planes are updated through
8874d12c36fSMaxime Ripard 	 * the async update path.
888b501baccSEric Anholt 	 *
8894d12c36fSMaxime Ripard 	 * FIXME: we should move to generic async-page-flip when
8904d12c36fSMaxime Ripard 	 * it's available, so that we can get rid of this
8914d12c36fSMaxime Ripard 	 * hand-made cleanup_fb() logic.
892b501baccSEric Anholt 	 */
8934d12c36fSMaxime Ripard 	if (bo)
8944d12c36fSMaxime Ripard 		vc4_bo_dec_usecnt(bo);
8952523e9dcSMaxime Ripard }
8962523e9dcSMaxime Ripard 
vc4_async_page_flip_fence_complete(struct dma_fence * fence,struct dma_fence_cb * cb)897d19e00eeSMaxime Ripard static void vc4_async_page_flip_fence_complete(struct dma_fence *fence,
898d19e00eeSMaxime Ripard 					       struct dma_fence_cb *cb)
899d19e00eeSMaxime Ripard {
900d19e00eeSMaxime Ripard 	struct vc4_async_flip_state *flip_state =
901d19e00eeSMaxime Ripard 		container_of(cb, struct vc4_async_flip_state, cb.fence);
902d19e00eeSMaxime Ripard 
903d19e00eeSMaxime Ripard 	vc4_async_page_flip_complete(flip_state);
904d19e00eeSMaxime Ripard 	dma_fence_put(fence);
905d19e00eeSMaxime Ripard }
906d19e00eeSMaxime Ripard 
vc4_async_set_fence_cb(struct drm_device * dev,struct vc4_async_flip_state * flip_state)907d19e00eeSMaxime Ripard static int vc4_async_set_fence_cb(struct drm_device *dev,
908d19e00eeSMaxime Ripard 				  struct vc4_async_flip_state *flip_state)
909d19e00eeSMaxime Ripard {
910d19e00eeSMaxime Ripard 	struct drm_framebuffer *fb = flip_state->fb;
9114a83c26aSDanilo Krummrich 	struct drm_gem_dma_object *dma_bo = drm_fb_dma_get_gem_obj(fb, 0);
912d19e00eeSMaxime Ripard 	struct vc4_dev *vc4 = to_vc4_dev(dev);
913d19e00eeSMaxime Ripard 	struct dma_fence *fence;
914d19e00eeSMaxime Ripard 	int ret;
915d19e00eeSMaxime Ripard 
916d19e00eeSMaxime Ripard 	if (!vc4->is_vc5) {
9174a83c26aSDanilo Krummrich 		struct vc4_bo *bo = to_vc4_bo(&dma_bo->base);
918d19e00eeSMaxime Ripard 
919d19e00eeSMaxime Ripard 		return vc4_queue_seqno_cb(dev, &flip_state->cb.seqno, bo->seqno,
920d19e00eeSMaxime Ripard 					  vc4_async_page_flip_seqno_complete);
921d19e00eeSMaxime Ripard 	}
922d19e00eeSMaxime Ripard 
9234a83c26aSDanilo Krummrich 	ret = dma_resv_get_singleton(dma_bo->base.resv, DMA_RESV_USAGE_READ, &fence);
924d19e00eeSMaxime Ripard 	if (ret)
925d19e00eeSMaxime Ripard 		return ret;
926d19e00eeSMaxime Ripard 
927d19e00eeSMaxime Ripard 	/* If there's no fence, complete the page flip immediately */
928d19e00eeSMaxime Ripard 	if (!fence) {
929d19e00eeSMaxime Ripard 		vc4_async_page_flip_fence_complete(fence, &flip_state->cb.fence);
930d19e00eeSMaxime Ripard 		return 0;
931d19e00eeSMaxime Ripard 	}
932d19e00eeSMaxime Ripard 
933d19e00eeSMaxime Ripard 	/* If the fence has already been completed, complete the page flip */
934d19e00eeSMaxime Ripard 	if (dma_fence_add_callback(fence, &flip_state->cb.fence,
935d19e00eeSMaxime Ripard 				   vc4_async_page_flip_fence_complete))
936d19e00eeSMaxime Ripard 		vc4_async_page_flip_fence_complete(fence, &flip_state->cb.fence);
937d19e00eeSMaxime Ripard 
938d19e00eeSMaxime Ripard 	return 0;
939d19e00eeSMaxime Ripard }
940d19e00eeSMaxime Ripard 
941f6766fb2SMaxime Ripard static int
vc4_async_page_flip_common(struct drm_crtc * crtc,struct drm_framebuffer * fb,struct drm_pending_vblank_event * event,uint32_t flags)942f6766fb2SMaxime Ripard vc4_async_page_flip_common(struct drm_crtc *crtc,
943b501baccSEric Anholt 			   struct drm_framebuffer *fb,
944b501baccSEric Anholt 			   struct drm_pending_vblank_event *event,
945b501baccSEric Anholt 			   uint32_t flags)
946b501baccSEric Anholt {
947b501baccSEric Anholt 	struct drm_device *dev = crtc->dev;
948b501baccSEric Anholt 	struct drm_plane *plane = crtc->primary;
949b501baccSEric Anholt 	struct vc4_async_flip_state *flip_state;
950f7aef1c2SBoris Brezillon 
951b501baccSEric Anholt 	flip_state = kzalloc(sizeof(*flip_state), GFP_KERNEL);
952f6766fb2SMaxime Ripard 	if (!flip_state)
953b501baccSEric Anholt 		return -ENOMEM;
954b501baccSEric Anholt 
9551d5494e9SCihangir Akturk 	drm_framebuffer_get(fb);
956b501baccSEric Anholt 	flip_state->fb = fb;
957b501baccSEric Anholt 	flip_state->crtc = crtc;
958b501baccSEric Anholt 	flip_state->event = event;
959b501baccSEric Anholt 
960f7aef1c2SBoris Brezillon 	/* Save the current FB before it's replaced by the new one in
961f7aef1c2SBoris Brezillon 	 * drm_atomic_set_fb_for_plane(). We'll need the old FB in
962f7aef1c2SBoris Brezillon 	 * vc4_async_page_flip_complete() to decrement the BO usecnt and keep
963f7aef1c2SBoris Brezillon 	 * it consistent.
964f7aef1c2SBoris Brezillon 	 * FIXME: we should move to generic async-page-flip when it's
965f7aef1c2SBoris Brezillon 	 * available, so that we can get rid of this hand-made cleanup_fb()
966f7aef1c2SBoris Brezillon 	 * logic.
967f7aef1c2SBoris Brezillon 	 */
968f7aef1c2SBoris Brezillon 	flip_state->old_fb = plane->state->fb;
969f7aef1c2SBoris Brezillon 	if (flip_state->old_fb)
970f7aef1c2SBoris Brezillon 		drm_framebuffer_get(flip_state->old_fb);
971f7aef1c2SBoris Brezillon 
972ee7c10e1SMario Kleiner 	WARN_ON(drm_crtc_vblank_get(crtc) != 0);
973ee7c10e1SMario Kleiner 
974b501baccSEric Anholt 	/* Immediately update the plane's legacy fb pointer, so that later
975b501baccSEric Anholt 	 * modeset prep sees the state that will be present when the semaphore
976b501baccSEric Anholt 	 * is released.
977b501baccSEric Anholt 	 */
978b501baccSEric Anholt 	drm_atomic_set_fb_for_plane(plane->state, fb);
979b501baccSEric Anholt 
980d19e00eeSMaxime Ripard 	vc4_async_set_fence_cb(dev, flip_state);
981b501baccSEric Anholt 
982b501baccSEric Anholt 	/* Driver takes ownership of state on successful async commit. */
983b501baccSEric Anholt 	return 0;
984b501baccSEric Anholt }
985b501baccSEric Anholt 
986f6766fb2SMaxime Ripard /* Implements async (non-vblank-synced) page flips.
987f6766fb2SMaxime Ripard  *
988f6766fb2SMaxime Ripard  * The page flip ioctl needs to return immediately, so we grab the
989f6766fb2SMaxime Ripard  * modeset semaphore on the pipe, and queue the address update for
990f6766fb2SMaxime Ripard  * when V3D is done with the BO being flipped to.
991f6766fb2SMaxime Ripard  */
vc4_async_page_flip(struct drm_crtc * crtc,struct drm_framebuffer * fb,struct drm_pending_vblank_event * event,uint32_t flags)992f6766fb2SMaxime Ripard static int vc4_async_page_flip(struct drm_crtc *crtc,
993f6766fb2SMaxime Ripard 			       struct drm_framebuffer *fb,
994f6766fb2SMaxime Ripard 			       struct drm_pending_vblank_event *event,
995f6766fb2SMaxime Ripard 			       uint32_t flags)
996f6766fb2SMaxime Ripard {
997f6766fb2SMaxime Ripard 	struct drm_device *dev = crtc->dev;
998f6766fb2SMaxime Ripard 	struct vc4_dev *vc4 = to_vc4_dev(dev);
9994a83c26aSDanilo Krummrich 	struct drm_gem_dma_object *dma_bo = drm_fb_dma_get_gem_obj(fb, 0);
10004a83c26aSDanilo Krummrich 	struct vc4_bo *bo = to_vc4_bo(&dma_bo->base);
1001f6766fb2SMaxime Ripard 	int ret;
1002f6766fb2SMaxime Ripard 
1003f6766fb2SMaxime Ripard 	if (WARN_ON_ONCE(vc4->is_vc5))
1004f6766fb2SMaxime Ripard 		return -ENODEV;
1005f6766fb2SMaxime Ripard 
1006f6766fb2SMaxime Ripard 	/*
1007f6766fb2SMaxime Ripard 	 * Increment the BO usecnt here, so that we never end up with an
1008f6766fb2SMaxime Ripard 	 * unbalanced number of vc4_bo_{dec,inc}_usecnt() calls when the
1009f6766fb2SMaxime Ripard 	 * plane is later updated through the non-async path.
1010f6766fb2SMaxime Ripard 	 *
1011f6766fb2SMaxime Ripard 	 * FIXME: we should move to generic async-page-flip when
1012f6766fb2SMaxime Ripard 	 * it's available, so that we can get rid of this
1013f6766fb2SMaxime Ripard 	 * hand-made prepare_fb() logic.
1014f6766fb2SMaxime Ripard 	 */
1015f6766fb2SMaxime Ripard 	ret = vc4_bo_inc_usecnt(bo);
1016f6766fb2SMaxime Ripard 	if (ret)
1017f6766fb2SMaxime Ripard 		return ret;
1018f6766fb2SMaxime Ripard 
1019f6766fb2SMaxime Ripard 	ret = vc4_async_page_flip_common(crtc, fb, event, flags);
1020f6766fb2SMaxime Ripard 	if (ret) {
1021f6766fb2SMaxime Ripard 		vc4_bo_dec_usecnt(bo);
1022f6766fb2SMaxime Ripard 		return ret;
1023f6766fb2SMaxime Ripard 	}
1024f6766fb2SMaxime Ripard 
1025f6766fb2SMaxime Ripard 	return 0;
1026f6766fb2SMaxime Ripard }
1027f6766fb2SMaxime Ripard 
vc5_async_page_flip(struct drm_crtc * crtc,struct drm_framebuffer * fb,struct drm_pending_vblank_event * event,uint32_t flags)1028d87db1c7SMaxime Ripard static int vc5_async_page_flip(struct drm_crtc *crtc,
1029d87db1c7SMaxime Ripard 			       struct drm_framebuffer *fb,
1030d87db1c7SMaxime Ripard 			       struct drm_pending_vblank_event *event,
1031d87db1c7SMaxime Ripard 			       uint32_t flags)
1032d87db1c7SMaxime Ripard {
1033d87db1c7SMaxime Ripard 	return vc4_async_page_flip_common(crtc, fb, event, flags);
1034d87db1c7SMaxime Ripard }
1035d87db1c7SMaxime Ripard 
vc4_page_flip(struct drm_crtc * crtc,struct drm_framebuffer * fb,struct drm_pending_vblank_event * event,uint32_t flags,struct drm_modeset_acquire_ctx * ctx)1036bdd96472SMaxime Ripard int vc4_page_flip(struct drm_crtc *crtc,
1037b501baccSEric Anholt 		  struct drm_framebuffer *fb,
1038b501baccSEric Anholt 		  struct drm_pending_vblank_event *event,
103941292b1fSDaniel Vetter 		  uint32_t flags,
104041292b1fSDaniel Vetter 		  struct drm_modeset_acquire_ctx *ctx)
1041b501baccSEric Anholt {
1042d87db1c7SMaxime Ripard 	if (flags & DRM_MODE_PAGE_FLIP_ASYNC) {
1043d87db1c7SMaxime Ripard 		struct drm_device *dev = crtc->dev;
1044d87db1c7SMaxime Ripard 		struct vc4_dev *vc4 = to_vc4_dev(dev);
1045d87db1c7SMaxime Ripard 
1046d87db1c7SMaxime Ripard 		if (vc4->is_vc5)
1047d87db1c7SMaxime Ripard 			return vc5_async_page_flip(crtc, fb, event, flags);
1048b501baccSEric Anholt 		else
1049d87db1c7SMaxime Ripard 			return vc4_async_page_flip(crtc, fb, event, flags);
1050d87db1c7SMaxime Ripard 	} else {
105141292b1fSDaniel Vetter 		return drm_atomic_helper_page_flip(crtc, fb, event, flags, ctx);
1052b501baccSEric Anholt 	}
1053d87db1c7SMaxime Ripard }
1054b501baccSEric Anholt 
vc4_crtc_duplicate_state(struct drm_crtc * crtc)1055bdd96472SMaxime Ripard struct drm_crtc_state *vc4_crtc_duplicate_state(struct drm_crtc *crtc)
1056d8dbf44fSEric Anholt {
1057008095e0SBoris Brezillon 	struct vc4_crtc_state *vc4_state, *old_vc4_state;
1058d8dbf44fSEric Anholt 
1059d8dbf44fSEric Anholt 	vc4_state = kzalloc(sizeof(*vc4_state), GFP_KERNEL);
1060d8dbf44fSEric Anholt 	if (!vc4_state)
1061d8dbf44fSEric Anholt 		return NULL;
1062d8dbf44fSEric Anholt 
1063008095e0SBoris Brezillon 	old_vc4_state = to_vc4_crtc_state(crtc->state);
1064666e7358SBoris Brezillon 	vc4_state->margins = old_vc4_state->margins;
106587ebcd42SMaxime Ripard 	vc4_state->assigned_channel = old_vc4_state->assigned_channel;
1066008095e0SBoris Brezillon 
1067d8dbf44fSEric Anholt 	__drm_atomic_helper_crtc_duplicate_state(crtc, &vc4_state->base);
1068d8dbf44fSEric Anholt 	return &vc4_state->base;
1069d8dbf44fSEric Anholt }
1070d8dbf44fSEric Anholt 
vc4_crtc_destroy_state(struct drm_crtc * crtc,struct drm_crtc_state * state)1071bdd96472SMaxime Ripard void vc4_crtc_destroy_state(struct drm_crtc *crtc,
1072d8dbf44fSEric Anholt 			    struct drm_crtc_state *state)
1073d8dbf44fSEric Anholt {
1074d8dbf44fSEric Anholt 	struct vc4_dev *vc4 = to_vc4_dev(crtc->dev);
1075d8dbf44fSEric Anholt 	struct vc4_crtc_state *vc4_state = to_vc4_crtc_state(state);
1076d8dbf44fSEric Anholt 
107771724f70SChris Wilson 	if (drm_mm_node_allocated(&vc4_state->mm)) {
1078d8dbf44fSEric Anholt 		unsigned long flags;
1079d8dbf44fSEric Anholt 
1080d8dbf44fSEric Anholt 		spin_lock_irqsave(&vc4->hvs->mm_lock, flags);
1081d8dbf44fSEric Anholt 		drm_mm_remove_node(&vc4_state->mm);
1082d8dbf44fSEric Anholt 		spin_unlock_irqrestore(&vc4->hvs->mm_lock, flags);
1083d8dbf44fSEric Anholt 
1084d8dbf44fSEric Anholt 	}
1085d8dbf44fSEric Anholt 
10867622b255SEric Anholt 	drm_atomic_helper_crtc_destroy_state(crtc, state);
1087d8dbf44fSEric Anholt }
1088d8dbf44fSEric Anholt 
vc4_crtc_reset(struct drm_crtc * crtc)1089bdd96472SMaxime Ripard void vc4_crtc_reset(struct drm_crtc *crtc)
10906d6e5003SEric Anholt {
1091427c4a06SMaxime Ripard 	struct vc4_crtc_state *vc4_crtc_state;
1092427c4a06SMaxime Ripard 
10936d6e5003SEric Anholt 	if (crtc->state)
1094462ce5d9SMaarten Lankhorst 		vc4_crtc_destroy_state(crtc, crtc->state);
1095427c4a06SMaxime Ripard 
1096427c4a06SMaxime Ripard 	vc4_crtc_state = kzalloc(sizeof(*vc4_crtc_state), GFP_KERNEL);
1097427c4a06SMaxime Ripard 	if (!vc4_crtc_state) {
1098427c4a06SMaxime Ripard 		crtc->state = NULL;
1099427c4a06SMaxime Ripard 		return;
1100427c4a06SMaxime Ripard 	}
1101427c4a06SMaxime Ripard 
11028ba0b6d1SMaxime Ripard 	vc4_crtc_state->assigned_channel = VC4_HVS_CHANNEL_DISABLED;
1103427c4a06SMaxime Ripard 	__drm_atomic_helper_crtc_reset(crtc, &vc4_crtc_state->base);
11046d6e5003SEric Anholt }
11056d6e5003SEric Anholt 
vc4_crtc_late_register(struct drm_crtc * crtc)1106445b287eSMaxime Ripard int vc4_crtc_late_register(struct drm_crtc *crtc)
1107445b287eSMaxime Ripard {
1108445b287eSMaxime Ripard 	struct drm_device *drm = crtc->dev;
1109445b287eSMaxime Ripard 	struct vc4_crtc *vc4_crtc = to_vc4_crtc(crtc);
1110445b287eSMaxime Ripard 	const struct vc4_crtc_data *crtc_data = vc4_crtc_to_vc4_crtc_data(vc4_crtc);
1111445b287eSMaxime Ripard 
1112f2ede40eSMaíra Canal 	vc4_debugfs_add_regset32(drm, crtc_data->debugfs_name,
1113445b287eSMaxime Ripard 				 &vc4_crtc->regset);
1114445b287eSMaxime Ripard 
1115445b287eSMaxime Ripard 	return 0;
1116445b287eSMaxime Ripard }
1117445b287eSMaxime Ripard 
1118c8b75bcaSEric Anholt static const struct drm_crtc_funcs vc4_crtc_funcs = {
1119c8b75bcaSEric Anholt 	.set_config = drm_atomic_helper_set_config,
1120b501baccSEric Anholt 	.page_flip = vc4_page_flip,
1121c8b75bcaSEric Anholt 	.set_property = NULL,
1122c8b75bcaSEric Anholt 	.cursor_set = NULL, /* handled by drm_mode_cursor_universal */
1123c8b75bcaSEric Anholt 	.cursor_move = NULL, /* handled by drm_mode_cursor_universal */
11246d6e5003SEric Anholt 	.reset = vc4_crtc_reset,
1125d8dbf44fSEric Anholt 	.atomic_duplicate_state = vc4_crtc_duplicate_state,
1126d8dbf44fSEric Anholt 	.atomic_destroy_state = vc4_crtc_destroy_state,
11270d5f46faSShawn Guo 	.enable_vblank = vc4_enable_vblank,
11280d5f46faSShawn Guo 	.disable_vblank = vc4_disable_vblank,
11297e69ed6eSThomas Zimmermann 	.get_vblank_timestamp = drm_crtc_vblank_helper_get_vblank_timestamp,
1130445b287eSMaxime Ripard 	.late_register = vc4_crtc_late_register,
1131c8b75bcaSEric Anholt };
1132c8b75bcaSEric Anholt 
1133c8b75bcaSEric Anholt static const struct drm_crtc_helper_funcs vc4_crtc_helper_funcs = {
1134c50a115bSJose Abreu 	.mode_valid = vc4_crtc_mode_valid,
1135c8b75bcaSEric Anholt 	.atomic_check = vc4_crtc_atomic_check,
1136eeb6ab46SMaxime Ripard 	.atomic_begin = vc4_hvs_atomic_begin,
11378175287bSMaxime Ripard 	.atomic_flush = vc4_hvs_atomic_flush,
11380b20a0f8SLaurent Pinchart 	.atomic_enable = vc4_crtc_atomic_enable,
113964581714SLaurent Pinchart 	.atomic_disable = vc4_crtc_atomic_disable,
11403c8639ceSThomas Zimmermann 	.get_scanout_position = vc4_crtc_get_scanout_position,
1141c8b75bcaSEric Anholt };
1142c8b75bcaSEric Anholt 
1143f759f5b5SMaxime Ripard const struct vc4_pv_data bcm2835_pv0_data = {
11445a20ff8bSMaxime Ripard 	.base = {
11459a49bf09SMaxime Ripard 		.name = "pixelvalve-0",
11466bad4774SMaxime Ripard 		.debugfs_name = "crtc0_regs",
114787ebcd42SMaxime Ripard 		.hvs_available_channels = BIT(0),
11488ebb2cf0SMaxime Ripard 		.hvs_output = 0,
11495a20ff8bSMaxime Ripard 	},
1150649abf2fSMaxime Ripard 	.fifo_depth = 64,
1151644df22fSMaxime Ripard 	.pixels_per_clock = 1,
1152ab8df60eSBoris Brezillon 	.encoder_types = {
1153ab8df60eSBoris Brezillon 		[PV_CONTROL_CLK_SELECT_DSI] = VC4_ENCODER_TYPE_DSI0,
1154ab8df60eSBoris Brezillon 		[PV_CONTROL_CLK_SELECT_DPI_SMI_HDMI] = VC4_ENCODER_TYPE_DPI,
1155ab8df60eSBoris Brezillon 	},
1156c8b75bcaSEric Anholt };
1157c8b75bcaSEric Anholt 
1158f759f5b5SMaxime Ripard const struct vc4_pv_data bcm2835_pv1_data = {
11595a20ff8bSMaxime Ripard 	.base = {
11609a49bf09SMaxime Ripard 		.name = "pixelvalve-1",
11616bad4774SMaxime Ripard 		.debugfs_name = "crtc1_regs",
116287ebcd42SMaxime Ripard 		.hvs_available_channels = BIT(2),
11638ebb2cf0SMaxime Ripard 		.hvs_output = 2,
11645a20ff8bSMaxime Ripard 	},
1165649abf2fSMaxime Ripard 	.fifo_depth = 64,
1166644df22fSMaxime Ripard 	.pixels_per_clock = 1,
1167ab8df60eSBoris Brezillon 	.encoder_types = {
1168ab8df60eSBoris Brezillon 		[PV_CONTROL_CLK_SELECT_DSI] = VC4_ENCODER_TYPE_DSI1,
1169ab8df60eSBoris Brezillon 		[PV_CONTROL_CLK_SELECT_DPI_SMI_HDMI] = VC4_ENCODER_TYPE_SMI,
1170ab8df60eSBoris Brezillon 	},
1171c8b75bcaSEric Anholt };
1172c8b75bcaSEric Anholt 
1173f759f5b5SMaxime Ripard const struct vc4_pv_data bcm2835_pv2_data = {
11745a20ff8bSMaxime Ripard 	.base = {
11759a49bf09SMaxime Ripard 		.name = "pixelvalve-2",
11766bad4774SMaxime Ripard 		.debugfs_name = "crtc2_regs",
117787ebcd42SMaxime Ripard 		.hvs_available_channels = BIT(1),
11788ebb2cf0SMaxime Ripard 		.hvs_output = 1,
11795a20ff8bSMaxime Ripard 	},
1180649abf2fSMaxime Ripard 	.fifo_depth = 64,
1181644df22fSMaxime Ripard 	.pixels_per_clock = 1,
1182ab8df60eSBoris Brezillon 	.encoder_types = {
1183ed024b22SMaxime Ripard 		[PV_CONTROL_CLK_SELECT_DPI_SMI_HDMI] = VC4_ENCODER_TYPE_HDMI0,
1184ab8df60eSBoris Brezillon 		[PV_CONTROL_CLK_SELECT_VEC] = VC4_ENCODER_TYPE_VEC,
1185ab8df60eSBoris Brezillon 	},
1186c8b75bcaSEric Anholt };
1187c8b75bcaSEric Anholt 
1188f759f5b5SMaxime Ripard const struct vc4_pv_data bcm2711_pv0_data = {
1189658a731cSMaxime Ripard 	.base = {
11909a49bf09SMaxime Ripard 		.name = "pixelvalve-0",
11916bad4774SMaxime Ripard 		.debugfs_name = "crtc0_regs",
1192658a731cSMaxime Ripard 		.hvs_available_channels = BIT(0),
1193658a731cSMaxime Ripard 		.hvs_output = 0,
1194658a731cSMaxime Ripard 	},
1195658a731cSMaxime Ripard 	.fifo_depth = 64,
1196658a731cSMaxime Ripard 	.pixels_per_clock = 1,
1197658a731cSMaxime Ripard 	.encoder_types = {
1198658a731cSMaxime Ripard 		[0] = VC4_ENCODER_TYPE_DSI0,
1199658a731cSMaxime Ripard 		[1] = VC4_ENCODER_TYPE_DPI,
1200658a731cSMaxime Ripard 	},
1201658a731cSMaxime Ripard };
1202658a731cSMaxime Ripard 
1203f759f5b5SMaxime Ripard const struct vc4_pv_data bcm2711_pv1_data = {
1204658a731cSMaxime Ripard 	.base = {
12059a49bf09SMaxime Ripard 		.name = "pixelvalve-1",
12066bad4774SMaxime Ripard 		.debugfs_name = "crtc1_regs",
1207658a731cSMaxime Ripard 		.hvs_available_channels = BIT(0) | BIT(1) | BIT(2),
1208658a731cSMaxime Ripard 		.hvs_output = 3,
1209658a731cSMaxime Ripard 	},
1210658a731cSMaxime Ripard 	.fifo_depth = 64,
1211658a731cSMaxime Ripard 	.pixels_per_clock = 1,
1212658a731cSMaxime Ripard 	.encoder_types = {
1213658a731cSMaxime Ripard 		[0] = VC4_ENCODER_TYPE_DSI1,
1214658a731cSMaxime Ripard 		[1] = VC4_ENCODER_TYPE_SMI,
1215658a731cSMaxime Ripard 	},
1216658a731cSMaxime Ripard };
1217658a731cSMaxime Ripard 
1218f759f5b5SMaxime Ripard const struct vc4_pv_data bcm2711_pv2_data = {
1219658a731cSMaxime Ripard 	.base = {
12209a49bf09SMaxime Ripard 		.name = "pixelvalve-2",
12216bad4774SMaxime Ripard 		.debugfs_name = "crtc2_regs",
1222658a731cSMaxime Ripard 		.hvs_available_channels = BIT(0) | BIT(1) | BIT(2),
1223658a731cSMaxime Ripard 		.hvs_output = 4,
1224658a731cSMaxime Ripard 	},
1225658a731cSMaxime Ripard 	.fifo_depth = 256,
1226658a731cSMaxime Ripard 	.pixels_per_clock = 2,
1227658a731cSMaxime Ripard 	.encoder_types = {
1228658a731cSMaxime Ripard 		[0] = VC4_ENCODER_TYPE_HDMI0,
1229658a731cSMaxime Ripard 	},
1230658a731cSMaxime Ripard };
1231658a731cSMaxime Ripard 
1232f759f5b5SMaxime Ripard const struct vc4_pv_data bcm2711_pv3_data = {
1233658a731cSMaxime Ripard 	.base = {
12349a49bf09SMaxime Ripard 		.name = "pixelvalve-3",
12356bad4774SMaxime Ripard 		.debugfs_name = "crtc3_regs",
1236658a731cSMaxime Ripard 		.hvs_available_channels = BIT(1),
1237658a731cSMaxime Ripard 		.hvs_output = 1,
1238658a731cSMaxime Ripard 	},
1239658a731cSMaxime Ripard 	.fifo_depth = 64,
1240658a731cSMaxime Ripard 	.pixels_per_clock = 1,
1241658a731cSMaxime Ripard 	.encoder_types = {
1242fc7a8abcSMateusz Kwiatkowski 		[PV_CONTROL_CLK_SELECT_VEC] = VC4_ENCODER_TYPE_VEC,
1243658a731cSMaxime Ripard 	},
1244658a731cSMaxime Ripard };
1245658a731cSMaxime Ripard 
1246f759f5b5SMaxime Ripard const struct vc4_pv_data bcm2711_pv4_data = {
1247658a731cSMaxime Ripard 	.base = {
12489a49bf09SMaxime Ripard 		.name = "pixelvalve-4",
12496bad4774SMaxime Ripard 		.debugfs_name = "crtc4_regs",
1250658a731cSMaxime Ripard 		.hvs_available_channels = BIT(0) | BIT(1) | BIT(2),
1251658a731cSMaxime Ripard 		.hvs_output = 5,
1252658a731cSMaxime Ripard 	},
1253658a731cSMaxime Ripard 	.fifo_depth = 64,
1254658a731cSMaxime Ripard 	.pixels_per_clock = 2,
1255658a731cSMaxime Ripard 	.encoder_types = {
1256658a731cSMaxime Ripard 		[0] = VC4_ENCODER_TYPE_HDMI1,
1257658a731cSMaxime Ripard 	},
1258658a731cSMaxime Ripard };
1259658a731cSMaxime Ripard 
1260c8b75bcaSEric Anholt static const struct of_device_id vc4_crtc_dt_match[] = {
1261debf585cSMaxime Ripard 	{ .compatible = "brcm,bcm2835-pixelvalve0", .data = &bcm2835_pv0_data },
1262debf585cSMaxime Ripard 	{ .compatible = "brcm,bcm2835-pixelvalve1", .data = &bcm2835_pv1_data },
1263debf585cSMaxime Ripard 	{ .compatible = "brcm,bcm2835-pixelvalve2", .data = &bcm2835_pv2_data },
1264658a731cSMaxime Ripard 	{ .compatible = "brcm,bcm2711-pixelvalve0", .data = &bcm2711_pv0_data },
1265658a731cSMaxime Ripard 	{ .compatible = "brcm,bcm2711-pixelvalve1", .data = &bcm2711_pv1_data },
1266658a731cSMaxime Ripard 	{ .compatible = "brcm,bcm2711-pixelvalve2", .data = &bcm2711_pv2_data },
1267658a731cSMaxime Ripard 	{ .compatible = "brcm,bcm2711-pixelvalve3", .data = &bcm2711_pv3_data },
1268658a731cSMaxime Ripard 	{ .compatible = "brcm,bcm2711-pixelvalve4", .data = &bcm2711_pv4_data },
1269c8b75bcaSEric Anholt 	{}
1270c8b75bcaSEric Anholt };
1271c8b75bcaSEric Anholt 
vc4_set_crtc_possible_masks(struct drm_device * drm,struct drm_crtc * crtc)1272c8b75bcaSEric Anholt static void vc4_set_crtc_possible_masks(struct drm_device *drm,
1273c8b75bcaSEric Anholt 					struct drm_crtc *crtc)
1274c8b75bcaSEric Anholt {
1275c8b75bcaSEric Anholt 	struct vc4_crtc *vc4_crtc = to_vc4_crtc(crtc);
12765a20ff8bSMaxime Ripard 	const struct vc4_pv_data *pv_data = vc4_crtc_to_vc4_pv_data(vc4_crtc);
12775a20ff8bSMaxime Ripard 	const enum vc4_encoder_type *encoder_types = pv_data->encoder_types;
1278c8b75bcaSEric Anholt 	struct drm_encoder *encoder;
1279c8b75bcaSEric Anholt 
1280c8b75bcaSEric Anholt 	drm_for_each_encoder(encoder, drm) {
1281008095e0SBoris Brezillon 		struct vc4_encoder *vc4_encoder;
1282ab8df60eSBoris Brezillon 		int i;
1283c8b75bcaSEric Anholt 
128447a50743SMaxime Ripard 		if (encoder->encoder_type == DRM_MODE_ENCODER_VIRTUAL)
128547a50743SMaxime Ripard 			continue;
128647a50743SMaxime Ripard 
1287008095e0SBoris Brezillon 		vc4_encoder = to_vc4_encoder(encoder);
12885a20ff8bSMaxime Ripard 		for (i = 0; i < ARRAY_SIZE(pv_data->encoder_types); i++) {
1289ab8df60eSBoris Brezillon 			if (vc4_encoder->type == encoder_types[i]) {
1290ab8df60eSBoris Brezillon 				vc4_encoder->clock_select = i;
1291c8b75bcaSEric Anholt 				encoder->possible_crtcs |= drm_crtc_mask(crtc);
1292ab8df60eSBoris Brezillon 				break;
1293ab8df60eSBoris Brezillon 			}
1294c8b75bcaSEric Anholt 		}
1295c8b75bcaSEric Anholt 	}
1296c8b75bcaSEric Anholt }
1297c8b75bcaSEric Anholt 
1298ee33ac27SMaxime Ripard /**
1299ee33ac27SMaxime Ripard  * __vc4_crtc_init - Initializes a CRTC
1300ee33ac27SMaxime Ripard  * @drm: DRM Device
1301ee33ac27SMaxime Ripard  * @pdev: CRTC Platform Device
1302ee33ac27SMaxime Ripard  * @vc4_crtc: CRTC Object to Initialize
1303ee33ac27SMaxime Ripard  * @data: Configuration data associated with this CRTC
1304ee33ac27SMaxime Ripard  * @primary_plane: Primary plane for CRTC
1305ee33ac27SMaxime Ripard  * @crtc_funcs: Callbacks for the new CRTC
1306ee33ac27SMaxime Ripard  * @crtc_helper_funcs: Helper Callbacks for the new CRTC
1307ee33ac27SMaxime Ripard  * @feeds_txp: Is this CRTC connected to the TXP?
1308ee33ac27SMaxime Ripard  *
1309ee33ac27SMaxime Ripard  * Initializes our private CRTC structure. This function is mostly
1310ee33ac27SMaxime Ripard  * relevant for KUnit testing, all other users should use
1311ee33ac27SMaxime Ripard  * vc4_crtc_init() instead.
1312ee33ac27SMaxime Ripard  *
1313ee33ac27SMaxime Ripard  * Returns:
1314ee33ac27SMaxime Ripard  * 0 on success, a negative error code on failure.
1315ee33ac27SMaxime Ripard  */
__vc4_crtc_init(struct drm_device * drm,struct platform_device * pdev,struct vc4_crtc * vc4_crtc,const struct vc4_crtc_data * data,struct drm_plane * primary_plane,const struct drm_crtc_funcs * crtc_funcs,const struct drm_crtc_helper_funcs * crtc_helper_funcs,bool feeds_txp)1316ee33ac27SMaxime Ripard int __vc4_crtc_init(struct drm_device *drm,
1317ee33ac27SMaxime Ripard 		    struct platform_device *pdev,
13183f98076fSMaxime Ripard 		    struct vc4_crtc *vc4_crtc,
13193f98076fSMaxime Ripard 		    const struct vc4_crtc_data *data,
1320ee33ac27SMaxime Ripard 		    struct drm_plane *primary_plane,
13215fefc601SMaxime Ripard 		    const struct drm_crtc_funcs *crtc_funcs,
13223f98076fSMaxime Ripard 		    const struct drm_crtc_helper_funcs *crtc_helper_funcs,
13233f98076fSMaxime Ripard 		    bool feeds_txp)
13245fefc601SMaxime Ripard {
1325eb92bc72SMaxime Ripard 	struct vc4_dev *vc4 = to_vc4_dev(drm);
13265fefc601SMaxime Ripard 	struct drm_crtc *crtc = &vc4_crtc->base;
13275fefc601SMaxime Ripard 	unsigned int i;
132877ef4c17SMaxime Ripard 	int ret;
13295fefc601SMaxime Ripard 
13303f98076fSMaxime Ripard 	vc4_crtc->data = data;
13313f98076fSMaxime Ripard 	vc4_crtc->pdev = pdev;
13323f98076fSMaxime Ripard 	vc4_crtc->feeds_txp = feeds_txp;
13330c250c15SMaxime Ripard 	spin_lock_init(&vc4_crtc->irq_lock);
133477ef4c17SMaxime Ripard 	ret = drmm_crtc_init_with_planes(drm, crtc, primary_plane, NULL,
13359a49bf09SMaxime Ripard 					 crtc_funcs, data->name);
133677ef4c17SMaxime Ripard 	if (ret)
133777ef4c17SMaxime Ripard 		return ret;
133877ef4c17SMaxime Ripard 
13395fefc601SMaxime Ripard 	drm_crtc_helper_add(crtc, crtc_helper_funcs);
1340eb92bc72SMaxime Ripard 
13411cbc91ebSMaxime Ripard 	if (!vc4->is_vc5) {
13425fefc601SMaxime Ripard 		drm_mode_crtc_set_gamma_size(crtc, ARRAY_SIZE(vc4_crtc->lut_r));
1343eb92bc72SMaxime Ripard 
13445fefc601SMaxime Ripard 		drm_crtc_enable_color_mgmt(crtc, 0, false, crtc->gamma_size);
13455fefc601SMaxime Ripard 
13465fefc601SMaxime Ripard 		/* We support CTM, but only for one CRTC at a time. It's therefore
13475fefc601SMaxime Ripard 		 * implemented as private driver state in vc4_kms, not here.
13485fefc601SMaxime Ripard 		 */
13495fefc601SMaxime Ripard 		drm_crtc_enable_color_mgmt(crtc, 0, true, crtc->gamma_size);
1350eb92bc72SMaxime Ripard 	}
13515fefc601SMaxime Ripard 
13525fefc601SMaxime Ripard 	for (i = 0; i < crtc->gamma_size; i++) {
13535fefc601SMaxime Ripard 		vc4_crtc->lut_r[i] = i;
13545fefc601SMaxime Ripard 		vc4_crtc->lut_g[i] = i;
13555fefc601SMaxime Ripard 		vc4_crtc->lut_b[i] = i;
13565fefc601SMaxime Ripard 	}
13575fefc601SMaxime Ripard 
13585fefc601SMaxime Ripard 	return 0;
13595fefc601SMaxime Ripard }
13605fefc601SMaxime Ripard 
vc4_crtc_init(struct drm_device * drm,struct platform_device * pdev,struct vc4_crtc * vc4_crtc,const struct vc4_crtc_data * data,const struct drm_crtc_funcs * crtc_funcs,const struct drm_crtc_helper_funcs * crtc_helper_funcs,bool feeds_txp)1361ee33ac27SMaxime Ripard int vc4_crtc_init(struct drm_device *drm, struct platform_device *pdev,
1362ee33ac27SMaxime Ripard 		  struct vc4_crtc *vc4_crtc,
1363ee33ac27SMaxime Ripard 		  const struct vc4_crtc_data *data,
1364ee33ac27SMaxime Ripard 		  const struct drm_crtc_funcs *crtc_funcs,
1365ee33ac27SMaxime Ripard 		  const struct drm_crtc_helper_funcs *crtc_helper_funcs,
1366ee33ac27SMaxime Ripard 		  bool feeds_txp)
1367ee33ac27SMaxime Ripard {
1368ee33ac27SMaxime Ripard 	struct drm_plane *primary_plane;
1369ee33ac27SMaxime Ripard 
1370ee33ac27SMaxime Ripard 	/* For now, we create just the primary and the legacy cursor
1371ee33ac27SMaxime Ripard 	 * planes.  We should be able to stack more planes on easily,
1372ee33ac27SMaxime Ripard 	 * but to do that we would need to compute the bandwidth
1373ee33ac27SMaxime Ripard 	 * requirement of the plane configuration, and reject ones
1374ee33ac27SMaxime Ripard 	 * that will take too much.
1375ee33ac27SMaxime Ripard 	 */
1376ee33ac27SMaxime Ripard 	primary_plane = vc4_plane_init(drm, DRM_PLANE_TYPE_PRIMARY, 0);
1377ee33ac27SMaxime Ripard 	if (IS_ERR(primary_plane)) {
1378ee33ac27SMaxime Ripard 		dev_err(drm->dev, "failed to construct primary plane\n");
1379ee33ac27SMaxime Ripard 		return PTR_ERR(primary_plane);
1380ee33ac27SMaxime Ripard 	}
1381ee33ac27SMaxime Ripard 
1382ee33ac27SMaxime Ripard 	return __vc4_crtc_init(drm, pdev, vc4_crtc, data, primary_plane,
1383ee33ac27SMaxime Ripard 			       crtc_funcs, crtc_helper_funcs, feeds_txp);
1384ee33ac27SMaxime Ripard }
1385ee33ac27SMaxime Ripard 
vc4_crtc_bind(struct device * dev,struct device * master,void * data)1386c8b75bcaSEric Anholt static int vc4_crtc_bind(struct device *dev, struct device *master, void *data)
1387c8b75bcaSEric Anholt {
1388c8b75bcaSEric Anholt 	struct platform_device *pdev = to_platform_device(dev);
1389c8b75bcaSEric Anholt 	struct drm_device *drm = dev_get_drvdata(master);
13905a20ff8bSMaxime Ripard 	const struct vc4_pv_data *pv_data;
1391c8b75bcaSEric Anholt 	struct vc4_crtc *vc4_crtc;
1392c8b75bcaSEric Anholt 	struct drm_crtc *crtc;
13935fefc601SMaxime Ripard 	int ret;
1394c8b75bcaSEric Anholt 
13957cc4214cSMaxime Ripard 	vc4_crtc = drmm_kzalloc(drm, sizeof(*vc4_crtc), GFP_KERNEL);
1396c8b75bcaSEric Anholt 	if (!vc4_crtc)
1397c8b75bcaSEric Anholt 		return -ENOMEM;
1398c8b75bcaSEric Anholt 	crtc = &vc4_crtc->base;
1399c8b75bcaSEric Anholt 
14007678142fSMaxime Ripard 	pv_data = of_device_get_match_data(dev);
14017678142fSMaxime Ripard 	if (!pv_data)
1402c8b75bcaSEric Anholt 		return -ENODEV;
1403c8b75bcaSEric Anholt 
1404c8b75bcaSEric Anholt 	vc4_crtc->regs = vc4_ioremap_regs(pdev, 0);
1405c8b75bcaSEric Anholt 	if (IS_ERR(vc4_crtc->regs))
1406c8b75bcaSEric Anholt 		return PTR_ERR(vc4_crtc->regs);
1407c8b75bcaSEric Anholt 
14083051719aSEric Anholt 	vc4_crtc->regset.base = vc4_crtc->regs;
14093051719aSEric Anholt 	vc4_crtc->regset.regs = crtc_regs;
14103051719aSEric Anholt 	vc4_crtc->regset.nregs = ARRAY_SIZE(crtc_regs);
14113051719aSEric Anholt 
14123f98076fSMaxime Ripard 	ret = vc4_crtc_init(drm, pdev, vc4_crtc, &pv_data->base,
14133f98076fSMaxime Ripard 			    &vc4_crtc_funcs, &vc4_crtc_helper_funcs,
14143f98076fSMaxime Ripard 			    false);
14155fefc601SMaxime Ripard 	if (ret)
14165fefc601SMaxime Ripard 		return ret;
14175fefc601SMaxime Ripard 	vc4_set_crtc_possible_masks(drm, crtc);
14181bf59f1dSMario Kleiner 
1419c8b75bcaSEric Anholt 	CRTC_WRITE(PV_INTEN, 0);
1420c8b75bcaSEric Anholt 	CRTC_WRITE(PV_INTSTAT, PV_INT_VFP_START);
1421c8b75bcaSEric Anholt 	ret = devm_request_irq(dev, platform_get_irq(pdev, 0),
1422a1962d6eSMaxime Ripard 			       vc4_crtc_irq_handler,
1423a1962d6eSMaxime Ripard 			       IRQF_SHARED,
1424a1962d6eSMaxime Ripard 			       "vc4 crtc", vc4_crtc);
1425c8b75bcaSEric Anholt 	if (ret)
142602792a93SMaxime Ripard 		return ret;
1427c8b75bcaSEric Anholt 
1428c8b75bcaSEric Anholt 	platform_set_drvdata(pdev, vc4_crtc);
1429c8b75bcaSEric Anholt 
1430c8b75bcaSEric Anholt 	return 0;
1431c8b75bcaSEric Anholt }
1432c8b75bcaSEric Anholt 
vc4_crtc_unbind(struct device * dev,struct device * master,void * data)1433c8b75bcaSEric Anholt static void vc4_crtc_unbind(struct device *dev, struct device *master,
1434c8b75bcaSEric Anholt 			    void *data)
1435c8b75bcaSEric Anholt {
1436c8b75bcaSEric Anholt 	struct platform_device *pdev = to_platform_device(dev);
1437c8b75bcaSEric Anholt 	struct vc4_crtc *vc4_crtc = dev_get_drvdata(dev);
1438c8b75bcaSEric Anholt 
1439c8b75bcaSEric Anholt 	CRTC_WRITE(PV_INTEN, 0);
1440c8b75bcaSEric Anholt 
1441c8b75bcaSEric Anholt 	platform_set_drvdata(pdev, NULL);
1442c8b75bcaSEric Anholt }
1443c8b75bcaSEric Anholt 
1444c8b75bcaSEric Anholt static const struct component_ops vc4_crtc_ops = {
1445c8b75bcaSEric Anholt 	.bind   = vc4_crtc_bind,
1446c8b75bcaSEric Anholt 	.unbind = vc4_crtc_unbind,
1447c8b75bcaSEric Anholt };
1448c8b75bcaSEric Anholt 
vc4_crtc_dev_probe(struct platform_device * pdev)1449c8b75bcaSEric Anholt static int vc4_crtc_dev_probe(struct platform_device *pdev)
1450c8b75bcaSEric Anholt {
1451c8b75bcaSEric Anholt 	return component_add(&pdev->dev, &vc4_crtc_ops);
1452c8b75bcaSEric Anholt }
1453c8b75bcaSEric Anholt 
vc4_crtc_dev_remove(struct platform_device * pdev)14541ed54a19SUwe Kleine-König static void vc4_crtc_dev_remove(struct platform_device *pdev)
1455c8b75bcaSEric Anholt {
1456c8b75bcaSEric Anholt 	component_del(&pdev->dev, &vc4_crtc_ops);
1457c8b75bcaSEric Anholt }
1458c8b75bcaSEric Anholt 
1459c8b75bcaSEric Anholt struct platform_driver vc4_crtc_driver = {
1460c8b75bcaSEric Anholt 	.probe = vc4_crtc_dev_probe,
14611ed54a19SUwe Kleine-König 	.remove_new = vc4_crtc_dev_remove,
1462c8b75bcaSEric Anholt 	.driver = {
1463c8b75bcaSEric Anholt 		.name = "vc4_crtc",
1464c8b75bcaSEric Anholt 		.of_match_table = vc4_crtc_dt_match,
1465c8b75bcaSEric Anholt 	},
1466c8b75bcaSEric Anholt };
1467