18c2ecf20Sopenharmony_ci/* 28c2ecf20Sopenharmony_ci * Copyright 2018 Red Hat Inc. 38c2ecf20Sopenharmony_ci * 48c2ecf20Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a 58c2ecf20Sopenharmony_ci * copy of this software and associated documentation files (the "Software"), 68c2ecf20Sopenharmony_ci * to deal in the Software without restriction, including without limitation 78c2ecf20Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense, 88c2ecf20Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the 98c2ecf20Sopenharmony_ci * Software is furnished to do so, subject to the following conditions: 108c2ecf20Sopenharmony_ci * 118c2ecf20Sopenharmony_ci * The above copyright notice and this permission notice shall be included in 128c2ecf20Sopenharmony_ci * all copies or substantial portions of the Software. 138c2ecf20Sopenharmony_ci * 148c2ecf20Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 158c2ecf20Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 168c2ecf20Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 178c2ecf20Sopenharmony_ci * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR 188c2ecf20Sopenharmony_ci * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 198c2ecf20Sopenharmony_ci * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 208c2ecf20Sopenharmony_ci * OTHER DEALINGS IN THE SOFTWARE. 218c2ecf20Sopenharmony_ci */ 228c2ecf20Sopenharmony_ci#include "head.h" 238c2ecf20Sopenharmony_ci#include "base.h" 248c2ecf20Sopenharmony_ci#include "core.h" 258c2ecf20Sopenharmony_ci#include "curs.h" 268c2ecf20Sopenharmony_ci#include "ovly.h" 278c2ecf20Sopenharmony_ci#include "crc.h" 288c2ecf20Sopenharmony_ci 298c2ecf20Sopenharmony_ci#include <nvif/class.h> 308c2ecf20Sopenharmony_ci#include <nvif/event.h> 318c2ecf20Sopenharmony_ci#include <nvif/cl0046.h> 328c2ecf20Sopenharmony_ci 338c2ecf20Sopenharmony_ci#include <drm/drm_atomic_helper.h> 348c2ecf20Sopenharmony_ci#include <drm/drm_crtc_helper.h> 358c2ecf20Sopenharmony_ci#include <drm/drm_vblank.h> 368c2ecf20Sopenharmony_ci#include "nouveau_connector.h" 378c2ecf20Sopenharmony_ci 388c2ecf20Sopenharmony_civoid 398c2ecf20Sopenharmony_cinv50_head_flush_clr(struct nv50_head *head, 408c2ecf20Sopenharmony_ci struct nv50_head_atom *asyh, bool flush) 418c2ecf20Sopenharmony_ci{ 428c2ecf20Sopenharmony_ci union nv50_head_atom_mask clr = { 438c2ecf20Sopenharmony_ci .mask = asyh->clr.mask & ~(flush ? 0 : asyh->set.mask), 448c2ecf20Sopenharmony_ci }; 458c2ecf20Sopenharmony_ci if (clr.crc) nv50_crc_atomic_clr(head); 468c2ecf20Sopenharmony_ci if (clr.olut) head->func->olut_clr(head); 478c2ecf20Sopenharmony_ci if (clr.core) head->func->core_clr(head); 488c2ecf20Sopenharmony_ci if (clr.curs) head->func->curs_clr(head); 498c2ecf20Sopenharmony_ci} 508c2ecf20Sopenharmony_ci 518c2ecf20Sopenharmony_civoid 528c2ecf20Sopenharmony_cinv50_head_flush_set_wndw(struct nv50_head *head, struct nv50_head_atom *asyh) 538c2ecf20Sopenharmony_ci{ 548c2ecf20Sopenharmony_ci if (asyh->set.curs ) head->func->curs_set(head, asyh); 558c2ecf20Sopenharmony_ci if (asyh->set.olut ) { 568c2ecf20Sopenharmony_ci asyh->olut.offset = nv50_lut_load(&head->olut, 578c2ecf20Sopenharmony_ci asyh->olut.buffer, 588c2ecf20Sopenharmony_ci asyh->state.gamma_lut, 598c2ecf20Sopenharmony_ci asyh->olut.load); 608c2ecf20Sopenharmony_ci head->func->olut_set(head, asyh); 618c2ecf20Sopenharmony_ci } 628c2ecf20Sopenharmony_ci} 638c2ecf20Sopenharmony_ci 648c2ecf20Sopenharmony_civoid 658c2ecf20Sopenharmony_cinv50_head_flush_set(struct nv50_head *head, struct nv50_head_atom *asyh) 668c2ecf20Sopenharmony_ci{ 678c2ecf20Sopenharmony_ci if (asyh->set.view ) head->func->view (head, asyh); 688c2ecf20Sopenharmony_ci if (asyh->set.mode ) head->func->mode (head, asyh); 698c2ecf20Sopenharmony_ci if (asyh->set.core ) head->func->core_set(head, asyh); 708c2ecf20Sopenharmony_ci if (asyh->set.base ) head->func->base (head, asyh); 718c2ecf20Sopenharmony_ci if (asyh->set.ovly ) head->func->ovly (head, asyh); 728c2ecf20Sopenharmony_ci if (asyh->set.dither ) head->func->dither (head, asyh); 738c2ecf20Sopenharmony_ci if (asyh->set.procamp) head->func->procamp (head, asyh); 748c2ecf20Sopenharmony_ci if (asyh->set.crc ) nv50_crc_atomic_set (head, asyh); 758c2ecf20Sopenharmony_ci if (asyh->set.or ) head->func->or (head, asyh); 768c2ecf20Sopenharmony_ci} 778c2ecf20Sopenharmony_ci 788c2ecf20Sopenharmony_cistatic void 798c2ecf20Sopenharmony_cinv50_head_atomic_check_procamp(struct nv50_head_atom *armh, 808c2ecf20Sopenharmony_ci struct nv50_head_atom *asyh, 818c2ecf20Sopenharmony_ci struct nouveau_conn_atom *asyc) 828c2ecf20Sopenharmony_ci{ 838c2ecf20Sopenharmony_ci const int vib = asyc->procamp.color_vibrance - 100; 848c2ecf20Sopenharmony_ci const int hue = asyc->procamp.vibrant_hue - 90; 858c2ecf20Sopenharmony_ci const int adj = (vib > 0) ? 50 : 0; 868c2ecf20Sopenharmony_ci asyh->procamp.sat.cos = ((vib * 2047 + adj) / 100) & 0xfff; 878c2ecf20Sopenharmony_ci asyh->procamp.sat.sin = ((hue * 2047) / 100) & 0xfff; 888c2ecf20Sopenharmony_ci asyh->set.procamp = true; 898c2ecf20Sopenharmony_ci} 908c2ecf20Sopenharmony_ci 918c2ecf20Sopenharmony_cistatic void 928c2ecf20Sopenharmony_cinv50_head_atomic_check_dither(struct nv50_head_atom *armh, 938c2ecf20Sopenharmony_ci struct nv50_head_atom *asyh, 948c2ecf20Sopenharmony_ci struct nouveau_conn_atom *asyc) 958c2ecf20Sopenharmony_ci{ 968c2ecf20Sopenharmony_ci u32 mode = 0x00; 978c2ecf20Sopenharmony_ci 988c2ecf20Sopenharmony_ci if (asyc->dither.mode) { 998c2ecf20Sopenharmony_ci if (asyc->dither.mode == DITHERING_MODE_AUTO) { 1008c2ecf20Sopenharmony_ci if (asyh->base.depth > asyh->or.bpc * 3) 1018c2ecf20Sopenharmony_ci mode = DITHERING_MODE_DYNAMIC2X2; 1028c2ecf20Sopenharmony_ci } else { 1038c2ecf20Sopenharmony_ci mode = asyc->dither.mode; 1048c2ecf20Sopenharmony_ci } 1058c2ecf20Sopenharmony_ci 1068c2ecf20Sopenharmony_ci if (asyc->dither.depth == DITHERING_DEPTH_AUTO) { 1078c2ecf20Sopenharmony_ci if (asyh->or.bpc >= 8) 1088c2ecf20Sopenharmony_ci mode |= DITHERING_DEPTH_8BPC; 1098c2ecf20Sopenharmony_ci } else { 1108c2ecf20Sopenharmony_ci mode |= asyc->dither.depth; 1118c2ecf20Sopenharmony_ci } 1128c2ecf20Sopenharmony_ci } 1138c2ecf20Sopenharmony_ci 1148c2ecf20Sopenharmony_ci asyh->dither.enable = NVVAL_GET(mode, NV507D, HEAD_SET_DITHER_CONTROL, ENABLE); 1158c2ecf20Sopenharmony_ci asyh->dither.bits = NVVAL_GET(mode, NV507D, HEAD_SET_DITHER_CONTROL, BITS); 1168c2ecf20Sopenharmony_ci asyh->dither.mode = NVVAL_GET(mode, NV507D, HEAD_SET_DITHER_CONTROL, MODE); 1178c2ecf20Sopenharmony_ci asyh->set.dither = true; 1188c2ecf20Sopenharmony_ci} 1198c2ecf20Sopenharmony_ci 1208c2ecf20Sopenharmony_cistatic void 1218c2ecf20Sopenharmony_cinv50_head_atomic_check_view(struct nv50_head_atom *armh, 1228c2ecf20Sopenharmony_ci struct nv50_head_atom *asyh, 1238c2ecf20Sopenharmony_ci struct nouveau_conn_atom *asyc) 1248c2ecf20Sopenharmony_ci{ 1258c2ecf20Sopenharmony_ci struct drm_connector *connector = asyc->state.connector; 1268c2ecf20Sopenharmony_ci struct drm_display_mode *omode = &asyh->state.adjusted_mode; 1278c2ecf20Sopenharmony_ci struct drm_display_mode *umode = &asyh->state.mode; 1288c2ecf20Sopenharmony_ci int mode = asyc->scaler.mode; 1298c2ecf20Sopenharmony_ci struct edid *edid; 1308c2ecf20Sopenharmony_ci int umode_vdisplay, omode_hdisplay, omode_vdisplay; 1318c2ecf20Sopenharmony_ci 1328c2ecf20Sopenharmony_ci if (connector->edid_blob_ptr) 1338c2ecf20Sopenharmony_ci edid = (struct edid *)connector->edid_blob_ptr->data; 1348c2ecf20Sopenharmony_ci else 1358c2ecf20Sopenharmony_ci edid = NULL; 1368c2ecf20Sopenharmony_ci 1378c2ecf20Sopenharmony_ci if (!asyc->scaler.full) { 1388c2ecf20Sopenharmony_ci if (mode == DRM_MODE_SCALE_NONE) 1398c2ecf20Sopenharmony_ci omode = umode; 1408c2ecf20Sopenharmony_ci } else { 1418c2ecf20Sopenharmony_ci /* Non-EDID LVDS/eDP mode. */ 1428c2ecf20Sopenharmony_ci mode = DRM_MODE_SCALE_FULLSCREEN; 1438c2ecf20Sopenharmony_ci } 1448c2ecf20Sopenharmony_ci 1458c2ecf20Sopenharmony_ci /* For the user-specified mode, we must ignore doublescan and 1468c2ecf20Sopenharmony_ci * the like, but honor frame packing. 1478c2ecf20Sopenharmony_ci */ 1488c2ecf20Sopenharmony_ci umode_vdisplay = umode->vdisplay; 1498c2ecf20Sopenharmony_ci if ((umode->flags & DRM_MODE_FLAG_3D_MASK) == DRM_MODE_FLAG_3D_FRAME_PACKING) 1508c2ecf20Sopenharmony_ci umode_vdisplay += umode->vtotal; 1518c2ecf20Sopenharmony_ci asyh->view.iW = umode->hdisplay; 1528c2ecf20Sopenharmony_ci asyh->view.iH = umode_vdisplay; 1538c2ecf20Sopenharmony_ci /* For the output mode, we can just use the stock helper. */ 1548c2ecf20Sopenharmony_ci drm_mode_get_hv_timing(omode, &omode_hdisplay, &omode_vdisplay); 1558c2ecf20Sopenharmony_ci asyh->view.oW = omode_hdisplay; 1568c2ecf20Sopenharmony_ci asyh->view.oH = omode_vdisplay; 1578c2ecf20Sopenharmony_ci 1588c2ecf20Sopenharmony_ci /* Add overscan compensation if necessary, will keep the aspect 1598c2ecf20Sopenharmony_ci * ratio the same as the backend mode unless overridden by the 1608c2ecf20Sopenharmony_ci * user setting both hborder and vborder properties. 1618c2ecf20Sopenharmony_ci */ 1628c2ecf20Sopenharmony_ci if ((asyc->scaler.underscan.mode == UNDERSCAN_ON || 1638c2ecf20Sopenharmony_ci (asyc->scaler.underscan.mode == UNDERSCAN_AUTO && 1648c2ecf20Sopenharmony_ci drm_detect_hdmi_monitor(edid)))) { 1658c2ecf20Sopenharmony_ci u32 bX = asyc->scaler.underscan.hborder; 1668c2ecf20Sopenharmony_ci u32 bY = asyc->scaler.underscan.vborder; 1678c2ecf20Sopenharmony_ci u32 r = (asyh->view.oH << 19) / asyh->view.oW; 1688c2ecf20Sopenharmony_ci 1698c2ecf20Sopenharmony_ci if (bX) { 1708c2ecf20Sopenharmony_ci asyh->view.oW -= (bX * 2); 1718c2ecf20Sopenharmony_ci if (bY) asyh->view.oH -= (bY * 2); 1728c2ecf20Sopenharmony_ci else asyh->view.oH = ((asyh->view.oW * r) + (r / 2)) >> 19; 1738c2ecf20Sopenharmony_ci } else { 1748c2ecf20Sopenharmony_ci asyh->view.oW -= (asyh->view.oW >> 4) + 32; 1758c2ecf20Sopenharmony_ci if (bY) asyh->view.oH -= (bY * 2); 1768c2ecf20Sopenharmony_ci else asyh->view.oH = ((asyh->view.oW * r) + (r / 2)) >> 19; 1778c2ecf20Sopenharmony_ci } 1788c2ecf20Sopenharmony_ci } 1798c2ecf20Sopenharmony_ci 1808c2ecf20Sopenharmony_ci /* Handle CENTER/ASPECT scaling, taking into account the areas 1818c2ecf20Sopenharmony_ci * removed already for overscan compensation. 1828c2ecf20Sopenharmony_ci */ 1838c2ecf20Sopenharmony_ci switch (mode) { 1848c2ecf20Sopenharmony_ci case DRM_MODE_SCALE_CENTER: 1858c2ecf20Sopenharmony_ci /* NOTE: This will cause scaling when the input is 1868c2ecf20Sopenharmony_ci * larger than the output. 1878c2ecf20Sopenharmony_ci */ 1888c2ecf20Sopenharmony_ci asyh->view.oW = min(asyh->view.iW, asyh->view.oW); 1898c2ecf20Sopenharmony_ci asyh->view.oH = min(asyh->view.iH, asyh->view.oH); 1908c2ecf20Sopenharmony_ci break; 1918c2ecf20Sopenharmony_ci case DRM_MODE_SCALE_ASPECT: 1928c2ecf20Sopenharmony_ci /* Determine whether the scaling should be on width or on 1938c2ecf20Sopenharmony_ci * height. This is done by comparing the aspect ratios of the 1948c2ecf20Sopenharmony_ci * sizes. If the output AR is larger than input AR, that means 1958c2ecf20Sopenharmony_ci * we want to change the width (letterboxed on the 1968c2ecf20Sopenharmony_ci * left/right), otherwise on the height (letterboxed on the 1978c2ecf20Sopenharmony_ci * top/bottom). 1988c2ecf20Sopenharmony_ci * 1998c2ecf20Sopenharmony_ci * E.g. 4:3 (1.333) AR image displayed on a 16:10 (1.6) AR 2008c2ecf20Sopenharmony_ci * screen will have letterboxes on the left/right. However a 2018c2ecf20Sopenharmony_ci * 16:9 (1.777) AR image on that same screen will have 2028c2ecf20Sopenharmony_ci * letterboxes on the top/bottom. 2038c2ecf20Sopenharmony_ci * 2048c2ecf20Sopenharmony_ci * inputAR = iW / iH; outputAR = oW / oH 2058c2ecf20Sopenharmony_ci * outputAR > inputAR is equivalent to oW * iH > iW * oH 2068c2ecf20Sopenharmony_ci */ 2078c2ecf20Sopenharmony_ci if (asyh->view.oW * asyh->view.iH > asyh->view.iW * asyh->view.oH) { 2088c2ecf20Sopenharmony_ci /* Recompute output width, i.e. left/right letterbox */ 2098c2ecf20Sopenharmony_ci u32 r = (asyh->view.iW << 19) / asyh->view.iH; 2108c2ecf20Sopenharmony_ci asyh->view.oW = ((asyh->view.oH * r) + (r / 2)) >> 19; 2118c2ecf20Sopenharmony_ci } else { 2128c2ecf20Sopenharmony_ci /* Recompute output height, i.e. top/bottom letterbox */ 2138c2ecf20Sopenharmony_ci u32 r = (asyh->view.iH << 19) / asyh->view.iW; 2148c2ecf20Sopenharmony_ci asyh->view.oH = ((asyh->view.oW * r) + (r / 2)) >> 19; 2158c2ecf20Sopenharmony_ci } 2168c2ecf20Sopenharmony_ci break; 2178c2ecf20Sopenharmony_ci default: 2188c2ecf20Sopenharmony_ci break; 2198c2ecf20Sopenharmony_ci } 2208c2ecf20Sopenharmony_ci 2218c2ecf20Sopenharmony_ci asyh->set.view = true; 2228c2ecf20Sopenharmony_ci} 2238c2ecf20Sopenharmony_ci 2248c2ecf20Sopenharmony_cistatic int 2258c2ecf20Sopenharmony_cinv50_head_atomic_check_lut(struct nv50_head *head, 2268c2ecf20Sopenharmony_ci struct nv50_head_atom *asyh) 2278c2ecf20Sopenharmony_ci{ 2288c2ecf20Sopenharmony_ci struct nv50_disp *disp = nv50_disp(head->base.base.dev); 2298c2ecf20Sopenharmony_ci struct drm_property_blob *olut = asyh->state.gamma_lut; 2308c2ecf20Sopenharmony_ci int size; 2318c2ecf20Sopenharmony_ci 2328c2ecf20Sopenharmony_ci /* Determine whether core output LUT should be enabled. */ 2338c2ecf20Sopenharmony_ci if (olut) { 2348c2ecf20Sopenharmony_ci /* Check if any window(s) have stolen the core output LUT 2358c2ecf20Sopenharmony_ci * to as an input LUT for legacy gamma + I8 colour format. 2368c2ecf20Sopenharmony_ci */ 2378c2ecf20Sopenharmony_ci if (asyh->wndw.olut) { 2388c2ecf20Sopenharmony_ci /* If any window has stolen the core output LUT, 2398c2ecf20Sopenharmony_ci * all of them must. 2408c2ecf20Sopenharmony_ci */ 2418c2ecf20Sopenharmony_ci if (asyh->wndw.olut != asyh->wndw.mask) 2428c2ecf20Sopenharmony_ci return -EINVAL; 2438c2ecf20Sopenharmony_ci olut = NULL; 2448c2ecf20Sopenharmony_ci } 2458c2ecf20Sopenharmony_ci } 2468c2ecf20Sopenharmony_ci 2478c2ecf20Sopenharmony_ci if (!olut) { 2488c2ecf20Sopenharmony_ci if (!head->func->olut_identity) { 2498c2ecf20Sopenharmony_ci asyh->olut.handle = 0; 2508c2ecf20Sopenharmony_ci return 0; 2518c2ecf20Sopenharmony_ci } 2528c2ecf20Sopenharmony_ci size = 0; 2538c2ecf20Sopenharmony_ci } else { 2548c2ecf20Sopenharmony_ci size = drm_color_lut_size(olut); 2558c2ecf20Sopenharmony_ci } 2568c2ecf20Sopenharmony_ci 2578c2ecf20Sopenharmony_ci if (!head->func->olut(head, asyh, size)) { 2588c2ecf20Sopenharmony_ci DRM_DEBUG_KMS("Invalid olut\n"); 2598c2ecf20Sopenharmony_ci return -EINVAL; 2608c2ecf20Sopenharmony_ci } 2618c2ecf20Sopenharmony_ci asyh->olut.handle = disp->core->chan.vram.handle; 2628c2ecf20Sopenharmony_ci asyh->olut.buffer = !asyh->olut.buffer; 2638c2ecf20Sopenharmony_ci 2648c2ecf20Sopenharmony_ci return 0; 2658c2ecf20Sopenharmony_ci} 2668c2ecf20Sopenharmony_ci 2678c2ecf20Sopenharmony_cistatic void 2688c2ecf20Sopenharmony_cinv50_head_atomic_check_mode(struct nv50_head *head, struct nv50_head_atom *asyh) 2698c2ecf20Sopenharmony_ci{ 2708c2ecf20Sopenharmony_ci struct drm_display_mode *mode = &asyh->state.adjusted_mode; 2718c2ecf20Sopenharmony_ci struct nv50_head_mode *m = &asyh->mode; 2728c2ecf20Sopenharmony_ci u32 blankus; 2738c2ecf20Sopenharmony_ci 2748c2ecf20Sopenharmony_ci drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V | CRTC_STEREO_DOUBLE); 2758c2ecf20Sopenharmony_ci 2768c2ecf20Sopenharmony_ci /* 2778c2ecf20Sopenharmony_ci * DRM modes are defined in terms of a repeating interval 2788c2ecf20Sopenharmony_ci * starting with the active display area. The hardware modes 2798c2ecf20Sopenharmony_ci * are defined in terms of a repeating interval starting one 2808c2ecf20Sopenharmony_ci * unit (pixel or line) into the sync pulse. So, add bias. 2818c2ecf20Sopenharmony_ci */ 2828c2ecf20Sopenharmony_ci 2838c2ecf20Sopenharmony_ci m->h.active = mode->crtc_htotal; 2848c2ecf20Sopenharmony_ci m->h.synce = mode->crtc_hsync_end - mode->crtc_hsync_start - 1; 2858c2ecf20Sopenharmony_ci m->h.blanke = mode->crtc_hblank_end - mode->crtc_hsync_start - 1; 2868c2ecf20Sopenharmony_ci m->h.blanks = m->h.blanke + mode->crtc_hdisplay; 2878c2ecf20Sopenharmony_ci 2888c2ecf20Sopenharmony_ci m->v.active = mode->crtc_vtotal; 2898c2ecf20Sopenharmony_ci m->v.synce = mode->crtc_vsync_end - mode->crtc_vsync_start - 1; 2908c2ecf20Sopenharmony_ci m->v.blanke = mode->crtc_vblank_end - mode->crtc_vsync_start - 1; 2918c2ecf20Sopenharmony_ci m->v.blanks = m->v.blanke + mode->crtc_vdisplay; 2928c2ecf20Sopenharmony_ci 2938c2ecf20Sopenharmony_ci /*XXX: Safe underestimate, even "0" works */ 2948c2ecf20Sopenharmony_ci blankus = (m->v.active - mode->crtc_vdisplay - 2) * m->h.active; 2958c2ecf20Sopenharmony_ci blankus *= 1000; 2968c2ecf20Sopenharmony_ci blankus /= mode->crtc_clock; 2978c2ecf20Sopenharmony_ci m->v.blankus = blankus; 2988c2ecf20Sopenharmony_ci 2998c2ecf20Sopenharmony_ci if (mode->flags & DRM_MODE_FLAG_INTERLACE) { 3008c2ecf20Sopenharmony_ci m->v.blank2e = m->v.active + m->v.blanke; 3018c2ecf20Sopenharmony_ci m->v.blank2s = m->v.blank2e + mode->crtc_vdisplay; 3028c2ecf20Sopenharmony_ci m->v.active = (m->v.active * 2) + 1; 3038c2ecf20Sopenharmony_ci m->interlace = true; 3048c2ecf20Sopenharmony_ci } else { 3058c2ecf20Sopenharmony_ci m->v.blank2e = 0; 3068c2ecf20Sopenharmony_ci m->v.blank2s = 1; 3078c2ecf20Sopenharmony_ci m->interlace = false; 3088c2ecf20Sopenharmony_ci } 3098c2ecf20Sopenharmony_ci m->clock = mode->crtc_clock; 3108c2ecf20Sopenharmony_ci 3118c2ecf20Sopenharmony_ci asyh->or.nhsync = !!(mode->flags & DRM_MODE_FLAG_NHSYNC); 3128c2ecf20Sopenharmony_ci asyh->or.nvsync = !!(mode->flags & DRM_MODE_FLAG_NVSYNC); 3138c2ecf20Sopenharmony_ci asyh->set.or = head->func->or != NULL; 3148c2ecf20Sopenharmony_ci asyh->set.mode = true; 3158c2ecf20Sopenharmony_ci} 3168c2ecf20Sopenharmony_ci 3178c2ecf20Sopenharmony_cistatic int 3188c2ecf20Sopenharmony_cinv50_head_atomic_check(struct drm_crtc *crtc, struct drm_crtc_state *state) 3198c2ecf20Sopenharmony_ci{ 3208c2ecf20Sopenharmony_ci struct nouveau_drm *drm = nouveau_drm(crtc->dev); 3218c2ecf20Sopenharmony_ci struct nv50_head *head = nv50_head(crtc); 3228c2ecf20Sopenharmony_ci struct nv50_head_atom *armh = nv50_head_atom(crtc->state); 3238c2ecf20Sopenharmony_ci struct nv50_head_atom *asyh = nv50_head_atom(state); 3248c2ecf20Sopenharmony_ci struct nouveau_conn_atom *asyc = NULL; 3258c2ecf20Sopenharmony_ci struct drm_connector_state *conns; 3268c2ecf20Sopenharmony_ci struct drm_connector *conn; 3278c2ecf20Sopenharmony_ci int i, ret; 3288c2ecf20Sopenharmony_ci 3298c2ecf20Sopenharmony_ci NV_ATOMIC(drm, "%s atomic_check %d\n", crtc->name, asyh->state.active); 3308c2ecf20Sopenharmony_ci if (asyh->state.active) { 3318c2ecf20Sopenharmony_ci for_each_new_connector_in_state(asyh->state.state, conn, conns, i) { 3328c2ecf20Sopenharmony_ci if (conns->crtc == crtc) { 3338c2ecf20Sopenharmony_ci asyc = nouveau_conn_atom(conns); 3348c2ecf20Sopenharmony_ci break; 3358c2ecf20Sopenharmony_ci } 3368c2ecf20Sopenharmony_ci } 3378c2ecf20Sopenharmony_ci 3388c2ecf20Sopenharmony_ci if (armh->state.active) { 3398c2ecf20Sopenharmony_ci if (asyc) { 3408c2ecf20Sopenharmony_ci if (asyh->state.mode_changed) 3418c2ecf20Sopenharmony_ci asyc->set.scaler = true; 3428c2ecf20Sopenharmony_ci if (armh->base.depth != asyh->base.depth) 3438c2ecf20Sopenharmony_ci asyc->set.dither = true; 3448c2ecf20Sopenharmony_ci } 3458c2ecf20Sopenharmony_ci } else { 3468c2ecf20Sopenharmony_ci if (asyc) 3478c2ecf20Sopenharmony_ci asyc->set.mask = ~0; 3488c2ecf20Sopenharmony_ci asyh->set.mask = ~0; 3498c2ecf20Sopenharmony_ci asyh->set.or = head->func->or != NULL; 3508c2ecf20Sopenharmony_ci } 3518c2ecf20Sopenharmony_ci 3528c2ecf20Sopenharmony_ci if (asyh->state.mode_changed || asyh->state.connectors_changed) 3538c2ecf20Sopenharmony_ci nv50_head_atomic_check_mode(head, asyh); 3548c2ecf20Sopenharmony_ci 3558c2ecf20Sopenharmony_ci if (asyh->state.color_mgmt_changed || 3568c2ecf20Sopenharmony_ci memcmp(&armh->wndw, &asyh->wndw, sizeof(asyh->wndw))) { 3578c2ecf20Sopenharmony_ci int ret = nv50_head_atomic_check_lut(head, asyh); 3588c2ecf20Sopenharmony_ci if (ret) 3598c2ecf20Sopenharmony_ci return ret; 3608c2ecf20Sopenharmony_ci 3618c2ecf20Sopenharmony_ci asyh->olut.visible = asyh->olut.handle != 0; 3628c2ecf20Sopenharmony_ci } 3638c2ecf20Sopenharmony_ci 3648c2ecf20Sopenharmony_ci if (asyc) { 3658c2ecf20Sopenharmony_ci if (asyc->set.scaler) 3668c2ecf20Sopenharmony_ci nv50_head_atomic_check_view(armh, asyh, asyc); 3678c2ecf20Sopenharmony_ci if (asyc->set.dither) 3688c2ecf20Sopenharmony_ci nv50_head_atomic_check_dither(armh, asyh, asyc); 3698c2ecf20Sopenharmony_ci if (asyc->set.procamp) 3708c2ecf20Sopenharmony_ci nv50_head_atomic_check_procamp(armh, asyh, asyc); 3718c2ecf20Sopenharmony_ci } 3728c2ecf20Sopenharmony_ci 3738c2ecf20Sopenharmony_ci if (head->func->core_calc) { 3748c2ecf20Sopenharmony_ci head->func->core_calc(head, asyh); 3758c2ecf20Sopenharmony_ci if (!asyh->core.visible) 3768c2ecf20Sopenharmony_ci asyh->olut.visible = false; 3778c2ecf20Sopenharmony_ci } 3788c2ecf20Sopenharmony_ci 3798c2ecf20Sopenharmony_ci asyh->set.base = armh->base.cpp != asyh->base.cpp; 3808c2ecf20Sopenharmony_ci asyh->set.ovly = armh->ovly.cpp != asyh->ovly.cpp; 3818c2ecf20Sopenharmony_ci } else { 3828c2ecf20Sopenharmony_ci asyh->olut.visible = false; 3838c2ecf20Sopenharmony_ci asyh->core.visible = false; 3848c2ecf20Sopenharmony_ci asyh->curs.visible = false; 3858c2ecf20Sopenharmony_ci asyh->base.cpp = 0; 3868c2ecf20Sopenharmony_ci asyh->ovly.cpp = 0; 3878c2ecf20Sopenharmony_ci } 3888c2ecf20Sopenharmony_ci 3898c2ecf20Sopenharmony_ci if (!drm_atomic_crtc_needs_modeset(&asyh->state)) { 3908c2ecf20Sopenharmony_ci if (asyh->core.visible) { 3918c2ecf20Sopenharmony_ci if (memcmp(&armh->core, &asyh->core, sizeof(asyh->core))) 3928c2ecf20Sopenharmony_ci asyh->set.core = true; 3938c2ecf20Sopenharmony_ci } else 3948c2ecf20Sopenharmony_ci if (armh->core.visible) { 3958c2ecf20Sopenharmony_ci asyh->clr.core = true; 3968c2ecf20Sopenharmony_ci } 3978c2ecf20Sopenharmony_ci 3988c2ecf20Sopenharmony_ci if (asyh->curs.visible) { 3998c2ecf20Sopenharmony_ci if (memcmp(&armh->curs, &asyh->curs, sizeof(asyh->curs))) 4008c2ecf20Sopenharmony_ci asyh->set.curs = true; 4018c2ecf20Sopenharmony_ci } else 4028c2ecf20Sopenharmony_ci if (armh->curs.visible) { 4038c2ecf20Sopenharmony_ci asyh->clr.curs = true; 4048c2ecf20Sopenharmony_ci } 4058c2ecf20Sopenharmony_ci 4068c2ecf20Sopenharmony_ci if (asyh->olut.visible) { 4078c2ecf20Sopenharmony_ci if (memcmp(&armh->olut, &asyh->olut, sizeof(asyh->olut))) 4088c2ecf20Sopenharmony_ci asyh->set.olut = true; 4098c2ecf20Sopenharmony_ci } else 4108c2ecf20Sopenharmony_ci if (armh->olut.visible) { 4118c2ecf20Sopenharmony_ci asyh->clr.olut = true; 4128c2ecf20Sopenharmony_ci } 4138c2ecf20Sopenharmony_ci } else { 4148c2ecf20Sopenharmony_ci asyh->clr.olut = armh->olut.visible; 4158c2ecf20Sopenharmony_ci asyh->clr.core = armh->core.visible; 4168c2ecf20Sopenharmony_ci asyh->clr.curs = armh->curs.visible; 4178c2ecf20Sopenharmony_ci asyh->set.olut = asyh->olut.visible; 4188c2ecf20Sopenharmony_ci asyh->set.core = asyh->core.visible; 4198c2ecf20Sopenharmony_ci asyh->set.curs = asyh->curs.visible; 4208c2ecf20Sopenharmony_ci } 4218c2ecf20Sopenharmony_ci 4228c2ecf20Sopenharmony_ci ret = nv50_crc_atomic_check_head(head, asyh, armh); 4238c2ecf20Sopenharmony_ci if (ret) 4248c2ecf20Sopenharmony_ci return ret; 4258c2ecf20Sopenharmony_ci 4268c2ecf20Sopenharmony_ci if (asyh->clr.mask || asyh->set.mask) 4278c2ecf20Sopenharmony_ci nv50_atom(asyh->state.state)->lock_core = true; 4288c2ecf20Sopenharmony_ci return 0; 4298c2ecf20Sopenharmony_ci} 4308c2ecf20Sopenharmony_ci 4318c2ecf20Sopenharmony_cistatic const struct drm_crtc_helper_funcs 4328c2ecf20Sopenharmony_cinv50_head_help = { 4338c2ecf20Sopenharmony_ci .atomic_check = nv50_head_atomic_check, 4348c2ecf20Sopenharmony_ci .get_scanout_position = nouveau_display_scanoutpos, 4358c2ecf20Sopenharmony_ci}; 4368c2ecf20Sopenharmony_ci 4378c2ecf20Sopenharmony_cistatic void 4388c2ecf20Sopenharmony_cinv50_head_atomic_destroy_state(struct drm_crtc *crtc, 4398c2ecf20Sopenharmony_ci struct drm_crtc_state *state) 4408c2ecf20Sopenharmony_ci{ 4418c2ecf20Sopenharmony_ci struct nv50_head_atom *asyh = nv50_head_atom(state); 4428c2ecf20Sopenharmony_ci __drm_atomic_helper_crtc_destroy_state(&asyh->state); 4438c2ecf20Sopenharmony_ci kfree(asyh); 4448c2ecf20Sopenharmony_ci} 4458c2ecf20Sopenharmony_ci 4468c2ecf20Sopenharmony_cistatic struct drm_crtc_state * 4478c2ecf20Sopenharmony_cinv50_head_atomic_duplicate_state(struct drm_crtc *crtc) 4488c2ecf20Sopenharmony_ci{ 4498c2ecf20Sopenharmony_ci struct nv50_head_atom *armh = nv50_head_atom(crtc->state); 4508c2ecf20Sopenharmony_ci struct nv50_head_atom *asyh; 4518c2ecf20Sopenharmony_ci if (!(asyh = kmalloc(sizeof(*asyh), GFP_KERNEL))) 4528c2ecf20Sopenharmony_ci return NULL; 4538c2ecf20Sopenharmony_ci __drm_atomic_helper_crtc_duplicate_state(crtc, &asyh->state); 4548c2ecf20Sopenharmony_ci asyh->wndw = armh->wndw; 4558c2ecf20Sopenharmony_ci asyh->view = armh->view; 4568c2ecf20Sopenharmony_ci asyh->mode = armh->mode; 4578c2ecf20Sopenharmony_ci asyh->olut = armh->olut; 4588c2ecf20Sopenharmony_ci asyh->core = armh->core; 4598c2ecf20Sopenharmony_ci asyh->curs = armh->curs; 4608c2ecf20Sopenharmony_ci asyh->base = armh->base; 4618c2ecf20Sopenharmony_ci asyh->ovly = armh->ovly; 4628c2ecf20Sopenharmony_ci asyh->dither = armh->dither; 4638c2ecf20Sopenharmony_ci asyh->procamp = armh->procamp; 4648c2ecf20Sopenharmony_ci asyh->crc = armh->crc; 4658c2ecf20Sopenharmony_ci asyh->or = armh->or; 4668c2ecf20Sopenharmony_ci asyh->dp = armh->dp; 4678c2ecf20Sopenharmony_ci asyh->clr.mask = 0; 4688c2ecf20Sopenharmony_ci asyh->set.mask = 0; 4698c2ecf20Sopenharmony_ci return &asyh->state; 4708c2ecf20Sopenharmony_ci} 4718c2ecf20Sopenharmony_ci 4728c2ecf20Sopenharmony_cistatic void 4738c2ecf20Sopenharmony_cinv50_head_reset(struct drm_crtc *crtc) 4748c2ecf20Sopenharmony_ci{ 4758c2ecf20Sopenharmony_ci struct nv50_head_atom *asyh; 4768c2ecf20Sopenharmony_ci 4778c2ecf20Sopenharmony_ci if (WARN_ON(!(asyh = kzalloc(sizeof(*asyh), GFP_KERNEL)))) 4788c2ecf20Sopenharmony_ci return; 4798c2ecf20Sopenharmony_ci 4808c2ecf20Sopenharmony_ci if (crtc->state) 4818c2ecf20Sopenharmony_ci nv50_head_atomic_destroy_state(crtc, crtc->state); 4828c2ecf20Sopenharmony_ci 4838c2ecf20Sopenharmony_ci __drm_atomic_helper_crtc_reset(crtc, &asyh->state); 4848c2ecf20Sopenharmony_ci} 4858c2ecf20Sopenharmony_ci 4868c2ecf20Sopenharmony_cistatic int 4878c2ecf20Sopenharmony_cinv50_head_late_register(struct drm_crtc *crtc) 4888c2ecf20Sopenharmony_ci{ 4898c2ecf20Sopenharmony_ci return nv50_head_crc_late_register(nv50_head(crtc)); 4908c2ecf20Sopenharmony_ci} 4918c2ecf20Sopenharmony_ci 4928c2ecf20Sopenharmony_cistatic void 4938c2ecf20Sopenharmony_cinv50_head_destroy(struct drm_crtc *crtc) 4948c2ecf20Sopenharmony_ci{ 4958c2ecf20Sopenharmony_ci struct nv50_head *head = nv50_head(crtc); 4968c2ecf20Sopenharmony_ci 4978c2ecf20Sopenharmony_ci nvif_notify_dtor(&head->base.vblank); 4988c2ecf20Sopenharmony_ci nv50_lut_fini(&head->olut); 4998c2ecf20Sopenharmony_ci drm_crtc_cleanup(crtc); 5008c2ecf20Sopenharmony_ci kfree(head); 5018c2ecf20Sopenharmony_ci} 5028c2ecf20Sopenharmony_ci 5038c2ecf20Sopenharmony_cistatic const struct drm_crtc_funcs 5048c2ecf20Sopenharmony_cinv50_head_func = { 5058c2ecf20Sopenharmony_ci .reset = nv50_head_reset, 5068c2ecf20Sopenharmony_ci .gamma_set = drm_atomic_helper_legacy_gamma_set, 5078c2ecf20Sopenharmony_ci .destroy = nv50_head_destroy, 5088c2ecf20Sopenharmony_ci .set_config = drm_atomic_helper_set_config, 5098c2ecf20Sopenharmony_ci .page_flip = drm_atomic_helper_page_flip, 5108c2ecf20Sopenharmony_ci .atomic_duplicate_state = nv50_head_atomic_duplicate_state, 5118c2ecf20Sopenharmony_ci .atomic_destroy_state = nv50_head_atomic_destroy_state, 5128c2ecf20Sopenharmony_ci .enable_vblank = nouveau_display_vblank_enable, 5138c2ecf20Sopenharmony_ci .disable_vblank = nouveau_display_vblank_disable, 5148c2ecf20Sopenharmony_ci .get_vblank_timestamp = drm_crtc_vblank_helper_get_vblank_timestamp, 5158c2ecf20Sopenharmony_ci .late_register = nv50_head_late_register, 5168c2ecf20Sopenharmony_ci}; 5178c2ecf20Sopenharmony_ci 5188c2ecf20Sopenharmony_cistatic const struct drm_crtc_funcs 5198c2ecf20Sopenharmony_cinvd9_head_func = { 5208c2ecf20Sopenharmony_ci .reset = nv50_head_reset, 5218c2ecf20Sopenharmony_ci .gamma_set = drm_atomic_helper_legacy_gamma_set, 5228c2ecf20Sopenharmony_ci .destroy = nv50_head_destroy, 5238c2ecf20Sopenharmony_ci .set_config = drm_atomic_helper_set_config, 5248c2ecf20Sopenharmony_ci .page_flip = drm_atomic_helper_page_flip, 5258c2ecf20Sopenharmony_ci .atomic_duplicate_state = nv50_head_atomic_duplicate_state, 5268c2ecf20Sopenharmony_ci .atomic_destroy_state = nv50_head_atomic_destroy_state, 5278c2ecf20Sopenharmony_ci .enable_vblank = nouveau_display_vblank_enable, 5288c2ecf20Sopenharmony_ci .disable_vblank = nouveau_display_vblank_disable, 5298c2ecf20Sopenharmony_ci .get_vblank_timestamp = drm_crtc_vblank_helper_get_vblank_timestamp, 5308c2ecf20Sopenharmony_ci .verify_crc_source = nv50_crc_verify_source, 5318c2ecf20Sopenharmony_ci .get_crc_sources = nv50_crc_get_sources, 5328c2ecf20Sopenharmony_ci .set_crc_source = nv50_crc_set_source, 5338c2ecf20Sopenharmony_ci .late_register = nv50_head_late_register, 5348c2ecf20Sopenharmony_ci}; 5358c2ecf20Sopenharmony_ci 5368c2ecf20Sopenharmony_cistatic int nv50_head_vblank_handler(struct nvif_notify *notify) 5378c2ecf20Sopenharmony_ci{ 5388c2ecf20Sopenharmony_ci struct nouveau_crtc *nv_crtc = 5398c2ecf20Sopenharmony_ci container_of(notify, struct nouveau_crtc, vblank); 5408c2ecf20Sopenharmony_ci 5418c2ecf20Sopenharmony_ci if (drm_crtc_handle_vblank(&nv_crtc->base)) 5428c2ecf20Sopenharmony_ci nv50_crc_handle_vblank(nv50_head(&nv_crtc->base)); 5438c2ecf20Sopenharmony_ci 5448c2ecf20Sopenharmony_ci return NVIF_NOTIFY_KEEP; 5458c2ecf20Sopenharmony_ci} 5468c2ecf20Sopenharmony_ci 5478c2ecf20Sopenharmony_cistruct nv50_head * 5488c2ecf20Sopenharmony_cinv50_head_create(struct drm_device *dev, int index) 5498c2ecf20Sopenharmony_ci{ 5508c2ecf20Sopenharmony_ci struct nouveau_drm *drm = nouveau_drm(dev); 5518c2ecf20Sopenharmony_ci struct nv50_disp *disp = nv50_disp(dev); 5528c2ecf20Sopenharmony_ci struct nv50_head *head; 5538c2ecf20Sopenharmony_ci struct nv50_wndw *base, *ovly, *curs; 5548c2ecf20Sopenharmony_ci struct nouveau_crtc *nv_crtc; 5558c2ecf20Sopenharmony_ci struct drm_crtc *crtc; 5568c2ecf20Sopenharmony_ci const struct drm_crtc_funcs *funcs; 5578c2ecf20Sopenharmony_ci int ret; 5588c2ecf20Sopenharmony_ci 5598c2ecf20Sopenharmony_ci head = kzalloc(sizeof(*head), GFP_KERNEL); 5608c2ecf20Sopenharmony_ci if (!head) 5618c2ecf20Sopenharmony_ci return ERR_PTR(-ENOMEM); 5628c2ecf20Sopenharmony_ci 5638c2ecf20Sopenharmony_ci head->func = disp->core->func->head; 5648c2ecf20Sopenharmony_ci head->base.index = index; 5658c2ecf20Sopenharmony_ci 5668c2ecf20Sopenharmony_ci if (disp->disp->object.oclass < GF110_DISP) 5678c2ecf20Sopenharmony_ci funcs = &nv50_head_func; 5688c2ecf20Sopenharmony_ci else 5698c2ecf20Sopenharmony_ci funcs = &nvd9_head_func; 5708c2ecf20Sopenharmony_ci 5718c2ecf20Sopenharmony_ci if (disp->disp->object.oclass < GV100_DISP) { 5728c2ecf20Sopenharmony_ci ret = nv50_base_new(drm, head->base.index, &base); 5738c2ecf20Sopenharmony_ci ret = nv50_ovly_new(drm, head->base.index, &ovly); 5748c2ecf20Sopenharmony_ci } else { 5758c2ecf20Sopenharmony_ci ret = nv50_wndw_new(drm, DRM_PLANE_TYPE_PRIMARY, 5768c2ecf20Sopenharmony_ci head->base.index * 2 + 0, &base); 5778c2ecf20Sopenharmony_ci ret = nv50_wndw_new(drm, DRM_PLANE_TYPE_OVERLAY, 5788c2ecf20Sopenharmony_ci head->base.index * 2 + 1, &ovly); 5798c2ecf20Sopenharmony_ci } 5808c2ecf20Sopenharmony_ci if (ret == 0) 5818c2ecf20Sopenharmony_ci ret = nv50_curs_new(drm, head->base.index, &curs); 5828c2ecf20Sopenharmony_ci if (ret) { 5838c2ecf20Sopenharmony_ci kfree(head); 5848c2ecf20Sopenharmony_ci return ERR_PTR(ret); 5858c2ecf20Sopenharmony_ci } 5868c2ecf20Sopenharmony_ci 5878c2ecf20Sopenharmony_ci nv_crtc = &head->base; 5888c2ecf20Sopenharmony_ci crtc = &nv_crtc->base; 5898c2ecf20Sopenharmony_ci drm_crtc_init_with_planes(dev, crtc, &base->plane, &curs->plane, 5908c2ecf20Sopenharmony_ci funcs, "head-%d", head->base.index); 5918c2ecf20Sopenharmony_ci drm_crtc_helper_add(crtc, &nv50_head_help); 5928c2ecf20Sopenharmony_ci /* Keep the legacy gamma size at 256 to avoid compatibility issues */ 5938c2ecf20Sopenharmony_ci drm_mode_crtc_set_gamma_size(crtc, 256); 5948c2ecf20Sopenharmony_ci drm_crtc_enable_color_mgmt(crtc, base->func->ilut_size, 5958c2ecf20Sopenharmony_ci disp->disp->object.oclass >= GF110_DISP, 5968c2ecf20Sopenharmony_ci head->func->olut_size); 5978c2ecf20Sopenharmony_ci 5988c2ecf20Sopenharmony_ci if (head->func->olut_set) { 5998c2ecf20Sopenharmony_ci ret = nv50_lut_init(disp, &drm->client.mmu, &head->olut); 6008c2ecf20Sopenharmony_ci if (ret) { 6018c2ecf20Sopenharmony_ci nv50_head_destroy(crtc); 6028c2ecf20Sopenharmony_ci return ERR_PTR(ret); 6038c2ecf20Sopenharmony_ci } 6048c2ecf20Sopenharmony_ci } 6058c2ecf20Sopenharmony_ci 6068c2ecf20Sopenharmony_ci ret = nvif_notify_ctor(&disp->disp->object, "kmsVbl", nv50_head_vblank_handler, 6078c2ecf20Sopenharmony_ci false, NV04_DISP_NTFY_VBLANK, 6088c2ecf20Sopenharmony_ci &(struct nvif_notify_head_req_v0) { 6098c2ecf20Sopenharmony_ci .head = nv_crtc->index, 6108c2ecf20Sopenharmony_ci }, 6118c2ecf20Sopenharmony_ci sizeof(struct nvif_notify_head_req_v0), 6128c2ecf20Sopenharmony_ci sizeof(struct nvif_notify_head_rep_v0), 6138c2ecf20Sopenharmony_ci &nv_crtc->vblank); 6148c2ecf20Sopenharmony_ci if (ret) 6158c2ecf20Sopenharmony_ci return ERR_PTR(ret); 6168c2ecf20Sopenharmony_ci 6178c2ecf20Sopenharmony_ci return head; 6188c2ecf20Sopenharmony_ci} 619