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 KMS 88c2ecf20Sopenharmony_ci * 98c2ecf20Sopenharmony_ci * This is the general code for implementing KMS mode setting that 108c2ecf20Sopenharmony_ci * doesn't clearly associate with any of the other objects (plane, 118c2ecf20Sopenharmony_ci * crtc, HDMI encoder). 128c2ecf20Sopenharmony_ci */ 138c2ecf20Sopenharmony_ci 148c2ecf20Sopenharmony_ci#include <linux/clk.h> 158c2ecf20Sopenharmony_ci 168c2ecf20Sopenharmony_ci#include <drm/drm_atomic.h> 178c2ecf20Sopenharmony_ci#include <drm/drm_atomic_helper.h> 188c2ecf20Sopenharmony_ci#include <drm/drm_crtc.h> 198c2ecf20Sopenharmony_ci#include <drm/drm_gem_framebuffer_helper.h> 208c2ecf20Sopenharmony_ci#include <drm/drm_plane_helper.h> 218c2ecf20Sopenharmony_ci#include <drm/drm_probe_helper.h> 228c2ecf20Sopenharmony_ci#include <drm/drm_vblank.h> 238c2ecf20Sopenharmony_ci 248c2ecf20Sopenharmony_ci#include "vc4_drv.h" 258c2ecf20Sopenharmony_ci#include "vc4_regs.h" 268c2ecf20Sopenharmony_ci 278c2ecf20Sopenharmony_ci#define HVS_NUM_CHANNELS 3 288c2ecf20Sopenharmony_ci 298c2ecf20Sopenharmony_cistruct vc4_ctm_state { 308c2ecf20Sopenharmony_ci struct drm_private_state base; 318c2ecf20Sopenharmony_ci struct drm_color_ctm *ctm; 328c2ecf20Sopenharmony_ci int fifo; 338c2ecf20Sopenharmony_ci}; 348c2ecf20Sopenharmony_ci 358c2ecf20Sopenharmony_cistatic struct vc4_ctm_state *to_vc4_ctm_state(struct drm_private_state *priv) 368c2ecf20Sopenharmony_ci{ 378c2ecf20Sopenharmony_ci return container_of(priv, struct vc4_ctm_state, base); 388c2ecf20Sopenharmony_ci} 398c2ecf20Sopenharmony_ci 408c2ecf20Sopenharmony_cistruct vc4_hvs_state { 418c2ecf20Sopenharmony_ci struct drm_private_state base; 428c2ecf20Sopenharmony_ci unsigned int unassigned_channels; 438c2ecf20Sopenharmony_ci}; 448c2ecf20Sopenharmony_ci 458c2ecf20Sopenharmony_cistatic struct vc4_hvs_state * 468c2ecf20Sopenharmony_cito_vc4_hvs_state(struct drm_private_state *priv) 478c2ecf20Sopenharmony_ci{ 488c2ecf20Sopenharmony_ci return container_of(priv, struct vc4_hvs_state, base); 498c2ecf20Sopenharmony_ci} 508c2ecf20Sopenharmony_ci 518c2ecf20Sopenharmony_cistruct vc4_load_tracker_state { 528c2ecf20Sopenharmony_ci struct drm_private_state base; 538c2ecf20Sopenharmony_ci u64 hvs_load; 548c2ecf20Sopenharmony_ci u64 membus_load; 558c2ecf20Sopenharmony_ci}; 568c2ecf20Sopenharmony_ci 578c2ecf20Sopenharmony_cistatic struct vc4_load_tracker_state * 588c2ecf20Sopenharmony_cito_vc4_load_tracker_state(struct drm_private_state *priv) 598c2ecf20Sopenharmony_ci{ 608c2ecf20Sopenharmony_ci return container_of(priv, struct vc4_load_tracker_state, base); 618c2ecf20Sopenharmony_ci} 628c2ecf20Sopenharmony_ci 638c2ecf20Sopenharmony_cistatic struct vc4_ctm_state *vc4_get_ctm_state(struct drm_atomic_state *state, 648c2ecf20Sopenharmony_ci struct drm_private_obj *manager) 658c2ecf20Sopenharmony_ci{ 668c2ecf20Sopenharmony_ci struct drm_device *dev = state->dev; 678c2ecf20Sopenharmony_ci struct vc4_dev *vc4 = to_vc4_dev(dev); 688c2ecf20Sopenharmony_ci struct drm_private_state *priv_state; 698c2ecf20Sopenharmony_ci int ret; 708c2ecf20Sopenharmony_ci 718c2ecf20Sopenharmony_ci ret = drm_modeset_lock(&vc4->ctm_state_lock, state->acquire_ctx); 728c2ecf20Sopenharmony_ci if (ret) 738c2ecf20Sopenharmony_ci return ERR_PTR(ret); 748c2ecf20Sopenharmony_ci 758c2ecf20Sopenharmony_ci priv_state = drm_atomic_get_private_obj_state(state, manager); 768c2ecf20Sopenharmony_ci if (IS_ERR(priv_state)) 778c2ecf20Sopenharmony_ci return ERR_CAST(priv_state); 788c2ecf20Sopenharmony_ci 798c2ecf20Sopenharmony_ci return to_vc4_ctm_state(priv_state); 808c2ecf20Sopenharmony_ci} 818c2ecf20Sopenharmony_ci 828c2ecf20Sopenharmony_cistatic struct drm_private_state * 838c2ecf20Sopenharmony_civc4_ctm_duplicate_state(struct drm_private_obj *obj) 848c2ecf20Sopenharmony_ci{ 858c2ecf20Sopenharmony_ci struct vc4_ctm_state *state; 868c2ecf20Sopenharmony_ci 878c2ecf20Sopenharmony_ci state = kmemdup(obj->state, sizeof(*state), GFP_KERNEL); 888c2ecf20Sopenharmony_ci if (!state) 898c2ecf20Sopenharmony_ci return NULL; 908c2ecf20Sopenharmony_ci 918c2ecf20Sopenharmony_ci __drm_atomic_helper_private_obj_duplicate_state(obj, &state->base); 928c2ecf20Sopenharmony_ci 938c2ecf20Sopenharmony_ci return &state->base; 948c2ecf20Sopenharmony_ci} 958c2ecf20Sopenharmony_ci 968c2ecf20Sopenharmony_cistatic void vc4_ctm_destroy_state(struct drm_private_obj *obj, 978c2ecf20Sopenharmony_ci struct drm_private_state *state) 988c2ecf20Sopenharmony_ci{ 998c2ecf20Sopenharmony_ci struct vc4_ctm_state *ctm_state = to_vc4_ctm_state(state); 1008c2ecf20Sopenharmony_ci 1018c2ecf20Sopenharmony_ci kfree(ctm_state); 1028c2ecf20Sopenharmony_ci} 1038c2ecf20Sopenharmony_ci 1048c2ecf20Sopenharmony_cistatic const struct drm_private_state_funcs vc4_ctm_state_funcs = { 1058c2ecf20Sopenharmony_ci .atomic_duplicate_state = vc4_ctm_duplicate_state, 1068c2ecf20Sopenharmony_ci .atomic_destroy_state = vc4_ctm_destroy_state, 1078c2ecf20Sopenharmony_ci}; 1088c2ecf20Sopenharmony_ci 1098c2ecf20Sopenharmony_cistatic void vc4_ctm_obj_fini(struct drm_device *dev, void *unused) 1108c2ecf20Sopenharmony_ci{ 1118c2ecf20Sopenharmony_ci struct vc4_dev *vc4 = to_vc4_dev(dev); 1128c2ecf20Sopenharmony_ci 1138c2ecf20Sopenharmony_ci drm_atomic_private_obj_fini(&vc4->ctm_manager); 1148c2ecf20Sopenharmony_ci} 1158c2ecf20Sopenharmony_ci 1168c2ecf20Sopenharmony_cistatic int vc4_ctm_obj_init(struct vc4_dev *vc4) 1178c2ecf20Sopenharmony_ci{ 1188c2ecf20Sopenharmony_ci struct vc4_ctm_state *ctm_state; 1198c2ecf20Sopenharmony_ci 1208c2ecf20Sopenharmony_ci drm_modeset_lock_init(&vc4->ctm_state_lock); 1218c2ecf20Sopenharmony_ci 1228c2ecf20Sopenharmony_ci ctm_state = kzalloc(sizeof(*ctm_state), GFP_KERNEL); 1238c2ecf20Sopenharmony_ci if (!ctm_state) 1248c2ecf20Sopenharmony_ci return -ENOMEM; 1258c2ecf20Sopenharmony_ci 1268c2ecf20Sopenharmony_ci drm_atomic_private_obj_init(&vc4->base, &vc4->ctm_manager, &ctm_state->base, 1278c2ecf20Sopenharmony_ci &vc4_ctm_state_funcs); 1288c2ecf20Sopenharmony_ci 1298c2ecf20Sopenharmony_ci return drmm_add_action_or_reset(&vc4->base, vc4_ctm_obj_fini, NULL); 1308c2ecf20Sopenharmony_ci} 1318c2ecf20Sopenharmony_ci 1328c2ecf20Sopenharmony_ci/* Converts a DRM S31.32 value to the HW S0.9 format. */ 1338c2ecf20Sopenharmony_cistatic u16 vc4_ctm_s31_32_to_s0_9(u64 in) 1348c2ecf20Sopenharmony_ci{ 1358c2ecf20Sopenharmony_ci u16 r; 1368c2ecf20Sopenharmony_ci 1378c2ecf20Sopenharmony_ci /* Sign bit. */ 1388c2ecf20Sopenharmony_ci r = in & BIT_ULL(63) ? BIT(9) : 0; 1398c2ecf20Sopenharmony_ci 1408c2ecf20Sopenharmony_ci if ((in & GENMASK_ULL(62, 32)) > 0) { 1418c2ecf20Sopenharmony_ci /* We have zero integer bits so we can only saturate here. */ 1428c2ecf20Sopenharmony_ci r |= GENMASK(8, 0); 1438c2ecf20Sopenharmony_ci } else { 1448c2ecf20Sopenharmony_ci /* Otherwise take the 9 most important fractional bits. */ 1458c2ecf20Sopenharmony_ci r |= (in >> 23) & GENMASK(8, 0); 1468c2ecf20Sopenharmony_ci } 1478c2ecf20Sopenharmony_ci 1488c2ecf20Sopenharmony_ci return r; 1498c2ecf20Sopenharmony_ci} 1508c2ecf20Sopenharmony_ci 1518c2ecf20Sopenharmony_cistatic void 1528c2ecf20Sopenharmony_civc4_ctm_commit(struct vc4_dev *vc4, struct drm_atomic_state *state) 1538c2ecf20Sopenharmony_ci{ 1548c2ecf20Sopenharmony_ci struct vc4_ctm_state *ctm_state = to_vc4_ctm_state(vc4->ctm_manager.state); 1558c2ecf20Sopenharmony_ci struct drm_color_ctm *ctm = ctm_state->ctm; 1568c2ecf20Sopenharmony_ci 1578c2ecf20Sopenharmony_ci if (ctm_state->fifo) { 1588c2ecf20Sopenharmony_ci HVS_WRITE(SCALER_OLEDCOEF2, 1598c2ecf20Sopenharmony_ci VC4_SET_FIELD(vc4_ctm_s31_32_to_s0_9(ctm->matrix[0]), 1608c2ecf20Sopenharmony_ci SCALER_OLEDCOEF2_R_TO_R) | 1618c2ecf20Sopenharmony_ci VC4_SET_FIELD(vc4_ctm_s31_32_to_s0_9(ctm->matrix[3]), 1628c2ecf20Sopenharmony_ci SCALER_OLEDCOEF2_R_TO_G) | 1638c2ecf20Sopenharmony_ci VC4_SET_FIELD(vc4_ctm_s31_32_to_s0_9(ctm->matrix[6]), 1648c2ecf20Sopenharmony_ci SCALER_OLEDCOEF2_R_TO_B)); 1658c2ecf20Sopenharmony_ci HVS_WRITE(SCALER_OLEDCOEF1, 1668c2ecf20Sopenharmony_ci VC4_SET_FIELD(vc4_ctm_s31_32_to_s0_9(ctm->matrix[1]), 1678c2ecf20Sopenharmony_ci SCALER_OLEDCOEF1_G_TO_R) | 1688c2ecf20Sopenharmony_ci VC4_SET_FIELD(vc4_ctm_s31_32_to_s0_9(ctm->matrix[4]), 1698c2ecf20Sopenharmony_ci SCALER_OLEDCOEF1_G_TO_G) | 1708c2ecf20Sopenharmony_ci VC4_SET_FIELD(vc4_ctm_s31_32_to_s0_9(ctm->matrix[7]), 1718c2ecf20Sopenharmony_ci SCALER_OLEDCOEF1_G_TO_B)); 1728c2ecf20Sopenharmony_ci HVS_WRITE(SCALER_OLEDCOEF0, 1738c2ecf20Sopenharmony_ci VC4_SET_FIELD(vc4_ctm_s31_32_to_s0_9(ctm->matrix[2]), 1748c2ecf20Sopenharmony_ci SCALER_OLEDCOEF0_B_TO_R) | 1758c2ecf20Sopenharmony_ci VC4_SET_FIELD(vc4_ctm_s31_32_to_s0_9(ctm->matrix[5]), 1768c2ecf20Sopenharmony_ci SCALER_OLEDCOEF0_B_TO_G) | 1778c2ecf20Sopenharmony_ci VC4_SET_FIELD(vc4_ctm_s31_32_to_s0_9(ctm->matrix[8]), 1788c2ecf20Sopenharmony_ci SCALER_OLEDCOEF0_B_TO_B)); 1798c2ecf20Sopenharmony_ci } 1808c2ecf20Sopenharmony_ci 1818c2ecf20Sopenharmony_ci HVS_WRITE(SCALER_OLEDOFFS, 1828c2ecf20Sopenharmony_ci VC4_SET_FIELD(ctm_state->fifo, SCALER_OLEDOFFS_DISPFIFO)); 1838c2ecf20Sopenharmony_ci} 1848c2ecf20Sopenharmony_ci 1858c2ecf20Sopenharmony_cistatic struct vc4_hvs_state * 1868c2ecf20Sopenharmony_civc4_hvs_get_global_state(struct drm_atomic_state *state) 1878c2ecf20Sopenharmony_ci{ 1888c2ecf20Sopenharmony_ci struct vc4_dev *vc4 = to_vc4_dev(state->dev); 1898c2ecf20Sopenharmony_ci struct drm_private_state *priv_state; 1908c2ecf20Sopenharmony_ci 1918c2ecf20Sopenharmony_ci priv_state = drm_atomic_get_private_obj_state(state, &vc4->hvs_channels); 1928c2ecf20Sopenharmony_ci if (IS_ERR(priv_state)) 1938c2ecf20Sopenharmony_ci return ERR_CAST(priv_state); 1948c2ecf20Sopenharmony_ci 1958c2ecf20Sopenharmony_ci return to_vc4_hvs_state(priv_state); 1968c2ecf20Sopenharmony_ci} 1978c2ecf20Sopenharmony_ci 1988c2ecf20Sopenharmony_cistatic void vc4_hvs_pv_muxing_commit(struct vc4_dev *vc4, 1998c2ecf20Sopenharmony_ci struct drm_atomic_state *state) 2008c2ecf20Sopenharmony_ci{ 2018c2ecf20Sopenharmony_ci struct drm_crtc_state *crtc_state; 2028c2ecf20Sopenharmony_ci struct drm_crtc *crtc; 2038c2ecf20Sopenharmony_ci unsigned int i; 2048c2ecf20Sopenharmony_ci 2058c2ecf20Sopenharmony_ci for_each_new_crtc_in_state(state, crtc, crtc_state, i) { 2068c2ecf20Sopenharmony_ci struct vc4_crtc_state *vc4_state = to_vc4_crtc_state(crtc_state); 2078c2ecf20Sopenharmony_ci u32 dispctrl; 2088c2ecf20Sopenharmony_ci u32 dsp3_mux; 2098c2ecf20Sopenharmony_ci 2108c2ecf20Sopenharmony_ci if (!crtc_state->active) 2118c2ecf20Sopenharmony_ci continue; 2128c2ecf20Sopenharmony_ci 2138c2ecf20Sopenharmony_ci if (vc4_state->assigned_channel != 2) 2148c2ecf20Sopenharmony_ci continue; 2158c2ecf20Sopenharmony_ci 2168c2ecf20Sopenharmony_ci /* 2178c2ecf20Sopenharmony_ci * SCALER_DISPCTRL_DSP3 = X, where X < 2 means 'connect DSP3 to 2188c2ecf20Sopenharmony_ci * FIFO X'. 2198c2ecf20Sopenharmony_ci * SCALER_DISPCTRL_DSP3 = 3 means 'disable DSP 3'. 2208c2ecf20Sopenharmony_ci * 2218c2ecf20Sopenharmony_ci * DSP3 is connected to FIFO2 unless the transposer is 2228c2ecf20Sopenharmony_ci * enabled. In this case, FIFO 2 is directly accessed by the 2238c2ecf20Sopenharmony_ci * TXP IP, and we need to disable the FIFO2 -> pixelvalve1 2248c2ecf20Sopenharmony_ci * route. 2258c2ecf20Sopenharmony_ci */ 2268c2ecf20Sopenharmony_ci if (vc4_state->feed_txp) 2278c2ecf20Sopenharmony_ci dsp3_mux = VC4_SET_FIELD(3, SCALER_DISPCTRL_DSP3_MUX); 2288c2ecf20Sopenharmony_ci else 2298c2ecf20Sopenharmony_ci dsp3_mux = VC4_SET_FIELD(2, SCALER_DISPCTRL_DSP3_MUX); 2308c2ecf20Sopenharmony_ci 2318c2ecf20Sopenharmony_ci dispctrl = HVS_READ(SCALER_DISPCTRL) & 2328c2ecf20Sopenharmony_ci ~SCALER_DISPCTRL_DSP3_MUX_MASK; 2338c2ecf20Sopenharmony_ci HVS_WRITE(SCALER_DISPCTRL, dispctrl | dsp3_mux); 2348c2ecf20Sopenharmony_ci } 2358c2ecf20Sopenharmony_ci} 2368c2ecf20Sopenharmony_ci 2378c2ecf20Sopenharmony_cistatic void vc5_hvs_pv_muxing_commit(struct vc4_dev *vc4, 2388c2ecf20Sopenharmony_ci struct drm_atomic_state *state) 2398c2ecf20Sopenharmony_ci{ 2408c2ecf20Sopenharmony_ci struct drm_crtc_state *crtc_state; 2418c2ecf20Sopenharmony_ci struct drm_crtc *crtc; 2428c2ecf20Sopenharmony_ci unsigned char mux; 2438c2ecf20Sopenharmony_ci unsigned int i; 2448c2ecf20Sopenharmony_ci u32 reg; 2458c2ecf20Sopenharmony_ci 2468c2ecf20Sopenharmony_ci for_each_new_crtc_in_state(state, crtc, crtc_state, i) { 2478c2ecf20Sopenharmony_ci struct vc4_crtc_state *vc4_state = to_vc4_crtc_state(crtc_state); 2488c2ecf20Sopenharmony_ci struct vc4_crtc *vc4_crtc = to_vc4_crtc(crtc); 2498c2ecf20Sopenharmony_ci 2508c2ecf20Sopenharmony_ci if (!vc4_state->update_muxing) 2518c2ecf20Sopenharmony_ci continue; 2528c2ecf20Sopenharmony_ci 2538c2ecf20Sopenharmony_ci switch (vc4_crtc->data->hvs_output) { 2548c2ecf20Sopenharmony_ci case 2: 2558c2ecf20Sopenharmony_ci mux = (vc4_state->assigned_channel == 2) ? 0 : 1; 2568c2ecf20Sopenharmony_ci reg = HVS_READ(SCALER_DISPECTRL); 2578c2ecf20Sopenharmony_ci HVS_WRITE(SCALER_DISPECTRL, 2588c2ecf20Sopenharmony_ci (reg & ~SCALER_DISPECTRL_DSP2_MUX_MASK) | 2598c2ecf20Sopenharmony_ci VC4_SET_FIELD(mux, SCALER_DISPECTRL_DSP2_MUX)); 2608c2ecf20Sopenharmony_ci break; 2618c2ecf20Sopenharmony_ci 2628c2ecf20Sopenharmony_ci case 3: 2638c2ecf20Sopenharmony_ci if (vc4_state->assigned_channel == VC4_HVS_CHANNEL_DISABLED) 2648c2ecf20Sopenharmony_ci mux = 3; 2658c2ecf20Sopenharmony_ci else 2668c2ecf20Sopenharmony_ci mux = vc4_state->assigned_channel; 2678c2ecf20Sopenharmony_ci 2688c2ecf20Sopenharmony_ci reg = HVS_READ(SCALER_DISPCTRL); 2698c2ecf20Sopenharmony_ci HVS_WRITE(SCALER_DISPCTRL, 2708c2ecf20Sopenharmony_ci (reg & ~SCALER_DISPCTRL_DSP3_MUX_MASK) | 2718c2ecf20Sopenharmony_ci VC4_SET_FIELD(mux, SCALER_DISPCTRL_DSP3_MUX)); 2728c2ecf20Sopenharmony_ci break; 2738c2ecf20Sopenharmony_ci 2748c2ecf20Sopenharmony_ci case 4: 2758c2ecf20Sopenharmony_ci if (vc4_state->assigned_channel == VC4_HVS_CHANNEL_DISABLED) 2768c2ecf20Sopenharmony_ci mux = 3; 2778c2ecf20Sopenharmony_ci else 2788c2ecf20Sopenharmony_ci mux = vc4_state->assigned_channel; 2798c2ecf20Sopenharmony_ci 2808c2ecf20Sopenharmony_ci reg = HVS_READ(SCALER_DISPEOLN); 2818c2ecf20Sopenharmony_ci HVS_WRITE(SCALER_DISPEOLN, 2828c2ecf20Sopenharmony_ci (reg & ~SCALER_DISPEOLN_DSP4_MUX_MASK) | 2838c2ecf20Sopenharmony_ci VC4_SET_FIELD(mux, SCALER_DISPEOLN_DSP4_MUX)); 2848c2ecf20Sopenharmony_ci 2858c2ecf20Sopenharmony_ci break; 2868c2ecf20Sopenharmony_ci 2878c2ecf20Sopenharmony_ci case 5: 2888c2ecf20Sopenharmony_ci if (vc4_state->assigned_channel == VC4_HVS_CHANNEL_DISABLED) 2898c2ecf20Sopenharmony_ci mux = 3; 2908c2ecf20Sopenharmony_ci else 2918c2ecf20Sopenharmony_ci mux = vc4_state->assigned_channel; 2928c2ecf20Sopenharmony_ci 2938c2ecf20Sopenharmony_ci reg = HVS_READ(SCALER_DISPDITHER); 2948c2ecf20Sopenharmony_ci HVS_WRITE(SCALER_DISPDITHER, 2958c2ecf20Sopenharmony_ci (reg & ~SCALER_DISPDITHER_DSP5_MUX_MASK) | 2968c2ecf20Sopenharmony_ci VC4_SET_FIELD(mux, SCALER_DISPDITHER_DSP5_MUX)); 2978c2ecf20Sopenharmony_ci break; 2988c2ecf20Sopenharmony_ci 2998c2ecf20Sopenharmony_ci default: 3008c2ecf20Sopenharmony_ci break; 3018c2ecf20Sopenharmony_ci } 3028c2ecf20Sopenharmony_ci } 3038c2ecf20Sopenharmony_ci} 3048c2ecf20Sopenharmony_ci 3058c2ecf20Sopenharmony_cistatic void 3068c2ecf20Sopenharmony_civc4_atomic_complete_commit(struct drm_atomic_state *state) 3078c2ecf20Sopenharmony_ci{ 3088c2ecf20Sopenharmony_ci struct drm_device *dev = state->dev; 3098c2ecf20Sopenharmony_ci struct vc4_dev *vc4 = to_vc4_dev(dev); 3108c2ecf20Sopenharmony_ci struct vc4_hvs *hvs = vc4->hvs; 3118c2ecf20Sopenharmony_ci struct drm_crtc_state *new_crtc_state; 3128c2ecf20Sopenharmony_ci struct drm_crtc *crtc; 3138c2ecf20Sopenharmony_ci int i; 3148c2ecf20Sopenharmony_ci 3158c2ecf20Sopenharmony_ci for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) { 3168c2ecf20Sopenharmony_ci struct vc4_crtc_state *vc4_crtc_state; 3178c2ecf20Sopenharmony_ci 3188c2ecf20Sopenharmony_ci if (!new_crtc_state->commit) 3198c2ecf20Sopenharmony_ci continue; 3208c2ecf20Sopenharmony_ci 3218c2ecf20Sopenharmony_ci vc4_crtc_state = to_vc4_crtc_state(new_crtc_state); 3228c2ecf20Sopenharmony_ci vc4_hvs_mask_underrun(dev, vc4_crtc_state->assigned_channel); 3238c2ecf20Sopenharmony_ci } 3248c2ecf20Sopenharmony_ci 3258c2ecf20Sopenharmony_ci if (vc4->hvs->hvs5) 3268c2ecf20Sopenharmony_ci clk_set_min_rate(hvs->core_clk, 500000000); 3278c2ecf20Sopenharmony_ci 3288c2ecf20Sopenharmony_ci drm_atomic_helper_wait_for_fences(dev, state, false); 3298c2ecf20Sopenharmony_ci 3308c2ecf20Sopenharmony_ci drm_atomic_helper_wait_for_dependencies(state); 3318c2ecf20Sopenharmony_ci 3328c2ecf20Sopenharmony_ci drm_atomic_helper_commit_modeset_disables(dev, state); 3338c2ecf20Sopenharmony_ci 3348c2ecf20Sopenharmony_ci vc4_ctm_commit(vc4, state); 3358c2ecf20Sopenharmony_ci 3368c2ecf20Sopenharmony_ci if (vc4->hvs->hvs5) 3378c2ecf20Sopenharmony_ci vc5_hvs_pv_muxing_commit(vc4, state); 3388c2ecf20Sopenharmony_ci else 3398c2ecf20Sopenharmony_ci vc4_hvs_pv_muxing_commit(vc4, state); 3408c2ecf20Sopenharmony_ci 3418c2ecf20Sopenharmony_ci drm_atomic_helper_commit_planes(dev, state, 0); 3428c2ecf20Sopenharmony_ci 3438c2ecf20Sopenharmony_ci drm_atomic_helper_commit_modeset_enables(dev, state); 3448c2ecf20Sopenharmony_ci 3458c2ecf20Sopenharmony_ci drm_atomic_helper_fake_vblank(state); 3468c2ecf20Sopenharmony_ci 3478c2ecf20Sopenharmony_ci drm_atomic_helper_commit_hw_done(state); 3488c2ecf20Sopenharmony_ci 3498c2ecf20Sopenharmony_ci drm_atomic_helper_wait_for_flip_done(dev, state); 3508c2ecf20Sopenharmony_ci 3518c2ecf20Sopenharmony_ci drm_atomic_helper_cleanup_planes(dev, state); 3528c2ecf20Sopenharmony_ci 3538c2ecf20Sopenharmony_ci drm_atomic_helper_commit_cleanup_done(state); 3548c2ecf20Sopenharmony_ci 3558c2ecf20Sopenharmony_ci if (vc4->hvs->hvs5) 3568c2ecf20Sopenharmony_ci clk_set_min_rate(hvs->core_clk, 0); 3578c2ecf20Sopenharmony_ci 3588c2ecf20Sopenharmony_ci drm_atomic_state_put(state); 3598c2ecf20Sopenharmony_ci 3608c2ecf20Sopenharmony_ci up(&vc4->async_modeset); 3618c2ecf20Sopenharmony_ci} 3628c2ecf20Sopenharmony_ci 3638c2ecf20Sopenharmony_cistatic void commit_work(struct work_struct *work) 3648c2ecf20Sopenharmony_ci{ 3658c2ecf20Sopenharmony_ci struct drm_atomic_state *state = container_of(work, 3668c2ecf20Sopenharmony_ci struct drm_atomic_state, 3678c2ecf20Sopenharmony_ci commit_work); 3688c2ecf20Sopenharmony_ci vc4_atomic_complete_commit(state); 3698c2ecf20Sopenharmony_ci} 3708c2ecf20Sopenharmony_ci 3718c2ecf20Sopenharmony_ci/** 3728c2ecf20Sopenharmony_ci * vc4_atomic_commit - commit validated state object 3738c2ecf20Sopenharmony_ci * @dev: DRM device 3748c2ecf20Sopenharmony_ci * @state: the driver state object 3758c2ecf20Sopenharmony_ci * @nonblock: nonblocking commit 3768c2ecf20Sopenharmony_ci * 3778c2ecf20Sopenharmony_ci * This function commits a with drm_atomic_helper_check() pre-validated state 3788c2ecf20Sopenharmony_ci * object. This can still fail when e.g. the framebuffer reservation fails. For 3798c2ecf20Sopenharmony_ci * now this doesn't implement asynchronous commits. 3808c2ecf20Sopenharmony_ci * 3818c2ecf20Sopenharmony_ci * RETURNS 3828c2ecf20Sopenharmony_ci * Zero for success or -errno. 3838c2ecf20Sopenharmony_ci */ 3848c2ecf20Sopenharmony_cistatic int vc4_atomic_commit(struct drm_device *dev, 3858c2ecf20Sopenharmony_ci struct drm_atomic_state *state, 3868c2ecf20Sopenharmony_ci bool nonblock) 3878c2ecf20Sopenharmony_ci{ 3888c2ecf20Sopenharmony_ci struct vc4_dev *vc4 = to_vc4_dev(dev); 3898c2ecf20Sopenharmony_ci int ret; 3908c2ecf20Sopenharmony_ci 3918c2ecf20Sopenharmony_ci if (state->async_update) { 3928c2ecf20Sopenharmony_ci ret = down_interruptible(&vc4->async_modeset); 3938c2ecf20Sopenharmony_ci if (ret) 3948c2ecf20Sopenharmony_ci return ret; 3958c2ecf20Sopenharmony_ci 3968c2ecf20Sopenharmony_ci ret = drm_atomic_helper_prepare_planes(dev, state); 3978c2ecf20Sopenharmony_ci if (ret) { 3988c2ecf20Sopenharmony_ci up(&vc4->async_modeset); 3998c2ecf20Sopenharmony_ci return ret; 4008c2ecf20Sopenharmony_ci } 4018c2ecf20Sopenharmony_ci 4028c2ecf20Sopenharmony_ci drm_atomic_helper_async_commit(dev, state); 4038c2ecf20Sopenharmony_ci 4048c2ecf20Sopenharmony_ci drm_atomic_helper_cleanup_planes(dev, state); 4058c2ecf20Sopenharmony_ci 4068c2ecf20Sopenharmony_ci up(&vc4->async_modeset); 4078c2ecf20Sopenharmony_ci 4088c2ecf20Sopenharmony_ci return 0; 4098c2ecf20Sopenharmony_ci } 4108c2ecf20Sopenharmony_ci 4118c2ecf20Sopenharmony_ci /* We know for sure we don't want an async update here. Set 4128c2ecf20Sopenharmony_ci * state->legacy_cursor_update to false to prevent 4138c2ecf20Sopenharmony_ci * drm_atomic_helper_setup_commit() from auto-completing 4148c2ecf20Sopenharmony_ci * commit->flip_done. 4158c2ecf20Sopenharmony_ci */ 4168c2ecf20Sopenharmony_ci state->legacy_cursor_update = false; 4178c2ecf20Sopenharmony_ci ret = drm_atomic_helper_setup_commit(state, nonblock); 4188c2ecf20Sopenharmony_ci if (ret) 4198c2ecf20Sopenharmony_ci return ret; 4208c2ecf20Sopenharmony_ci 4218c2ecf20Sopenharmony_ci INIT_WORK(&state->commit_work, commit_work); 4228c2ecf20Sopenharmony_ci 4238c2ecf20Sopenharmony_ci ret = down_interruptible(&vc4->async_modeset); 4248c2ecf20Sopenharmony_ci if (ret) 4258c2ecf20Sopenharmony_ci return ret; 4268c2ecf20Sopenharmony_ci 4278c2ecf20Sopenharmony_ci ret = drm_atomic_helper_prepare_planes(dev, state); 4288c2ecf20Sopenharmony_ci if (ret) { 4298c2ecf20Sopenharmony_ci up(&vc4->async_modeset); 4308c2ecf20Sopenharmony_ci return ret; 4318c2ecf20Sopenharmony_ci } 4328c2ecf20Sopenharmony_ci 4338c2ecf20Sopenharmony_ci if (!nonblock) { 4348c2ecf20Sopenharmony_ci ret = drm_atomic_helper_wait_for_fences(dev, state, true); 4358c2ecf20Sopenharmony_ci if (ret) { 4368c2ecf20Sopenharmony_ci drm_atomic_helper_cleanup_planes(dev, state); 4378c2ecf20Sopenharmony_ci up(&vc4->async_modeset); 4388c2ecf20Sopenharmony_ci return ret; 4398c2ecf20Sopenharmony_ci } 4408c2ecf20Sopenharmony_ci } 4418c2ecf20Sopenharmony_ci 4428c2ecf20Sopenharmony_ci /* 4438c2ecf20Sopenharmony_ci * This is the point of no return - everything below never fails except 4448c2ecf20Sopenharmony_ci * when the hw goes bonghits. Which means we can commit the new state on 4458c2ecf20Sopenharmony_ci * the software side now. 4468c2ecf20Sopenharmony_ci */ 4478c2ecf20Sopenharmony_ci 4488c2ecf20Sopenharmony_ci BUG_ON(drm_atomic_helper_swap_state(state, false) < 0); 4498c2ecf20Sopenharmony_ci 4508c2ecf20Sopenharmony_ci /* 4518c2ecf20Sopenharmony_ci * Everything below can be run asynchronously without the need to grab 4528c2ecf20Sopenharmony_ci * any modeset locks at all under one condition: It must be guaranteed 4538c2ecf20Sopenharmony_ci * that the asynchronous work has either been cancelled (if the driver 4548c2ecf20Sopenharmony_ci * supports it, which at least requires that the framebuffers get 4558c2ecf20Sopenharmony_ci * cleaned up with drm_atomic_helper_cleanup_planes()) or completed 4568c2ecf20Sopenharmony_ci * before the new state gets committed on the software side with 4578c2ecf20Sopenharmony_ci * drm_atomic_helper_swap_state(). 4588c2ecf20Sopenharmony_ci * 4598c2ecf20Sopenharmony_ci * This scheme allows new atomic state updates to be prepared and 4608c2ecf20Sopenharmony_ci * checked in parallel to the asynchronous completion of the previous 4618c2ecf20Sopenharmony_ci * update. Which is important since compositors need to figure out the 4628c2ecf20Sopenharmony_ci * composition of the next frame right after having submitted the 4638c2ecf20Sopenharmony_ci * current layout. 4648c2ecf20Sopenharmony_ci */ 4658c2ecf20Sopenharmony_ci 4668c2ecf20Sopenharmony_ci drm_atomic_state_get(state); 4678c2ecf20Sopenharmony_ci if (nonblock) 4688c2ecf20Sopenharmony_ci queue_work(system_unbound_wq, &state->commit_work); 4698c2ecf20Sopenharmony_ci else 4708c2ecf20Sopenharmony_ci vc4_atomic_complete_commit(state); 4718c2ecf20Sopenharmony_ci 4728c2ecf20Sopenharmony_ci return 0; 4738c2ecf20Sopenharmony_ci} 4748c2ecf20Sopenharmony_ci 4758c2ecf20Sopenharmony_cistatic struct drm_framebuffer *vc4_fb_create(struct drm_device *dev, 4768c2ecf20Sopenharmony_ci struct drm_file *file_priv, 4778c2ecf20Sopenharmony_ci const struct drm_mode_fb_cmd2 *mode_cmd) 4788c2ecf20Sopenharmony_ci{ 4798c2ecf20Sopenharmony_ci struct drm_mode_fb_cmd2 mode_cmd_local; 4808c2ecf20Sopenharmony_ci 4818c2ecf20Sopenharmony_ci /* If the user didn't specify a modifier, use the 4828c2ecf20Sopenharmony_ci * vc4_set_tiling_ioctl() state for the BO. 4838c2ecf20Sopenharmony_ci */ 4848c2ecf20Sopenharmony_ci if (!(mode_cmd->flags & DRM_MODE_FB_MODIFIERS)) { 4858c2ecf20Sopenharmony_ci struct drm_gem_object *gem_obj; 4868c2ecf20Sopenharmony_ci struct vc4_bo *bo; 4878c2ecf20Sopenharmony_ci 4888c2ecf20Sopenharmony_ci gem_obj = drm_gem_object_lookup(file_priv, 4898c2ecf20Sopenharmony_ci mode_cmd->handles[0]); 4908c2ecf20Sopenharmony_ci if (!gem_obj) { 4918c2ecf20Sopenharmony_ci DRM_DEBUG("Failed to look up GEM BO %d\n", 4928c2ecf20Sopenharmony_ci mode_cmd->handles[0]); 4938c2ecf20Sopenharmony_ci return ERR_PTR(-ENOENT); 4948c2ecf20Sopenharmony_ci } 4958c2ecf20Sopenharmony_ci bo = to_vc4_bo(gem_obj); 4968c2ecf20Sopenharmony_ci 4978c2ecf20Sopenharmony_ci mode_cmd_local = *mode_cmd; 4988c2ecf20Sopenharmony_ci 4998c2ecf20Sopenharmony_ci if (bo->t_format) { 5008c2ecf20Sopenharmony_ci mode_cmd_local.modifier[0] = 5018c2ecf20Sopenharmony_ci DRM_FORMAT_MOD_BROADCOM_VC4_T_TILED; 5028c2ecf20Sopenharmony_ci } else { 5038c2ecf20Sopenharmony_ci mode_cmd_local.modifier[0] = DRM_FORMAT_MOD_NONE; 5048c2ecf20Sopenharmony_ci } 5058c2ecf20Sopenharmony_ci 5068c2ecf20Sopenharmony_ci drm_gem_object_put(gem_obj); 5078c2ecf20Sopenharmony_ci 5088c2ecf20Sopenharmony_ci mode_cmd = &mode_cmd_local; 5098c2ecf20Sopenharmony_ci } 5108c2ecf20Sopenharmony_ci 5118c2ecf20Sopenharmony_ci return drm_gem_fb_create(dev, file_priv, mode_cmd); 5128c2ecf20Sopenharmony_ci} 5138c2ecf20Sopenharmony_ci 5148c2ecf20Sopenharmony_ci/* Our CTM has some peculiar limitations: we can only enable it for one CRTC 5158c2ecf20Sopenharmony_ci * at a time and the HW only supports S0.9 scalars. To account for the latter, 5168c2ecf20Sopenharmony_ci * we don't allow userland to set a CTM that we have no hope of approximating. 5178c2ecf20Sopenharmony_ci */ 5188c2ecf20Sopenharmony_cistatic int 5198c2ecf20Sopenharmony_civc4_ctm_atomic_check(struct drm_device *dev, struct drm_atomic_state *state) 5208c2ecf20Sopenharmony_ci{ 5218c2ecf20Sopenharmony_ci struct vc4_dev *vc4 = to_vc4_dev(dev); 5228c2ecf20Sopenharmony_ci struct vc4_ctm_state *ctm_state = NULL; 5238c2ecf20Sopenharmony_ci struct drm_crtc *crtc; 5248c2ecf20Sopenharmony_ci struct drm_crtc_state *old_crtc_state, *new_crtc_state; 5258c2ecf20Sopenharmony_ci struct drm_color_ctm *ctm; 5268c2ecf20Sopenharmony_ci int i; 5278c2ecf20Sopenharmony_ci 5288c2ecf20Sopenharmony_ci for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) { 5298c2ecf20Sopenharmony_ci /* CTM is being disabled. */ 5308c2ecf20Sopenharmony_ci if (!new_crtc_state->ctm && old_crtc_state->ctm) { 5318c2ecf20Sopenharmony_ci ctm_state = vc4_get_ctm_state(state, &vc4->ctm_manager); 5328c2ecf20Sopenharmony_ci if (IS_ERR(ctm_state)) 5338c2ecf20Sopenharmony_ci return PTR_ERR(ctm_state); 5348c2ecf20Sopenharmony_ci ctm_state->fifo = 0; 5358c2ecf20Sopenharmony_ci } 5368c2ecf20Sopenharmony_ci } 5378c2ecf20Sopenharmony_ci 5388c2ecf20Sopenharmony_ci for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) { 5398c2ecf20Sopenharmony_ci if (new_crtc_state->ctm == old_crtc_state->ctm) 5408c2ecf20Sopenharmony_ci continue; 5418c2ecf20Sopenharmony_ci 5428c2ecf20Sopenharmony_ci if (!ctm_state) { 5438c2ecf20Sopenharmony_ci ctm_state = vc4_get_ctm_state(state, &vc4->ctm_manager); 5448c2ecf20Sopenharmony_ci if (IS_ERR(ctm_state)) 5458c2ecf20Sopenharmony_ci return PTR_ERR(ctm_state); 5468c2ecf20Sopenharmony_ci } 5478c2ecf20Sopenharmony_ci 5488c2ecf20Sopenharmony_ci /* CTM is being enabled or the matrix changed. */ 5498c2ecf20Sopenharmony_ci if (new_crtc_state->ctm) { 5508c2ecf20Sopenharmony_ci struct vc4_crtc_state *vc4_crtc_state = 5518c2ecf20Sopenharmony_ci to_vc4_crtc_state(new_crtc_state); 5528c2ecf20Sopenharmony_ci 5538c2ecf20Sopenharmony_ci /* fifo is 1-based since 0 disables CTM. */ 5548c2ecf20Sopenharmony_ci int fifo = vc4_crtc_state->assigned_channel + 1; 5558c2ecf20Sopenharmony_ci 5568c2ecf20Sopenharmony_ci /* Check userland isn't trying to turn on CTM for more 5578c2ecf20Sopenharmony_ci * than one CRTC at a time. 5588c2ecf20Sopenharmony_ci */ 5598c2ecf20Sopenharmony_ci if (ctm_state->fifo && ctm_state->fifo != fifo) { 5608c2ecf20Sopenharmony_ci DRM_DEBUG_DRIVER("Too many CTM configured\n"); 5618c2ecf20Sopenharmony_ci return -EINVAL; 5628c2ecf20Sopenharmony_ci } 5638c2ecf20Sopenharmony_ci 5648c2ecf20Sopenharmony_ci /* Check we can approximate the specified CTM. 5658c2ecf20Sopenharmony_ci * We disallow scalars |c| > 1.0 since the HW has 5668c2ecf20Sopenharmony_ci * no integer bits. 5678c2ecf20Sopenharmony_ci */ 5688c2ecf20Sopenharmony_ci ctm = new_crtc_state->ctm->data; 5698c2ecf20Sopenharmony_ci for (i = 0; i < ARRAY_SIZE(ctm->matrix); i++) { 5708c2ecf20Sopenharmony_ci u64 val = ctm->matrix[i]; 5718c2ecf20Sopenharmony_ci 5728c2ecf20Sopenharmony_ci val &= ~BIT_ULL(63); 5738c2ecf20Sopenharmony_ci if (val > BIT_ULL(32)) 5748c2ecf20Sopenharmony_ci return -EINVAL; 5758c2ecf20Sopenharmony_ci } 5768c2ecf20Sopenharmony_ci 5778c2ecf20Sopenharmony_ci ctm_state->fifo = fifo; 5788c2ecf20Sopenharmony_ci ctm_state->ctm = ctm; 5798c2ecf20Sopenharmony_ci } 5808c2ecf20Sopenharmony_ci } 5818c2ecf20Sopenharmony_ci 5828c2ecf20Sopenharmony_ci return 0; 5838c2ecf20Sopenharmony_ci} 5848c2ecf20Sopenharmony_ci 5858c2ecf20Sopenharmony_cistatic int vc4_load_tracker_atomic_check(struct drm_atomic_state *state) 5868c2ecf20Sopenharmony_ci{ 5878c2ecf20Sopenharmony_ci struct drm_plane_state *old_plane_state, *new_plane_state; 5888c2ecf20Sopenharmony_ci struct vc4_dev *vc4 = to_vc4_dev(state->dev); 5898c2ecf20Sopenharmony_ci struct vc4_load_tracker_state *load_state; 5908c2ecf20Sopenharmony_ci struct drm_private_state *priv_state; 5918c2ecf20Sopenharmony_ci struct drm_plane *plane; 5928c2ecf20Sopenharmony_ci int i; 5938c2ecf20Sopenharmony_ci 5948c2ecf20Sopenharmony_ci if (!vc4->load_tracker_available) 5958c2ecf20Sopenharmony_ci return 0; 5968c2ecf20Sopenharmony_ci 5978c2ecf20Sopenharmony_ci priv_state = drm_atomic_get_private_obj_state(state, 5988c2ecf20Sopenharmony_ci &vc4->load_tracker); 5998c2ecf20Sopenharmony_ci if (IS_ERR(priv_state)) 6008c2ecf20Sopenharmony_ci return PTR_ERR(priv_state); 6018c2ecf20Sopenharmony_ci 6028c2ecf20Sopenharmony_ci load_state = to_vc4_load_tracker_state(priv_state); 6038c2ecf20Sopenharmony_ci for_each_oldnew_plane_in_state(state, plane, old_plane_state, 6048c2ecf20Sopenharmony_ci new_plane_state, i) { 6058c2ecf20Sopenharmony_ci struct vc4_plane_state *vc4_plane_state; 6068c2ecf20Sopenharmony_ci 6078c2ecf20Sopenharmony_ci if (old_plane_state->fb && old_plane_state->crtc) { 6088c2ecf20Sopenharmony_ci vc4_plane_state = to_vc4_plane_state(old_plane_state); 6098c2ecf20Sopenharmony_ci load_state->membus_load -= vc4_plane_state->membus_load; 6108c2ecf20Sopenharmony_ci load_state->hvs_load -= vc4_plane_state->hvs_load; 6118c2ecf20Sopenharmony_ci } 6128c2ecf20Sopenharmony_ci 6138c2ecf20Sopenharmony_ci if (new_plane_state->fb && new_plane_state->crtc) { 6148c2ecf20Sopenharmony_ci vc4_plane_state = to_vc4_plane_state(new_plane_state); 6158c2ecf20Sopenharmony_ci load_state->membus_load += vc4_plane_state->membus_load; 6168c2ecf20Sopenharmony_ci load_state->hvs_load += vc4_plane_state->hvs_load; 6178c2ecf20Sopenharmony_ci } 6188c2ecf20Sopenharmony_ci } 6198c2ecf20Sopenharmony_ci 6208c2ecf20Sopenharmony_ci /* Don't check the load when the tracker is disabled. */ 6218c2ecf20Sopenharmony_ci if (!vc4->load_tracker_enabled) 6228c2ecf20Sopenharmony_ci return 0; 6238c2ecf20Sopenharmony_ci 6248c2ecf20Sopenharmony_ci /* The absolute limit is 2Gbyte/sec, but let's take a margin to let 6258c2ecf20Sopenharmony_ci * the system work when other blocks are accessing the memory. 6268c2ecf20Sopenharmony_ci */ 6278c2ecf20Sopenharmony_ci if (load_state->membus_load > SZ_1G + SZ_512M) 6288c2ecf20Sopenharmony_ci return -ENOSPC; 6298c2ecf20Sopenharmony_ci 6308c2ecf20Sopenharmony_ci /* HVS clock is supposed to run @ 250Mhz, let's take a margin and 6318c2ecf20Sopenharmony_ci * consider the maximum number of cycles is 240M. 6328c2ecf20Sopenharmony_ci */ 6338c2ecf20Sopenharmony_ci if (load_state->hvs_load > 240000000ULL) 6348c2ecf20Sopenharmony_ci return -ENOSPC; 6358c2ecf20Sopenharmony_ci 6368c2ecf20Sopenharmony_ci return 0; 6378c2ecf20Sopenharmony_ci} 6388c2ecf20Sopenharmony_ci 6398c2ecf20Sopenharmony_cistatic struct drm_private_state * 6408c2ecf20Sopenharmony_civc4_load_tracker_duplicate_state(struct drm_private_obj *obj) 6418c2ecf20Sopenharmony_ci{ 6428c2ecf20Sopenharmony_ci struct vc4_load_tracker_state *state; 6438c2ecf20Sopenharmony_ci 6448c2ecf20Sopenharmony_ci state = kmemdup(obj->state, sizeof(*state), GFP_KERNEL); 6458c2ecf20Sopenharmony_ci if (!state) 6468c2ecf20Sopenharmony_ci return NULL; 6478c2ecf20Sopenharmony_ci 6488c2ecf20Sopenharmony_ci __drm_atomic_helper_private_obj_duplicate_state(obj, &state->base); 6498c2ecf20Sopenharmony_ci 6508c2ecf20Sopenharmony_ci return &state->base; 6518c2ecf20Sopenharmony_ci} 6528c2ecf20Sopenharmony_ci 6538c2ecf20Sopenharmony_cistatic void vc4_load_tracker_destroy_state(struct drm_private_obj *obj, 6548c2ecf20Sopenharmony_ci struct drm_private_state *state) 6558c2ecf20Sopenharmony_ci{ 6568c2ecf20Sopenharmony_ci struct vc4_load_tracker_state *load_state; 6578c2ecf20Sopenharmony_ci 6588c2ecf20Sopenharmony_ci load_state = to_vc4_load_tracker_state(state); 6598c2ecf20Sopenharmony_ci kfree(load_state); 6608c2ecf20Sopenharmony_ci} 6618c2ecf20Sopenharmony_ci 6628c2ecf20Sopenharmony_cistatic const struct drm_private_state_funcs vc4_load_tracker_state_funcs = { 6638c2ecf20Sopenharmony_ci .atomic_duplicate_state = vc4_load_tracker_duplicate_state, 6648c2ecf20Sopenharmony_ci .atomic_destroy_state = vc4_load_tracker_destroy_state, 6658c2ecf20Sopenharmony_ci}; 6668c2ecf20Sopenharmony_ci 6678c2ecf20Sopenharmony_cistatic void vc4_load_tracker_obj_fini(struct drm_device *dev, void *unused) 6688c2ecf20Sopenharmony_ci{ 6698c2ecf20Sopenharmony_ci struct vc4_dev *vc4 = to_vc4_dev(dev); 6708c2ecf20Sopenharmony_ci 6718c2ecf20Sopenharmony_ci if (!vc4->load_tracker_available) 6728c2ecf20Sopenharmony_ci return; 6738c2ecf20Sopenharmony_ci 6748c2ecf20Sopenharmony_ci drm_atomic_private_obj_fini(&vc4->load_tracker); 6758c2ecf20Sopenharmony_ci} 6768c2ecf20Sopenharmony_ci 6778c2ecf20Sopenharmony_cistatic int vc4_load_tracker_obj_init(struct vc4_dev *vc4) 6788c2ecf20Sopenharmony_ci{ 6798c2ecf20Sopenharmony_ci struct vc4_load_tracker_state *load_state; 6808c2ecf20Sopenharmony_ci 6818c2ecf20Sopenharmony_ci if (!vc4->load_tracker_available) 6828c2ecf20Sopenharmony_ci return 0; 6838c2ecf20Sopenharmony_ci 6848c2ecf20Sopenharmony_ci load_state = kzalloc(sizeof(*load_state), GFP_KERNEL); 6858c2ecf20Sopenharmony_ci if (!load_state) 6868c2ecf20Sopenharmony_ci return -ENOMEM; 6878c2ecf20Sopenharmony_ci 6888c2ecf20Sopenharmony_ci drm_atomic_private_obj_init(&vc4->base, &vc4->load_tracker, 6898c2ecf20Sopenharmony_ci &load_state->base, 6908c2ecf20Sopenharmony_ci &vc4_load_tracker_state_funcs); 6918c2ecf20Sopenharmony_ci 6928c2ecf20Sopenharmony_ci return drmm_add_action_or_reset(&vc4->base, vc4_load_tracker_obj_fini, NULL); 6938c2ecf20Sopenharmony_ci} 6948c2ecf20Sopenharmony_ci 6958c2ecf20Sopenharmony_cistatic struct drm_private_state * 6968c2ecf20Sopenharmony_civc4_hvs_channels_duplicate_state(struct drm_private_obj *obj) 6978c2ecf20Sopenharmony_ci{ 6988c2ecf20Sopenharmony_ci struct vc4_hvs_state *old_state = to_vc4_hvs_state(obj->state); 6998c2ecf20Sopenharmony_ci struct vc4_hvs_state *state; 7008c2ecf20Sopenharmony_ci 7018c2ecf20Sopenharmony_ci state = kzalloc(sizeof(*state), GFP_KERNEL); 7028c2ecf20Sopenharmony_ci if (!state) 7038c2ecf20Sopenharmony_ci return NULL; 7048c2ecf20Sopenharmony_ci 7058c2ecf20Sopenharmony_ci __drm_atomic_helper_private_obj_duplicate_state(obj, &state->base); 7068c2ecf20Sopenharmony_ci 7078c2ecf20Sopenharmony_ci state->unassigned_channels = old_state->unassigned_channels; 7088c2ecf20Sopenharmony_ci 7098c2ecf20Sopenharmony_ci return &state->base; 7108c2ecf20Sopenharmony_ci} 7118c2ecf20Sopenharmony_ci 7128c2ecf20Sopenharmony_cistatic void vc4_hvs_channels_destroy_state(struct drm_private_obj *obj, 7138c2ecf20Sopenharmony_ci struct drm_private_state *state) 7148c2ecf20Sopenharmony_ci{ 7158c2ecf20Sopenharmony_ci struct vc4_hvs_state *hvs_state = to_vc4_hvs_state(state); 7168c2ecf20Sopenharmony_ci 7178c2ecf20Sopenharmony_ci kfree(hvs_state); 7188c2ecf20Sopenharmony_ci} 7198c2ecf20Sopenharmony_ci 7208c2ecf20Sopenharmony_cistatic const struct drm_private_state_funcs vc4_hvs_state_funcs = { 7218c2ecf20Sopenharmony_ci .atomic_duplicate_state = vc4_hvs_channels_duplicate_state, 7228c2ecf20Sopenharmony_ci .atomic_destroy_state = vc4_hvs_channels_destroy_state, 7238c2ecf20Sopenharmony_ci}; 7248c2ecf20Sopenharmony_ci 7258c2ecf20Sopenharmony_cistatic void vc4_hvs_channels_obj_fini(struct drm_device *dev, void *unused) 7268c2ecf20Sopenharmony_ci{ 7278c2ecf20Sopenharmony_ci struct vc4_dev *vc4 = to_vc4_dev(dev); 7288c2ecf20Sopenharmony_ci 7298c2ecf20Sopenharmony_ci drm_atomic_private_obj_fini(&vc4->hvs_channels); 7308c2ecf20Sopenharmony_ci} 7318c2ecf20Sopenharmony_ci 7328c2ecf20Sopenharmony_cistatic int vc4_hvs_channels_obj_init(struct vc4_dev *vc4) 7338c2ecf20Sopenharmony_ci{ 7348c2ecf20Sopenharmony_ci struct vc4_hvs_state *state; 7358c2ecf20Sopenharmony_ci 7368c2ecf20Sopenharmony_ci state = kzalloc(sizeof(*state), GFP_KERNEL); 7378c2ecf20Sopenharmony_ci if (!state) 7388c2ecf20Sopenharmony_ci return -ENOMEM; 7398c2ecf20Sopenharmony_ci 7408c2ecf20Sopenharmony_ci state->unassigned_channels = GENMASK(HVS_NUM_CHANNELS - 1, 0); 7418c2ecf20Sopenharmony_ci drm_atomic_private_obj_init(&vc4->base, &vc4->hvs_channels, 7428c2ecf20Sopenharmony_ci &state->base, 7438c2ecf20Sopenharmony_ci &vc4_hvs_state_funcs); 7448c2ecf20Sopenharmony_ci 7458c2ecf20Sopenharmony_ci return drmm_add_action_or_reset(&vc4->base, vc4_hvs_channels_obj_fini, NULL); 7468c2ecf20Sopenharmony_ci} 7478c2ecf20Sopenharmony_ci 7488c2ecf20Sopenharmony_ci/* 7498c2ecf20Sopenharmony_ci * The BCM2711 HVS has up to 7 outputs connected to the pixelvalves and 7508c2ecf20Sopenharmony_ci * the TXP (and therefore all the CRTCs found on that platform). 7518c2ecf20Sopenharmony_ci * 7528c2ecf20Sopenharmony_ci * The naive (and our initial) implementation would just iterate over 7538c2ecf20Sopenharmony_ci * all the active CRTCs, try to find a suitable FIFO, and then remove it 7548c2ecf20Sopenharmony_ci * from the pool of available FIFOs. However, there are a few corner 7558c2ecf20Sopenharmony_ci * cases that need to be considered: 7568c2ecf20Sopenharmony_ci * 7578c2ecf20Sopenharmony_ci * - When running in a dual-display setup (so with two CRTCs involved), 7588c2ecf20Sopenharmony_ci * we can update the state of a single CRTC (for example by changing 7598c2ecf20Sopenharmony_ci * its mode using xrandr under X11) without affecting the other. In 7608c2ecf20Sopenharmony_ci * this case, the other CRTC wouldn't be in the state at all, so we 7618c2ecf20Sopenharmony_ci * need to consider all the running CRTCs in the DRM device to assign 7628c2ecf20Sopenharmony_ci * a FIFO, not just the one in the state. 7638c2ecf20Sopenharmony_ci * 7648c2ecf20Sopenharmony_ci * - To fix the above, we can't use drm_atomic_get_crtc_state on all 7658c2ecf20Sopenharmony_ci * enabled CRTCs to pull their CRTC state into the global state, since 7668c2ecf20Sopenharmony_ci * a page flip would start considering their vblank to complete. Since 7678c2ecf20Sopenharmony_ci * we don't have a guarantee that they are actually active, that 7688c2ecf20Sopenharmony_ci * vblank might never happen, and shouldn't even be considered if we 7698c2ecf20Sopenharmony_ci * want to do a page flip on a single CRTC. That can be tested by 7708c2ecf20Sopenharmony_ci * doing a modetest -v first on HDMI1 and then on HDMI0. 7718c2ecf20Sopenharmony_ci * 7728c2ecf20Sopenharmony_ci * - Since we need the pixelvalve to be disabled and enabled back when 7738c2ecf20Sopenharmony_ci * the FIFO is changed, we should keep the FIFO assigned for as long 7748c2ecf20Sopenharmony_ci * as the CRTC is enabled, only considering it free again once that 7758c2ecf20Sopenharmony_ci * CRTC has been disabled. This can be tested by booting X11 on a 7768c2ecf20Sopenharmony_ci * single display, and changing the resolution down and then back up. 7778c2ecf20Sopenharmony_ci */ 7788c2ecf20Sopenharmony_cistatic int vc4_pv_muxing_atomic_check(struct drm_device *dev, 7798c2ecf20Sopenharmony_ci struct drm_atomic_state *state) 7808c2ecf20Sopenharmony_ci{ 7818c2ecf20Sopenharmony_ci struct vc4_hvs_state *hvs_new_state; 7828c2ecf20Sopenharmony_ci struct drm_crtc_state *old_crtc_state, *new_crtc_state; 7838c2ecf20Sopenharmony_ci struct drm_crtc *crtc; 7848c2ecf20Sopenharmony_ci unsigned int i; 7858c2ecf20Sopenharmony_ci 7868c2ecf20Sopenharmony_ci hvs_new_state = vc4_hvs_get_global_state(state); 7878c2ecf20Sopenharmony_ci if (!hvs_new_state) 7888c2ecf20Sopenharmony_ci return -EINVAL; 7898c2ecf20Sopenharmony_ci 7908c2ecf20Sopenharmony_ci for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) { 7918c2ecf20Sopenharmony_ci struct vc4_crtc_state *old_vc4_crtc_state = 7928c2ecf20Sopenharmony_ci to_vc4_crtc_state(old_crtc_state); 7938c2ecf20Sopenharmony_ci struct vc4_crtc_state *new_vc4_crtc_state = 7948c2ecf20Sopenharmony_ci to_vc4_crtc_state(new_crtc_state); 7958c2ecf20Sopenharmony_ci struct vc4_crtc *vc4_crtc = to_vc4_crtc(crtc); 7968c2ecf20Sopenharmony_ci unsigned int matching_channels; 7978c2ecf20Sopenharmony_ci 7988c2ecf20Sopenharmony_ci /* Nothing to do here, let's skip it */ 7998c2ecf20Sopenharmony_ci if (old_crtc_state->enable == new_crtc_state->enable) 8008c2ecf20Sopenharmony_ci continue; 8018c2ecf20Sopenharmony_ci 8028c2ecf20Sopenharmony_ci /* Muxing will need to be modified, mark it as such */ 8038c2ecf20Sopenharmony_ci new_vc4_crtc_state->update_muxing = true; 8048c2ecf20Sopenharmony_ci 8058c2ecf20Sopenharmony_ci /* If we're disabling our CRTC, we put back our channel */ 8068c2ecf20Sopenharmony_ci if (!new_crtc_state->enable) { 8078c2ecf20Sopenharmony_ci hvs_new_state->unassigned_channels |= BIT(old_vc4_crtc_state->assigned_channel); 8088c2ecf20Sopenharmony_ci new_vc4_crtc_state->assigned_channel = VC4_HVS_CHANNEL_DISABLED; 8098c2ecf20Sopenharmony_ci continue; 8108c2ecf20Sopenharmony_ci } 8118c2ecf20Sopenharmony_ci 8128c2ecf20Sopenharmony_ci /* 8138c2ecf20Sopenharmony_ci * The problem we have to solve here is that we have 8148c2ecf20Sopenharmony_ci * up to 7 encoders, connected to up to 6 CRTCs. 8158c2ecf20Sopenharmony_ci * 8168c2ecf20Sopenharmony_ci * Those CRTCs, depending on the instance, can be 8178c2ecf20Sopenharmony_ci * routed to 1, 2 or 3 HVS FIFOs, and we need to set 8188c2ecf20Sopenharmony_ci * the change the muxing between FIFOs and outputs in 8198c2ecf20Sopenharmony_ci * the HVS accordingly. 8208c2ecf20Sopenharmony_ci * 8218c2ecf20Sopenharmony_ci * It would be pretty hard to come up with an 8228c2ecf20Sopenharmony_ci * algorithm that would generically solve 8238c2ecf20Sopenharmony_ci * this. However, the current routing trees we support 8248c2ecf20Sopenharmony_ci * allow us to simplify a bit the problem. 8258c2ecf20Sopenharmony_ci * 8268c2ecf20Sopenharmony_ci * Indeed, with the current supported layouts, if we 8278c2ecf20Sopenharmony_ci * try to assign in the ascending crtc index order the 8288c2ecf20Sopenharmony_ci * FIFOs, we can't fall into the situation where an 8298c2ecf20Sopenharmony_ci * earlier CRTC that had multiple routes is assigned 8308c2ecf20Sopenharmony_ci * one that was the only option for a later CRTC. 8318c2ecf20Sopenharmony_ci * 8328c2ecf20Sopenharmony_ci * If the layout changes and doesn't give us that in 8338c2ecf20Sopenharmony_ci * the future, we will need to have something smarter, 8348c2ecf20Sopenharmony_ci * but it works so far. 8358c2ecf20Sopenharmony_ci */ 8368c2ecf20Sopenharmony_ci matching_channels = hvs_new_state->unassigned_channels & vc4_crtc->data->hvs_available_channels; 8378c2ecf20Sopenharmony_ci if (matching_channels) { 8388c2ecf20Sopenharmony_ci unsigned int channel = ffs(matching_channels) - 1; 8398c2ecf20Sopenharmony_ci 8408c2ecf20Sopenharmony_ci new_vc4_crtc_state->assigned_channel = channel; 8418c2ecf20Sopenharmony_ci hvs_new_state->unassigned_channels &= ~BIT(channel); 8428c2ecf20Sopenharmony_ci } else { 8438c2ecf20Sopenharmony_ci return -EINVAL; 8448c2ecf20Sopenharmony_ci } 8458c2ecf20Sopenharmony_ci } 8468c2ecf20Sopenharmony_ci 8478c2ecf20Sopenharmony_ci return 0; 8488c2ecf20Sopenharmony_ci} 8498c2ecf20Sopenharmony_ci 8508c2ecf20Sopenharmony_cistatic int 8518c2ecf20Sopenharmony_civc4_atomic_check(struct drm_device *dev, struct drm_atomic_state *state) 8528c2ecf20Sopenharmony_ci{ 8538c2ecf20Sopenharmony_ci int ret; 8548c2ecf20Sopenharmony_ci 8558c2ecf20Sopenharmony_ci ret = vc4_pv_muxing_atomic_check(dev, state); 8568c2ecf20Sopenharmony_ci if (ret) 8578c2ecf20Sopenharmony_ci return ret; 8588c2ecf20Sopenharmony_ci 8598c2ecf20Sopenharmony_ci ret = vc4_ctm_atomic_check(dev, state); 8608c2ecf20Sopenharmony_ci if (ret < 0) 8618c2ecf20Sopenharmony_ci return ret; 8628c2ecf20Sopenharmony_ci 8638c2ecf20Sopenharmony_ci ret = drm_atomic_helper_check(dev, state); 8648c2ecf20Sopenharmony_ci if (ret) 8658c2ecf20Sopenharmony_ci return ret; 8668c2ecf20Sopenharmony_ci 8678c2ecf20Sopenharmony_ci return vc4_load_tracker_atomic_check(state); 8688c2ecf20Sopenharmony_ci} 8698c2ecf20Sopenharmony_ci 8708c2ecf20Sopenharmony_cistatic const struct drm_mode_config_funcs vc4_mode_funcs = { 8718c2ecf20Sopenharmony_ci .atomic_check = vc4_atomic_check, 8728c2ecf20Sopenharmony_ci .atomic_commit = vc4_atomic_commit, 8738c2ecf20Sopenharmony_ci .fb_create = vc4_fb_create, 8748c2ecf20Sopenharmony_ci}; 8758c2ecf20Sopenharmony_ci 8768c2ecf20Sopenharmony_ciint vc4_kms_load(struct drm_device *dev) 8778c2ecf20Sopenharmony_ci{ 8788c2ecf20Sopenharmony_ci struct vc4_dev *vc4 = to_vc4_dev(dev); 8798c2ecf20Sopenharmony_ci bool is_vc5 = of_device_is_compatible(dev->dev->of_node, 8808c2ecf20Sopenharmony_ci "brcm,bcm2711-vc5"); 8818c2ecf20Sopenharmony_ci int ret; 8828c2ecf20Sopenharmony_ci 8838c2ecf20Sopenharmony_ci if (!is_vc5) { 8848c2ecf20Sopenharmony_ci vc4->load_tracker_available = true; 8858c2ecf20Sopenharmony_ci 8868c2ecf20Sopenharmony_ci /* Start with the load tracker enabled. Can be 8878c2ecf20Sopenharmony_ci * disabled through the debugfs load_tracker file. 8888c2ecf20Sopenharmony_ci */ 8898c2ecf20Sopenharmony_ci vc4->load_tracker_enabled = true; 8908c2ecf20Sopenharmony_ci } 8918c2ecf20Sopenharmony_ci 8928c2ecf20Sopenharmony_ci sema_init(&vc4->async_modeset, 1); 8938c2ecf20Sopenharmony_ci 8948c2ecf20Sopenharmony_ci /* Set support for vblank irq fast disable, before drm_vblank_init() */ 8958c2ecf20Sopenharmony_ci dev->vblank_disable_immediate = true; 8968c2ecf20Sopenharmony_ci 8978c2ecf20Sopenharmony_ci dev->irq_enabled = true; 8988c2ecf20Sopenharmony_ci ret = drm_vblank_init(dev, dev->mode_config.num_crtc); 8998c2ecf20Sopenharmony_ci if (ret < 0) { 9008c2ecf20Sopenharmony_ci dev_err(dev->dev, "failed to initialize vblank\n"); 9018c2ecf20Sopenharmony_ci return ret; 9028c2ecf20Sopenharmony_ci } 9038c2ecf20Sopenharmony_ci 9048c2ecf20Sopenharmony_ci if (is_vc5) { 9058c2ecf20Sopenharmony_ci dev->mode_config.max_width = 7680; 9068c2ecf20Sopenharmony_ci dev->mode_config.max_height = 7680; 9078c2ecf20Sopenharmony_ci } else { 9088c2ecf20Sopenharmony_ci dev->mode_config.max_width = 2048; 9098c2ecf20Sopenharmony_ci dev->mode_config.max_height = 2048; 9108c2ecf20Sopenharmony_ci } 9118c2ecf20Sopenharmony_ci 9128c2ecf20Sopenharmony_ci dev->mode_config.funcs = &vc4_mode_funcs; 9138c2ecf20Sopenharmony_ci dev->mode_config.preferred_depth = 24; 9148c2ecf20Sopenharmony_ci dev->mode_config.async_page_flip = true; 9158c2ecf20Sopenharmony_ci dev->mode_config.allow_fb_modifiers = true; 9168c2ecf20Sopenharmony_ci 9178c2ecf20Sopenharmony_ci ret = vc4_ctm_obj_init(vc4); 9188c2ecf20Sopenharmony_ci if (ret) 9198c2ecf20Sopenharmony_ci return ret; 9208c2ecf20Sopenharmony_ci 9218c2ecf20Sopenharmony_ci ret = vc4_load_tracker_obj_init(vc4); 9228c2ecf20Sopenharmony_ci if (ret) 9238c2ecf20Sopenharmony_ci return ret; 9248c2ecf20Sopenharmony_ci 9258c2ecf20Sopenharmony_ci ret = vc4_hvs_channels_obj_init(vc4); 9268c2ecf20Sopenharmony_ci if (ret) 9278c2ecf20Sopenharmony_ci return ret; 9288c2ecf20Sopenharmony_ci 9298c2ecf20Sopenharmony_ci drm_mode_config_reset(dev); 9308c2ecf20Sopenharmony_ci 9318c2ecf20Sopenharmony_ci drm_kms_helper_poll_init(dev); 9328c2ecf20Sopenharmony_ci 9338c2ecf20Sopenharmony_ci return 0; 9348c2ecf20Sopenharmony_ci} 935