18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * Copyright (C) 2015 Broadcom 48c2ecf20Sopenharmony_ci */ 58c2ecf20Sopenharmony_ci 68c2ecf20Sopenharmony_ci/** 78c2ecf20Sopenharmony_ci * DOC: VC4 CRTC module 88c2ecf20Sopenharmony_ci * 98c2ecf20Sopenharmony_ci * In VC4, the Pixel Valve is what most closely corresponds to the 108c2ecf20Sopenharmony_ci * DRM's concept of a CRTC. The PV generates video timings from the 118c2ecf20Sopenharmony_ci * encoder's clock plus its configuration. It pulls scaled pixels from 128c2ecf20Sopenharmony_ci * the HVS at that timing, and feeds it to the encoder. 138c2ecf20Sopenharmony_ci * 148c2ecf20Sopenharmony_ci * However, the DRM CRTC also collects the configuration of all the 158c2ecf20Sopenharmony_ci * DRM planes attached to it. As a result, the CRTC is also 168c2ecf20Sopenharmony_ci * responsible for writing the display list for the HVS channel that 178c2ecf20Sopenharmony_ci * the CRTC will use. 188c2ecf20Sopenharmony_ci * 198c2ecf20Sopenharmony_ci * The 2835 has 3 different pixel valves. pv0 in the audio power 208c2ecf20Sopenharmony_ci * domain feeds DSI0 or DPI, while pv1 feeds DS1 or SMI. pv2 in the 218c2ecf20Sopenharmony_ci * image domain can feed either HDMI or the SDTV controller. The 228c2ecf20Sopenharmony_ci * pixel valve chooses from the CPRMAN clocks (HSM for HDMI, VEC for 238c2ecf20Sopenharmony_ci * SDTV, etc.) according to which output type is chosen in the mux. 248c2ecf20Sopenharmony_ci * 258c2ecf20Sopenharmony_ci * For power management, the pixel valve's registers are all clocked 268c2ecf20Sopenharmony_ci * by the AXI clock, while the timings and FIFOs make use of the 278c2ecf20Sopenharmony_ci * output-specific clock. Since the encoders also directly consume 288c2ecf20Sopenharmony_ci * the CPRMAN clocks, and know what timings they need, they are the 298c2ecf20Sopenharmony_ci * ones that set the clock. 308c2ecf20Sopenharmony_ci */ 318c2ecf20Sopenharmony_ci 328c2ecf20Sopenharmony_ci#include <linux/clk.h> 338c2ecf20Sopenharmony_ci#include <linux/component.h> 348c2ecf20Sopenharmony_ci#include <linux/of_device.h> 358c2ecf20Sopenharmony_ci 368c2ecf20Sopenharmony_ci#include <drm/drm_atomic.h> 378c2ecf20Sopenharmony_ci#include <drm/drm_atomic_helper.h> 388c2ecf20Sopenharmony_ci#include <drm/drm_atomic_uapi.h> 398c2ecf20Sopenharmony_ci#include <drm/drm_fb_cma_helper.h> 408c2ecf20Sopenharmony_ci#include <drm/drm_print.h> 418c2ecf20Sopenharmony_ci#include <drm/drm_probe_helper.h> 428c2ecf20Sopenharmony_ci#include <drm/drm_vblank.h> 438c2ecf20Sopenharmony_ci 448c2ecf20Sopenharmony_ci#include "vc4_drv.h" 458c2ecf20Sopenharmony_ci#include "vc4_regs.h" 468c2ecf20Sopenharmony_ci 478c2ecf20Sopenharmony_ci#define HVS_FIFO_LATENCY_PIX 6 488c2ecf20Sopenharmony_ci 498c2ecf20Sopenharmony_ci#define CRTC_WRITE(offset, val) writel(val, vc4_crtc->regs + (offset)) 508c2ecf20Sopenharmony_ci#define CRTC_READ(offset) readl(vc4_crtc->regs + (offset)) 518c2ecf20Sopenharmony_ci 528c2ecf20Sopenharmony_cistatic const struct debugfs_reg32 crtc_regs[] = { 538c2ecf20Sopenharmony_ci VC4_REG32(PV_CONTROL), 548c2ecf20Sopenharmony_ci VC4_REG32(PV_V_CONTROL), 558c2ecf20Sopenharmony_ci VC4_REG32(PV_VSYNCD_EVEN), 568c2ecf20Sopenharmony_ci VC4_REG32(PV_HORZA), 578c2ecf20Sopenharmony_ci VC4_REG32(PV_HORZB), 588c2ecf20Sopenharmony_ci VC4_REG32(PV_VERTA), 598c2ecf20Sopenharmony_ci VC4_REG32(PV_VERTB), 608c2ecf20Sopenharmony_ci VC4_REG32(PV_VERTA_EVEN), 618c2ecf20Sopenharmony_ci VC4_REG32(PV_VERTB_EVEN), 628c2ecf20Sopenharmony_ci VC4_REG32(PV_INTEN), 638c2ecf20Sopenharmony_ci VC4_REG32(PV_INTSTAT), 648c2ecf20Sopenharmony_ci VC4_REG32(PV_STAT), 658c2ecf20Sopenharmony_ci VC4_REG32(PV_HACT_ACT), 668c2ecf20Sopenharmony_ci}; 678c2ecf20Sopenharmony_ci 688c2ecf20Sopenharmony_cistatic unsigned int 698c2ecf20Sopenharmony_civc4_crtc_get_cob_allocation(struct vc4_dev *vc4, unsigned int channel) 708c2ecf20Sopenharmony_ci{ 718c2ecf20Sopenharmony_ci u32 dispbase = HVS_READ(SCALER_DISPBASEX(channel)); 728c2ecf20Sopenharmony_ci /* Top/base are supposed to be 4-pixel aligned, but the 738c2ecf20Sopenharmony_ci * Raspberry Pi firmware fills the low bits (which are 748c2ecf20Sopenharmony_ci * presumably ignored). 758c2ecf20Sopenharmony_ci */ 768c2ecf20Sopenharmony_ci u32 top = VC4_GET_FIELD(dispbase, SCALER_DISPBASEX_TOP) & ~3; 778c2ecf20Sopenharmony_ci u32 base = VC4_GET_FIELD(dispbase, SCALER_DISPBASEX_BASE) & ~3; 788c2ecf20Sopenharmony_ci 798c2ecf20Sopenharmony_ci return top - base + 4; 808c2ecf20Sopenharmony_ci} 818c2ecf20Sopenharmony_ci 828c2ecf20Sopenharmony_cistatic bool vc4_crtc_get_scanout_position(struct drm_crtc *crtc, 838c2ecf20Sopenharmony_ci bool in_vblank_irq, 848c2ecf20Sopenharmony_ci int *vpos, int *hpos, 858c2ecf20Sopenharmony_ci ktime_t *stime, ktime_t *etime, 868c2ecf20Sopenharmony_ci const struct drm_display_mode *mode) 878c2ecf20Sopenharmony_ci{ 888c2ecf20Sopenharmony_ci struct drm_device *dev = crtc->dev; 898c2ecf20Sopenharmony_ci struct vc4_dev *vc4 = to_vc4_dev(dev); 908c2ecf20Sopenharmony_ci struct vc4_crtc *vc4_crtc = to_vc4_crtc(crtc); 918c2ecf20Sopenharmony_ci struct vc4_crtc_state *vc4_crtc_state = to_vc4_crtc_state(crtc->state); 928c2ecf20Sopenharmony_ci unsigned int cob_size; 938c2ecf20Sopenharmony_ci u32 val; 948c2ecf20Sopenharmony_ci int fifo_lines; 958c2ecf20Sopenharmony_ci int vblank_lines; 968c2ecf20Sopenharmony_ci bool ret = false; 978c2ecf20Sopenharmony_ci 988c2ecf20Sopenharmony_ci /* preempt_disable_rt() should go right here in PREEMPT_RT patchset. */ 998c2ecf20Sopenharmony_ci 1008c2ecf20Sopenharmony_ci /* Get optional system timestamp before query. */ 1018c2ecf20Sopenharmony_ci if (stime) 1028c2ecf20Sopenharmony_ci *stime = ktime_get(); 1038c2ecf20Sopenharmony_ci 1048c2ecf20Sopenharmony_ci /* 1058c2ecf20Sopenharmony_ci * Read vertical scanline which is currently composed for our 1068c2ecf20Sopenharmony_ci * pixelvalve by the HVS, and also the scaler status. 1078c2ecf20Sopenharmony_ci */ 1088c2ecf20Sopenharmony_ci val = HVS_READ(SCALER_DISPSTATX(vc4_crtc_state->assigned_channel)); 1098c2ecf20Sopenharmony_ci 1108c2ecf20Sopenharmony_ci /* Get optional system timestamp after query. */ 1118c2ecf20Sopenharmony_ci if (etime) 1128c2ecf20Sopenharmony_ci *etime = ktime_get(); 1138c2ecf20Sopenharmony_ci 1148c2ecf20Sopenharmony_ci /* preempt_enable_rt() should go right here in PREEMPT_RT patchset. */ 1158c2ecf20Sopenharmony_ci 1168c2ecf20Sopenharmony_ci /* Vertical position of hvs composed scanline. */ 1178c2ecf20Sopenharmony_ci *vpos = VC4_GET_FIELD(val, SCALER_DISPSTATX_LINE); 1188c2ecf20Sopenharmony_ci *hpos = 0; 1198c2ecf20Sopenharmony_ci 1208c2ecf20Sopenharmony_ci if (mode->flags & DRM_MODE_FLAG_INTERLACE) { 1218c2ecf20Sopenharmony_ci *vpos /= 2; 1228c2ecf20Sopenharmony_ci 1238c2ecf20Sopenharmony_ci /* Use hpos to correct for field offset in interlaced mode. */ 1248c2ecf20Sopenharmony_ci if (VC4_GET_FIELD(val, SCALER_DISPSTATX_FRAME_COUNT) % 2) 1258c2ecf20Sopenharmony_ci *hpos += mode->crtc_htotal / 2; 1268c2ecf20Sopenharmony_ci } 1278c2ecf20Sopenharmony_ci 1288c2ecf20Sopenharmony_ci cob_size = vc4_crtc_get_cob_allocation(vc4, vc4_crtc_state->assigned_channel); 1298c2ecf20Sopenharmony_ci /* This is the offset we need for translating hvs -> pv scanout pos. */ 1308c2ecf20Sopenharmony_ci fifo_lines = cob_size / mode->crtc_hdisplay; 1318c2ecf20Sopenharmony_ci 1328c2ecf20Sopenharmony_ci if (fifo_lines > 0) 1338c2ecf20Sopenharmony_ci ret = true; 1348c2ecf20Sopenharmony_ci 1358c2ecf20Sopenharmony_ci /* HVS more than fifo_lines into frame for compositing? */ 1368c2ecf20Sopenharmony_ci if (*vpos > fifo_lines) { 1378c2ecf20Sopenharmony_ci /* 1388c2ecf20Sopenharmony_ci * We are in active scanout and can get some meaningful results 1398c2ecf20Sopenharmony_ci * from HVS. The actual PV scanout can not trail behind more 1408c2ecf20Sopenharmony_ci * than fifo_lines as that is the fifo's capacity. Assume that 1418c2ecf20Sopenharmony_ci * in active scanout the HVS and PV work in lockstep wrt. HVS 1428c2ecf20Sopenharmony_ci * refilling the fifo and PV consuming from the fifo, ie. 1438c2ecf20Sopenharmony_ci * whenever the PV consumes and frees up a scanline in the 1448c2ecf20Sopenharmony_ci * fifo, the HVS will immediately refill it, therefore 1458c2ecf20Sopenharmony_ci * incrementing vpos. Therefore we choose HVS read position - 1468c2ecf20Sopenharmony_ci * fifo size in scanlines as a estimate of the real scanout 1478c2ecf20Sopenharmony_ci * position of the PV. 1488c2ecf20Sopenharmony_ci */ 1498c2ecf20Sopenharmony_ci *vpos -= fifo_lines + 1; 1508c2ecf20Sopenharmony_ci 1518c2ecf20Sopenharmony_ci return ret; 1528c2ecf20Sopenharmony_ci } 1538c2ecf20Sopenharmony_ci 1548c2ecf20Sopenharmony_ci /* 1558c2ecf20Sopenharmony_ci * Less: This happens when we are in vblank and the HVS, after getting 1568c2ecf20Sopenharmony_ci * the VSTART restart signal from the PV, just started refilling its 1578c2ecf20Sopenharmony_ci * fifo with new lines from the top-most lines of the new framebuffers. 1588c2ecf20Sopenharmony_ci * The PV does not scan out in vblank, so does not remove lines from 1598c2ecf20Sopenharmony_ci * the fifo, so the fifo will be full quickly and the HVS has to pause. 1608c2ecf20Sopenharmony_ci * We can't get meaningful readings wrt. scanline position of the PV 1618c2ecf20Sopenharmony_ci * and need to make things up in a approximative but consistent way. 1628c2ecf20Sopenharmony_ci */ 1638c2ecf20Sopenharmony_ci vblank_lines = mode->vtotal - mode->vdisplay; 1648c2ecf20Sopenharmony_ci 1658c2ecf20Sopenharmony_ci if (in_vblank_irq) { 1668c2ecf20Sopenharmony_ci /* 1678c2ecf20Sopenharmony_ci * Assume the irq handler got called close to first 1688c2ecf20Sopenharmony_ci * line of vblank, so PV has about a full vblank 1698c2ecf20Sopenharmony_ci * scanlines to go, and as a base timestamp use the 1708c2ecf20Sopenharmony_ci * one taken at entry into vblank irq handler, so it 1718c2ecf20Sopenharmony_ci * is not affected by random delays due to lock 1728c2ecf20Sopenharmony_ci * contention on event_lock or vblank_time lock in 1738c2ecf20Sopenharmony_ci * the core. 1748c2ecf20Sopenharmony_ci */ 1758c2ecf20Sopenharmony_ci *vpos = -vblank_lines; 1768c2ecf20Sopenharmony_ci 1778c2ecf20Sopenharmony_ci if (stime) 1788c2ecf20Sopenharmony_ci *stime = vc4_crtc->t_vblank; 1798c2ecf20Sopenharmony_ci if (etime) 1808c2ecf20Sopenharmony_ci *etime = vc4_crtc->t_vblank; 1818c2ecf20Sopenharmony_ci 1828c2ecf20Sopenharmony_ci /* 1838c2ecf20Sopenharmony_ci * If the HVS fifo is not yet full then we know for certain 1848c2ecf20Sopenharmony_ci * we are at the very beginning of vblank, as the hvs just 1858c2ecf20Sopenharmony_ci * started refilling, and the stime and etime timestamps 1868c2ecf20Sopenharmony_ci * truly correspond to start of vblank. 1878c2ecf20Sopenharmony_ci * 1888c2ecf20Sopenharmony_ci * Unfortunately there's no way to report this to upper levels 1898c2ecf20Sopenharmony_ci * and make it more useful. 1908c2ecf20Sopenharmony_ci */ 1918c2ecf20Sopenharmony_ci } else { 1928c2ecf20Sopenharmony_ci /* 1938c2ecf20Sopenharmony_ci * No clue where we are inside vblank. Return a vpos of zero, 1948c2ecf20Sopenharmony_ci * which will cause calling code to just return the etime 1958c2ecf20Sopenharmony_ci * timestamp uncorrected. At least this is no worse than the 1968c2ecf20Sopenharmony_ci * standard fallback. 1978c2ecf20Sopenharmony_ci */ 1988c2ecf20Sopenharmony_ci *vpos = 0; 1998c2ecf20Sopenharmony_ci } 2008c2ecf20Sopenharmony_ci 2018c2ecf20Sopenharmony_ci return ret; 2028c2ecf20Sopenharmony_ci} 2038c2ecf20Sopenharmony_ci 2048c2ecf20Sopenharmony_civoid vc4_crtc_destroy(struct drm_crtc *crtc) 2058c2ecf20Sopenharmony_ci{ 2068c2ecf20Sopenharmony_ci drm_crtc_cleanup(crtc); 2078c2ecf20Sopenharmony_ci} 2088c2ecf20Sopenharmony_ci 2098c2ecf20Sopenharmony_cistatic u32 vc4_get_fifo_full_level(struct vc4_crtc *vc4_crtc, u32 format) 2108c2ecf20Sopenharmony_ci{ 2118c2ecf20Sopenharmony_ci const struct vc4_crtc_data *crtc_data = vc4_crtc_to_vc4_crtc_data(vc4_crtc); 2128c2ecf20Sopenharmony_ci const struct vc4_pv_data *pv_data = vc4_crtc_to_vc4_pv_data(vc4_crtc); 2138c2ecf20Sopenharmony_ci struct vc4_dev *vc4 = to_vc4_dev(vc4_crtc->base.dev); 2148c2ecf20Sopenharmony_ci u32 fifo_len_bytes = pv_data->fifo_depth; 2158c2ecf20Sopenharmony_ci 2168c2ecf20Sopenharmony_ci /* 2178c2ecf20Sopenharmony_ci * Pixels are pulled from the HVS if the number of bytes is 2188c2ecf20Sopenharmony_ci * lower than the FIFO full level. 2198c2ecf20Sopenharmony_ci * 2208c2ecf20Sopenharmony_ci * The latency of the pixel fetch mechanism is 6 pixels, so we 2218c2ecf20Sopenharmony_ci * need to convert those 6 pixels in bytes, depending on the 2228c2ecf20Sopenharmony_ci * format, and then subtract that from the length of the FIFO 2238c2ecf20Sopenharmony_ci * to make sure we never end up in a situation where the FIFO 2248c2ecf20Sopenharmony_ci * is full. 2258c2ecf20Sopenharmony_ci */ 2268c2ecf20Sopenharmony_ci switch (format) { 2278c2ecf20Sopenharmony_ci case PV_CONTROL_FORMAT_DSIV_16: 2288c2ecf20Sopenharmony_ci case PV_CONTROL_FORMAT_DSIC_16: 2298c2ecf20Sopenharmony_ci return fifo_len_bytes - 2 * HVS_FIFO_LATENCY_PIX; 2308c2ecf20Sopenharmony_ci case PV_CONTROL_FORMAT_DSIV_18: 2318c2ecf20Sopenharmony_ci return fifo_len_bytes - 14; 2328c2ecf20Sopenharmony_ci case PV_CONTROL_FORMAT_24: 2338c2ecf20Sopenharmony_ci case PV_CONTROL_FORMAT_DSIV_24: 2348c2ecf20Sopenharmony_ci default: 2358c2ecf20Sopenharmony_ci /* 2368c2ecf20Sopenharmony_ci * For some reason, the pixelvalve4 doesn't work with 2378c2ecf20Sopenharmony_ci * the usual formula and will only work with 32. 2388c2ecf20Sopenharmony_ci */ 2398c2ecf20Sopenharmony_ci if (crtc_data->hvs_output == 5) 2408c2ecf20Sopenharmony_ci return 32; 2418c2ecf20Sopenharmony_ci 2428c2ecf20Sopenharmony_ci /* 2438c2ecf20Sopenharmony_ci * It looks like in some situations, we will overflow 2448c2ecf20Sopenharmony_ci * the PixelValve FIFO (with the bit 10 of PV stat being 2458c2ecf20Sopenharmony_ci * set) and stall the HVS / PV, eventually resulting in 2468c2ecf20Sopenharmony_ci * a page flip timeout. 2478c2ecf20Sopenharmony_ci * 2488c2ecf20Sopenharmony_ci * Displaying the video overlay during a playback with 2498c2ecf20Sopenharmony_ci * Kodi on an RPi3 seems to be a great solution with a 2508c2ecf20Sopenharmony_ci * failure rate around 50%. 2518c2ecf20Sopenharmony_ci * 2528c2ecf20Sopenharmony_ci * Removing 1 from the FIFO full level however 2538c2ecf20Sopenharmony_ci * seems to completely remove that issue. 2548c2ecf20Sopenharmony_ci */ 2558c2ecf20Sopenharmony_ci if (!vc4->hvs->hvs5) 2568c2ecf20Sopenharmony_ci return fifo_len_bytes - 3 * HVS_FIFO_LATENCY_PIX - 1; 2578c2ecf20Sopenharmony_ci 2588c2ecf20Sopenharmony_ci return fifo_len_bytes - 3 * HVS_FIFO_LATENCY_PIX; 2598c2ecf20Sopenharmony_ci } 2608c2ecf20Sopenharmony_ci} 2618c2ecf20Sopenharmony_ci 2628c2ecf20Sopenharmony_cistatic u32 vc4_crtc_get_fifo_full_level_bits(struct vc4_crtc *vc4_crtc, 2638c2ecf20Sopenharmony_ci u32 format) 2648c2ecf20Sopenharmony_ci{ 2658c2ecf20Sopenharmony_ci u32 level = vc4_get_fifo_full_level(vc4_crtc, format); 2668c2ecf20Sopenharmony_ci u32 ret = 0; 2678c2ecf20Sopenharmony_ci 2688c2ecf20Sopenharmony_ci ret |= VC4_SET_FIELD((level >> 6), 2698c2ecf20Sopenharmony_ci PV5_CONTROL_FIFO_LEVEL_HIGH); 2708c2ecf20Sopenharmony_ci 2718c2ecf20Sopenharmony_ci return ret | VC4_SET_FIELD(level & 0x3f, 2728c2ecf20Sopenharmony_ci PV_CONTROL_FIFO_LEVEL); 2738c2ecf20Sopenharmony_ci} 2748c2ecf20Sopenharmony_ci 2758c2ecf20Sopenharmony_ci/* 2768c2ecf20Sopenharmony_ci * Returns the encoder attached to the CRTC. 2778c2ecf20Sopenharmony_ci * 2788c2ecf20Sopenharmony_ci * VC4 can only scan out to one encoder at a time, while the DRM core 2798c2ecf20Sopenharmony_ci * allows drivers to push pixels to more than one encoder from the 2808c2ecf20Sopenharmony_ci * same CRTC. 2818c2ecf20Sopenharmony_ci */ 2828c2ecf20Sopenharmony_cistatic struct drm_encoder *vc4_get_crtc_encoder(struct drm_crtc *crtc) 2838c2ecf20Sopenharmony_ci{ 2848c2ecf20Sopenharmony_ci struct drm_connector *connector; 2858c2ecf20Sopenharmony_ci struct drm_connector_list_iter conn_iter; 2868c2ecf20Sopenharmony_ci 2878c2ecf20Sopenharmony_ci drm_connector_list_iter_begin(crtc->dev, &conn_iter); 2888c2ecf20Sopenharmony_ci drm_for_each_connector_iter(connector, &conn_iter) { 2898c2ecf20Sopenharmony_ci if (connector->state->crtc == crtc) { 2908c2ecf20Sopenharmony_ci drm_connector_list_iter_end(&conn_iter); 2918c2ecf20Sopenharmony_ci return connector->encoder; 2928c2ecf20Sopenharmony_ci } 2938c2ecf20Sopenharmony_ci } 2948c2ecf20Sopenharmony_ci drm_connector_list_iter_end(&conn_iter); 2958c2ecf20Sopenharmony_ci 2968c2ecf20Sopenharmony_ci return NULL; 2978c2ecf20Sopenharmony_ci} 2988c2ecf20Sopenharmony_ci 2998c2ecf20Sopenharmony_cistatic void vc4_crtc_pixelvalve_reset(struct drm_crtc *crtc) 3008c2ecf20Sopenharmony_ci{ 3018c2ecf20Sopenharmony_ci struct vc4_crtc *vc4_crtc = to_vc4_crtc(crtc); 3028c2ecf20Sopenharmony_ci 3038c2ecf20Sopenharmony_ci /* The PV needs to be disabled before it can be flushed */ 3048c2ecf20Sopenharmony_ci CRTC_WRITE(PV_CONTROL, CRTC_READ(PV_CONTROL) & ~PV_CONTROL_EN); 3058c2ecf20Sopenharmony_ci CRTC_WRITE(PV_CONTROL, CRTC_READ(PV_CONTROL) | PV_CONTROL_FIFO_CLR); 3068c2ecf20Sopenharmony_ci} 3078c2ecf20Sopenharmony_ci 3088c2ecf20Sopenharmony_cistatic void vc4_crtc_config_pv(struct drm_crtc *crtc) 3098c2ecf20Sopenharmony_ci{ 3108c2ecf20Sopenharmony_ci struct drm_device *dev = crtc->dev; 3118c2ecf20Sopenharmony_ci struct vc4_dev *vc4 = to_vc4_dev(dev); 3128c2ecf20Sopenharmony_ci struct drm_encoder *encoder = vc4_get_crtc_encoder(crtc); 3138c2ecf20Sopenharmony_ci struct vc4_encoder *vc4_encoder = to_vc4_encoder(encoder); 3148c2ecf20Sopenharmony_ci struct vc4_crtc *vc4_crtc = to_vc4_crtc(crtc); 3158c2ecf20Sopenharmony_ci const struct vc4_pv_data *pv_data = vc4_crtc_to_vc4_pv_data(vc4_crtc); 3168c2ecf20Sopenharmony_ci struct drm_crtc_state *state = crtc->state; 3178c2ecf20Sopenharmony_ci struct drm_display_mode *mode = &state->adjusted_mode; 3188c2ecf20Sopenharmony_ci bool interlace = mode->flags & DRM_MODE_FLAG_INTERLACE; 3198c2ecf20Sopenharmony_ci u32 pixel_rep = (mode->flags & DRM_MODE_FLAG_DBLCLK) ? 2 : 1; 3208c2ecf20Sopenharmony_ci bool is_dsi = (vc4_encoder->type == VC4_ENCODER_TYPE_DSI0 || 3218c2ecf20Sopenharmony_ci vc4_encoder->type == VC4_ENCODER_TYPE_DSI1); 3228c2ecf20Sopenharmony_ci bool is_dsi1 = vc4_encoder->type == VC4_ENCODER_TYPE_DSI1; 3238c2ecf20Sopenharmony_ci u32 format = is_dsi1 ? PV_CONTROL_FORMAT_DSIV_24 : PV_CONTROL_FORMAT_24; 3248c2ecf20Sopenharmony_ci u8 ppc = pv_data->pixels_per_clock; 3258c2ecf20Sopenharmony_ci bool debug_dump_regs = false; 3268c2ecf20Sopenharmony_ci 3278c2ecf20Sopenharmony_ci if (debug_dump_regs) { 3288c2ecf20Sopenharmony_ci struct drm_printer p = drm_info_printer(&vc4_crtc->pdev->dev); 3298c2ecf20Sopenharmony_ci dev_info(&vc4_crtc->pdev->dev, "CRTC %d regs before:\n", 3308c2ecf20Sopenharmony_ci drm_crtc_index(crtc)); 3318c2ecf20Sopenharmony_ci drm_print_regset32(&p, &vc4_crtc->regset); 3328c2ecf20Sopenharmony_ci } 3338c2ecf20Sopenharmony_ci 3348c2ecf20Sopenharmony_ci vc4_crtc_pixelvalve_reset(crtc); 3358c2ecf20Sopenharmony_ci 3368c2ecf20Sopenharmony_ci CRTC_WRITE(PV_HORZA, 3378c2ecf20Sopenharmony_ci VC4_SET_FIELD((mode->htotal - mode->hsync_end) * pixel_rep / ppc, 3388c2ecf20Sopenharmony_ci PV_HORZA_HBP) | 3398c2ecf20Sopenharmony_ci VC4_SET_FIELD((mode->hsync_end - mode->hsync_start) * pixel_rep / ppc, 3408c2ecf20Sopenharmony_ci PV_HORZA_HSYNC)); 3418c2ecf20Sopenharmony_ci 3428c2ecf20Sopenharmony_ci CRTC_WRITE(PV_HORZB, 3438c2ecf20Sopenharmony_ci VC4_SET_FIELD((mode->hsync_start - mode->hdisplay) * pixel_rep / ppc, 3448c2ecf20Sopenharmony_ci PV_HORZB_HFP) | 3458c2ecf20Sopenharmony_ci VC4_SET_FIELD(mode->hdisplay * pixel_rep / ppc, 3468c2ecf20Sopenharmony_ci PV_HORZB_HACTIVE)); 3478c2ecf20Sopenharmony_ci 3488c2ecf20Sopenharmony_ci CRTC_WRITE(PV_VERTA, 3498c2ecf20Sopenharmony_ci VC4_SET_FIELD(mode->crtc_vtotal - mode->crtc_vsync_end + 3508c2ecf20Sopenharmony_ci interlace, 3518c2ecf20Sopenharmony_ci PV_VERTA_VBP) | 3528c2ecf20Sopenharmony_ci VC4_SET_FIELD(mode->crtc_vsync_end - mode->crtc_vsync_start, 3538c2ecf20Sopenharmony_ci PV_VERTA_VSYNC)); 3548c2ecf20Sopenharmony_ci CRTC_WRITE(PV_VERTB, 3558c2ecf20Sopenharmony_ci VC4_SET_FIELD(mode->crtc_vsync_start - mode->crtc_vdisplay, 3568c2ecf20Sopenharmony_ci PV_VERTB_VFP) | 3578c2ecf20Sopenharmony_ci VC4_SET_FIELD(mode->crtc_vdisplay, PV_VERTB_VACTIVE)); 3588c2ecf20Sopenharmony_ci 3598c2ecf20Sopenharmony_ci if (interlace) { 3608c2ecf20Sopenharmony_ci CRTC_WRITE(PV_VERTA_EVEN, 3618c2ecf20Sopenharmony_ci VC4_SET_FIELD(mode->crtc_vtotal - 3628c2ecf20Sopenharmony_ci mode->crtc_vsync_end, 3638c2ecf20Sopenharmony_ci PV_VERTA_VBP) | 3648c2ecf20Sopenharmony_ci VC4_SET_FIELD(mode->crtc_vsync_end - 3658c2ecf20Sopenharmony_ci mode->crtc_vsync_start, 3668c2ecf20Sopenharmony_ci PV_VERTA_VSYNC)); 3678c2ecf20Sopenharmony_ci CRTC_WRITE(PV_VERTB_EVEN, 3688c2ecf20Sopenharmony_ci VC4_SET_FIELD(mode->crtc_vsync_start - 3698c2ecf20Sopenharmony_ci mode->crtc_vdisplay, 3708c2ecf20Sopenharmony_ci PV_VERTB_VFP) | 3718c2ecf20Sopenharmony_ci VC4_SET_FIELD(mode->crtc_vdisplay, PV_VERTB_VACTIVE)); 3728c2ecf20Sopenharmony_ci 3738c2ecf20Sopenharmony_ci /* We set up first field even mode for HDMI. VEC's 3748c2ecf20Sopenharmony_ci * NTSC mode would want first field odd instead, once 3758c2ecf20Sopenharmony_ci * we support it (to do so, set ODD_FIRST and put the 3768c2ecf20Sopenharmony_ci * delay in VSYNCD_EVEN instead). 3778c2ecf20Sopenharmony_ci */ 3788c2ecf20Sopenharmony_ci CRTC_WRITE(PV_V_CONTROL, 3798c2ecf20Sopenharmony_ci PV_VCONTROL_CONTINUOUS | 3808c2ecf20Sopenharmony_ci (is_dsi ? PV_VCONTROL_DSI : 0) | 3818c2ecf20Sopenharmony_ci PV_VCONTROL_INTERLACE | 3828c2ecf20Sopenharmony_ci VC4_SET_FIELD(mode->htotal * pixel_rep / (2 * ppc), 3838c2ecf20Sopenharmony_ci PV_VCONTROL_ODD_DELAY)); 3848c2ecf20Sopenharmony_ci CRTC_WRITE(PV_VSYNCD_EVEN, 0); 3858c2ecf20Sopenharmony_ci } else { 3868c2ecf20Sopenharmony_ci CRTC_WRITE(PV_V_CONTROL, 3878c2ecf20Sopenharmony_ci PV_VCONTROL_CONTINUOUS | 3888c2ecf20Sopenharmony_ci (is_dsi ? PV_VCONTROL_DSI : 0)); 3898c2ecf20Sopenharmony_ci } 3908c2ecf20Sopenharmony_ci 3918c2ecf20Sopenharmony_ci if (is_dsi) 3928c2ecf20Sopenharmony_ci CRTC_WRITE(PV_HACT_ACT, mode->hdisplay * pixel_rep); 3938c2ecf20Sopenharmony_ci 3948c2ecf20Sopenharmony_ci if (vc4->hvs->hvs5) 3958c2ecf20Sopenharmony_ci CRTC_WRITE(PV_MUX_CFG, 3968c2ecf20Sopenharmony_ci VC4_SET_FIELD(PV_MUX_CFG_RGB_PIXEL_MUX_MODE_NO_SWAP, 3978c2ecf20Sopenharmony_ci PV_MUX_CFG_RGB_PIXEL_MUX_MODE)); 3988c2ecf20Sopenharmony_ci 3998c2ecf20Sopenharmony_ci CRTC_WRITE(PV_CONTROL, PV_CONTROL_FIFO_CLR | 4008c2ecf20Sopenharmony_ci vc4_crtc_get_fifo_full_level_bits(vc4_crtc, format) | 4018c2ecf20Sopenharmony_ci VC4_SET_FIELD(format, PV_CONTROL_FORMAT) | 4028c2ecf20Sopenharmony_ci VC4_SET_FIELD(pixel_rep - 1, PV_CONTROL_PIXEL_REP) | 4038c2ecf20Sopenharmony_ci PV_CONTROL_CLR_AT_START | 4048c2ecf20Sopenharmony_ci PV_CONTROL_TRIGGER_UNDERFLOW | 4058c2ecf20Sopenharmony_ci PV_CONTROL_WAIT_HSTART | 4068c2ecf20Sopenharmony_ci VC4_SET_FIELD(vc4_encoder->clock_select, 4078c2ecf20Sopenharmony_ci PV_CONTROL_CLK_SELECT)); 4088c2ecf20Sopenharmony_ci 4098c2ecf20Sopenharmony_ci if (debug_dump_regs) { 4108c2ecf20Sopenharmony_ci struct drm_printer p = drm_info_printer(&vc4_crtc->pdev->dev); 4118c2ecf20Sopenharmony_ci dev_info(&vc4_crtc->pdev->dev, "CRTC %d regs after:\n", 4128c2ecf20Sopenharmony_ci drm_crtc_index(crtc)); 4138c2ecf20Sopenharmony_ci drm_print_regset32(&p, &vc4_crtc->regset); 4148c2ecf20Sopenharmony_ci } 4158c2ecf20Sopenharmony_ci} 4168c2ecf20Sopenharmony_ci 4178c2ecf20Sopenharmony_cistatic void require_hvs_enabled(struct drm_device *dev) 4188c2ecf20Sopenharmony_ci{ 4198c2ecf20Sopenharmony_ci struct vc4_dev *vc4 = to_vc4_dev(dev); 4208c2ecf20Sopenharmony_ci 4218c2ecf20Sopenharmony_ci WARN_ON_ONCE((HVS_READ(SCALER_DISPCTRL) & SCALER_DISPCTRL_ENABLE) != 4228c2ecf20Sopenharmony_ci SCALER_DISPCTRL_ENABLE); 4238c2ecf20Sopenharmony_ci} 4248c2ecf20Sopenharmony_ci 4258c2ecf20Sopenharmony_cistatic int vc4_crtc_disable(struct drm_crtc *crtc, unsigned int channel) 4268c2ecf20Sopenharmony_ci{ 4278c2ecf20Sopenharmony_ci struct drm_encoder *encoder = vc4_get_crtc_encoder(crtc); 4288c2ecf20Sopenharmony_ci struct vc4_encoder *vc4_encoder = to_vc4_encoder(encoder); 4298c2ecf20Sopenharmony_ci struct vc4_crtc *vc4_crtc = to_vc4_crtc(crtc); 4308c2ecf20Sopenharmony_ci struct drm_device *dev = crtc->dev; 4318c2ecf20Sopenharmony_ci int ret; 4328c2ecf20Sopenharmony_ci 4338c2ecf20Sopenharmony_ci CRTC_WRITE(PV_V_CONTROL, 4348c2ecf20Sopenharmony_ci CRTC_READ(PV_V_CONTROL) & ~PV_VCONTROL_VIDEN); 4358c2ecf20Sopenharmony_ci ret = wait_for(!(CRTC_READ(PV_V_CONTROL) & PV_VCONTROL_VIDEN), 1); 4368c2ecf20Sopenharmony_ci WARN_ONCE(ret, "Timeout waiting for !PV_VCONTROL_VIDEN\n"); 4378c2ecf20Sopenharmony_ci 4388c2ecf20Sopenharmony_ci /* 4398c2ecf20Sopenharmony_ci * This delay is needed to avoid to get a pixel stuck in an 4408c2ecf20Sopenharmony_ci * unflushable FIFO between the pixelvalve and the HDMI 4418c2ecf20Sopenharmony_ci * controllers on the BCM2711. 4428c2ecf20Sopenharmony_ci * 4438c2ecf20Sopenharmony_ci * Timing is fairly sensitive here, so mdelay is the safest 4448c2ecf20Sopenharmony_ci * approach. 4458c2ecf20Sopenharmony_ci * 4468c2ecf20Sopenharmony_ci * If it was to be reworked, the stuck pixel happens on a 4478c2ecf20Sopenharmony_ci * BCM2711 when changing mode with a good probability, so a 4488c2ecf20Sopenharmony_ci * script that changes mode on a regular basis should trigger 4498c2ecf20Sopenharmony_ci * the bug after less than 10 attempts. It manifests itself with 4508c2ecf20Sopenharmony_ci * every pixels being shifted by one to the right, and thus the 4518c2ecf20Sopenharmony_ci * last pixel of a line actually being displayed as the first 4528c2ecf20Sopenharmony_ci * pixel on the next line. 4538c2ecf20Sopenharmony_ci */ 4548c2ecf20Sopenharmony_ci mdelay(20); 4558c2ecf20Sopenharmony_ci 4568c2ecf20Sopenharmony_ci if (vc4_encoder && vc4_encoder->post_crtc_disable) 4578c2ecf20Sopenharmony_ci vc4_encoder->post_crtc_disable(encoder); 4588c2ecf20Sopenharmony_ci 4598c2ecf20Sopenharmony_ci vc4_crtc_pixelvalve_reset(crtc); 4608c2ecf20Sopenharmony_ci vc4_hvs_stop_channel(dev, channel); 4618c2ecf20Sopenharmony_ci 4628c2ecf20Sopenharmony_ci if (vc4_encoder && vc4_encoder->post_crtc_powerdown) 4638c2ecf20Sopenharmony_ci vc4_encoder->post_crtc_powerdown(encoder); 4648c2ecf20Sopenharmony_ci 4658c2ecf20Sopenharmony_ci return 0; 4668c2ecf20Sopenharmony_ci} 4678c2ecf20Sopenharmony_ci 4688c2ecf20Sopenharmony_ciint vc4_crtc_disable_at_boot(struct drm_crtc *crtc) 4698c2ecf20Sopenharmony_ci{ 4708c2ecf20Sopenharmony_ci struct drm_device *drm = crtc->dev; 4718c2ecf20Sopenharmony_ci struct vc4_crtc *vc4_crtc = to_vc4_crtc(crtc); 4728c2ecf20Sopenharmony_ci int channel; 4738c2ecf20Sopenharmony_ci 4748c2ecf20Sopenharmony_ci if (!(of_device_is_compatible(vc4_crtc->pdev->dev.of_node, 4758c2ecf20Sopenharmony_ci "brcm,bcm2711-pixelvalve2") || 4768c2ecf20Sopenharmony_ci of_device_is_compatible(vc4_crtc->pdev->dev.of_node, 4778c2ecf20Sopenharmony_ci "brcm,bcm2711-pixelvalve4"))) 4788c2ecf20Sopenharmony_ci return 0; 4798c2ecf20Sopenharmony_ci 4808c2ecf20Sopenharmony_ci if (!(CRTC_READ(PV_CONTROL) & PV_CONTROL_EN)) 4818c2ecf20Sopenharmony_ci return 0; 4828c2ecf20Sopenharmony_ci 4838c2ecf20Sopenharmony_ci if (!(CRTC_READ(PV_V_CONTROL) & PV_VCONTROL_VIDEN)) 4848c2ecf20Sopenharmony_ci return 0; 4858c2ecf20Sopenharmony_ci 4868c2ecf20Sopenharmony_ci channel = vc4_hvs_get_fifo_from_output(drm, vc4_crtc->data->hvs_output); 4878c2ecf20Sopenharmony_ci if (channel < 0) 4888c2ecf20Sopenharmony_ci return 0; 4898c2ecf20Sopenharmony_ci 4908c2ecf20Sopenharmony_ci return vc4_crtc_disable(crtc, channel); 4918c2ecf20Sopenharmony_ci} 4928c2ecf20Sopenharmony_ci 4938c2ecf20Sopenharmony_cistatic void vc4_crtc_atomic_disable(struct drm_crtc *crtc, 4948c2ecf20Sopenharmony_ci struct drm_crtc_state *old_state) 4958c2ecf20Sopenharmony_ci{ 4968c2ecf20Sopenharmony_ci struct vc4_crtc_state *old_vc4_state = to_vc4_crtc_state(old_state); 4978c2ecf20Sopenharmony_ci struct drm_device *dev = crtc->dev; 4988c2ecf20Sopenharmony_ci 4998c2ecf20Sopenharmony_ci require_hvs_enabled(dev); 5008c2ecf20Sopenharmony_ci 5018c2ecf20Sopenharmony_ci /* Disable vblank irq handling before crtc is disabled. */ 5028c2ecf20Sopenharmony_ci drm_crtc_vblank_off(crtc); 5038c2ecf20Sopenharmony_ci 5048c2ecf20Sopenharmony_ci vc4_crtc_disable(crtc, old_vc4_state->assigned_channel); 5058c2ecf20Sopenharmony_ci 5068c2ecf20Sopenharmony_ci /* 5078c2ecf20Sopenharmony_ci * Make sure we issue a vblank event after disabling the CRTC if 5088c2ecf20Sopenharmony_ci * someone was waiting it. 5098c2ecf20Sopenharmony_ci */ 5108c2ecf20Sopenharmony_ci if (crtc->state->event) { 5118c2ecf20Sopenharmony_ci unsigned long flags; 5128c2ecf20Sopenharmony_ci 5138c2ecf20Sopenharmony_ci spin_lock_irqsave(&dev->event_lock, flags); 5148c2ecf20Sopenharmony_ci drm_crtc_send_vblank_event(crtc, crtc->state->event); 5158c2ecf20Sopenharmony_ci crtc->state->event = NULL; 5168c2ecf20Sopenharmony_ci spin_unlock_irqrestore(&dev->event_lock, flags); 5178c2ecf20Sopenharmony_ci } 5188c2ecf20Sopenharmony_ci} 5198c2ecf20Sopenharmony_ci 5208c2ecf20Sopenharmony_cistatic void vc4_crtc_atomic_enable(struct drm_crtc *crtc, 5218c2ecf20Sopenharmony_ci struct drm_crtc_state *old_state) 5228c2ecf20Sopenharmony_ci{ 5238c2ecf20Sopenharmony_ci struct drm_device *dev = crtc->dev; 5248c2ecf20Sopenharmony_ci struct vc4_crtc *vc4_crtc = to_vc4_crtc(crtc); 5258c2ecf20Sopenharmony_ci struct drm_encoder *encoder = vc4_get_crtc_encoder(crtc); 5268c2ecf20Sopenharmony_ci struct vc4_encoder *vc4_encoder = to_vc4_encoder(encoder); 5278c2ecf20Sopenharmony_ci 5288c2ecf20Sopenharmony_ci require_hvs_enabled(dev); 5298c2ecf20Sopenharmony_ci 5308c2ecf20Sopenharmony_ci /* Enable vblank irq handling before crtc is started otherwise 5318c2ecf20Sopenharmony_ci * drm_crtc_get_vblank() fails in vc4_crtc_update_dlist(). 5328c2ecf20Sopenharmony_ci */ 5338c2ecf20Sopenharmony_ci drm_crtc_vblank_on(crtc); 5348c2ecf20Sopenharmony_ci 5358c2ecf20Sopenharmony_ci vc4_hvs_atomic_enable(crtc, old_state); 5368c2ecf20Sopenharmony_ci 5378c2ecf20Sopenharmony_ci if (vc4_encoder->pre_crtc_configure) 5388c2ecf20Sopenharmony_ci vc4_encoder->pre_crtc_configure(encoder); 5398c2ecf20Sopenharmony_ci 5408c2ecf20Sopenharmony_ci vc4_crtc_config_pv(crtc); 5418c2ecf20Sopenharmony_ci 5428c2ecf20Sopenharmony_ci CRTC_WRITE(PV_CONTROL, CRTC_READ(PV_CONTROL) | PV_CONTROL_EN); 5438c2ecf20Sopenharmony_ci 5448c2ecf20Sopenharmony_ci if (vc4_encoder->pre_crtc_enable) 5458c2ecf20Sopenharmony_ci vc4_encoder->pre_crtc_enable(encoder); 5468c2ecf20Sopenharmony_ci 5478c2ecf20Sopenharmony_ci /* When feeding the transposer block the pixelvalve is unneeded and 5488c2ecf20Sopenharmony_ci * should not be enabled. 5498c2ecf20Sopenharmony_ci */ 5508c2ecf20Sopenharmony_ci CRTC_WRITE(PV_V_CONTROL, 5518c2ecf20Sopenharmony_ci CRTC_READ(PV_V_CONTROL) | PV_VCONTROL_VIDEN); 5528c2ecf20Sopenharmony_ci 5538c2ecf20Sopenharmony_ci if (vc4_encoder->post_crtc_enable) 5548c2ecf20Sopenharmony_ci vc4_encoder->post_crtc_enable(encoder); 5558c2ecf20Sopenharmony_ci} 5568c2ecf20Sopenharmony_ci 5578c2ecf20Sopenharmony_cistatic enum drm_mode_status vc4_crtc_mode_valid(struct drm_crtc *crtc, 5588c2ecf20Sopenharmony_ci const struct drm_display_mode *mode) 5598c2ecf20Sopenharmony_ci{ 5608c2ecf20Sopenharmony_ci /* Do not allow doublescan modes from user space */ 5618c2ecf20Sopenharmony_ci if (mode->flags & DRM_MODE_FLAG_DBLSCAN) { 5628c2ecf20Sopenharmony_ci DRM_DEBUG_KMS("[CRTC:%d] Doublescan mode rejected.\n", 5638c2ecf20Sopenharmony_ci crtc->base.id); 5648c2ecf20Sopenharmony_ci return MODE_NO_DBLESCAN; 5658c2ecf20Sopenharmony_ci } 5668c2ecf20Sopenharmony_ci 5678c2ecf20Sopenharmony_ci return MODE_OK; 5688c2ecf20Sopenharmony_ci} 5698c2ecf20Sopenharmony_ci 5708c2ecf20Sopenharmony_civoid vc4_crtc_get_margins(struct drm_crtc_state *state, 5718c2ecf20Sopenharmony_ci unsigned int *left, unsigned int *right, 5728c2ecf20Sopenharmony_ci unsigned int *top, unsigned int *bottom) 5738c2ecf20Sopenharmony_ci{ 5748c2ecf20Sopenharmony_ci struct vc4_crtc_state *vc4_state = to_vc4_crtc_state(state); 5758c2ecf20Sopenharmony_ci struct drm_connector_state *conn_state; 5768c2ecf20Sopenharmony_ci struct drm_connector *conn; 5778c2ecf20Sopenharmony_ci int i; 5788c2ecf20Sopenharmony_ci 5798c2ecf20Sopenharmony_ci *left = vc4_state->margins.left; 5808c2ecf20Sopenharmony_ci *right = vc4_state->margins.right; 5818c2ecf20Sopenharmony_ci *top = vc4_state->margins.top; 5828c2ecf20Sopenharmony_ci *bottom = vc4_state->margins.bottom; 5838c2ecf20Sopenharmony_ci 5848c2ecf20Sopenharmony_ci /* We have to interate over all new connector states because 5858c2ecf20Sopenharmony_ci * vc4_crtc_get_margins() might be called before 5868c2ecf20Sopenharmony_ci * vc4_crtc_atomic_check() which means margins info in vc4_crtc_state 5878c2ecf20Sopenharmony_ci * might be outdated. 5888c2ecf20Sopenharmony_ci */ 5898c2ecf20Sopenharmony_ci for_each_new_connector_in_state(state->state, conn, conn_state, i) { 5908c2ecf20Sopenharmony_ci if (conn_state->crtc != state->crtc) 5918c2ecf20Sopenharmony_ci continue; 5928c2ecf20Sopenharmony_ci 5938c2ecf20Sopenharmony_ci *left = conn_state->tv.margins.left; 5948c2ecf20Sopenharmony_ci *right = conn_state->tv.margins.right; 5958c2ecf20Sopenharmony_ci *top = conn_state->tv.margins.top; 5968c2ecf20Sopenharmony_ci *bottom = conn_state->tv.margins.bottom; 5978c2ecf20Sopenharmony_ci break; 5988c2ecf20Sopenharmony_ci } 5998c2ecf20Sopenharmony_ci} 6008c2ecf20Sopenharmony_ci 6018c2ecf20Sopenharmony_cistatic int vc4_crtc_atomic_check(struct drm_crtc *crtc, 6028c2ecf20Sopenharmony_ci struct drm_crtc_state *state) 6038c2ecf20Sopenharmony_ci{ 6048c2ecf20Sopenharmony_ci struct vc4_crtc_state *vc4_state = to_vc4_crtc_state(state); 6058c2ecf20Sopenharmony_ci struct drm_connector *conn; 6068c2ecf20Sopenharmony_ci struct drm_connector_state *conn_state; 6078c2ecf20Sopenharmony_ci int ret, i; 6088c2ecf20Sopenharmony_ci 6098c2ecf20Sopenharmony_ci ret = vc4_hvs_atomic_check(crtc, state); 6108c2ecf20Sopenharmony_ci if (ret) 6118c2ecf20Sopenharmony_ci return ret; 6128c2ecf20Sopenharmony_ci 6138c2ecf20Sopenharmony_ci for_each_new_connector_in_state(state->state, conn, conn_state, i) { 6148c2ecf20Sopenharmony_ci if (conn_state->crtc != crtc) 6158c2ecf20Sopenharmony_ci continue; 6168c2ecf20Sopenharmony_ci 6178c2ecf20Sopenharmony_ci vc4_state->margins.left = conn_state->tv.margins.left; 6188c2ecf20Sopenharmony_ci vc4_state->margins.right = conn_state->tv.margins.right; 6198c2ecf20Sopenharmony_ci vc4_state->margins.top = conn_state->tv.margins.top; 6208c2ecf20Sopenharmony_ci vc4_state->margins.bottom = conn_state->tv.margins.bottom; 6218c2ecf20Sopenharmony_ci break; 6228c2ecf20Sopenharmony_ci } 6238c2ecf20Sopenharmony_ci 6248c2ecf20Sopenharmony_ci return 0; 6258c2ecf20Sopenharmony_ci} 6268c2ecf20Sopenharmony_ci 6278c2ecf20Sopenharmony_cistatic int vc4_enable_vblank(struct drm_crtc *crtc) 6288c2ecf20Sopenharmony_ci{ 6298c2ecf20Sopenharmony_ci struct vc4_crtc *vc4_crtc = to_vc4_crtc(crtc); 6308c2ecf20Sopenharmony_ci 6318c2ecf20Sopenharmony_ci CRTC_WRITE(PV_INTEN, PV_INT_VFP_START); 6328c2ecf20Sopenharmony_ci 6338c2ecf20Sopenharmony_ci return 0; 6348c2ecf20Sopenharmony_ci} 6358c2ecf20Sopenharmony_ci 6368c2ecf20Sopenharmony_cistatic void vc4_disable_vblank(struct drm_crtc *crtc) 6378c2ecf20Sopenharmony_ci{ 6388c2ecf20Sopenharmony_ci struct vc4_crtc *vc4_crtc = to_vc4_crtc(crtc); 6398c2ecf20Sopenharmony_ci 6408c2ecf20Sopenharmony_ci CRTC_WRITE(PV_INTEN, 0); 6418c2ecf20Sopenharmony_ci} 6428c2ecf20Sopenharmony_ci 6438c2ecf20Sopenharmony_cistatic void vc4_crtc_handle_page_flip(struct vc4_crtc *vc4_crtc) 6448c2ecf20Sopenharmony_ci{ 6458c2ecf20Sopenharmony_ci struct drm_crtc *crtc = &vc4_crtc->base; 6468c2ecf20Sopenharmony_ci struct drm_device *dev = crtc->dev; 6478c2ecf20Sopenharmony_ci struct vc4_dev *vc4 = to_vc4_dev(dev); 6488c2ecf20Sopenharmony_ci struct vc4_crtc_state *vc4_state = to_vc4_crtc_state(crtc->state); 6498c2ecf20Sopenharmony_ci u32 chan = vc4_state->assigned_channel; 6508c2ecf20Sopenharmony_ci unsigned long flags; 6518c2ecf20Sopenharmony_ci 6528c2ecf20Sopenharmony_ci spin_lock_irqsave(&dev->event_lock, flags); 6538c2ecf20Sopenharmony_ci if (vc4_crtc->event && 6548c2ecf20Sopenharmony_ci (vc4_state->mm.start == HVS_READ(SCALER_DISPLACTX(chan)) || 6558c2ecf20Sopenharmony_ci vc4_state->feed_txp)) { 6568c2ecf20Sopenharmony_ci drm_crtc_send_vblank_event(crtc, vc4_crtc->event); 6578c2ecf20Sopenharmony_ci vc4_crtc->event = NULL; 6588c2ecf20Sopenharmony_ci drm_crtc_vblank_put(crtc); 6598c2ecf20Sopenharmony_ci 6608c2ecf20Sopenharmony_ci /* Wait for the page flip to unmask the underrun to ensure that 6618c2ecf20Sopenharmony_ci * the display list was updated by the hardware. Before that 6628c2ecf20Sopenharmony_ci * happens, the HVS will be using the previous display list with 6638c2ecf20Sopenharmony_ci * the CRTC and encoder already reconfigured, leading to 6648c2ecf20Sopenharmony_ci * underruns. This can be seen when reconfiguring the CRTC. 6658c2ecf20Sopenharmony_ci */ 6668c2ecf20Sopenharmony_ci vc4_hvs_unmask_underrun(dev, chan); 6678c2ecf20Sopenharmony_ci } 6688c2ecf20Sopenharmony_ci spin_unlock_irqrestore(&dev->event_lock, flags); 6698c2ecf20Sopenharmony_ci} 6708c2ecf20Sopenharmony_ci 6718c2ecf20Sopenharmony_civoid vc4_crtc_handle_vblank(struct vc4_crtc *crtc) 6728c2ecf20Sopenharmony_ci{ 6738c2ecf20Sopenharmony_ci crtc->t_vblank = ktime_get(); 6748c2ecf20Sopenharmony_ci drm_crtc_handle_vblank(&crtc->base); 6758c2ecf20Sopenharmony_ci vc4_crtc_handle_page_flip(crtc); 6768c2ecf20Sopenharmony_ci} 6778c2ecf20Sopenharmony_ci 6788c2ecf20Sopenharmony_cistatic irqreturn_t vc4_crtc_irq_handler(int irq, void *data) 6798c2ecf20Sopenharmony_ci{ 6808c2ecf20Sopenharmony_ci struct vc4_crtc *vc4_crtc = data; 6818c2ecf20Sopenharmony_ci u32 stat = CRTC_READ(PV_INTSTAT); 6828c2ecf20Sopenharmony_ci irqreturn_t ret = IRQ_NONE; 6838c2ecf20Sopenharmony_ci 6848c2ecf20Sopenharmony_ci if (stat & PV_INT_VFP_START) { 6858c2ecf20Sopenharmony_ci CRTC_WRITE(PV_INTSTAT, PV_INT_VFP_START); 6868c2ecf20Sopenharmony_ci vc4_crtc_handle_vblank(vc4_crtc); 6878c2ecf20Sopenharmony_ci ret = IRQ_HANDLED; 6888c2ecf20Sopenharmony_ci } 6898c2ecf20Sopenharmony_ci 6908c2ecf20Sopenharmony_ci return ret; 6918c2ecf20Sopenharmony_ci} 6928c2ecf20Sopenharmony_ci 6938c2ecf20Sopenharmony_cistruct vc4_async_flip_state { 6948c2ecf20Sopenharmony_ci struct drm_crtc *crtc; 6958c2ecf20Sopenharmony_ci struct drm_framebuffer *fb; 6968c2ecf20Sopenharmony_ci struct drm_framebuffer *old_fb; 6978c2ecf20Sopenharmony_ci struct drm_pending_vblank_event *event; 6988c2ecf20Sopenharmony_ci 6998c2ecf20Sopenharmony_ci struct vc4_seqno_cb cb; 7008c2ecf20Sopenharmony_ci}; 7018c2ecf20Sopenharmony_ci 7028c2ecf20Sopenharmony_ci/* Called when the V3D execution for the BO being flipped to is done, so that 7038c2ecf20Sopenharmony_ci * we can actually update the plane's address to point to it. 7048c2ecf20Sopenharmony_ci */ 7058c2ecf20Sopenharmony_cistatic void 7068c2ecf20Sopenharmony_civc4_async_page_flip_complete(struct vc4_seqno_cb *cb) 7078c2ecf20Sopenharmony_ci{ 7088c2ecf20Sopenharmony_ci struct vc4_async_flip_state *flip_state = 7098c2ecf20Sopenharmony_ci container_of(cb, struct vc4_async_flip_state, cb); 7108c2ecf20Sopenharmony_ci struct drm_crtc *crtc = flip_state->crtc; 7118c2ecf20Sopenharmony_ci struct drm_device *dev = crtc->dev; 7128c2ecf20Sopenharmony_ci struct vc4_dev *vc4 = to_vc4_dev(dev); 7138c2ecf20Sopenharmony_ci struct drm_plane *plane = crtc->primary; 7148c2ecf20Sopenharmony_ci 7158c2ecf20Sopenharmony_ci vc4_plane_async_set_fb(plane, flip_state->fb); 7168c2ecf20Sopenharmony_ci if (flip_state->event) { 7178c2ecf20Sopenharmony_ci unsigned long flags; 7188c2ecf20Sopenharmony_ci 7198c2ecf20Sopenharmony_ci spin_lock_irqsave(&dev->event_lock, flags); 7208c2ecf20Sopenharmony_ci drm_crtc_send_vblank_event(crtc, flip_state->event); 7218c2ecf20Sopenharmony_ci spin_unlock_irqrestore(&dev->event_lock, flags); 7228c2ecf20Sopenharmony_ci } 7238c2ecf20Sopenharmony_ci 7248c2ecf20Sopenharmony_ci drm_crtc_vblank_put(crtc); 7258c2ecf20Sopenharmony_ci drm_framebuffer_put(flip_state->fb); 7268c2ecf20Sopenharmony_ci 7278c2ecf20Sopenharmony_ci /* Decrement the BO usecnt in order to keep the inc/dec calls balanced 7288c2ecf20Sopenharmony_ci * when the planes are updated through the async update path. 7298c2ecf20Sopenharmony_ci * FIXME: we should move to generic async-page-flip when it's 7308c2ecf20Sopenharmony_ci * available, so that we can get rid of this hand-made cleanup_fb() 7318c2ecf20Sopenharmony_ci * logic. 7328c2ecf20Sopenharmony_ci */ 7338c2ecf20Sopenharmony_ci if (flip_state->old_fb) { 7348c2ecf20Sopenharmony_ci struct drm_gem_cma_object *cma_bo; 7358c2ecf20Sopenharmony_ci struct vc4_bo *bo; 7368c2ecf20Sopenharmony_ci 7378c2ecf20Sopenharmony_ci cma_bo = drm_fb_cma_get_gem_obj(flip_state->old_fb, 0); 7388c2ecf20Sopenharmony_ci bo = to_vc4_bo(&cma_bo->base); 7398c2ecf20Sopenharmony_ci vc4_bo_dec_usecnt(bo); 7408c2ecf20Sopenharmony_ci drm_framebuffer_put(flip_state->old_fb); 7418c2ecf20Sopenharmony_ci } 7428c2ecf20Sopenharmony_ci 7438c2ecf20Sopenharmony_ci kfree(flip_state); 7448c2ecf20Sopenharmony_ci 7458c2ecf20Sopenharmony_ci up(&vc4->async_modeset); 7468c2ecf20Sopenharmony_ci} 7478c2ecf20Sopenharmony_ci 7488c2ecf20Sopenharmony_ci/* Implements async (non-vblank-synced) page flips. 7498c2ecf20Sopenharmony_ci * 7508c2ecf20Sopenharmony_ci * The page flip ioctl needs to return immediately, so we grab the 7518c2ecf20Sopenharmony_ci * modeset semaphore on the pipe, and queue the address update for 7528c2ecf20Sopenharmony_ci * when V3D is done with the BO being flipped to. 7538c2ecf20Sopenharmony_ci */ 7548c2ecf20Sopenharmony_cistatic int vc4_async_page_flip(struct drm_crtc *crtc, 7558c2ecf20Sopenharmony_ci struct drm_framebuffer *fb, 7568c2ecf20Sopenharmony_ci struct drm_pending_vblank_event *event, 7578c2ecf20Sopenharmony_ci uint32_t flags) 7588c2ecf20Sopenharmony_ci{ 7598c2ecf20Sopenharmony_ci struct drm_device *dev = crtc->dev; 7608c2ecf20Sopenharmony_ci struct vc4_dev *vc4 = to_vc4_dev(dev); 7618c2ecf20Sopenharmony_ci struct drm_plane *plane = crtc->primary; 7628c2ecf20Sopenharmony_ci int ret = 0; 7638c2ecf20Sopenharmony_ci struct vc4_async_flip_state *flip_state; 7648c2ecf20Sopenharmony_ci struct drm_gem_cma_object *cma_bo = drm_fb_cma_get_gem_obj(fb, 0); 7658c2ecf20Sopenharmony_ci struct vc4_bo *bo = to_vc4_bo(&cma_bo->base); 7668c2ecf20Sopenharmony_ci 7678c2ecf20Sopenharmony_ci /* Increment the BO usecnt here, so that we never end up with an 7688c2ecf20Sopenharmony_ci * unbalanced number of vc4_bo_{dec,inc}_usecnt() calls when the 7698c2ecf20Sopenharmony_ci * plane is later updated through the non-async path. 7708c2ecf20Sopenharmony_ci * FIXME: we should move to generic async-page-flip when it's 7718c2ecf20Sopenharmony_ci * available, so that we can get rid of this hand-made prepare_fb() 7728c2ecf20Sopenharmony_ci * logic. 7738c2ecf20Sopenharmony_ci */ 7748c2ecf20Sopenharmony_ci ret = vc4_bo_inc_usecnt(bo); 7758c2ecf20Sopenharmony_ci if (ret) 7768c2ecf20Sopenharmony_ci return ret; 7778c2ecf20Sopenharmony_ci 7788c2ecf20Sopenharmony_ci flip_state = kzalloc(sizeof(*flip_state), GFP_KERNEL); 7798c2ecf20Sopenharmony_ci if (!flip_state) { 7808c2ecf20Sopenharmony_ci vc4_bo_dec_usecnt(bo); 7818c2ecf20Sopenharmony_ci return -ENOMEM; 7828c2ecf20Sopenharmony_ci } 7838c2ecf20Sopenharmony_ci 7848c2ecf20Sopenharmony_ci drm_framebuffer_get(fb); 7858c2ecf20Sopenharmony_ci flip_state->fb = fb; 7868c2ecf20Sopenharmony_ci flip_state->crtc = crtc; 7878c2ecf20Sopenharmony_ci flip_state->event = event; 7888c2ecf20Sopenharmony_ci 7898c2ecf20Sopenharmony_ci /* Make sure all other async modesetes have landed. */ 7908c2ecf20Sopenharmony_ci ret = down_interruptible(&vc4->async_modeset); 7918c2ecf20Sopenharmony_ci if (ret) { 7928c2ecf20Sopenharmony_ci drm_framebuffer_put(fb); 7938c2ecf20Sopenharmony_ci vc4_bo_dec_usecnt(bo); 7948c2ecf20Sopenharmony_ci kfree(flip_state); 7958c2ecf20Sopenharmony_ci return ret; 7968c2ecf20Sopenharmony_ci } 7978c2ecf20Sopenharmony_ci 7988c2ecf20Sopenharmony_ci /* Save the current FB before it's replaced by the new one in 7998c2ecf20Sopenharmony_ci * drm_atomic_set_fb_for_plane(). We'll need the old FB in 8008c2ecf20Sopenharmony_ci * vc4_async_page_flip_complete() to decrement the BO usecnt and keep 8018c2ecf20Sopenharmony_ci * it consistent. 8028c2ecf20Sopenharmony_ci * FIXME: we should move to generic async-page-flip when it's 8038c2ecf20Sopenharmony_ci * available, so that we can get rid of this hand-made cleanup_fb() 8048c2ecf20Sopenharmony_ci * logic. 8058c2ecf20Sopenharmony_ci */ 8068c2ecf20Sopenharmony_ci flip_state->old_fb = plane->state->fb; 8078c2ecf20Sopenharmony_ci if (flip_state->old_fb) 8088c2ecf20Sopenharmony_ci drm_framebuffer_get(flip_state->old_fb); 8098c2ecf20Sopenharmony_ci 8108c2ecf20Sopenharmony_ci WARN_ON(drm_crtc_vblank_get(crtc) != 0); 8118c2ecf20Sopenharmony_ci 8128c2ecf20Sopenharmony_ci /* Immediately update the plane's legacy fb pointer, so that later 8138c2ecf20Sopenharmony_ci * modeset prep sees the state that will be present when the semaphore 8148c2ecf20Sopenharmony_ci * is released. 8158c2ecf20Sopenharmony_ci */ 8168c2ecf20Sopenharmony_ci drm_atomic_set_fb_for_plane(plane->state, fb); 8178c2ecf20Sopenharmony_ci 8188c2ecf20Sopenharmony_ci vc4_queue_seqno_cb(dev, &flip_state->cb, bo->seqno, 8198c2ecf20Sopenharmony_ci vc4_async_page_flip_complete); 8208c2ecf20Sopenharmony_ci 8218c2ecf20Sopenharmony_ci /* Driver takes ownership of state on successful async commit. */ 8228c2ecf20Sopenharmony_ci return 0; 8238c2ecf20Sopenharmony_ci} 8248c2ecf20Sopenharmony_ci 8258c2ecf20Sopenharmony_ciint vc4_page_flip(struct drm_crtc *crtc, 8268c2ecf20Sopenharmony_ci struct drm_framebuffer *fb, 8278c2ecf20Sopenharmony_ci struct drm_pending_vblank_event *event, 8288c2ecf20Sopenharmony_ci uint32_t flags, 8298c2ecf20Sopenharmony_ci struct drm_modeset_acquire_ctx *ctx) 8308c2ecf20Sopenharmony_ci{ 8318c2ecf20Sopenharmony_ci if (flags & DRM_MODE_PAGE_FLIP_ASYNC) 8328c2ecf20Sopenharmony_ci return vc4_async_page_flip(crtc, fb, event, flags); 8338c2ecf20Sopenharmony_ci else 8348c2ecf20Sopenharmony_ci return drm_atomic_helper_page_flip(crtc, fb, event, flags, ctx); 8358c2ecf20Sopenharmony_ci} 8368c2ecf20Sopenharmony_ci 8378c2ecf20Sopenharmony_cistruct drm_crtc_state *vc4_crtc_duplicate_state(struct drm_crtc *crtc) 8388c2ecf20Sopenharmony_ci{ 8398c2ecf20Sopenharmony_ci struct vc4_crtc_state *vc4_state, *old_vc4_state; 8408c2ecf20Sopenharmony_ci 8418c2ecf20Sopenharmony_ci vc4_state = kzalloc(sizeof(*vc4_state), GFP_KERNEL); 8428c2ecf20Sopenharmony_ci if (!vc4_state) 8438c2ecf20Sopenharmony_ci return NULL; 8448c2ecf20Sopenharmony_ci 8458c2ecf20Sopenharmony_ci old_vc4_state = to_vc4_crtc_state(crtc->state); 8468c2ecf20Sopenharmony_ci vc4_state->feed_txp = old_vc4_state->feed_txp; 8478c2ecf20Sopenharmony_ci vc4_state->margins = old_vc4_state->margins; 8488c2ecf20Sopenharmony_ci vc4_state->assigned_channel = old_vc4_state->assigned_channel; 8498c2ecf20Sopenharmony_ci 8508c2ecf20Sopenharmony_ci __drm_atomic_helper_crtc_duplicate_state(crtc, &vc4_state->base); 8518c2ecf20Sopenharmony_ci return &vc4_state->base; 8528c2ecf20Sopenharmony_ci} 8538c2ecf20Sopenharmony_ci 8548c2ecf20Sopenharmony_civoid vc4_crtc_destroy_state(struct drm_crtc *crtc, 8558c2ecf20Sopenharmony_ci struct drm_crtc_state *state) 8568c2ecf20Sopenharmony_ci{ 8578c2ecf20Sopenharmony_ci struct vc4_dev *vc4 = to_vc4_dev(crtc->dev); 8588c2ecf20Sopenharmony_ci struct vc4_crtc_state *vc4_state = to_vc4_crtc_state(state); 8598c2ecf20Sopenharmony_ci 8608c2ecf20Sopenharmony_ci if (drm_mm_node_allocated(&vc4_state->mm)) { 8618c2ecf20Sopenharmony_ci unsigned long flags; 8628c2ecf20Sopenharmony_ci 8638c2ecf20Sopenharmony_ci spin_lock_irqsave(&vc4->hvs->mm_lock, flags); 8648c2ecf20Sopenharmony_ci drm_mm_remove_node(&vc4_state->mm); 8658c2ecf20Sopenharmony_ci spin_unlock_irqrestore(&vc4->hvs->mm_lock, flags); 8668c2ecf20Sopenharmony_ci 8678c2ecf20Sopenharmony_ci } 8688c2ecf20Sopenharmony_ci 8698c2ecf20Sopenharmony_ci drm_atomic_helper_crtc_destroy_state(crtc, state); 8708c2ecf20Sopenharmony_ci} 8718c2ecf20Sopenharmony_ci 8728c2ecf20Sopenharmony_civoid vc4_crtc_reset(struct drm_crtc *crtc) 8738c2ecf20Sopenharmony_ci{ 8748c2ecf20Sopenharmony_ci struct vc4_crtc_state *vc4_crtc_state; 8758c2ecf20Sopenharmony_ci 8768c2ecf20Sopenharmony_ci if (crtc->state) 8778c2ecf20Sopenharmony_ci vc4_crtc_destroy_state(crtc, crtc->state); 8788c2ecf20Sopenharmony_ci 8798c2ecf20Sopenharmony_ci vc4_crtc_state = kzalloc(sizeof(*vc4_crtc_state), GFP_KERNEL); 8808c2ecf20Sopenharmony_ci if (!vc4_crtc_state) { 8818c2ecf20Sopenharmony_ci crtc->state = NULL; 8828c2ecf20Sopenharmony_ci return; 8838c2ecf20Sopenharmony_ci } 8848c2ecf20Sopenharmony_ci 8858c2ecf20Sopenharmony_ci vc4_crtc_state->assigned_channel = VC4_HVS_CHANNEL_DISABLED; 8868c2ecf20Sopenharmony_ci __drm_atomic_helper_crtc_reset(crtc, &vc4_crtc_state->base); 8878c2ecf20Sopenharmony_ci} 8888c2ecf20Sopenharmony_ci 8898c2ecf20Sopenharmony_cistatic const struct drm_crtc_funcs vc4_crtc_funcs = { 8908c2ecf20Sopenharmony_ci .set_config = drm_atomic_helper_set_config, 8918c2ecf20Sopenharmony_ci .destroy = vc4_crtc_destroy, 8928c2ecf20Sopenharmony_ci .page_flip = vc4_page_flip, 8938c2ecf20Sopenharmony_ci .set_property = NULL, 8948c2ecf20Sopenharmony_ci .cursor_set = NULL, /* handled by drm_mode_cursor_universal */ 8958c2ecf20Sopenharmony_ci .cursor_move = NULL, /* handled by drm_mode_cursor_universal */ 8968c2ecf20Sopenharmony_ci .reset = vc4_crtc_reset, 8978c2ecf20Sopenharmony_ci .atomic_duplicate_state = vc4_crtc_duplicate_state, 8988c2ecf20Sopenharmony_ci .atomic_destroy_state = vc4_crtc_destroy_state, 8998c2ecf20Sopenharmony_ci .gamma_set = drm_atomic_helper_legacy_gamma_set, 9008c2ecf20Sopenharmony_ci .enable_vblank = vc4_enable_vblank, 9018c2ecf20Sopenharmony_ci .disable_vblank = vc4_disable_vblank, 9028c2ecf20Sopenharmony_ci .get_vblank_timestamp = drm_crtc_vblank_helper_get_vblank_timestamp, 9038c2ecf20Sopenharmony_ci}; 9048c2ecf20Sopenharmony_ci 9058c2ecf20Sopenharmony_cistatic const struct drm_crtc_helper_funcs vc4_crtc_helper_funcs = { 9068c2ecf20Sopenharmony_ci .mode_valid = vc4_crtc_mode_valid, 9078c2ecf20Sopenharmony_ci .atomic_check = vc4_crtc_atomic_check, 9088c2ecf20Sopenharmony_ci .atomic_flush = vc4_hvs_atomic_flush, 9098c2ecf20Sopenharmony_ci .atomic_enable = vc4_crtc_atomic_enable, 9108c2ecf20Sopenharmony_ci .atomic_disable = vc4_crtc_atomic_disable, 9118c2ecf20Sopenharmony_ci .get_scanout_position = vc4_crtc_get_scanout_position, 9128c2ecf20Sopenharmony_ci}; 9138c2ecf20Sopenharmony_ci 9148c2ecf20Sopenharmony_cistatic const struct vc4_pv_data bcm2835_pv0_data = { 9158c2ecf20Sopenharmony_ci .base = { 9168c2ecf20Sopenharmony_ci .hvs_available_channels = BIT(0), 9178c2ecf20Sopenharmony_ci .hvs_output = 0, 9188c2ecf20Sopenharmony_ci }, 9198c2ecf20Sopenharmony_ci .debugfs_name = "crtc0_regs", 9208c2ecf20Sopenharmony_ci .fifo_depth = 64, 9218c2ecf20Sopenharmony_ci .pixels_per_clock = 1, 9228c2ecf20Sopenharmony_ci .encoder_types = { 9238c2ecf20Sopenharmony_ci [PV_CONTROL_CLK_SELECT_DSI] = VC4_ENCODER_TYPE_DSI0, 9248c2ecf20Sopenharmony_ci [PV_CONTROL_CLK_SELECT_DPI_SMI_HDMI] = VC4_ENCODER_TYPE_DPI, 9258c2ecf20Sopenharmony_ci }, 9268c2ecf20Sopenharmony_ci}; 9278c2ecf20Sopenharmony_ci 9288c2ecf20Sopenharmony_cistatic const struct vc4_pv_data bcm2835_pv1_data = { 9298c2ecf20Sopenharmony_ci .base = { 9308c2ecf20Sopenharmony_ci .hvs_available_channels = BIT(2), 9318c2ecf20Sopenharmony_ci .hvs_output = 2, 9328c2ecf20Sopenharmony_ci }, 9338c2ecf20Sopenharmony_ci .debugfs_name = "crtc1_regs", 9348c2ecf20Sopenharmony_ci .fifo_depth = 64, 9358c2ecf20Sopenharmony_ci .pixels_per_clock = 1, 9368c2ecf20Sopenharmony_ci .encoder_types = { 9378c2ecf20Sopenharmony_ci [PV_CONTROL_CLK_SELECT_DSI] = VC4_ENCODER_TYPE_DSI1, 9388c2ecf20Sopenharmony_ci [PV_CONTROL_CLK_SELECT_DPI_SMI_HDMI] = VC4_ENCODER_TYPE_SMI, 9398c2ecf20Sopenharmony_ci }, 9408c2ecf20Sopenharmony_ci}; 9418c2ecf20Sopenharmony_ci 9428c2ecf20Sopenharmony_cistatic const struct vc4_pv_data bcm2835_pv2_data = { 9438c2ecf20Sopenharmony_ci .base = { 9448c2ecf20Sopenharmony_ci .hvs_available_channels = BIT(1), 9458c2ecf20Sopenharmony_ci .hvs_output = 1, 9468c2ecf20Sopenharmony_ci }, 9478c2ecf20Sopenharmony_ci .debugfs_name = "crtc2_regs", 9488c2ecf20Sopenharmony_ci .fifo_depth = 64, 9498c2ecf20Sopenharmony_ci .pixels_per_clock = 1, 9508c2ecf20Sopenharmony_ci .encoder_types = { 9518c2ecf20Sopenharmony_ci [PV_CONTROL_CLK_SELECT_DPI_SMI_HDMI] = VC4_ENCODER_TYPE_HDMI0, 9528c2ecf20Sopenharmony_ci [PV_CONTROL_CLK_SELECT_VEC] = VC4_ENCODER_TYPE_VEC, 9538c2ecf20Sopenharmony_ci }, 9548c2ecf20Sopenharmony_ci}; 9558c2ecf20Sopenharmony_ci 9568c2ecf20Sopenharmony_cistatic const struct vc4_pv_data bcm2711_pv0_data = { 9578c2ecf20Sopenharmony_ci .base = { 9588c2ecf20Sopenharmony_ci .hvs_available_channels = BIT(0), 9598c2ecf20Sopenharmony_ci .hvs_output = 0, 9608c2ecf20Sopenharmony_ci }, 9618c2ecf20Sopenharmony_ci .debugfs_name = "crtc0_regs", 9628c2ecf20Sopenharmony_ci .fifo_depth = 64, 9638c2ecf20Sopenharmony_ci .pixels_per_clock = 1, 9648c2ecf20Sopenharmony_ci .encoder_types = { 9658c2ecf20Sopenharmony_ci [0] = VC4_ENCODER_TYPE_DSI0, 9668c2ecf20Sopenharmony_ci [1] = VC4_ENCODER_TYPE_DPI, 9678c2ecf20Sopenharmony_ci }, 9688c2ecf20Sopenharmony_ci}; 9698c2ecf20Sopenharmony_ci 9708c2ecf20Sopenharmony_cistatic const struct vc4_pv_data bcm2711_pv1_data = { 9718c2ecf20Sopenharmony_ci .base = { 9728c2ecf20Sopenharmony_ci .hvs_available_channels = BIT(0) | BIT(1) | BIT(2), 9738c2ecf20Sopenharmony_ci .hvs_output = 3, 9748c2ecf20Sopenharmony_ci }, 9758c2ecf20Sopenharmony_ci .debugfs_name = "crtc1_regs", 9768c2ecf20Sopenharmony_ci .fifo_depth = 64, 9778c2ecf20Sopenharmony_ci .pixels_per_clock = 1, 9788c2ecf20Sopenharmony_ci .encoder_types = { 9798c2ecf20Sopenharmony_ci [0] = VC4_ENCODER_TYPE_DSI1, 9808c2ecf20Sopenharmony_ci [1] = VC4_ENCODER_TYPE_SMI, 9818c2ecf20Sopenharmony_ci }, 9828c2ecf20Sopenharmony_ci}; 9838c2ecf20Sopenharmony_ci 9848c2ecf20Sopenharmony_cistatic const struct vc4_pv_data bcm2711_pv2_data = { 9858c2ecf20Sopenharmony_ci .base = { 9868c2ecf20Sopenharmony_ci .hvs_available_channels = BIT(0) | BIT(1) | BIT(2), 9878c2ecf20Sopenharmony_ci .hvs_output = 4, 9888c2ecf20Sopenharmony_ci }, 9898c2ecf20Sopenharmony_ci .debugfs_name = "crtc2_regs", 9908c2ecf20Sopenharmony_ci .fifo_depth = 256, 9918c2ecf20Sopenharmony_ci .pixels_per_clock = 2, 9928c2ecf20Sopenharmony_ci .encoder_types = { 9938c2ecf20Sopenharmony_ci [0] = VC4_ENCODER_TYPE_HDMI0, 9948c2ecf20Sopenharmony_ci }, 9958c2ecf20Sopenharmony_ci}; 9968c2ecf20Sopenharmony_ci 9978c2ecf20Sopenharmony_cistatic const struct vc4_pv_data bcm2711_pv3_data = { 9988c2ecf20Sopenharmony_ci .base = { 9998c2ecf20Sopenharmony_ci .hvs_available_channels = BIT(1), 10008c2ecf20Sopenharmony_ci .hvs_output = 1, 10018c2ecf20Sopenharmony_ci }, 10028c2ecf20Sopenharmony_ci .debugfs_name = "crtc3_regs", 10038c2ecf20Sopenharmony_ci .fifo_depth = 64, 10048c2ecf20Sopenharmony_ci .pixels_per_clock = 1, 10058c2ecf20Sopenharmony_ci .encoder_types = { 10068c2ecf20Sopenharmony_ci [PV_CONTROL_CLK_SELECT_VEC] = VC4_ENCODER_TYPE_VEC, 10078c2ecf20Sopenharmony_ci }, 10088c2ecf20Sopenharmony_ci}; 10098c2ecf20Sopenharmony_ci 10108c2ecf20Sopenharmony_cistatic const struct vc4_pv_data bcm2711_pv4_data = { 10118c2ecf20Sopenharmony_ci .base = { 10128c2ecf20Sopenharmony_ci .hvs_available_channels = BIT(0) | BIT(1) | BIT(2), 10138c2ecf20Sopenharmony_ci .hvs_output = 5, 10148c2ecf20Sopenharmony_ci }, 10158c2ecf20Sopenharmony_ci .debugfs_name = "crtc4_regs", 10168c2ecf20Sopenharmony_ci .fifo_depth = 64, 10178c2ecf20Sopenharmony_ci .pixels_per_clock = 2, 10188c2ecf20Sopenharmony_ci .encoder_types = { 10198c2ecf20Sopenharmony_ci [0] = VC4_ENCODER_TYPE_HDMI1, 10208c2ecf20Sopenharmony_ci }, 10218c2ecf20Sopenharmony_ci}; 10228c2ecf20Sopenharmony_ci 10238c2ecf20Sopenharmony_cistatic const struct of_device_id vc4_crtc_dt_match[] = { 10248c2ecf20Sopenharmony_ci { .compatible = "brcm,bcm2835-pixelvalve0", .data = &bcm2835_pv0_data }, 10258c2ecf20Sopenharmony_ci { .compatible = "brcm,bcm2835-pixelvalve1", .data = &bcm2835_pv1_data }, 10268c2ecf20Sopenharmony_ci { .compatible = "brcm,bcm2835-pixelvalve2", .data = &bcm2835_pv2_data }, 10278c2ecf20Sopenharmony_ci { .compatible = "brcm,bcm2711-pixelvalve0", .data = &bcm2711_pv0_data }, 10288c2ecf20Sopenharmony_ci { .compatible = "brcm,bcm2711-pixelvalve1", .data = &bcm2711_pv1_data }, 10298c2ecf20Sopenharmony_ci { .compatible = "brcm,bcm2711-pixelvalve2", .data = &bcm2711_pv2_data }, 10308c2ecf20Sopenharmony_ci { .compatible = "brcm,bcm2711-pixelvalve3", .data = &bcm2711_pv3_data }, 10318c2ecf20Sopenharmony_ci { .compatible = "brcm,bcm2711-pixelvalve4", .data = &bcm2711_pv4_data }, 10328c2ecf20Sopenharmony_ci {} 10338c2ecf20Sopenharmony_ci}; 10348c2ecf20Sopenharmony_ci 10358c2ecf20Sopenharmony_cistatic void vc4_set_crtc_possible_masks(struct drm_device *drm, 10368c2ecf20Sopenharmony_ci struct drm_crtc *crtc) 10378c2ecf20Sopenharmony_ci{ 10388c2ecf20Sopenharmony_ci struct vc4_crtc *vc4_crtc = to_vc4_crtc(crtc); 10398c2ecf20Sopenharmony_ci const struct vc4_pv_data *pv_data = vc4_crtc_to_vc4_pv_data(vc4_crtc); 10408c2ecf20Sopenharmony_ci const enum vc4_encoder_type *encoder_types = pv_data->encoder_types; 10418c2ecf20Sopenharmony_ci struct drm_encoder *encoder; 10428c2ecf20Sopenharmony_ci 10438c2ecf20Sopenharmony_ci drm_for_each_encoder(encoder, drm) { 10448c2ecf20Sopenharmony_ci struct vc4_encoder *vc4_encoder; 10458c2ecf20Sopenharmony_ci int i; 10468c2ecf20Sopenharmony_ci 10478c2ecf20Sopenharmony_ci if (encoder->encoder_type == DRM_MODE_ENCODER_VIRTUAL) 10488c2ecf20Sopenharmony_ci continue; 10498c2ecf20Sopenharmony_ci 10508c2ecf20Sopenharmony_ci vc4_encoder = to_vc4_encoder(encoder); 10518c2ecf20Sopenharmony_ci for (i = 0; i < ARRAY_SIZE(pv_data->encoder_types); i++) { 10528c2ecf20Sopenharmony_ci if (vc4_encoder->type == encoder_types[i]) { 10538c2ecf20Sopenharmony_ci vc4_encoder->clock_select = i; 10548c2ecf20Sopenharmony_ci encoder->possible_crtcs |= drm_crtc_mask(crtc); 10558c2ecf20Sopenharmony_ci break; 10568c2ecf20Sopenharmony_ci } 10578c2ecf20Sopenharmony_ci } 10588c2ecf20Sopenharmony_ci } 10598c2ecf20Sopenharmony_ci} 10608c2ecf20Sopenharmony_ci 10618c2ecf20Sopenharmony_ciint vc4_crtc_init(struct drm_device *drm, struct vc4_crtc *vc4_crtc, 10628c2ecf20Sopenharmony_ci const struct drm_crtc_funcs *crtc_funcs, 10638c2ecf20Sopenharmony_ci const struct drm_crtc_helper_funcs *crtc_helper_funcs) 10648c2ecf20Sopenharmony_ci{ 10658c2ecf20Sopenharmony_ci struct vc4_dev *vc4 = to_vc4_dev(drm); 10668c2ecf20Sopenharmony_ci struct drm_crtc *crtc = &vc4_crtc->base; 10678c2ecf20Sopenharmony_ci struct drm_plane *primary_plane; 10688c2ecf20Sopenharmony_ci unsigned int i; 10698c2ecf20Sopenharmony_ci 10708c2ecf20Sopenharmony_ci /* For now, we create just the primary and the legacy cursor 10718c2ecf20Sopenharmony_ci * planes. We should be able to stack more planes on easily, 10728c2ecf20Sopenharmony_ci * but to do that we would need to compute the bandwidth 10738c2ecf20Sopenharmony_ci * requirement of the plane configuration, and reject ones 10748c2ecf20Sopenharmony_ci * that will take too much. 10758c2ecf20Sopenharmony_ci */ 10768c2ecf20Sopenharmony_ci primary_plane = vc4_plane_init(drm, DRM_PLANE_TYPE_PRIMARY); 10778c2ecf20Sopenharmony_ci if (IS_ERR(primary_plane)) { 10788c2ecf20Sopenharmony_ci dev_err(drm->dev, "failed to construct primary plane\n"); 10798c2ecf20Sopenharmony_ci return PTR_ERR(primary_plane); 10808c2ecf20Sopenharmony_ci } 10818c2ecf20Sopenharmony_ci 10828c2ecf20Sopenharmony_ci drm_crtc_init_with_planes(drm, crtc, primary_plane, NULL, 10838c2ecf20Sopenharmony_ci crtc_funcs, NULL); 10848c2ecf20Sopenharmony_ci drm_crtc_helper_add(crtc, crtc_helper_funcs); 10858c2ecf20Sopenharmony_ci 10868c2ecf20Sopenharmony_ci if (!vc4->hvs->hvs5) { 10878c2ecf20Sopenharmony_ci drm_mode_crtc_set_gamma_size(crtc, ARRAY_SIZE(vc4_crtc->lut_r)); 10888c2ecf20Sopenharmony_ci 10898c2ecf20Sopenharmony_ci drm_crtc_enable_color_mgmt(crtc, 0, false, crtc->gamma_size); 10908c2ecf20Sopenharmony_ci 10918c2ecf20Sopenharmony_ci /* We support CTM, but only for one CRTC at a time. It's therefore 10928c2ecf20Sopenharmony_ci * implemented as private driver state in vc4_kms, not here. 10938c2ecf20Sopenharmony_ci */ 10948c2ecf20Sopenharmony_ci drm_crtc_enable_color_mgmt(crtc, 0, true, crtc->gamma_size); 10958c2ecf20Sopenharmony_ci } 10968c2ecf20Sopenharmony_ci 10978c2ecf20Sopenharmony_ci for (i = 0; i < crtc->gamma_size; i++) { 10988c2ecf20Sopenharmony_ci vc4_crtc->lut_r[i] = i; 10998c2ecf20Sopenharmony_ci vc4_crtc->lut_g[i] = i; 11008c2ecf20Sopenharmony_ci vc4_crtc->lut_b[i] = i; 11018c2ecf20Sopenharmony_ci } 11028c2ecf20Sopenharmony_ci 11038c2ecf20Sopenharmony_ci return 0; 11048c2ecf20Sopenharmony_ci} 11058c2ecf20Sopenharmony_ci 11068c2ecf20Sopenharmony_cistatic int vc4_crtc_bind(struct device *dev, struct device *master, void *data) 11078c2ecf20Sopenharmony_ci{ 11088c2ecf20Sopenharmony_ci struct platform_device *pdev = to_platform_device(dev); 11098c2ecf20Sopenharmony_ci struct drm_device *drm = dev_get_drvdata(master); 11108c2ecf20Sopenharmony_ci const struct vc4_pv_data *pv_data; 11118c2ecf20Sopenharmony_ci struct vc4_crtc *vc4_crtc; 11128c2ecf20Sopenharmony_ci struct drm_crtc *crtc; 11138c2ecf20Sopenharmony_ci struct drm_plane *destroy_plane, *temp; 11148c2ecf20Sopenharmony_ci int ret; 11158c2ecf20Sopenharmony_ci 11168c2ecf20Sopenharmony_ci vc4_crtc = devm_kzalloc(dev, sizeof(*vc4_crtc), GFP_KERNEL); 11178c2ecf20Sopenharmony_ci if (!vc4_crtc) 11188c2ecf20Sopenharmony_ci return -ENOMEM; 11198c2ecf20Sopenharmony_ci crtc = &vc4_crtc->base; 11208c2ecf20Sopenharmony_ci 11218c2ecf20Sopenharmony_ci pv_data = of_device_get_match_data(dev); 11228c2ecf20Sopenharmony_ci if (!pv_data) 11238c2ecf20Sopenharmony_ci return -ENODEV; 11248c2ecf20Sopenharmony_ci vc4_crtc->data = &pv_data->base; 11258c2ecf20Sopenharmony_ci vc4_crtc->pdev = pdev; 11268c2ecf20Sopenharmony_ci 11278c2ecf20Sopenharmony_ci vc4_crtc->regs = vc4_ioremap_regs(pdev, 0); 11288c2ecf20Sopenharmony_ci if (IS_ERR(vc4_crtc->regs)) 11298c2ecf20Sopenharmony_ci return PTR_ERR(vc4_crtc->regs); 11308c2ecf20Sopenharmony_ci 11318c2ecf20Sopenharmony_ci vc4_crtc->regset.base = vc4_crtc->regs; 11328c2ecf20Sopenharmony_ci vc4_crtc->regset.regs = crtc_regs; 11338c2ecf20Sopenharmony_ci vc4_crtc->regset.nregs = ARRAY_SIZE(crtc_regs); 11348c2ecf20Sopenharmony_ci 11358c2ecf20Sopenharmony_ci ret = vc4_crtc_init(drm, vc4_crtc, 11368c2ecf20Sopenharmony_ci &vc4_crtc_funcs, &vc4_crtc_helper_funcs); 11378c2ecf20Sopenharmony_ci if (ret) 11388c2ecf20Sopenharmony_ci return ret; 11398c2ecf20Sopenharmony_ci vc4_set_crtc_possible_masks(drm, crtc); 11408c2ecf20Sopenharmony_ci 11418c2ecf20Sopenharmony_ci CRTC_WRITE(PV_INTEN, 0); 11428c2ecf20Sopenharmony_ci CRTC_WRITE(PV_INTSTAT, PV_INT_VFP_START); 11438c2ecf20Sopenharmony_ci ret = devm_request_irq(dev, platform_get_irq(pdev, 0), 11448c2ecf20Sopenharmony_ci vc4_crtc_irq_handler, 11458c2ecf20Sopenharmony_ci IRQF_SHARED, 11468c2ecf20Sopenharmony_ci "vc4 crtc", vc4_crtc); 11478c2ecf20Sopenharmony_ci if (ret) 11488c2ecf20Sopenharmony_ci goto err_destroy_planes; 11498c2ecf20Sopenharmony_ci 11508c2ecf20Sopenharmony_ci platform_set_drvdata(pdev, vc4_crtc); 11518c2ecf20Sopenharmony_ci 11528c2ecf20Sopenharmony_ci vc4_debugfs_add_regset32(drm, pv_data->debugfs_name, 11538c2ecf20Sopenharmony_ci &vc4_crtc->regset); 11548c2ecf20Sopenharmony_ci 11558c2ecf20Sopenharmony_ci return 0; 11568c2ecf20Sopenharmony_ci 11578c2ecf20Sopenharmony_cierr_destroy_planes: 11588c2ecf20Sopenharmony_ci list_for_each_entry_safe(destroy_plane, temp, 11598c2ecf20Sopenharmony_ci &drm->mode_config.plane_list, head) { 11608c2ecf20Sopenharmony_ci if (destroy_plane->possible_crtcs == drm_crtc_mask(crtc)) 11618c2ecf20Sopenharmony_ci destroy_plane->funcs->destroy(destroy_plane); 11628c2ecf20Sopenharmony_ci } 11638c2ecf20Sopenharmony_ci 11648c2ecf20Sopenharmony_ci return ret; 11658c2ecf20Sopenharmony_ci} 11668c2ecf20Sopenharmony_ci 11678c2ecf20Sopenharmony_cistatic void vc4_crtc_unbind(struct device *dev, struct device *master, 11688c2ecf20Sopenharmony_ci void *data) 11698c2ecf20Sopenharmony_ci{ 11708c2ecf20Sopenharmony_ci struct platform_device *pdev = to_platform_device(dev); 11718c2ecf20Sopenharmony_ci struct vc4_crtc *vc4_crtc = dev_get_drvdata(dev); 11728c2ecf20Sopenharmony_ci 11738c2ecf20Sopenharmony_ci vc4_crtc_destroy(&vc4_crtc->base); 11748c2ecf20Sopenharmony_ci 11758c2ecf20Sopenharmony_ci CRTC_WRITE(PV_INTEN, 0); 11768c2ecf20Sopenharmony_ci 11778c2ecf20Sopenharmony_ci platform_set_drvdata(pdev, NULL); 11788c2ecf20Sopenharmony_ci} 11798c2ecf20Sopenharmony_ci 11808c2ecf20Sopenharmony_cistatic const struct component_ops vc4_crtc_ops = { 11818c2ecf20Sopenharmony_ci .bind = vc4_crtc_bind, 11828c2ecf20Sopenharmony_ci .unbind = vc4_crtc_unbind, 11838c2ecf20Sopenharmony_ci}; 11848c2ecf20Sopenharmony_ci 11858c2ecf20Sopenharmony_cistatic int vc4_crtc_dev_probe(struct platform_device *pdev) 11868c2ecf20Sopenharmony_ci{ 11878c2ecf20Sopenharmony_ci return component_add(&pdev->dev, &vc4_crtc_ops); 11888c2ecf20Sopenharmony_ci} 11898c2ecf20Sopenharmony_ci 11908c2ecf20Sopenharmony_cistatic int vc4_crtc_dev_remove(struct platform_device *pdev) 11918c2ecf20Sopenharmony_ci{ 11928c2ecf20Sopenharmony_ci component_del(&pdev->dev, &vc4_crtc_ops); 11938c2ecf20Sopenharmony_ci return 0; 11948c2ecf20Sopenharmony_ci} 11958c2ecf20Sopenharmony_ci 11968c2ecf20Sopenharmony_cistruct platform_driver vc4_crtc_driver = { 11978c2ecf20Sopenharmony_ci .probe = vc4_crtc_dev_probe, 11988c2ecf20Sopenharmony_ci .remove = vc4_crtc_dev_remove, 11998c2ecf20Sopenharmony_ci .driver = { 12008c2ecf20Sopenharmony_ci .name = "vc4_crtc", 12018c2ecf20Sopenharmony_ci .of_match_table = vc4_crtc_dt_match, 12028c2ecf20Sopenharmony_ci }, 12038c2ecf20Sopenharmony_ci}; 1204