• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2015 Broadcom
4  */
5 
6 /**
7  * DOC: VC4 CRTC module
8  *
9  * In VC4, the Pixel Valve is what most closely corresponds to the
10  * DRM's concept of a CRTC.  The PV generates video timings from the
11  * encoder's clock plus its configuration.  It pulls scaled pixels from
12  * the HVS at that timing, and feeds it to the encoder.
13  *
14  * However, the DRM CRTC also collects the configuration of all the
15  * DRM planes attached to it.  As a result, the CRTC is also
16  * responsible for writing the display list for the HVS channel that
17  * the CRTC will use.
18  *
19  * The 2835 has 3 different pixel valves.  pv0 in the audio power
20  * domain feeds DSI0 or DPI, while pv1 feeds DS1 or SMI.  pv2 in the
21  * image domain can feed either HDMI or the SDTV controller.  The
22  * pixel valve chooses from the CPRMAN clocks (HSM for HDMI, VEC for
23  * SDTV, etc.) according to which output type is chosen in the mux.
24  *
25  * For power management, the pixel valve's registers are all clocked
26  * by the AXI clock, while the timings and FIFOs make use of the
27  * output-specific clock.  Since the encoders also directly consume
28  * the CPRMAN clocks, and know what timings they need, they are the
29  * ones that set the clock.
30  */
31 
32 #include <linux/clk.h>
33 #include <linux/component.h>
34 #include <linux/of_device.h>
35 #include <linux/pm_runtime.h>
36 
37 #include <drm/drm_atomic.h>
38 #include <drm/drm_atomic_helper.h>
39 #include <drm/drm_atomic_uapi.h>
40 #include <drm/drm_fb_cma_helper.h>
41 #include <drm/drm_print.h>
42 #include <drm/drm_probe_helper.h>
43 #include <drm/drm_vblank.h>
44 
45 #include "vc4_drv.h"
46 #include "vc4_hdmi.h"
47 #include "vc4_regs.h"
48 
49 #define HVS_FIFO_LATENCY_PIX	6
50 
51 #define CRTC_WRITE(offset, val) writel(val, vc4_crtc->regs + (offset))
52 #define CRTC_READ(offset) readl(vc4_crtc->regs + (offset))
53 
54 static const struct debugfs_reg32 crtc_regs[] = {
55 	VC4_REG32(PV_CONTROL),
56 	VC4_REG32(PV_V_CONTROL),
57 	VC4_REG32(PV_VSYNCD_EVEN),
58 	VC4_REG32(PV_HORZA),
59 	VC4_REG32(PV_HORZB),
60 	VC4_REG32(PV_VERTA),
61 	VC4_REG32(PV_VERTB),
62 	VC4_REG32(PV_VERTA_EVEN),
63 	VC4_REG32(PV_VERTB_EVEN),
64 	VC4_REG32(PV_INTEN),
65 	VC4_REG32(PV_INTSTAT),
66 	VC4_REG32(PV_STAT),
67 	VC4_REG32(PV_HACT_ACT),
68 };
69 
70 static unsigned int
vc4_crtc_get_cob_allocation(struct vc4_dev * vc4,unsigned int channel)71 vc4_crtc_get_cob_allocation(struct vc4_dev *vc4, unsigned int channel)
72 {
73 	u32 dispbase = HVS_READ(SCALER_DISPBASEX(channel));
74 	/* Top/base are supposed to be 4-pixel aligned, but the
75 	 * Raspberry Pi firmware fills the low bits (which are
76 	 * presumably ignored).
77 	 */
78 	u32 top = VC4_GET_FIELD(dispbase, SCALER_DISPBASEX_TOP) & ~3;
79 	u32 base = VC4_GET_FIELD(dispbase, SCALER_DISPBASEX_BASE) & ~3;
80 
81 	return top - base + 4;
82 }
83 
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)84 static bool vc4_crtc_get_scanout_position(struct drm_crtc *crtc,
85 					  bool in_vblank_irq,
86 					  int *vpos, int *hpos,
87 					  ktime_t *stime, ktime_t *etime,
88 					  const struct drm_display_mode *mode)
89 {
90 	struct drm_device *dev = crtc->dev;
91 	struct vc4_dev *vc4 = to_vc4_dev(dev);
92 	struct vc4_crtc *vc4_crtc = to_vc4_crtc(crtc);
93 	struct vc4_crtc_state *vc4_crtc_state = to_vc4_crtc_state(crtc->state);
94 	unsigned int cob_size;
95 	u32 val;
96 	int fifo_lines;
97 	int vblank_lines;
98 	bool ret = false;
99 
100 	/* preempt_disable_rt() should go right here in PREEMPT_RT patchset. */
101 
102 	/* Get optional system timestamp before query. */
103 	if (stime)
104 		*stime = ktime_get();
105 
106 	/*
107 	 * Read vertical scanline which is currently composed for our
108 	 * pixelvalve by the HVS, and also the scaler status.
109 	 */
110 	val = HVS_READ(SCALER_DISPSTATX(vc4_crtc_state->assigned_channel));
111 
112 	/* Get optional system timestamp after query. */
113 	if (etime)
114 		*etime = ktime_get();
115 
116 	/* preempt_enable_rt() should go right here in PREEMPT_RT patchset. */
117 
118 	/* Vertical position of hvs composed scanline. */
119 	*vpos = VC4_GET_FIELD(val, SCALER_DISPSTATX_LINE);
120 	*hpos = 0;
121 
122 	if (mode->flags & DRM_MODE_FLAG_INTERLACE) {
123 		*vpos /= 2;
124 
125 		/* Use hpos to correct for field offset in interlaced mode. */
126 		if (vc4_hvs_get_fifo_frame_count(dev, vc4_crtc_state->assigned_channel) % 2)
127 			*hpos += mode->crtc_htotal / 2;
128 	}
129 
130 	cob_size = vc4_crtc_get_cob_allocation(vc4, vc4_crtc_state->assigned_channel);
131 	/* This is the offset we need for translating hvs -> pv scanout pos. */
132 	fifo_lines = cob_size / mode->crtc_hdisplay;
133 
134 	if (fifo_lines > 0)
135 		ret = true;
136 
137 	/* HVS more than fifo_lines into frame for compositing? */
138 	if (*vpos > fifo_lines) {
139 		/*
140 		 * We are in active scanout and can get some meaningful results
141 		 * from HVS. The actual PV scanout can not trail behind more
142 		 * than fifo_lines as that is the fifo's capacity. Assume that
143 		 * in active scanout the HVS and PV work in lockstep wrt. HVS
144 		 * refilling the fifo and PV consuming from the fifo, ie.
145 		 * whenever the PV consumes and frees up a scanline in the
146 		 * fifo, the HVS will immediately refill it, therefore
147 		 * incrementing vpos. Therefore we choose HVS read position -
148 		 * fifo size in scanlines as a estimate of the real scanout
149 		 * position of the PV.
150 		 */
151 		*vpos -= fifo_lines + 1;
152 
153 		return ret;
154 	}
155 
156 	/*
157 	 * Less: This happens when we are in vblank and the HVS, after getting
158 	 * the VSTART restart signal from the PV, just started refilling its
159 	 * fifo with new lines from the top-most lines of the new framebuffers.
160 	 * The PV does not scan out in vblank, so does not remove lines from
161 	 * the fifo, so the fifo will be full quickly and the HVS has to pause.
162 	 * We can't get meaningful readings wrt. scanline position of the PV
163 	 * and need to make things up in a approximative but consistent way.
164 	 */
165 	vblank_lines = mode->vtotal - mode->vdisplay;
166 
167 	if (in_vblank_irq) {
168 		/*
169 		 * Assume the irq handler got called close to first
170 		 * line of vblank, so PV has about a full vblank
171 		 * scanlines to go, and as a base timestamp use the
172 		 * one taken at entry into vblank irq handler, so it
173 		 * is not affected by random delays due to lock
174 		 * contention on event_lock or vblank_time lock in
175 		 * the core.
176 		 */
177 		*vpos = -vblank_lines;
178 
179 		if (stime)
180 			*stime = vc4_crtc->t_vblank;
181 		if (etime)
182 			*etime = vc4_crtc->t_vblank;
183 
184 		/*
185 		 * If the HVS fifo is not yet full then we know for certain
186 		 * we are at the very beginning of vblank, as the hvs just
187 		 * started refilling, and the stime and etime timestamps
188 		 * truly correspond to start of vblank.
189 		 *
190 		 * Unfortunately there's no way to report this to upper levels
191 		 * and make it more useful.
192 		 */
193 	} else {
194 		/*
195 		 * No clue where we are inside vblank. Return a vpos of zero,
196 		 * which will cause calling code to just return the etime
197 		 * timestamp uncorrected. At least this is no worse than the
198 		 * standard fallback.
199 		 */
200 		*vpos = 0;
201 	}
202 
203 	return ret;
204 }
205 
vc4_crtc_destroy(struct drm_crtc * crtc)206 void vc4_crtc_destroy(struct drm_crtc *crtc)
207 {
208 	drm_crtc_cleanup(crtc);
209 }
210 
vc4_get_fifo_full_level(struct vc4_crtc * vc4_crtc,u32 format)211 static u32 vc4_get_fifo_full_level(struct vc4_crtc *vc4_crtc, u32 format)
212 {
213 	const struct vc4_crtc_data *crtc_data = vc4_crtc_to_vc4_crtc_data(vc4_crtc);
214 	const struct vc4_pv_data *pv_data = vc4_crtc_to_vc4_pv_data(vc4_crtc);
215 	struct vc4_dev *vc4 = to_vc4_dev(vc4_crtc->base.dev);
216 	u32 fifo_len_bytes = pv_data->fifo_depth;
217 
218 	/*
219 	 * Pixels are pulled from the HVS if the number of bytes is
220 	 * lower than the FIFO full level.
221 	 *
222 	 * The latency of the pixel fetch mechanism is 6 pixels, so we
223 	 * need to convert those 6 pixels in bytes, depending on the
224 	 * format, and then subtract that from the length of the FIFO
225 	 * to make sure we never end up in a situation where the FIFO
226 	 * is full.
227 	 */
228 	switch (format) {
229 	case PV_CONTROL_FORMAT_DSIV_16:
230 	case PV_CONTROL_FORMAT_DSIC_16:
231 		return fifo_len_bytes - 2 * HVS_FIFO_LATENCY_PIX;
232 	case PV_CONTROL_FORMAT_DSIV_18:
233 		return fifo_len_bytes - 14;
234 	case PV_CONTROL_FORMAT_24:
235 	case PV_CONTROL_FORMAT_DSIV_24:
236 	default:
237 		/*
238 		 * For some reason, the pixelvalve4 doesn't work with
239 		 * the usual formula and will only work with 32.
240 		 */
241 		if (crtc_data->hvs_output == 5)
242 			return 32;
243 
244 		/*
245 		 * It looks like in some situations, we will overflow
246 		 * the PixelValve FIFO (with the bit 10 of PV stat being
247 		 * set) and stall the HVS / PV, eventually resulting in
248 		 * a page flip timeout.
249 		 *
250 		 * Displaying the video overlay during a playback with
251 		 * Kodi on an RPi3 seems to be a great solution with a
252 		 * failure rate around 50%.
253 		 *
254 		 * Removing 1 from the FIFO full level however
255 		 * seems to completely remove that issue.
256 		 */
257 		if (!vc4->hvs->hvs5)
258 			return fifo_len_bytes - 3 * HVS_FIFO_LATENCY_PIX - 1;
259 
260 		return fifo_len_bytes - 3 * HVS_FIFO_LATENCY_PIX;
261 	}
262 }
263 
vc4_crtc_get_fifo_full_level_bits(struct vc4_crtc * vc4_crtc,u32 format)264 static u32 vc4_crtc_get_fifo_full_level_bits(struct vc4_crtc *vc4_crtc,
265 					     u32 format)
266 {
267 	u32 level = vc4_get_fifo_full_level(vc4_crtc, format);
268 	u32 ret = 0;
269 
270 	ret |= VC4_SET_FIELD((level >> 6),
271 			     PV5_CONTROL_FIFO_LEVEL_HIGH);
272 
273 	return ret | VC4_SET_FIELD(level & 0x3f,
274 				   PV_CONTROL_FIFO_LEVEL);
275 }
276 
277 /*
278  * Returns the encoder attached to the CRTC.
279  *
280  * VC4 can only scan out to one encoder at a time, while the DRM core
281  * allows drivers to push pixels to more than one encoder from the
282  * same CRTC.
283  */
vc4_get_crtc_encoder(struct drm_crtc * crtc,struct drm_atomic_state * state,struct drm_connector_state * (* get_state)(struct drm_atomic_state * state,struct drm_connector * connector))284 static struct drm_encoder *vc4_get_crtc_encoder(struct drm_crtc *crtc,
285 						struct drm_atomic_state *state,
286 						struct drm_connector_state *(*get_state)(struct drm_atomic_state *state,
287 											 struct drm_connector *connector))
288 {
289 	struct drm_connector *connector;
290 	struct drm_connector_list_iter conn_iter;
291 
292 	drm_connector_list_iter_begin(crtc->dev, &conn_iter);
293 	drm_for_each_connector_iter(connector, &conn_iter) {
294 		struct drm_connector_state *conn_state = get_state(state, connector);
295 
296 		if (!conn_state)
297 			continue;
298 
299 		if (conn_state->crtc == crtc) {
300 			drm_connector_list_iter_end(&conn_iter);
301 			return connector->encoder;
302 		}
303 	}
304 	drm_connector_list_iter_end(&conn_iter);
305 
306 	return NULL;
307 }
308 
vc4_crtc_pixelvalve_reset(struct drm_crtc * crtc)309 static void vc4_crtc_pixelvalve_reset(struct drm_crtc *crtc)
310 {
311 	struct vc4_crtc *vc4_crtc = to_vc4_crtc(crtc);
312 
313 	/* The PV needs to be disabled before it can be flushed */
314 	CRTC_WRITE(PV_CONTROL, CRTC_READ(PV_CONTROL) & ~PV_CONTROL_EN);
315 	CRTC_WRITE(PV_CONTROL, CRTC_READ(PV_CONTROL) | PV_CONTROL_FIFO_CLR);
316 }
317 
vc4_crtc_config_pv(struct drm_crtc * crtc,struct drm_atomic_state * state)318 static void vc4_crtc_config_pv(struct drm_crtc *crtc, struct drm_atomic_state *state)
319 {
320 	struct drm_device *dev = crtc->dev;
321 	struct vc4_dev *vc4 = to_vc4_dev(dev);
322 	struct drm_encoder *encoder = vc4_get_crtc_encoder(crtc, state,
323 							   drm_atomic_get_new_connector_state);
324 	struct vc4_encoder *vc4_encoder = to_vc4_encoder(encoder);
325 	struct vc4_crtc *vc4_crtc = to_vc4_crtc(crtc);
326 	const struct vc4_pv_data *pv_data = vc4_crtc_to_vc4_pv_data(vc4_crtc);
327 	struct drm_crtc_state *crtc_state = crtc->state;
328 	struct drm_display_mode *mode = &crtc_state->adjusted_mode;
329 	bool interlace = mode->flags & DRM_MODE_FLAG_INTERLACE;
330 	u32 pixel_rep = (mode->flags & DRM_MODE_FLAG_DBLCLK) ? 2 : 1;
331 	bool is_dsi = (vc4_encoder->type == VC4_ENCODER_TYPE_DSI0 ||
332 		       vc4_encoder->type == VC4_ENCODER_TYPE_DSI1);
333 	bool is_dsi1 = vc4_encoder->type == VC4_ENCODER_TYPE_DSI1;
334 	u32 format = is_dsi1 ? PV_CONTROL_FORMAT_DSIV_24 : PV_CONTROL_FORMAT_24;
335 	u8 ppc = pv_data->pixels_per_clock;
336 	bool debug_dump_regs = false;
337 
338 	if (debug_dump_regs) {
339 		struct drm_printer p = drm_info_printer(&vc4_crtc->pdev->dev);
340 		dev_info(&vc4_crtc->pdev->dev, "CRTC %d regs before:\n",
341 			 drm_crtc_index(crtc));
342 		drm_print_regset32(&p, &vc4_crtc->regset);
343 	}
344 
345 	vc4_crtc_pixelvalve_reset(crtc);
346 
347 	CRTC_WRITE(PV_HORZA,
348 		   VC4_SET_FIELD((mode->htotal - mode->hsync_end) * pixel_rep / ppc,
349 				 PV_HORZA_HBP) |
350 		   VC4_SET_FIELD((mode->hsync_end - mode->hsync_start) * pixel_rep / ppc,
351 				 PV_HORZA_HSYNC));
352 
353 	CRTC_WRITE(PV_HORZB,
354 		   VC4_SET_FIELD((mode->hsync_start - mode->hdisplay) * pixel_rep / ppc,
355 				 PV_HORZB_HFP) |
356 		   VC4_SET_FIELD(mode->hdisplay * pixel_rep / ppc,
357 				 PV_HORZB_HACTIVE));
358 
359 	CRTC_WRITE(PV_VERTA,
360 		   VC4_SET_FIELD(mode->crtc_vtotal - mode->crtc_vsync_end +
361 				 interlace,
362 				 PV_VERTA_VBP) |
363 		   VC4_SET_FIELD(mode->crtc_vsync_end - mode->crtc_vsync_start,
364 				 PV_VERTA_VSYNC));
365 	CRTC_WRITE(PV_VERTB,
366 		   VC4_SET_FIELD(mode->crtc_vsync_start - mode->crtc_vdisplay,
367 				 PV_VERTB_VFP) |
368 		   VC4_SET_FIELD(mode->crtc_vdisplay, PV_VERTB_VACTIVE));
369 
370 	if (interlace) {
371 		CRTC_WRITE(PV_VERTA_EVEN,
372 			   VC4_SET_FIELD(mode->crtc_vtotal -
373 					 mode->crtc_vsync_end,
374 					 PV_VERTA_VBP) |
375 			   VC4_SET_FIELD(mode->crtc_vsync_end -
376 					 mode->crtc_vsync_start,
377 					 PV_VERTA_VSYNC));
378 		CRTC_WRITE(PV_VERTB_EVEN,
379 			   VC4_SET_FIELD(mode->crtc_vsync_start -
380 					 mode->crtc_vdisplay,
381 					 PV_VERTB_VFP) |
382 			   VC4_SET_FIELD(mode->crtc_vdisplay, PV_VERTB_VACTIVE));
383 
384 		/* We set up first field even mode for HDMI.  VEC's
385 		 * NTSC mode would want first field odd instead, once
386 		 * we support it (to do so, set ODD_FIRST and put the
387 		 * delay in VSYNCD_EVEN instead).
388 		 */
389 		CRTC_WRITE(PV_V_CONTROL,
390 			   PV_VCONTROL_CONTINUOUS |
391 			   (is_dsi ? PV_VCONTROL_DSI : 0) |
392 			   PV_VCONTROL_INTERLACE |
393 			   VC4_SET_FIELD(mode->htotal * pixel_rep / (2 * ppc),
394 					 PV_VCONTROL_ODD_DELAY));
395 		CRTC_WRITE(PV_VSYNCD_EVEN, 0);
396 	} else {
397 		CRTC_WRITE(PV_V_CONTROL,
398 			   PV_VCONTROL_CONTINUOUS |
399 			   (is_dsi ? PV_VCONTROL_DSI : 0));
400 	}
401 
402 	if (is_dsi)
403 		CRTC_WRITE(PV_HACT_ACT, mode->hdisplay * pixel_rep);
404 
405 	if (vc4->hvs->hvs5)
406 		CRTC_WRITE(PV_MUX_CFG,
407 			   VC4_SET_FIELD(PV_MUX_CFG_RGB_PIXEL_MUX_MODE_NO_SWAP,
408 					 PV_MUX_CFG_RGB_PIXEL_MUX_MODE));
409 
410 	CRTC_WRITE(PV_CONTROL, PV_CONTROL_FIFO_CLR |
411 		   vc4_crtc_get_fifo_full_level_bits(vc4_crtc, format) |
412 		   VC4_SET_FIELD(format, PV_CONTROL_FORMAT) |
413 		   VC4_SET_FIELD(pixel_rep - 1, PV_CONTROL_PIXEL_REP) |
414 		   PV_CONTROL_CLR_AT_START |
415 		   PV_CONTROL_TRIGGER_UNDERFLOW |
416 		   PV_CONTROL_WAIT_HSTART |
417 		   VC4_SET_FIELD(vc4_encoder->clock_select,
418 				 PV_CONTROL_CLK_SELECT));
419 
420 	if (debug_dump_regs) {
421 		struct drm_printer p = drm_info_printer(&vc4_crtc->pdev->dev);
422 		dev_info(&vc4_crtc->pdev->dev, "CRTC %d regs after:\n",
423 			 drm_crtc_index(crtc));
424 		drm_print_regset32(&p, &vc4_crtc->regset);
425 	}
426 }
427 
require_hvs_enabled(struct drm_device * dev)428 static void require_hvs_enabled(struct drm_device *dev)
429 {
430 	struct vc4_dev *vc4 = to_vc4_dev(dev);
431 
432 	WARN_ON_ONCE((HVS_READ(SCALER_DISPCTRL) & SCALER_DISPCTRL_ENABLE) !=
433 		     SCALER_DISPCTRL_ENABLE);
434 }
435 
vc4_crtc_disable(struct drm_crtc * crtc,struct drm_encoder * encoder,struct drm_atomic_state * state,unsigned int channel)436 static int vc4_crtc_disable(struct drm_crtc *crtc,
437 			    struct drm_encoder *encoder,
438 			    struct drm_atomic_state *state,
439 			    unsigned int channel)
440 {
441 	struct vc4_encoder *vc4_encoder = to_vc4_encoder(encoder);
442 	struct vc4_crtc *vc4_crtc = to_vc4_crtc(crtc);
443 	struct drm_device *dev = crtc->dev;
444 	int ret;
445 
446 	CRTC_WRITE(PV_V_CONTROL,
447 		   CRTC_READ(PV_V_CONTROL) & ~PV_VCONTROL_VIDEN);
448 	ret = wait_for(!(CRTC_READ(PV_V_CONTROL) & PV_VCONTROL_VIDEN), 1);
449 	WARN_ONCE(ret, "Timeout waiting for !PV_VCONTROL_VIDEN\n");
450 
451 	/*
452 	 * This delay is needed to avoid to get a pixel stuck in an
453 	 * unflushable FIFO between the pixelvalve and the HDMI
454 	 * controllers on the BCM2711.
455 	 *
456 	 * Timing is fairly sensitive here, so mdelay is the safest
457 	 * approach.
458 	 *
459 	 * If it was to be reworked, the stuck pixel happens on a
460 	 * BCM2711 when changing mode with a good probability, so a
461 	 * script that changes mode on a regular basis should trigger
462 	 * the bug after less than 10 attempts. It manifests itself with
463 	 * every pixels being shifted by one to the right, and thus the
464 	 * last pixel of a line actually being displayed as the first
465 	 * pixel on the next line.
466 	 */
467 	mdelay(20);
468 
469 	if (vc4_encoder && vc4_encoder->post_crtc_disable)
470 		vc4_encoder->post_crtc_disable(encoder, state);
471 
472 	vc4_crtc_pixelvalve_reset(crtc);
473 	vc4_hvs_stop_channel(dev, channel);
474 
475 	if (vc4_encoder && vc4_encoder->post_crtc_powerdown)
476 		vc4_encoder->post_crtc_powerdown(encoder, state);
477 
478 	return 0;
479 }
480 
vc4_crtc_get_encoder_by_type(struct drm_crtc * crtc,enum vc4_encoder_type type)481 static struct drm_encoder *vc4_crtc_get_encoder_by_type(struct drm_crtc *crtc,
482 							enum vc4_encoder_type type)
483 {
484 	struct drm_encoder *encoder;
485 
486 	drm_for_each_encoder(encoder, crtc->dev) {
487 		struct vc4_encoder *vc4_encoder = to_vc4_encoder(encoder);
488 
489 		if (vc4_encoder->type == type)
490 			return encoder;
491 	}
492 
493 	return NULL;
494 }
495 
vc4_crtc_disable_at_boot(struct drm_crtc * crtc)496 int vc4_crtc_disable_at_boot(struct drm_crtc *crtc)
497 {
498 	struct drm_device *drm = crtc->dev;
499 	struct vc4_crtc *vc4_crtc = to_vc4_crtc(crtc);
500 	enum vc4_encoder_type encoder_type;
501 	const struct vc4_pv_data *pv_data;
502 	struct drm_encoder *encoder;
503 	struct vc4_hdmi *vc4_hdmi;
504 	unsigned encoder_sel;
505 	int channel;
506 	int ret;
507 
508 	if (!(of_device_is_compatible(vc4_crtc->pdev->dev.of_node,
509 				      "brcm,bcm2711-pixelvalve2") ||
510 	      of_device_is_compatible(vc4_crtc->pdev->dev.of_node,
511 				      "brcm,bcm2711-pixelvalve4")))
512 		return 0;
513 
514 	if (!(CRTC_READ(PV_CONTROL) & PV_CONTROL_EN))
515 		return 0;
516 
517 	if (!(CRTC_READ(PV_V_CONTROL) & PV_VCONTROL_VIDEN))
518 		return 0;
519 
520 	channel = vc4_hvs_get_fifo_from_output(drm, vc4_crtc->data->hvs_output);
521 	if (channel < 0)
522 		return 0;
523 
524 	encoder_sel = VC4_GET_FIELD(CRTC_READ(PV_CONTROL), PV_CONTROL_CLK_SELECT);
525 	if (WARN_ON(encoder_sel != 0))
526 		return 0;
527 
528 	pv_data = vc4_crtc_to_vc4_pv_data(vc4_crtc);
529 	encoder_type = pv_data->encoder_types[encoder_sel];
530 	encoder = vc4_crtc_get_encoder_by_type(crtc, encoder_type);
531 	if (WARN_ON(!encoder))
532 		return 0;
533 
534 	vc4_hdmi = encoder_to_vc4_hdmi(encoder);
535 	ret = pm_runtime_resume_and_get(&vc4_hdmi->pdev->dev);
536 	if (ret)
537 		return ret;
538 
539 	ret = vc4_crtc_disable(crtc, encoder, NULL, channel);
540 	if (ret)
541 		return ret;
542 
543 	/*
544 	 * post_crtc_powerdown will have called pm_runtime_put, so we
545 	 * don't need it here otherwise we'll get the reference counting
546 	 * wrong.
547 	 */
548 
549 	return 0;
550 }
551 
vc4_crtc_atomic_disable(struct drm_crtc * crtc,struct drm_atomic_state * state)552 static void vc4_crtc_atomic_disable(struct drm_crtc *crtc,
553 				    struct drm_atomic_state *state)
554 {
555 	struct drm_crtc_state *old_state = drm_atomic_get_old_crtc_state(state,
556 									 crtc);
557 	struct vc4_crtc_state *old_vc4_state = to_vc4_crtc_state(old_state);
558 	struct drm_encoder *encoder = vc4_get_crtc_encoder(crtc, state,
559 							   drm_atomic_get_old_connector_state);
560 	struct drm_device *dev = crtc->dev;
561 
562 	require_hvs_enabled(dev);
563 
564 	/* Disable vblank irq handling before crtc is disabled. */
565 	drm_crtc_vblank_off(crtc);
566 
567 	vc4_crtc_disable(crtc, encoder, state, old_vc4_state->assigned_channel);
568 
569 	/*
570 	 * Make sure we issue a vblank event after disabling the CRTC if
571 	 * someone was waiting it.
572 	 */
573 	if (crtc->state->event) {
574 		unsigned long flags;
575 
576 		spin_lock_irqsave(&dev->event_lock, flags);
577 		drm_crtc_send_vblank_event(crtc, crtc->state->event);
578 		crtc->state->event = NULL;
579 		spin_unlock_irqrestore(&dev->event_lock, flags);
580 	}
581 }
582 
vc4_crtc_atomic_enable(struct drm_crtc * crtc,struct drm_atomic_state * state)583 static void vc4_crtc_atomic_enable(struct drm_crtc *crtc,
584 				   struct drm_atomic_state *state)
585 {
586 	struct drm_device *dev = crtc->dev;
587 	struct vc4_crtc *vc4_crtc = to_vc4_crtc(crtc);
588 	struct drm_encoder *encoder = vc4_get_crtc_encoder(crtc, state,
589 							   drm_atomic_get_new_connector_state);
590 	struct vc4_encoder *vc4_encoder = to_vc4_encoder(encoder);
591 
592 	require_hvs_enabled(dev);
593 
594 	/* Enable vblank irq handling before crtc is started otherwise
595 	 * drm_crtc_get_vblank() fails in vc4_crtc_update_dlist().
596 	 */
597 	drm_crtc_vblank_on(crtc);
598 
599 	vc4_hvs_atomic_enable(crtc, state);
600 
601 	if (vc4_encoder->pre_crtc_configure)
602 		vc4_encoder->pre_crtc_configure(encoder, state);
603 
604 	vc4_crtc_config_pv(crtc, state);
605 
606 	CRTC_WRITE(PV_CONTROL, CRTC_READ(PV_CONTROL) | PV_CONTROL_EN);
607 
608 	if (vc4_encoder->pre_crtc_enable)
609 		vc4_encoder->pre_crtc_enable(encoder, state);
610 
611 	/* When feeding the transposer block the pixelvalve is unneeded and
612 	 * should not be enabled.
613 	 */
614 	CRTC_WRITE(PV_V_CONTROL,
615 		   CRTC_READ(PV_V_CONTROL) | PV_VCONTROL_VIDEN);
616 
617 	if (vc4_encoder->post_crtc_enable)
618 		vc4_encoder->post_crtc_enable(encoder, state);
619 }
620 
vc4_crtc_mode_valid(struct drm_crtc * crtc,const struct drm_display_mode * mode)621 static enum drm_mode_status vc4_crtc_mode_valid(struct drm_crtc *crtc,
622 						const struct drm_display_mode *mode)
623 {
624 	/* Do not allow doublescan modes from user space */
625 	if (mode->flags & DRM_MODE_FLAG_DBLSCAN) {
626 		DRM_DEBUG_KMS("[CRTC:%d] Doublescan mode rejected.\n",
627 			      crtc->base.id);
628 		return MODE_NO_DBLESCAN;
629 	}
630 
631 	return MODE_OK;
632 }
633 
vc4_crtc_get_margins(struct drm_crtc_state * state,unsigned int * left,unsigned int * right,unsigned int * top,unsigned int * bottom)634 void vc4_crtc_get_margins(struct drm_crtc_state *state,
635 			  unsigned int *left, unsigned int *right,
636 			  unsigned int *top, unsigned int *bottom)
637 {
638 	struct vc4_crtc_state *vc4_state = to_vc4_crtc_state(state);
639 	struct drm_connector_state *conn_state;
640 	struct drm_connector *conn;
641 	int i;
642 
643 	*left = vc4_state->margins.left;
644 	*right = vc4_state->margins.right;
645 	*top = vc4_state->margins.top;
646 	*bottom = vc4_state->margins.bottom;
647 
648 	/* We have to interate over all new connector states because
649 	 * vc4_crtc_get_margins() might be called before
650 	 * vc4_crtc_atomic_check() which means margins info in vc4_crtc_state
651 	 * might be outdated.
652 	 */
653 	for_each_new_connector_in_state(state->state, conn, conn_state, i) {
654 		if (conn_state->crtc != state->crtc)
655 			continue;
656 
657 		*left = conn_state->tv.margins.left;
658 		*right = conn_state->tv.margins.right;
659 		*top = conn_state->tv.margins.top;
660 		*bottom = conn_state->tv.margins.bottom;
661 		break;
662 	}
663 }
664 
vc4_crtc_atomic_check(struct drm_crtc * crtc,struct drm_atomic_state * state)665 static int vc4_crtc_atomic_check(struct drm_crtc *crtc,
666 				 struct drm_atomic_state *state)
667 {
668 	struct drm_crtc_state *crtc_state = drm_atomic_get_new_crtc_state(state,
669 									  crtc);
670 	struct vc4_crtc_state *vc4_state = to_vc4_crtc_state(crtc_state);
671 	struct drm_connector *conn;
672 	struct drm_connector_state *conn_state;
673 	int ret, i;
674 
675 	ret = vc4_hvs_atomic_check(crtc, state);
676 	if (ret)
677 		return ret;
678 
679 	for_each_new_connector_in_state(state, conn, conn_state,
680 					i) {
681 		if (conn_state->crtc != crtc)
682 			continue;
683 
684 		vc4_state->margins.left = conn_state->tv.margins.left;
685 		vc4_state->margins.right = conn_state->tv.margins.right;
686 		vc4_state->margins.top = conn_state->tv.margins.top;
687 		vc4_state->margins.bottom = conn_state->tv.margins.bottom;
688 		break;
689 	}
690 
691 	return 0;
692 }
693 
vc4_enable_vblank(struct drm_crtc * crtc)694 static int vc4_enable_vblank(struct drm_crtc *crtc)
695 {
696 	struct vc4_crtc *vc4_crtc = to_vc4_crtc(crtc);
697 
698 	CRTC_WRITE(PV_INTEN, PV_INT_VFP_START);
699 
700 	return 0;
701 }
702 
vc4_disable_vblank(struct drm_crtc * crtc)703 static void vc4_disable_vblank(struct drm_crtc *crtc)
704 {
705 	struct vc4_crtc *vc4_crtc = to_vc4_crtc(crtc);
706 
707 	CRTC_WRITE(PV_INTEN, 0);
708 }
709 
vc4_crtc_handle_page_flip(struct vc4_crtc * vc4_crtc)710 static void vc4_crtc_handle_page_flip(struct vc4_crtc *vc4_crtc)
711 {
712 	struct drm_crtc *crtc = &vc4_crtc->base;
713 	struct drm_device *dev = crtc->dev;
714 	struct vc4_dev *vc4 = to_vc4_dev(dev);
715 	u32 chan = vc4_crtc->current_hvs_channel;
716 	unsigned long flags;
717 
718 	spin_lock_irqsave(&dev->event_lock, flags);
719 	spin_lock(&vc4_crtc->irq_lock);
720 	if (vc4_crtc->event &&
721 	    (vc4_crtc->current_dlist == HVS_READ(SCALER_DISPLACTX(chan)) ||
722 	     vc4_crtc->feeds_txp)) {
723 		drm_crtc_send_vblank_event(crtc, vc4_crtc->event);
724 		vc4_crtc->event = NULL;
725 		drm_crtc_vblank_put(crtc);
726 
727 		/* Wait for the page flip to unmask the underrun to ensure that
728 		 * the display list was updated by the hardware. Before that
729 		 * happens, the HVS will be using the previous display list with
730 		 * the CRTC and encoder already reconfigured, leading to
731 		 * underruns. This can be seen when reconfiguring the CRTC.
732 		 */
733 		vc4_hvs_unmask_underrun(dev, chan);
734 	}
735 	spin_unlock(&vc4_crtc->irq_lock);
736 	spin_unlock_irqrestore(&dev->event_lock, flags);
737 }
738 
vc4_crtc_handle_vblank(struct vc4_crtc * crtc)739 void vc4_crtc_handle_vblank(struct vc4_crtc *crtc)
740 {
741 	crtc->t_vblank = ktime_get();
742 	drm_crtc_handle_vblank(&crtc->base);
743 	vc4_crtc_handle_page_flip(crtc);
744 }
745 
vc4_crtc_irq_handler(int irq,void * data)746 static irqreturn_t vc4_crtc_irq_handler(int irq, void *data)
747 {
748 	struct vc4_crtc *vc4_crtc = data;
749 	u32 stat = CRTC_READ(PV_INTSTAT);
750 	irqreturn_t ret = IRQ_NONE;
751 
752 	if (stat & PV_INT_VFP_START) {
753 		CRTC_WRITE(PV_INTSTAT, PV_INT_VFP_START);
754 		vc4_crtc_handle_vblank(vc4_crtc);
755 		ret = IRQ_HANDLED;
756 	}
757 
758 	return ret;
759 }
760 
761 struct vc4_async_flip_state {
762 	struct drm_crtc *crtc;
763 	struct drm_framebuffer *fb;
764 	struct drm_framebuffer *old_fb;
765 	struct drm_pending_vblank_event *event;
766 
767 	struct vc4_seqno_cb cb;
768 };
769 
770 /* Called when the V3D execution for the BO being flipped to is done, so that
771  * we can actually update the plane's address to point to it.
772  */
773 static void
vc4_async_page_flip_complete(struct vc4_seqno_cb * cb)774 vc4_async_page_flip_complete(struct vc4_seqno_cb *cb)
775 {
776 	struct vc4_async_flip_state *flip_state =
777 		container_of(cb, struct vc4_async_flip_state, cb);
778 	struct drm_crtc *crtc = flip_state->crtc;
779 	struct drm_device *dev = crtc->dev;
780 	struct drm_plane *plane = crtc->primary;
781 
782 	vc4_plane_async_set_fb(plane, flip_state->fb);
783 	if (flip_state->event) {
784 		unsigned long flags;
785 
786 		spin_lock_irqsave(&dev->event_lock, flags);
787 		drm_crtc_send_vblank_event(crtc, flip_state->event);
788 		spin_unlock_irqrestore(&dev->event_lock, flags);
789 	}
790 
791 	drm_crtc_vblank_put(crtc);
792 	drm_framebuffer_put(flip_state->fb);
793 
794 	/* Decrement the BO usecnt in order to keep the inc/dec calls balanced
795 	 * when the planes are updated through the async update path.
796 	 * FIXME: we should move to generic async-page-flip when it's
797 	 * available, so that we can get rid of this hand-made cleanup_fb()
798 	 * logic.
799 	 */
800 	if (flip_state->old_fb) {
801 		struct drm_gem_cma_object *cma_bo;
802 		struct vc4_bo *bo;
803 
804 		cma_bo = drm_fb_cma_get_gem_obj(flip_state->old_fb, 0);
805 		bo = to_vc4_bo(&cma_bo->base);
806 		vc4_bo_dec_usecnt(bo);
807 		drm_framebuffer_put(flip_state->old_fb);
808 	}
809 
810 	kfree(flip_state);
811 }
812 
813 /* Implements async (non-vblank-synced) page flips.
814  *
815  * The page flip ioctl needs to return immediately, so we grab the
816  * modeset semaphore on the pipe, and queue the address update for
817  * when V3D is done with the BO being flipped to.
818  */
vc4_async_page_flip(struct drm_crtc * crtc,struct drm_framebuffer * fb,struct drm_pending_vblank_event * event,uint32_t flags)819 static int vc4_async_page_flip(struct drm_crtc *crtc,
820 			       struct drm_framebuffer *fb,
821 			       struct drm_pending_vblank_event *event,
822 			       uint32_t flags)
823 {
824 	struct drm_device *dev = crtc->dev;
825 	struct drm_plane *plane = crtc->primary;
826 	int ret = 0;
827 	struct vc4_async_flip_state *flip_state;
828 	struct drm_gem_cma_object *cma_bo = drm_fb_cma_get_gem_obj(fb, 0);
829 	struct vc4_bo *bo = to_vc4_bo(&cma_bo->base);
830 
831 	/* Increment the BO usecnt here, so that we never end up with an
832 	 * unbalanced number of vc4_bo_{dec,inc}_usecnt() calls when the
833 	 * plane is later updated through the non-async path.
834 	 * FIXME: we should move to generic async-page-flip when it's
835 	 * available, so that we can get rid of this hand-made prepare_fb()
836 	 * logic.
837 	 */
838 	ret = vc4_bo_inc_usecnt(bo);
839 	if (ret)
840 		return ret;
841 
842 	flip_state = kzalloc(sizeof(*flip_state), GFP_KERNEL);
843 	if (!flip_state) {
844 		vc4_bo_dec_usecnt(bo);
845 		return -ENOMEM;
846 	}
847 
848 	drm_framebuffer_get(fb);
849 	flip_state->fb = fb;
850 	flip_state->crtc = crtc;
851 	flip_state->event = event;
852 
853 	/* Save the current FB before it's replaced by the new one in
854 	 * drm_atomic_set_fb_for_plane(). We'll need the old FB in
855 	 * vc4_async_page_flip_complete() to decrement the BO usecnt and keep
856 	 * it consistent.
857 	 * FIXME: we should move to generic async-page-flip when it's
858 	 * available, so that we can get rid of this hand-made cleanup_fb()
859 	 * logic.
860 	 */
861 	flip_state->old_fb = plane->state->fb;
862 	if (flip_state->old_fb)
863 		drm_framebuffer_get(flip_state->old_fb);
864 
865 	WARN_ON(drm_crtc_vblank_get(crtc) != 0);
866 
867 	/* Immediately update the plane's legacy fb pointer, so that later
868 	 * modeset prep sees the state that will be present when the semaphore
869 	 * is released.
870 	 */
871 	drm_atomic_set_fb_for_plane(plane->state, fb);
872 
873 	vc4_queue_seqno_cb(dev, &flip_state->cb, bo->seqno,
874 			   vc4_async_page_flip_complete);
875 
876 	/* Driver takes ownership of state on successful async commit. */
877 	return 0;
878 }
879 
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)880 int vc4_page_flip(struct drm_crtc *crtc,
881 		  struct drm_framebuffer *fb,
882 		  struct drm_pending_vblank_event *event,
883 		  uint32_t flags,
884 		  struct drm_modeset_acquire_ctx *ctx)
885 {
886 	if (flags & DRM_MODE_PAGE_FLIP_ASYNC)
887 		return vc4_async_page_flip(crtc, fb, event, flags);
888 	else
889 		return drm_atomic_helper_page_flip(crtc, fb, event, flags, ctx);
890 }
891 
vc4_crtc_duplicate_state(struct drm_crtc * crtc)892 struct drm_crtc_state *vc4_crtc_duplicate_state(struct drm_crtc *crtc)
893 {
894 	struct vc4_crtc_state *vc4_state, *old_vc4_state;
895 
896 	vc4_state = kzalloc(sizeof(*vc4_state), GFP_KERNEL);
897 	if (!vc4_state)
898 		return NULL;
899 
900 	old_vc4_state = to_vc4_crtc_state(crtc->state);
901 	vc4_state->margins = old_vc4_state->margins;
902 	vc4_state->assigned_channel = old_vc4_state->assigned_channel;
903 
904 	__drm_atomic_helper_crtc_duplicate_state(crtc, &vc4_state->base);
905 	return &vc4_state->base;
906 }
907 
vc4_crtc_destroy_state(struct drm_crtc * crtc,struct drm_crtc_state * state)908 void vc4_crtc_destroy_state(struct drm_crtc *crtc,
909 			    struct drm_crtc_state *state)
910 {
911 	struct vc4_dev *vc4 = to_vc4_dev(crtc->dev);
912 	struct vc4_crtc_state *vc4_state = to_vc4_crtc_state(state);
913 
914 	if (drm_mm_node_allocated(&vc4_state->mm)) {
915 		unsigned long flags;
916 
917 		spin_lock_irqsave(&vc4->hvs->mm_lock, flags);
918 		drm_mm_remove_node(&vc4_state->mm);
919 		spin_unlock_irqrestore(&vc4->hvs->mm_lock, flags);
920 
921 	}
922 
923 	drm_atomic_helper_crtc_destroy_state(crtc, state);
924 }
925 
vc4_crtc_reset(struct drm_crtc * crtc)926 void vc4_crtc_reset(struct drm_crtc *crtc)
927 {
928 	struct vc4_crtc_state *vc4_crtc_state;
929 
930 	if (crtc->state)
931 		vc4_crtc_destroy_state(crtc, crtc->state);
932 
933 	vc4_crtc_state = kzalloc(sizeof(*vc4_crtc_state), GFP_KERNEL);
934 	if (!vc4_crtc_state) {
935 		crtc->state = NULL;
936 		return;
937 	}
938 
939 	vc4_crtc_state->assigned_channel = VC4_HVS_CHANNEL_DISABLED;
940 	__drm_atomic_helper_crtc_reset(crtc, &vc4_crtc_state->base);
941 }
942 
943 static const struct drm_crtc_funcs vc4_crtc_funcs = {
944 	.set_config = drm_atomic_helper_set_config,
945 	.destroy = vc4_crtc_destroy,
946 	.page_flip = vc4_page_flip,
947 	.set_property = NULL,
948 	.cursor_set = NULL, /* handled by drm_mode_cursor_universal */
949 	.cursor_move = NULL, /* handled by drm_mode_cursor_universal */
950 	.reset = vc4_crtc_reset,
951 	.atomic_duplicate_state = vc4_crtc_duplicate_state,
952 	.atomic_destroy_state = vc4_crtc_destroy_state,
953 	.enable_vblank = vc4_enable_vblank,
954 	.disable_vblank = vc4_disable_vblank,
955 	.get_vblank_timestamp = drm_crtc_vblank_helper_get_vblank_timestamp,
956 };
957 
958 static const struct drm_crtc_helper_funcs vc4_crtc_helper_funcs = {
959 	.mode_valid = vc4_crtc_mode_valid,
960 	.atomic_check = vc4_crtc_atomic_check,
961 	.atomic_begin = vc4_hvs_atomic_begin,
962 	.atomic_flush = vc4_hvs_atomic_flush,
963 	.atomic_enable = vc4_crtc_atomic_enable,
964 	.atomic_disable = vc4_crtc_atomic_disable,
965 	.get_scanout_position = vc4_crtc_get_scanout_position,
966 };
967 
968 static const struct vc4_pv_data bcm2835_pv0_data = {
969 	.base = {
970 		.hvs_available_channels = BIT(0),
971 		.hvs_output = 0,
972 	},
973 	.debugfs_name = "crtc0_regs",
974 	.fifo_depth = 64,
975 	.pixels_per_clock = 1,
976 	.encoder_types = {
977 		[PV_CONTROL_CLK_SELECT_DSI] = VC4_ENCODER_TYPE_DSI0,
978 		[PV_CONTROL_CLK_SELECT_DPI_SMI_HDMI] = VC4_ENCODER_TYPE_DPI,
979 	},
980 };
981 
982 static const struct vc4_pv_data bcm2835_pv1_data = {
983 	.base = {
984 		.hvs_available_channels = BIT(2),
985 		.hvs_output = 2,
986 	},
987 	.debugfs_name = "crtc1_regs",
988 	.fifo_depth = 64,
989 	.pixels_per_clock = 1,
990 	.encoder_types = {
991 		[PV_CONTROL_CLK_SELECT_DSI] = VC4_ENCODER_TYPE_DSI1,
992 		[PV_CONTROL_CLK_SELECT_DPI_SMI_HDMI] = VC4_ENCODER_TYPE_SMI,
993 	},
994 };
995 
996 static const struct vc4_pv_data bcm2835_pv2_data = {
997 	.base = {
998 		.hvs_available_channels = BIT(1),
999 		.hvs_output = 1,
1000 	},
1001 	.debugfs_name = "crtc2_regs",
1002 	.fifo_depth = 64,
1003 	.pixels_per_clock = 1,
1004 	.encoder_types = {
1005 		[PV_CONTROL_CLK_SELECT_DPI_SMI_HDMI] = VC4_ENCODER_TYPE_HDMI0,
1006 		[PV_CONTROL_CLK_SELECT_VEC] = VC4_ENCODER_TYPE_VEC,
1007 	},
1008 };
1009 
1010 static const struct vc4_pv_data bcm2711_pv0_data = {
1011 	.base = {
1012 		.hvs_available_channels = BIT(0),
1013 		.hvs_output = 0,
1014 	},
1015 	.debugfs_name = "crtc0_regs",
1016 	.fifo_depth = 64,
1017 	.pixels_per_clock = 1,
1018 	.encoder_types = {
1019 		[0] = VC4_ENCODER_TYPE_DSI0,
1020 		[1] = VC4_ENCODER_TYPE_DPI,
1021 	},
1022 };
1023 
1024 static const struct vc4_pv_data bcm2711_pv1_data = {
1025 	.base = {
1026 		.hvs_available_channels = BIT(0) | BIT(1) | BIT(2),
1027 		.hvs_output = 3,
1028 	},
1029 	.debugfs_name = "crtc1_regs",
1030 	.fifo_depth = 64,
1031 	.pixels_per_clock = 1,
1032 	.encoder_types = {
1033 		[0] = VC4_ENCODER_TYPE_DSI1,
1034 		[1] = VC4_ENCODER_TYPE_SMI,
1035 	},
1036 };
1037 
1038 static const struct vc4_pv_data bcm2711_pv2_data = {
1039 	.base = {
1040 		.hvs_available_channels = BIT(0) | BIT(1) | BIT(2),
1041 		.hvs_output = 4,
1042 	},
1043 	.debugfs_name = "crtc2_regs",
1044 	.fifo_depth = 256,
1045 	.pixels_per_clock = 2,
1046 	.encoder_types = {
1047 		[0] = VC4_ENCODER_TYPE_HDMI0,
1048 	},
1049 };
1050 
1051 static const struct vc4_pv_data bcm2711_pv3_data = {
1052 	.base = {
1053 		.hvs_available_channels = BIT(1),
1054 		.hvs_output = 1,
1055 	},
1056 	.debugfs_name = "crtc3_regs",
1057 	.fifo_depth = 64,
1058 	.pixels_per_clock = 1,
1059 	.encoder_types = {
1060 		[PV_CONTROL_CLK_SELECT_VEC] = VC4_ENCODER_TYPE_VEC,
1061 	},
1062 };
1063 
1064 static const struct vc4_pv_data bcm2711_pv4_data = {
1065 	.base = {
1066 		.hvs_available_channels = BIT(0) | BIT(1) | BIT(2),
1067 		.hvs_output = 5,
1068 	},
1069 	.debugfs_name = "crtc4_regs",
1070 	.fifo_depth = 64,
1071 	.pixels_per_clock = 2,
1072 	.encoder_types = {
1073 		[0] = VC4_ENCODER_TYPE_HDMI1,
1074 	},
1075 };
1076 
1077 static const struct of_device_id vc4_crtc_dt_match[] = {
1078 	{ .compatible = "brcm,bcm2835-pixelvalve0", .data = &bcm2835_pv0_data },
1079 	{ .compatible = "brcm,bcm2835-pixelvalve1", .data = &bcm2835_pv1_data },
1080 	{ .compatible = "brcm,bcm2835-pixelvalve2", .data = &bcm2835_pv2_data },
1081 	{ .compatible = "brcm,bcm2711-pixelvalve0", .data = &bcm2711_pv0_data },
1082 	{ .compatible = "brcm,bcm2711-pixelvalve1", .data = &bcm2711_pv1_data },
1083 	{ .compatible = "brcm,bcm2711-pixelvalve2", .data = &bcm2711_pv2_data },
1084 	{ .compatible = "brcm,bcm2711-pixelvalve3", .data = &bcm2711_pv3_data },
1085 	{ .compatible = "brcm,bcm2711-pixelvalve4", .data = &bcm2711_pv4_data },
1086 	{}
1087 };
1088 
vc4_set_crtc_possible_masks(struct drm_device * drm,struct drm_crtc * crtc)1089 static void vc4_set_crtc_possible_masks(struct drm_device *drm,
1090 					struct drm_crtc *crtc)
1091 {
1092 	struct vc4_crtc *vc4_crtc = to_vc4_crtc(crtc);
1093 	const struct vc4_pv_data *pv_data = vc4_crtc_to_vc4_pv_data(vc4_crtc);
1094 	const enum vc4_encoder_type *encoder_types = pv_data->encoder_types;
1095 	struct drm_encoder *encoder;
1096 
1097 	drm_for_each_encoder(encoder, drm) {
1098 		struct vc4_encoder *vc4_encoder;
1099 		int i;
1100 
1101 		if (encoder->encoder_type == DRM_MODE_ENCODER_VIRTUAL)
1102 			continue;
1103 
1104 		vc4_encoder = to_vc4_encoder(encoder);
1105 		for (i = 0; i < ARRAY_SIZE(pv_data->encoder_types); i++) {
1106 			if (vc4_encoder->type == encoder_types[i]) {
1107 				vc4_encoder->clock_select = i;
1108 				encoder->possible_crtcs |= drm_crtc_mask(crtc);
1109 				break;
1110 			}
1111 		}
1112 	}
1113 }
1114 
vc4_crtc_init(struct drm_device * drm,struct vc4_crtc * vc4_crtc,const struct drm_crtc_funcs * crtc_funcs,const struct drm_crtc_helper_funcs * crtc_helper_funcs)1115 int vc4_crtc_init(struct drm_device *drm, struct vc4_crtc *vc4_crtc,
1116 		  const struct drm_crtc_funcs *crtc_funcs,
1117 		  const struct drm_crtc_helper_funcs *crtc_helper_funcs)
1118 {
1119 	struct vc4_dev *vc4 = to_vc4_dev(drm);
1120 	struct drm_crtc *crtc = &vc4_crtc->base;
1121 	struct drm_plane *primary_plane;
1122 	unsigned int i;
1123 
1124 	/* For now, we create just the primary and the legacy cursor
1125 	 * planes.  We should be able to stack more planes on easily,
1126 	 * but to do that we would need to compute the bandwidth
1127 	 * requirement of the plane configuration, and reject ones
1128 	 * that will take too much.
1129 	 */
1130 	primary_plane = vc4_plane_init(drm, DRM_PLANE_TYPE_PRIMARY);
1131 	if (IS_ERR(primary_plane)) {
1132 		dev_err(drm->dev, "failed to construct primary plane\n");
1133 		return PTR_ERR(primary_plane);
1134 	}
1135 
1136 	spin_lock_init(&vc4_crtc->irq_lock);
1137 	drm_crtc_init_with_planes(drm, crtc, primary_plane, NULL,
1138 				  crtc_funcs, NULL);
1139 	drm_crtc_helper_add(crtc, crtc_helper_funcs);
1140 
1141 	if (!vc4->hvs->hvs5) {
1142 		drm_mode_crtc_set_gamma_size(crtc, ARRAY_SIZE(vc4_crtc->lut_r));
1143 
1144 		drm_crtc_enable_color_mgmt(crtc, 0, false, crtc->gamma_size);
1145 
1146 		/* We support CTM, but only for one CRTC at a time. It's therefore
1147 		 * implemented as private driver state in vc4_kms, not here.
1148 		 */
1149 		drm_crtc_enable_color_mgmt(crtc, 0, true, crtc->gamma_size);
1150 	}
1151 
1152 	for (i = 0; i < crtc->gamma_size; i++) {
1153 		vc4_crtc->lut_r[i] = i;
1154 		vc4_crtc->lut_g[i] = i;
1155 		vc4_crtc->lut_b[i] = i;
1156 	}
1157 
1158 	return 0;
1159 }
1160 
vc4_crtc_bind(struct device * dev,struct device * master,void * data)1161 static int vc4_crtc_bind(struct device *dev, struct device *master, void *data)
1162 {
1163 	struct platform_device *pdev = to_platform_device(dev);
1164 	struct drm_device *drm = dev_get_drvdata(master);
1165 	const struct vc4_pv_data *pv_data;
1166 	struct vc4_crtc *vc4_crtc;
1167 	struct drm_crtc *crtc;
1168 	struct drm_plane *destroy_plane, *temp;
1169 	int ret;
1170 
1171 	vc4_crtc = devm_kzalloc(dev, sizeof(*vc4_crtc), GFP_KERNEL);
1172 	if (!vc4_crtc)
1173 		return -ENOMEM;
1174 	crtc = &vc4_crtc->base;
1175 
1176 	pv_data = of_device_get_match_data(dev);
1177 	if (!pv_data)
1178 		return -ENODEV;
1179 	vc4_crtc->data = &pv_data->base;
1180 	vc4_crtc->pdev = pdev;
1181 
1182 	vc4_crtc->regs = vc4_ioremap_regs(pdev, 0);
1183 	if (IS_ERR(vc4_crtc->regs))
1184 		return PTR_ERR(vc4_crtc->regs);
1185 
1186 	vc4_crtc->regset.base = vc4_crtc->regs;
1187 	vc4_crtc->regset.regs = crtc_regs;
1188 	vc4_crtc->regset.nregs = ARRAY_SIZE(crtc_regs);
1189 
1190 	ret = vc4_crtc_init(drm, vc4_crtc,
1191 			    &vc4_crtc_funcs, &vc4_crtc_helper_funcs);
1192 	if (ret)
1193 		return ret;
1194 	vc4_set_crtc_possible_masks(drm, crtc);
1195 
1196 	CRTC_WRITE(PV_INTEN, 0);
1197 	CRTC_WRITE(PV_INTSTAT, PV_INT_VFP_START);
1198 	ret = devm_request_irq(dev, platform_get_irq(pdev, 0),
1199 			       vc4_crtc_irq_handler,
1200 			       IRQF_SHARED,
1201 			       "vc4 crtc", vc4_crtc);
1202 	if (ret)
1203 		goto err_destroy_planes;
1204 
1205 	platform_set_drvdata(pdev, vc4_crtc);
1206 
1207 	vc4_debugfs_add_regset32(drm, pv_data->debugfs_name,
1208 				 &vc4_crtc->regset);
1209 
1210 	return 0;
1211 
1212 err_destroy_planes:
1213 	list_for_each_entry_safe(destroy_plane, temp,
1214 				 &drm->mode_config.plane_list, head) {
1215 		if (destroy_plane->possible_crtcs == drm_crtc_mask(crtc))
1216 		    destroy_plane->funcs->destroy(destroy_plane);
1217 	}
1218 
1219 	return ret;
1220 }
1221 
vc4_crtc_unbind(struct device * dev,struct device * master,void * data)1222 static void vc4_crtc_unbind(struct device *dev, struct device *master,
1223 			    void *data)
1224 {
1225 	struct platform_device *pdev = to_platform_device(dev);
1226 	struct vc4_crtc *vc4_crtc = dev_get_drvdata(dev);
1227 
1228 	vc4_crtc_destroy(&vc4_crtc->base);
1229 
1230 	CRTC_WRITE(PV_INTEN, 0);
1231 
1232 	platform_set_drvdata(pdev, NULL);
1233 }
1234 
1235 static const struct component_ops vc4_crtc_ops = {
1236 	.bind   = vc4_crtc_bind,
1237 	.unbind = vc4_crtc_unbind,
1238 };
1239 
vc4_crtc_dev_probe(struct platform_device * pdev)1240 static int vc4_crtc_dev_probe(struct platform_device *pdev)
1241 {
1242 	return component_add(&pdev->dev, &vc4_crtc_ops);
1243 }
1244 
vc4_crtc_dev_remove(struct platform_device * pdev)1245 static int vc4_crtc_dev_remove(struct platform_device *pdev)
1246 {
1247 	component_del(&pdev->dev, &vc4_crtc_ops);
1248 	return 0;
1249 }
1250 
1251 struct platform_driver vc4_crtc_driver = {
1252 	.probe = vc4_crtc_dev_probe,
1253 	.remove = vc4_crtc_dev_remove,
1254 	.driver = {
1255 		.name = "vc4_crtc",
1256 		.of_match_table = vc4_crtc_dt_match,
1257 	},
1258 };
1259