1c51669eaSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 2414c4531SDave Airlie /* 3414c4531SDave Airlie * Copyright 2010 Matt Turner. 4414c4531SDave Airlie * Copyright 2012 Red Hat 5414c4531SDave Airlie * 6414c4531SDave Airlie * Authors: Matthew Garrett 7414c4531SDave Airlie * Matt Turner 8414c4531SDave Airlie * Dave Airlie 9414c4531SDave Airlie */ 10414c4531SDave Airlie 11414c4531SDave Airlie #include <linux/delay.h> 127938f421SLucas De Marchi #include <linux/iosys-map.h> 13414c4531SDave Airlie 142d70b9a1SThomas Zimmermann #include <drm/drm_atomic.h> 1588fabb75SThomas Zimmermann #include <drm/drm_atomic_helper.h> 16913ec479SThomas Zimmermann #include <drm/drm_damage_helper.h> 172e367ad4SJani Nikula #include <drm/drm_edid.h> 18913ec479SThomas Zimmermann #include <drm/drm_format_helper.h> 199f397801SSam Ravnborg #include <drm/drm_fourcc.h> 20720cf96dSVille Syrjälä #include <drm/drm_framebuffer.h> 214862ffaeSThomas Zimmermann #include <drm/drm_gem_atomic_helper.h> 225635b7cfSThomas Zimmermann #include <drm/drm_gem_framebuffer_helper.h> 237e64f7c8SJocelyn Falempe #include <drm/drm_panic.h> 2488fabb75SThomas Zimmermann #include <drm/drm_print.h> 25414c4531SDave Airlie 26f2e99524SThomas Zimmermann #include "mgag200_ddc.h" 27414c4531SDave Airlie #include "mgag200_drv.h" 28414c4531SDave Airlie 29414c4531SDave Airlie /* 30414c4531SDave Airlie * This file contains setup code for the CRTC. 31414c4531SDave Airlie */ 32414c4531SDave Airlie 3311f9eb89SJocelyn Falempe void mgag200_crtc_set_gamma_linear(struct mga_device *mdev, 34c577b2f4SJocelyn Falempe const struct drm_format_info *format) 35414c4531SDave Airlie { 36414c4531SDave Airlie int i; 37414c4531SDave Airlie 38414c4531SDave Airlie WREG8(DAC_INDEX + MGA1064_INDEX, 0); 39414c4531SDave Airlie 40c577b2f4SJocelyn Falempe switch (format->format) { 41c577b2f4SJocelyn Falempe case DRM_FORMAT_RGB565: 42c577b2f4SJocelyn Falempe /* Use better interpolation, to take 32 values from 0 to 255 */ 43c577b2f4SJocelyn Falempe for (i = 0; i < MGAG200_LUT_SIZE / 8; i++) { 44c577b2f4SJocelyn Falempe WREG8(DAC_INDEX + MGA1064_COL_PAL, i * 8 + i / 4); 45c577b2f4SJocelyn Falempe WREG8(DAC_INDEX + MGA1064_COL_PAL, i * 4 + i / 16); 46c577b2f4SJocelyn Falempe WREG8(DAC_INDEX + MGA1064_COL_PAL, i * 8 + i / 4); 47de7500eaSEgbert Eich } 48c577b2f4SJocelyn Falempe /* Green has one more bit, so add padding with 0 for red and blue. */ 49c577b2f4SJocelyn Falempe for (i = MGAG200_LUT_SIZE / 8; i < MGAG200_LUT_SIZE / 4; i++) { 50c577b2f4SJocelyn Falempe WREG8(DAC_INDEX + MGA1064_COL_PAL, 0); 51c577b2f4SJocelyn Falempe WREG8(DAC_INDEX + MGA1064_COL_PAL, i * 4 + i / 16); 52c577b2f4SJocelyn Falempe WREG8(DAC_INDEX + MGA1064_COL_PAL, 0); 53de7500eaSEgbert Eich } 54c577b2f4SJocelyn Falempe break; 55c577b2f4SJocelyn Falempe case DRM_FORMAT_RGB888: 56c577b2f4SJocelyn Falempe case DRM_FORMAT_XRGB8888: 57414c4531SDave Airlie for (i = 0; i < MGAG200_LUT_SIZE; i++) { 58c577b2f4SJocelyn Falempe WREG8(DAC_INDEX + MGA1064_COL_PAL, i); 59c577b2f4SJocelyn Falempe WREG8(DAC_INDEX + MGA1064_COL_PAL, i); 60c577b2f4SJocelyn Falempe WREG8(DAC_INDEX + MGA1064_COL_PAL, i); 61c577b2f4SJocelyn Falempe } 62c577b2f4SJocelyn Falempe break; 63c577b2f4SJocelyn Falempe default: 64c577b2f4SJocelyn Falempe drm_warn_once(&mdev->base, "Unsupported format %p4cc for gamma correction\n", 65c577b2f4SJocelyn Falempe &format->format); 66c577b2f4SJocelyn Falempe break; 67c577b2f4SJocelyn Falempe } 68c577b2f4SJocelyn Falempe } 69c577b2f4SJocelyn Falempe 7011f9eb89SJocelyn Falempe void mgag200_crtc_set_gamma(struct mga_device *mdev, 71c577b2f4SJocelyn Falempe const struct drm_format_info *format, 72c577b2f4SJocelyn Falempe struct drm_color_lut *lut) 73c577b2f4SJocelyn Falempe { 74c577b2f4SJocelyn Falempe int i; 75c577b2f4SJocelyn Falempe 76c577b2f4SJocelyn Falempe WREG8(DAC_INDEX + MGA1064_INDEX, 0); 77c577b2f4SJocelyn Falempe 78c577b2f4SJocelyn Falempe switch (format->format) { 79c577b2f4SJocelyn Falempe case DRM_FORMAT_RGB565: 80c577b2f4SJocelyn Falempe /* Use better interpolation, to take 32 values from lut[0] to lut[255] */ 81c577b2f4SJocelyn Falempe for (i = 0; i < MGAG200_LUT_SIZE / 8; i++) { 82c577b2f4SJocelyn Falempe WREG8(DAC_INDEX + MGA1064_COL_PAL, lut[i * 8 + i / 4].red >> 8); 83c577b2f4SJocelyn Falempe WREG8(DAC_INDEX + MGA1064_COL_PAL, lut[i * 4 + i / 16].green >> 8); 84c577b2f4SJocelyn Falempe WREG8(DAC_INDEX + MGA1064_COL_PAL, lut[i * 8 + i / 4].blue >> 8); 85c577b2f4SJocelyn Falempe } 86c577b2f4SJocelyn Falempe /* Green has one more bit, so add padding with 0 for red and blue. */ 87c577b2f4SJocelyn Falempe for (i = MGAG200_LUT_SIZE / 8; i < MGAG200_LUT_SIZE / 4; i++) { 88c577b2f4SJocelyn Falempe WREG8(DAC_INDEX + MGA1064_COL_PAL, 0); 89c577b2f4SJocelyn Falempe WREG8(DAC_INDEX + MGA1064_COL_PAL, lut[i * 4 + i / 16].green >> 8); 90c577b2f4SJocelyn Falempe WREG8(DAC_INDEX + MGA1064_COL_PAL, 0); 91c577b2f4SJocelyn Falempe } 92c577b2f4SJocelyn Falempe break; 93c577b2f4SJocelyn Falempe case DRM_FORMAT_RGB888: 94c577b2f4SJocelyn Falempe case DRM_FORMAT_XRGB8888: 95c577b2f4SJocelyn Falempe for (i = 0; i < MGAG200_LUT_SIZE; i++) { 96c577b2f4SJocelyn Falempe WREG8(DAC_INDEX + MGA1064_COL_PAL, lut[i].red >> 8); 97c577b2f4SJocelyn Falempe WREG8(DAC_INDEX + MGA1064_COL_PAL, lut[i].green >> 8); 98c577b2f4SJocelyn Falempe WREG8(DAC_INDEX + MGA1064_COL_PAL, lut[i].blue >> 8); 99c577b2f4SJocelyn Falempe } 100c577b2f4SJocelyn Falempe break; 101c577b2f4SJocelyn Falempe default: 102c577b2f4SJocelyn Falempe drm_warn_once(&mdev->base, "Unsupported format %p4cc for gamma correction\n", 103c577b2f4SJocelyn Falempe &format->format); 104c577b2f4SJocelyn Falempe break; 105414c4531SDave Airlie } 106414c4531SDave Airlie } 107414c4531SDave Airlie 108414c4531SDave Airlie static inline void mga_wait_vsync(struct mga_device *mdev) 109414c4531SDave Airlie { 1103cdc0e8dSChristopher Harvey unsigned long timeout = jiffies + HZ/10; 111414c4531SDave Airlie unsigned int status = 0; 112414c4531SDave Airlie 113414c4531SDave Airlie do { 11468550521SThomas Zimmermann status = RREG32(MGAREG_STATUS); 1153cdc0e8dSChristopher Harvey } while ((status & 0x08) && time_before(jiffies, timeout)); 1163cdc0e8dSChristopher Harvey timeout = jiffies + HZ/10; 117414c4531SDave Airlie status = 0; 118414c4531SDave Airlie do { 11968550521SThomas Zimmermann status = RREG32(MGAREG_STATUS); 1203cdc0e8dSChristopher Harvey } while (!(status & 0x08) && time_before(jiffies, timeout)); 121414c4531SDave Airlie } 122414c4531SDave Airlie 123414c4531SDave Airlie static inline void mga_wait_busy(struct mga_device *mdev) 124414c4531SDave Airlie { 1253cdc0e8dSChristopher Harvey unsigned long timeout = jiffies + HZ; 126414c4531SDave Airlie unsigned int status = 0; 127414c4531SDave Airlie do { 12868550521SThomas Zimmermann status = RREG8(MGAREG_STATUS + 2); 1293cdc0e8dSChristopher Harvey } while ((status & 0x01) && time_before(jiffies, timeout)); 130414c4531SDave Airlie } 131414c4531SDave Airlie 1329f1d0366SChristopher Harvey /* 133d6237687SThomas Zimmermann * This is how the framebuffer base address is stored in g200 cards: 134d6237687SThomas Zimmermann * * Assume @offset is the gpu_addr variable of the framebuffer object 135d6237687SThomas Zimmermann * * Then addr is the number of _pixels_ (not bytes) from the start of 136d6237687SThomas Zimmermann * VRAM to the first pixel we want to display. (divided by 2 for 32bit 137d6237687SThomas Zimmermann * framebuffers) 138d6237687SThomas Zimmermann * * addr is stored in the CRTCEXT0, CRTCC and CRTCD registers 139d6237687SThomas Zimmermann * addr<20> -> CRTCEXT0<6> 140d6237687SThomas Zimmermann * addr<19-16> -> CRTCEXT0<3-0> 141d6237687SThomas Zimmermann * addr<15-8> -> CRTCC<7-0> 142d6237687SThomas Zimmermann * addr<7-0> -> CRTCD<7-0> 143d6237687SThomas Zimmermann * 144d6237687SThomas Zimmermann * CRTCEXT0 has to be programmed last to trigger an update and make the 145d6237687SThomas Zimmermann * new addr variable take effect. 1469f1d0366SChristopher Harvey */ 147d6237687SThomas Zimmermann static void mgag200_set_startadd(struct mga_device *mdev, 148d6237687SThomas Zimmermann unsigned long offset) 149414c4531SDave Airlie { 150832eddf5SThomas Zimmermann struct drm_device *dev = &mdev->base; 151d6237687SThomas Zimmermann u32 startadd; 152d6237687SThomas Zimmermann u8 crtcc, crtcd, crtcext0; 153414c4531SDave Airlie 154d6237687SThomas Zimmermann startadd = offset / 8; 155414c4531SDave Airlie 156d2addf89SJocelyn Falempe if (startadd > 0) 15721e74bf9SThomas Zimmermann drm_WARN_ON_ONCE(dev, mdev->info->bug_no_startadd); 158d2addf89SJocelyn Falempe 159d6237687SThomas Zimmermann /* 160d6237687SThomas Zimmermann * Can't store addresses any higher than that, but we also 161d6237687SThomas Zimmermann * don't have more than 16 MiB of memory, so it should be fine. 162d6237687SThomas Zimmermann */ 163d6237687SThomas Zimmermann drm_WARN_ON(dev, startadd > 0x1fffff); 164414c4531SDave Airlie 165d6237687SThomas Zimmermann RREG_ECRT(0x00, crtcext0); 166d6237687SThomas Zimmermann 167d6237687SThomas Zimmermann crtcc = (startadd >> 8) & 0xff; 168d6237687SThomas Zimmermann crtcd = startadd & 0xff; 169d6237687SThomas Zimmermann crtcext0 &= 0xb0; 170d6237687SThomas Zimmermann crtcext0 |= ((startadd >> 14) & BIT(6)) | 171d6237687SThomas Zimmermann ((startadd >> 16) & 0x0f); 172d6237687SThomas Zimmermann 173d6237687SThomas Zimmermann WREG_CRT(0x0c, crtcc); 174d6237687SThomas Zimmermann WREG_CRT(0x0d, crtcd); 175d6237687SThomas Zimmermann WREG_ECRT(0x00, crtcext0); 176414c4531SDave Airlie } 177414c4531SDave Airlie 1781ee181feSThomas Zimmermann void mgag200_init_registers(struct mga_device *mdev) 1794f710d7cSThomas Zimmermann { 1809053cad2SThomas Zimmermann u8 crtc11, misc; 1814f710d7cSThomas Zimmermann 1824f710d7cSThomas Zimmermann WREG_SEQ(2, 0x0f); 1834f710d7cSThomas Zimmermann WREG_SEQ(3, 0x00); 1844f710d7cSThomas Zimmermann WREG_SEQ(4, 0x0e); 1854f710d7cSThomas Zimmermann 1864f710d7cSThomas Zimmermann WREG_CRT(10, 0); 1874f710d7cSThomas Zimmermann WREG_CRT(11, 0); 1884f710d7cSThomas Zimmermann WREG_CRT(12, 0); 1894f710d7cSThomas Zimmermann WREG_CRT(13, 0); 1904f710d7cSThomas Zimmermann WREG_CRT(14, 0); 1914f710d7cSThomas Zimmermann WREG_CRT(15, 0); 1924f710d7cSThomas Zimmermann 193da568d5eSThomas Zimmermann RREG_CRT(0x11, crtc11); 194da568d5eSThomas Zimmermann crtc11 &= ~(MGAREG_CRTC11_CRTCPROTECT | 195da568d5eSThomas Zimmermann MGAREG_CRTC11_VINTEN | 196da568d5eSThomas Zimmermann MGAREG_CRTC11_VINTCLR); 197da568d5eSThomas Zimmermann WREG_CRT(0x11, crtc11); 198da568d5eSThomas Zimmermann 1994f710d7cSThomas Zimmermann misc = RREG8(MGA_MISC_IN); 200b9fa77ecSThomas Zimmermann misc |= MGAREG_MISC_IOADSEL; 2014f710d7cSThomas Zimmermann WREG8(MGA_MISC_OUT, misc); 2024f710d7cSThomas Zimmermann } 2034f710d7cSThomas Zimmermann 204*cd3a2e8bSThomas Zimmermann void mgag200_set_mode_regs(struct mga_device *mdev, const struct drm_display_mode *mode, 205*cd3a2e8bSThomas Zimmermann bool set_vidrst) 206a6edae07SThomas Zimmermann { 207a6edae07SThomas Zimmermann unsigned int hdisplay, hsyncstart, hsyncend, htotal; 208a6edae07SThomas Zimmermann unsigned int vdisplay, vsyncstart, vsyncend, vtotal; 209db05f8d3SThomas Zimmermann u8 misc, crtcext1, crtcext2, crtcext5; 210a6edae07SThomas Zimmermann 211a6edae07SThomas Zimmermann hdisplay = mode->hdisplay / 8 - 1; 212a6edae07SThomas Zimmermann hsyncstart = mode->hsync_start / 8 - 1; 213a6edae07SThomas Zimmermann hsyncend = mode->hsync_end / 8 - 1; 214a6edae07SThomas Zimmermann htotal = mode->htotal / 8 - 1; 215a6edae07SThomas Zimmermann 216a6edae07SThomas Zimmermann /* Work around hardware quirk */ 217a6edae07SThomas Zimmermann if ((htotal & 0x07) == 0x06 || (htotal & 0x07) == 0x04) 218a6edae07SThomas Zimmermann htotal++; 219a6edae07SThomas Zimmermann 220a6edae07SThomas Zimmermann vdisplay = mode->vdisplay - 1; 221a6edae07SThomas Zimmermann vsyncstart = mode->vsync_start - 1; 222a6edae07SThomas Zimmermann vsyncend = mode->vsync_end - 1; 223a6edae07SThomas Zimmermann vtotal = mode->vtotal - 2; 224a6edae07SThomas Zimmermann 225db05f8d3SThomas Zimmermann misc = RREG8(MGA_MISC_IN); 226db05f8d3SThomas Zimmermann 227a6edae07SThomas Zimmermann if (mode->flags & DRM_MODE_FLAG_NHSYNC) 228db05f8d3SThomas Zimmermann misc |= MGAREG_MISC_HSYNCPOL; 229db05f8d3SThomas Zimmermann else 230db05f8d3SThomas Zimmermann misc &= ~MGAREG_MISC_HSYNCPOL; 231db05f8d3SThomas Zimmermann 232a6edae07SThomas Zimmermann if (mode->flags & DRM_MODE_FLAG_NVSYNC) 233db05f8d3SThomas Zimmermann misc |= MGAREG_MISC_VSYNCPOL; 234db05f8d3SThomas Zimmermann else 235db05f8d3SThomas Zimmermann misc &= ~MGAREG_MISC_VSYNCPOL; 236a6edae07SThomas Zimmermann 237a6edae07SThomas Zimmermann crtcext1 = (((htotal - 4) & 0x100) >> 8) | 238a6edae07SThomas Zimmermann ((hdisplay & 0x100) >> 7) | 239a6edae07SThomas Zimmermann ((hsyncstart & 0x100) >> 6) | 240a6edae07SThomas Zimmermann (htotal & 0x40); 241*cd3a2e8bSThomas Zimmermann if (set_vidrst) 242d1e40d8eSThomas Zimmermann crtcext1 |= MGAREG_CRTCEXT1_VRSTEN | 243d1e40d8eSThomas Zimmermann MGAREG_CRTCEXT1_HRSTEN; 244a6edae07SThomas Zimmermann 245a6edae07SThomas Zimmermann crtcext2 = ((vtotal & 0xc00) >> 10) | 246a6edae07SThomas Zimmermann ((vdisplay & 0x400) >> 8) | 247a6edae07SThomas Zimmermann ((vdisplay & 0xc00) >> 7) | 248a6edae07SThomas Zimmermann ((vsyncstart & 0xc00) >> 5) | 249a6edae07SThomas Zimmermann ((vdisplay & 0x400) >> 3); 250a6edae07SThomas Zimmermann crtcext5 = 0x00; 251a6edae07SThomas Zimmermann 252a6edae07SThomas Zimmermann WREG_CRT(0, htotal - 4); 253a6edae07SThomas Zimmermann WREG_CRT(1, hdisplay); 254a6edae07SThomas Zimmermann WREG_CRT(2, hdisplay); 255a6edae07SThomas Zimmermann WREG_CRT(3, (htotal & 0x1F) | 0x80); 256a6edae07SThomas Zimmermann WREG_CRT(4, hsyncstart); 257a6edae07SThomas Zimmermann WREG_CRT(5, ((htotal & 0x20) << 2) | (hsyncend & 0x1F)); 258a6edae07SThomas Zimmermann WREG_CRT(6, vtotal & 0xFF); 259a6edae07SThomas Zimmermann WREG_CRT(7, ((vtotal & 0x100) >> 8) | 260a6edae07SThomas Zimmermann ((vdisplay & 0x100) >> 7) | 261a6edae07SThomas Zimmermann ((vsyncstart & 0x100) >> 6) | 262a6edae07SThomas Zimmermann ((vdisplay & 0x100) >> 5) | 263a6edae07SThomas Zimmermann ((vdisplay & 0x100) >> 4) | /* linecomp */ 264a6edae07SThomas Zimmermann ((vtotal & 0x200) >> 4) | 265a6edae07SThomas Zimmermann ((vdisplay & 0x200) >> 3) | 266a6edae07SThomas Zimmermann ((vsyncstart & 0x200) >> 2)); 267a6edae07SThomas Zimmermann WREG_CRT(9, ((vdisplay & 0x200) >> 4) | 268a6edae07SThomas Zimmermann ((vdisplay & 0x200) >> 3)); 269a6edae07SThomas Zimmermann WREG_CRT(16, vsyncstart & 0xFF); 270a6edae07SThomas Zimmermann WREG_CRT(17, (vsyncend & 0x0F) | 0x20); 271a6edae07SThomas Zimmermann WREG_CRT(18, vdisplay & 0xFF); 272a6edae07SThomas Zimmermann WREG_CRT(20, 0); 273a6edae07SThomas Zimmermann WREG_CRT(21, vdisplay & 0xFF); 274a6edae07SThomas Zimmermann WREG_CRT(22, (vtotal + 1) & 0xFF); 275a6edae07SThomas Zimmermann WREG_CRT(23, 0xc3); 276a6edae07SThomas Zimmermann WREG_CRT(24, vdisplay & 0xFF); 277a6edae07SThomas Zimmermann 278a6edae07SThomas Zimmermann WREG_ECRT(0x01, crtcext1); 279a6edae07SThomas Zimmermann WREG_ECRT(0x02, crtcext2); 280a6edae07SThomas Zimmermann WREG_ECRT(0x05, crtcext5); 281db05f8d3SThomas Zimmermann 282db05f8d3SThomas Zimmermann WREG8(MGA_MISC_OUT, misc); 283a6edae07SThomas Zimmermann } 284a6edae07SThomas Zimmermann 285d9cc564bSThomas Zimmermann static u8 mgag200_get_bpp_shift(const struct drm_format_info *format) 28672a03a35SThomas Zimmermann { 287d9cc564bSThomas Zimmermann static const u8 bpp_shift[] = {0, 1, 0, 2}; 288d9cc564bSThomas Zimmermann 289d9cc564bSThomas Zimmermann return bpp_shift[format->cpp[0] - 1]; 29072a03a35SThomas Zimmermann } 29172a03a35SThomas Zimmermann 29272a03a35SThomas Zimmermann /* 29372a03a35SThomas Zimmermann * Calculates the HW offset value from the framebuffer's pitch. The 29472a03a35SThomas Zimmermann * offset is a multiple of the pixel size and depends on the display 29572a03a35SThomas Zimmermann * format. 29672a03a35SThomas Zimmermann */ 29772a03a35SThomas Zimmermann static u32 mgag200_calculate_offset(struct mga_device *mdev, 29872a03a35SThomas Zimmermann const struct drm_framebuffer *fb) 29972a03a35SThomas Zimmermann { 30072a03a35SThomas Zimmermann u32 offset = fb->pitches[0] / fb->format->cpp[0]; 301d9cc564bSThomas Zimmermann u8 bppshift = mgag200_get_bpp_shift(fb->format); 30272a03a35SThomas Zimmermann 30372a03a35SThomas Zimmermann if (fb->format->cpp[0] * 8 == 24) 30472a03a35SThomas Zimmermann offset = (offset * 3) >> (4 - bppshift); 30572a03a35SThomas Zimmermann else 30672a03a35SThomas Zimmermann offset = offset >> (4 - bppshift); 30772a03a35SThomas Zimmermann 30872a03a35SThomas Zimmermann return offset; 30972a03a35SThomas Zimmermann } 31072a03a35SThomas Zimmermann 31172a03a35SThomas Zimmermann static void mgag200_set_offset(struct mga_device *mdev, 31272a03a35SThomas Zimmermann const struct drm_framebuffer *fb) 31372a03a35SThomas Zimmermann { 31472a03a35SThomas Zimmermann u8 crtc13, crtcext0; 31572a03a35SThomas Zimmermann u32 offset = mgag200_calculate_offset(mdev, fb); 31672a03a35SThomas Zimmermann 31772a03a35SThomas Zimmermann RREG_ECRT(0, crtcext0); 31872a03a35SThomas Zimmermann 31972a03a35SThomas Zimmermann crtc13 = offset & 0xff; 32072a03a35SThomas Zimmermann 32172a03a35SThomas Zimmermann crtcext0 &= ~MGAREG_CRTCEXT0_OFFSET_MASK; 32272a03a35SThomas Zimmermann crtcext0 |= (offset >> 4) & MGAREG_CRTCEXT0_OFFSET_MASK; 32372a03a35SThomas Zimmermann 32472a03a35SThomas Zimmermann WREG_CRT(0x13, crtc13); 32572a03a35SThomas Zimmermann WREG_ECRT(0x00, crtcext0); 32672a03a35SThomas Zimmermann } 32772a03a35SThomas Zimmermann 328828369f2SThomas Zimmermann void mgag200_set_format_regs(struct mga_device *mdev, const struct drm_format_info *format) 329836d5368SThomas Zimmermann { 330832eddf5SThomas Zimmermann struct drm_device *dev = &mdev->base; 331836d5368SThomas Zimmermann unsigned int bpp, bppshift, scale; 332836d5368SThomas Zimmermann u8 crtcext3, xmulctrl; 333836d5368SThomas Zimmermann 334836d5368SThomas Zimmermann bpp = format->cpp[0] * 8; 335836d5368SThomas Zimmermann 336d9cc564bSThomas Zimmermann bppshift = mgag200_get_bpp_shift(format); 337836d5368SThomas Zimmermann switch (bpp) { 338836d5368SThomas Zimmermann case 24: 339836d5368SThomas Zimmermann scale = ((1 << bppshift) * 3) - 1; 340836d5368SThomas Zimmermann break; 341836d5368SThomas Zimmermann default: 342836d5368SThomas Zimmermann scale = (1 << bppshift) - 1; 343836d5368SThomas Zimmermann break; 344836d5368SThomas Zimmermann } 345836d5368SThomas Zimmermann 346836d5368SThomas Zimmermann RREG_ECRT(3, crtcext3); 347836d5368SThomas Zimmermann 348836d5368SThomas Zimmermann switch (bpp) { 349836d5368SThomas Zimmermann case 8: 350836d5368SThomas Zimmermann xmulctrl = MGA1064_MUL_CTL_8bits; 351836d5368SThomas Zimmermann break; 352836d5368SThomas Zimmermann case 16: 353836d5368SThomas Zimmermann if (format->depth == 15) 354836d5368SThomas Zimmermann xmulctrl = MGA1064_MUL_CTL_15bits; 355836d5368SThomas Zimmermann else 356836d5368SThomas Zimmermann xmulctrl = MGA1064_MUL_CTL_16bits; 357836d5368SThomas Zimmermann break; 358836d5368SThomas Zimmermann case 24: 359836d5368SThomas Zimmermann xmulctrl = MGA1064_MUL_CTL_24bits; 360836d5368SThomas Zimmermann break; 361836d5368SThomas Zimmermann case 32: 362836d5368SThomas Zimmermann xmulctrl = MGA1064_MUL_CTL_32_24bits; 363836d5368SThomas Zimmermann break; 364836d5368SThomas Zimmermann default: 365836d5368SThomas Zimmermann /* BUG: We should have caught this problem already. */ 366836d5368SThomas Zimmermann drm_WARN_ON(dev, "invalid format depth\n"); 367836d5368SThomas Zimmermann return; 368836d5368SThomas Zimmermann } 369836d5368SThomas Zimmermann 370836d5368SThomas Zimmermann crtcext3 &= ~GENMASK(2, 0); 371836d5368SThomas Zimmermann crtcext3 |= scale; 372836d5368SThomas Zimmermann 373836d5368SThomas Zimmermann WREG_DAC(MGA1064_MUL_CTL, xmulctrl); 374836d5368SThomas Zimmermann 375836d5368SThomas Zimmermann WREG_GFX(0, 0x00); 376836d5368SThomas Zimmermann WREG_GFX(1, 0x00); 377836d5368SThomas Zimmermann WREG_GFX(2, 0x00); 378836d5368SThomas Zimmermann WREG_GFX(3, 0x00); 379836d5368SThomas Zimmermann WREG_GFX(4, 0x00); 380836d5368SThomas Zimmermann WREG_GFX(5, 0x40); 381028a73e1SJocelyn Falempe /* GCTL6 should be 0x05, but we configure memmapsl to 0xb8000 (text mode), 382028a73e1SJocelyn Falempe * so that it doesn't hang when running kexec/kdump on G200_SE rev42. 383028a73e1SJocelyn Falempe */ 384028a73e1SJocelyn Falempe WREG_GFX(6, 0x0d); 385836d5368SThomas Zimmermann WREG_GFX(7, 0x0f); 386836d5368SThomas Zimmermann WREG_GFX(8, 0x0f); 387836d5368SThomas Zimmermann 388836d5368SThomas Zimmermann WREG_ECRT(3, crtcext3); 389836d5368SThomas Zimmermann } 390836d5368SThomas Zimmermann 391828369f2SThomas Zimmermann void mgag200_enable_display(struct mga_device *mdev) 392414c4531SDave Airlie { 3935cd062e3SThomas Zimmermann u8 seq0, crtcext1; 39470c3881eSThomas Zimmermann 39570c3881eSThomas Zimmermann RREG_SEQ(0x00, seq0); 39670c3881eSThomas Zimmermann seq0 |= MGAREG_SEQ0_SYNCRST | 39770c3881eSThomas Zimmermann MGAREG_SEQ0_ASYNCRST; 39870c3881eSThomas Zimmermann WREG_SEQ(0x00, seq0); 399414c4531SDave Airlie 400153fef41SThomas Zimmermann /* 401153fef41SThomas Zimmermann * TODO: replace busy waiting with vblank IRQ; put 402153fef41SThomas Zimmermann * msleep(50) before changing SCROFF 403153fef41SThomas Zimmermann */ 404414c4531SDave Airlie mga_wait_vsync(mdev); 405414c4531SDave Airlie mga_wait_busy(mdev); 406153fef41SThomas Zimmermann 407153fef41SThomas Zimmermann RREG_ECRT(0x01, crtcext1); 408153fef41SThomas Zimmermann crtcext1 &= ~MGAREG_CRTCEXT1_VSYNCOFF; 409153fef41SThomas Zimmermann crtcext1 &= ~MGAREG_CRTCEXT1_HSYNCOFF; 410153fef41SThomas Zimmermann WREG_ECRT(0x01, crtcext1); 411153fef41SThomas Zimmermann } 412153fef41SThomas Zimmermann 413153fef41SThomas Zimmermann static void mgag200_disable_display(struct mga_device *mdev) 414153fef41SThomas Zimmermann { 4155cd062e3SThomas Zimmermann u8 seq0, crtcext1; 41670c3881eSThomas Zimmermann 41770c3881eSThomas Zimmermann RREG_SEQ(0x00, seq0); 41870c3881eSThomas Zimmermann seq0 &= ~MGAREG_SEQ0_SYNCRST; 41970c3881eSThomas Zimmermann WREG_SEQ(0x00, seq0); 420153fef41SThomas Zimmermann 421153fef41SThomas Zimmermann /* 422153fef41SThomas Zimmermann * TODO: replace busy waiting with vblank IRQ; put 423153fef41SThomas Zimmermann * msleep(50) before changing SCROFF 424153fef41SThomas Zimmermann */ 425153fef41SThomas Zimmermann mga_wait_vsync(mdev); 426153fef41SThomas Zimmermann mga_wait_busy(mdev); 427153fef41SThomas Zimmermann 428153fef41SThomas Zimmermann RREG_ECRT(0x01, crtcext1); 429153fef41SThomas Zimmermann crtcext1 |= MGAREG_CRTCEXT1_VSYNCOFF | 430153fef41SThomas Zimmermann MGAREG_CRTCEXT1_HSYNCOFF; 431153fef41SThomas Zimmermann WREG_ECRT(0x01, crtcext1); 432414c4531SDave Airlie } 433414c4531SDave Airlie 4344f4dc37eSThomas Zimmermann static void mgag200_handle_damage(struct mga_device *mdev, const struct iosys_map *vmap, 435edbe262aSThomas Zimmermann struct drm_framebuffer *fb, struct drm_rect *clip) 436414c4531SDave Airlie { 437edbe262aSThomas Zimmermann struct iosys_map dst = IOSYS_MAP_INIT_VADDR_IOMEM(mdev->vram); 438414c4531SDave Airlie 439edbe262aSThomas Zimmermann iosys_map_incr(&dst, drm_fb_clip_offset(fb->pitches[0], fb->format, clip)); 440edbe262aSThomas Zimmermann drm_fb_memcpy(&dst, fb->pitches, vmap, fb, clip); 441414c4531SDave Airlie } 442414c4531SDave Airlie 44388fabb75SThomas Zimmermann /* 4441baf9127SThomas Zimmermann * Primary plane 44588fabb75SThomas Zimmermann */ 44688fabb75SThomas Zimmermann 447bc835040SThomas Zimmermann const uint32_t mgag200_primary_plane_formats[] = { 4484f4dc37eSThomas Zimmermann DRM_FORMAT_XRGB8888, 4494f4dc37eSThomas Zimmermann DRM_FORMAT_RGB565, 4504f4dc37eSThomas Zimmermann DRM_FORMAT_RGB888, 4514f4dc37eSThomas Zimmermann }; 4524f4dc37eSThomas Zimmermann 453bc835040SThomas Zimmermann const size_t mgag200_primary_plane_formats_size = ARRAY_SIZE(mgag200_primary_plane_formats); 454bc835040SThomas Zimmermann 455bc835040SThomas Zimmermann const uint64_t mgag200_primary_plane_fmtmods[] = { 4564f4dc37eSThomas Zimmermann DRM_FORMAT_MOD_LINEAR, 4574f4dc37eSThomas Zimmermann DRM_FORMAT_MOD_INVALID 4584f4dc37eSThomas Zimmermann }; 4594f4dc37eSThomas Zimmermann 460bc835040SThomas Zimmermann int mgag200_primary_plane_helper_atomic_check(struct drm_plane *plane, 4611baf9127SThomas Zimmermann struct drm_atomic_state *new_state) 4621baf9127SThomas Zimmermann { 4631baf9127SThomas Zimmermann struct drm_plane_state *new_plane_state = drm_atomic_get_new_plane_state(new_state, plane); 4641baf9127SThomas Zimmermann struct drm_framebuffer *new_fb = new_plane_state->fb; 4651baf9127SThomas Zimmermann struct drm_framebuffer *fb = NULL; 4661baf9127SThomas Zimmermann struct drm_crtc *new_crtc = new_plane_state->crtc; 4671baf9127SThomas Zimmermann struct drm_crtc_state *new_crtc_state = NULL; 4681baf9127SThomas Zimmermann struct mgag200_crtc_state *new_mgag200_crtc_state; 4691baf9127SThomas Zimmermann int ret; 4701baf9127SThomas Zimmermann 4711baf9127SThomas Zimmermann if (new_crtc) 4721baf9127SThomas Zimmermann new_crtc_state = drm_atomic_get_new_crtc_state(new_state, new_crtc); 4731baf9127SThomas Zimmermann 4741baf9127SThomas Zimmermann ret = drm_atomic_helper_check_plane_state(new_plane_state, new_crtc_state, 4751baf9127SThomas Zimmermann DRM_PLANE_NO_SCALING, 4761baf9127SThomas Zimmermann DRM_PLANE_NO_SCALING, 4771baf9127SThomas Zimmermann false, true); 4781baf9127SThomas Zimmermann if (ret) 4791baf9127SThomas Zimmermann return ret; 4801baf9127SThomas Zimmermann else if (!new_plane_state->visible) 4811baf9127SThomas Zimmermann return 0; 4821baf9127SThomas Zimmermann 4831baf9127SThomas Zimmermann if (plane->state) 4841baf9127SThomas Zimmermann fb = plane->state->fb; 4851baf9127SThomas Zimmermann 4861baf9127SThomas Zimmermann if (!fb || (fb->format != new_fb->format)) 4871baf9127SThomas Zimmermann new_crtc_state->mode_changed = true; /* update PLL settings */ 4881baf9127SThomas Zimmermann 4891baf9127SThomas Zimmermann new_mgag200_crtc_state = to_mgag200_crtc_state(new_crtc_state); 4901baf9127SThomas Zimmermann new_mgag200_crtc_state->format = new_fb->format; 4911baf9127SThomas Zimmermann 4921baf9127SThomas Zimmermann return 0; 4931baf9127SThomas Zimmermann } 4941baf9127SThomas Zimmermann 495bc835040SThomas Zimmermann void mgag200_primary_plane_helper_atomic_update(struct drm_plane *plane, 4961baf9127SThomas Zimmermann struct drm_atomic_state *old_state) 4971baf9127SThomas Zimmermann { 4981baf9127SThomas Zimmermann struct drm_device *dev = plane->dev; 4991baf9127SThomas Zimmermann struct mga_device *mdev = to_mga_device(dev); 5001baf9127SThomas Zimmermann struct drm_plane_state *plane_state = plane->state; 5011baf9127SThomas Zimmermann struct drm_plane_state *old_plane_state = drm_atomic_get_old_plane_state(old_state, plane); 5021baf9127SThomas Zimmermann struct drm_shadow_plane_state *shadow_plane_state = to_drm_shadow_plane_state(plane_state); 5031baf9127SThomas Zimmermann struct drm_framebuffer *fb = plane_state->fb; 5041baf9127SThomas Zimmermann struct drm_atomic_helper_damage_iter iter; 5051baf9127SThomas Zimmermann struct drm_rect damage; 5061baf9127SThomas Zimmermann 5071baf9127SThomas Zimmermann drm_atomic_helper_damage_iter_init(&iter, old_plane_state, plane_state); 5081baf9127SThomas Zimmermann drm_atomic_for_each_plane_damage(&iter, &damage) { 5091baf9127SThomas Zimmermann mgag200_handle_damage(mdev, shadow_plane_state->data, fb, &damage); 5101baf9127SThomas Zimmermann } 5111baf9127SThomas Zimmermann 5121baf9127SThomas Zimmermann /* Always scanout image at VRAM offset 0 */ 5131baf9127SThomas Zimmermann mgag200_set_startadd(mdev, (u32)0); 5141baf9127SThomas Zimmermann mgag200_set_offset(mdev, fb); 5152a742fd1SThomas Zimmermann } 5165cd062e3SThomas Zimmermann 5172a742fd1SThomas Zimmermann void mgag200_primary_plane_helper_atomic_enable(struct drm_plane *plane, 5182a742fd1SThomas Zimmermann struct drm_atomic_state *state) 5192a742fd1SThomas Zimmermann { 5202a742fd1SThomas Zimmermann struct drm_device *dev = plane->dev; 5212a742fd1SThomas Zimmermann struct mga_device *mdev = to_mga_device(dev); 5222a742fd1SThomas Zimmermann u8 seq1; 5232a742fd1SThomas Zimmermann 5245cd062e3SThomas Zimmermann RREG_SEQ(0x01, seq1); 5255cd062e3SThomas Zimmermann seq1 &= ~MGAREG_SEQ1_SCROFF; 5265cd062e3SThomas Zimmermann WREG_SEQ(0x01, seq1); 5275cd062e3SThomas Zimmermann msleep(20); 5285cd062e3SThomas Zimmermann } 5291baf9127SThomas Zimmermann 530bc835040SThomas Zimmermann void mgag200_primary_plane_helper_atomic_disable(struct drm_plane *plane, 5311baf9127SThomas Zimmermann struct drm_atomic_state *old_state) 5325cd062e3SThomas Zimmermann { 5335cd062e3SThomas Zimmermann struct drm_device *dev = plane->dev; 5345cd062e3SThomas Zimmermann struct mga_device *mdev = to_mga_device(dev); 5355cd062e3SThomas Zimmermann u8 seq1; 5365cd062e3SThomas Zimmermann 5375cd062e3SThomas Zimmermann RREG_SEQ(0x01, seq1); 5385cd062e3SThomas Zimmermann seq1 |= MGAREG_SEQ1_SCROFF; 5395cd062e3SThomas Zimmermann WREG_SEQ(0x01, seq1); 5405cd062e3SThomas Zimmermann msleep(20); 5415cd062e3SThomas Zimmermann } 5421baf9127SThomas Zimmermann 5437e64f7c8SJocelyn Falempe int mgag200_primary_plane_helper_get_scanout_buffer(struct drm_plane *plane, 5447e64f7c8SJocelyn Falempe struct drm_scanout_buffer *sb) 5457e64f7c8SJocelyn Falempe { 5467e64f7c8SJocelyn Falempe struct mga_device *mdev = to_mga_device(plane->dev); 5477e64f7c8SJocelyn Falempe struct iosys_map map = IOSYS_MAP_INIT_VADDR_IOMEM(mdev->vram); 5487e64f7c8SJocelyn Falempe 5497e64f7c8SJocelyn Falempe if (plane->state && plane->state->fb) { 5507e64f7c8SJocelyn Falempe sb->format = plane->state->fb->format; 5517e64f7c8SJocelyn Falempe sb->width = plane->state->fb->width; 5527e64f7c8SJocelyn Falempe sb->height = plane->state->fb->height; 5537e64f7c8SJocelyn Falempe sb->pitch[0] = plane->state->fb->pitches[0]; 5547e64f7c8SJocelyn Falempe sb->map[0] = map; 5557e64f7c8SJocelyn Falempe return 0; 5567e64f7c8SJocelyn Falempe } 5577e64f7c8SJocelyn Falempe return -ENODEV; 5587e64f7c8SJocelyn Falempe } 5597e64f7c8SJocelyn Falempe 5601baf9127SThomas Zimmermann /* 5611baf9127SThomas Zimmermann * CRTC 5621baf9127SThomas Zimmermann */ 5631baf9127SThomas Zimmermann 564bc835040SThomas Zimmermann enum drm_mode_status mgag200_crtc_helper_mode_valid(struct drm_crtc *crtc, 56588fabb75SThomas Zimmermann const struct drm_display_mode *mode) 56688fabb75SThomas Zimmermann { 5671baf9127SThomas Zimmermann struct mga_device *mdev = to_mga_device(crtc->dev); 56898da4b99SThomas Zimmermann const struct mgag200_device_info *info = mdev->info; 569475e2b97SThomas Zimmermann 57098da4b99SThomas Zimmermann /* 57198da4b99SThomas Zimmermann * Some devices have additional limits on the size of the 57298da4b99SThomas Zimmermann * display mode. 57398da4b99SThomas Zimmermann */ 57498da4b99SThomas Zimmermann if (mode->hdisplay > info->max_hdisplay) 575475e2b97SThomas Zimmermann return MODE_VIRTUAL_X; 57698da4b99SThomas Zimmermann if (mode->vdisplay > info->max_vdisplay) 577475e2b97SThomas Zimmermann return MODE_VIRTUAL_Y; 578475e2b97SThomas Zimmermann 579475e2b97SThomas Zimmermann if ((mode->hdisplay % 8) != 0 || (mode->hsync_start % 8) != 0 || 580475e2b97SThomas Zimmermann (mode->hsync_end % 8) != 0 || (mode->htotal % 8) != 0) { 581475e2b97SThomas Zimmermann return MODE_H_ILLEGAL; 582475e2b97SThomas Zimmermann } 583475e2b97SThomas Zimmermann 584475e2b97SThomas Zimmermann if (mode->crtc_hdisplay > 2048 || mode->crtc_hsync_start > 4096 || 585475e2b97SThomas Zimmermann mode->crtc_hsync_end > 4096 || mode->crtc_htotal > 4096 || 586475e2b97SThomas Zimmermann mode->crtc_vdisplay > 2048 || mode->crtc_vsync_start > 4096 || 587475e2b97SThomas Zimmermann mode->crtc_vsync_end > 4096 || mode->crtc_vtotal > 4096) { 588475e2b97SThomas Zimmermann return MODE_BAD; 589475e2b97SThomas Zimmermann } 590475e2b97SThomas Zimmermann 59188fabb75SThomas Zimmermann return MODE_OK; 59288fabb75SThomas Zimmermann } 59388fabb75SThomas Zimmermann 594bc835040SThomas Zimmermann int mgag200_crtc_helper_atomic_check(struct drm_crtc *crtc, struct drm_atomic_state *new_state) 59588fabb75SThomas Zimmermann { 59688fabb75SThomas Zimmermann struct drm_device *dev = crtc->dev; 59788fabb75SThomas Zimmermann struct mga_device *mdev = to_mga_device(dev); 598877507bbSThomas Zimmermann const struct mgag200_device_funcs *funcs = mdev->funcs; 5991baf9127SThomas Zimmermann struct drm_crtc_state *new_crtc_state = drm_atomic_get_new_crtc_state(new_state, crtc); 600*cd3a2e8bSThomas Zimmermann struct mgag200_crtc_state *new_mgag200_crtc_state = to_mgag200_crtc_state(new_crtc_state); 6011baf9127SThomas Zimmermann struct drm_property_blob *new_gamma_lut = new_crtc_state->gamma_lut; 6021baf9127SThomas Zimmermann int ret; 6031baf9127SThomas Zimmermann 6041baf9127SThomas Zimmermann if (!new_crtc_state->enable) 6051baf9127SThomas Zimmermann return 0; 6061baf9127SThomas Zimmermann 6078f2fd57dSThomas Zimmermann ret = drm_atomic_helper_check_crtc_primary_plane(new_crtc_state); 6088f2fd57dSThomas Zimmermann if (ret) 6098f2fd57dSThomas Zimmermann return ret; 6108f2fd57dSThomas Zimmermann 611*cd3a2e8bSThomas Zimmermann new_mgag200_crtc_state->set_vidrst = mdev->info->has_vidrst; 612*cd3a2e8bSThomas Zimmermann 6131baf9127SThomas Zimmermann if (new_crtc_state->mode_changed) { 614877507bbSThomas Zimmermann if (funcs->pixpllc_atomic_check) { 615877507bbSThomas Zimmermann ret = funcs->pixpllc_atomic_check(crtc, new_state); 6161baf9127SThomas Zimmermann if (ret) 6171baf9127SThomas Zimmermann return ret; 6181baf9127SThomas Zimmermann } 619877507bbSThomas Zimmermann } 6201baf9127SThomas Zimmermann 6211baf9127SThomas Zimmermann if (new_crtc_state->color_mgmt_changed && new_gamma_lut) { 6221baf9127SThomas Zimmermann if (new_gamma_lut->length != MGAG200_LUT_SIZE * sizeof(struct drm_color_lut)) { 6231baf9127SThomas Zimmermann drm_dbg(dev, "Wrong size for gamma_lut %zu\n", new_gamma_lut->length); 6241baf9127SThomas Zimmermann return -EINVAL; 6251baf9127SThomas Zimmermann } 6261baf9127SThomas Zimmermann } 6271baf9127SThomas Zimmermann 6289cebffdfSJavier Martinez Canillas return 0; 6291baf9127SThomas Zimmermann } 6301baf9127SThomas Zimmermann 631bc835040SThomas Zimmermann void mgag200_crtc_helper_atomic_flush(struct drm_crtc *crtc, struct drm_atomic_state *old_state) 6321baf9127SThomas Zimmermann { 6331baf9127SThomas Zimmermann struct drm_crtc_state *crtc_state = crtc->state; 6341baf9127SThomas Zimmermann struct mgag200_crtc_state *mgag200_crtc_state = to_mgag200_crtc_state(crtc_state); 6351baf9127SThomas Zimmermann struct drm_device *dev = crtc->dev; 6361baf9127SThomas Zimmermann struct mga_device *mdev = to_mga_device(dev); 6371baf9127SThomas Zimmermann 6381baf9127SThomas Zimmermann if (crtc_state->enable && crtc_state->color_mgmt_changed) { 6391baf9127SThomas Zimmermann const struct drm_format_info *format = mgag200_crtc_state->format; 6401baf9127SThomas Zimmermann 6411baf9127SThomas Zimmermann if (crtc_state->gamma_lut) 6421baf9127SThomas Zimmermann mgag200_crtc_set_gamma(mdev, format, crtc_state->gamma_lut->data); 6431baf9127SThomas Zimmermann else 6441baf9127SThomas Zimmermann mgag200_crtc_set_gamma_linear(mdev, format); 6451baf9127SThomas Zimmermann } 6461baf9127SThomas Zimmermann } 6471baf9127SThomas Zimmermann 648bc835040SThomas Zimmermann void mgag200_crtc_helper_atomic_enable(struct drm_crtc *crtc, struct drm_atomic_state *old_state) 6491baf9127SThomas Zimmermann { 6501baf9127SThomas Zimmermann struct drm_device *dev = crtc->dev; 6511baf9127SThomas Zimmermann struct mga_device *mdev = to_mga_device(dev); 6528aeeb314SThomas Zimmermann const struct mgag200_device_funcs *funcs = mdev->funcs; 6531baf9127SThomas Zimmermann struct drm_crtc_state *crtc_state = crtc->state; 65488fabb75SThomas Zimmermann struct drm_display_mode *adjusted_mode = &crtc_state->adjusted_mode; 6550a6dab7dSThomas Zimmermann struct mgag200_crtc_state *mgag200_crtc_state = to_mgag200_crtc_state(crtc_state); 656ed2ef21fSThomas Zimmermann const struct drm_format_info *format = mgag200_crtc_state->format; 65788fabb75SThomas Zimmermann 6588aeeb314SThomas Zimmermann if (funcs->disable_vidrst) 6598aeeb314SThomas Zimmermann funcs->disable_vidrst(mdev); 66088fabb75SThomas Zimmermann 661ed2ef21fSThomas Zimmermann mgag200_set_format_regs(mdev, format); 662*cd3a2e8bSThomas Zimmermann mgag200_set_mode_regs(mdev, adjusted_mode, mgag200_crtc_state->set_vidrst); 6630a6dab7dSThomas Zimmermann 664877507bbSThomas Zimmermann if (funcs->pixpllc_atomic_update) 665877507bbSThomas Zimmermann funcs->pixpllc_atomic_update(crtc, old_state); 66688fabb75SThomas Zimmermann 667ad81e234SJocelyn Falempe if (crtc_state->gamma_lut) 668ad81e234SJocelyn Falempe mgag200_crtc_set_gamma(mdev, format, crtc_state->gamma_lut->data); 669ad81e234SJocelyn Falempe else 670ad81e234SJocelyn Falempe mgag200_crtc_set_gamma_linear(mdev, format); 671ad81e234SJocelyn Falempe 672895a4790SThomas Zimmermann mgag200_enable_display(mdev); 673913ec479SThomas Zimmermann 6748aeeb314SThomas Zimmermann if (funcs->enable_vidrst) 6758aeeb314SThomas Zimmermann funcs->enable_vidrst(mdev); 67688fabb75SThomas Zimmermann } 67788fabb75SThomas Zimmermann 678bc835040SThomas Zimmermann void mgag200_crtc_helper_atomic_disable(struct drm_crtc *crtc, struct drm_atomic_state *old_state) 67988fabb75SThomas Zimmermann { 680153fef41SThomas Zimmermann struct mga_device *mdev = to_mga_device(crtc->dev); 6818aeeb314SThomas Zimmermann const struct mgag200_device_funcs *funcs = mdev->funcs; 68288fabb75SThomas Zimmermann 6838aeeb314SThomas Zimmermann if (funcs->disable_vidrst) 6848aeeb314SThomas Zimmermann funcs->disable_vidrst(mdev); 6851baf9127SThomas Zimmermann 686153fef41SThomas Zimmermann mgag200_disable_display(mdev); 6871baf9127SThomas Zimmermann 6888aeeb314SThomas Zimmermann if (funcs->enable_vidrst) 6898aeeb314SThomas Zimmermann funcs->enable_vidrst(mdev); 69088fabb75SThomas Zimmermann } 69188fabb75SThomas Zimmermann 692bc835040SThomas Zimmermann void mgag200_crtc_reset(struct drm_crtc *crtc) 69388fabb75SThomas Zimmermann { 6941baf9127SThomas Zimmermann struct mgag200_crtc_state *mgag200_crtc_state; 69588fabb75SThomas Zimmermann 6961baf9127SThomas Zimmermann if (crtc->state) 6971baf9127SThomas Zimmermann crtc->funcs->atomic_destroy_state(crtc, crtc->state); 69888fabb75SThomas Zimmermann 6991baf9127SThomas Zimmermann mgag200_crtc_state = kzalloc(sizeof(*mgag200_crtc_state), GFP_KERNEL); 7001baf9127SThomas Zimmermann if (mgag200_crtc_state) 7011baf9127SThomas Zimmermann __drm_atomic_helper_crtc_reset(crtc, &mgag200_crtc_state->base); 7021baf9127SThomas Zimmermann else 7031baf9127SThomas Zimmermann __drm_atomic_helper_crtc_reset(crtc, NULL); 7040a6dab7dSThomas Zimmermann } 7050a6dab7dSThomas Zimmermann 706bc835040SThomas Zimmermann struct drm_crtc_state *mgag200_crtc_atomic_duplicate_state(struct drm_crtc *crtc) 70788fabb75SThomas Zimmermann { 70851b56939SThomas Zimmermann struct drm_crtc_state *crtc_state = crtc->state; 7090a6dab7dSThomas Zimmermann struct mgag200_crtc_state *mgag200_crtc_state = to_mgag200_crtc_state(crtc_state); 71051b56939SThomas Zimmermann struct mgag200_crtc_state *new_mgag200_crtc_state; 71151b56939SThomas Zimmermann 71251b56939SThomas Zimmermann if (!crtc_state) 71351b56939SThomas Zimmermann return NULL; 71451b56939SThomas Zimmermann 71551b56939SThomas Zimmermann new_mgag200_crtc_state = kzalloc(sizeof(*new_mgag200_crtc_state), GFP_KERNEL); 71651b56939SThomas Zimmermann if (!new_mgag200_crtc_state) 71751b56939SThomas Zimmermann return NULL; 71851b56939SThomas Zimmermann __drm_atomic_helper_crtc_duplicate_state(crtc, &new_mgag200_crtc_state->base); 71951b56939SThomas Zimmermann 720ed2ef21fSThomas Zimmermann new_mgag200_crtc_state->format = mgag200_crtc_state->format; 7210a6dab7dSThomas Zimmermann memcpy(&new_mgag200_crtc_state->pixpllc, &mgag200_crtc_state->pixpllc, 7220a6dab7dSThomas Zimmermann sizeof(new_mgag200_crtc_state->pixpllc)); 723*cd3a2e8bSThomas Zimmermann new_mgag200_crtc_state->set_vidrst = mgag200_crtc_state->set_vidrst; 7240a6dab7dSThomas Zimmermann 72551b56939SThomas Zimmermann return &new_mgag200_crtc_state->base; 72651b56939SThomas Zimmermann } 72751b56939SThomas Zimmermann 728bc835040SThomas Zimmermann void mgag200_crtc_atomic_destroy_state(struct drm_crtc *crtc, struct drm_crtc_state *crtc_state) 72951b56939SThomas Zimmermann { 73051b56939SThomas Zimmermann struct mgag200_crtc_state *mgag200_crtc_state = to_mgag200_crtc_state(crtc_state); 73151b56939SThomas Zimmermann 73251b56939SThomas Zimmermann __drm_atomic_helper_crtc_destroy_state(&mgag200_crtc_state->base); 73351b56939SThomas Zimmermann kfree(mgag200_crtc_state); 73451b56939SThomas Zimmermann } 73551b56939SThomas Zimmermann 7364f4dc37eSThomas Zimmermann /* 73788fabb75SThomas Zimmermann * Mode config 73888fabb75SThomas Zimmermann */ 73988fabb75SThomas Zimmermann 7402d70b9a1SThomas Zimmermann static void mgag200_mode_config_helper_atomic_commit_tail(struct drm_atomic_state *state) 7412d70b9a1SThomas Zimmermann { 7422d70b9a1SThomas Zimmermann struct mga_device *mdev = to_mga_device(state->dev); 7432d70b9a1SThomas Zimmermann 7442d70b9a1SThomas Zimmermann /* 7452d70b9a1SThomas Zimmermann * Concurrent operations could possibly trigger a call to 7462d70b9a1SThomas Zimmermann * drm_connector_helper_funcs.get_modes by trying to read the 7472d70b9a1SThomas Zimmermann * display modes. Protect access to I/O registers by acquiring 7482d70b9a1SThomas Zimmermann * the I/O-register lock. 7492d70b9a1SThomas Zimmermann */ 7502d70b9a1SThomas Zimmermann mutex_lock(&mdev->rmmio_lock); 7512d70b9a1SThomas Zimmermann drm_atomic_helper_commit_tail(state); 7522d70b9a1SThomas Zimmermann mutex_unlock(&mdev->rmmio_lock); 7532d70b9a1SThomas Zimmermann } 7542d70b9a1SThomas Zimmermann 7552d70b9a1SThomas Zimmermann static const struct drm_mode_config_helper_funcs mgag200_mode_config_helper_funcs = { 7562d70b9a1SThomas Zimmermann .atomic_commit_tail = mgag200_mode_config_helper_atomic_commit_tail, 7572d70b9a1SThomas Zimmermann }; 7582d70b9a1SThomas Zimmermann 759475e2b97SThomas Zimmermann /* Calculates a mode's required memory bandwidth (in KiB/sec). */ 760475e2b97SThomas Zimmermann static uint32_t mgag200_calculate_mode_bandwidth(const struct drm_display_mode *mode, 761475e2b97SThomas Zimmermann unsigned int bits_per_pixel) 762475e2b97SThomas Zimmermann { 763475e2b97SThomas Zimmermann uint32_t total_area, divisor; 764475e2b97SThomas Zimmermann uint64_t active_area, pixels_per_second, bandwidth; 765475e2b97SThomas Zimmermann uint64_t bytes_per_pixel = (bits_per_pixel + 7) / 8; 766475e2b97SThomas Zimmermann 767475e2b97SThomas Zimmermann divisor = 1024; 768475e2b97SThomas Zimmermann 769475e2b97SThomas Zimmermann if (!mode->htotal || !mode->vtotal || !mode->clock) 770475e2b97SThomas Zimmermann return 0; 771475e2b97SThomas Zimmermann 772475e2b97SThomas Zimmermann active_area = mode->hdisplay * mode->vdisplay; 773475e2b97SThomas Zimmermann total_area = mode->htotal * mode->vtotal; 774475e2b97SThomas Zimmermann 775475e2b97SThomas Zimmermann pixels_per_second = active_area * mode->clock * 1000; 776475e2b97SThomas Zimmermann do_div(pixels_per_second, total_area); 777475e2b97SThomas Zimmermann 778475e2b97SThomas Zimmermann bandwidth = pixels_per_second * bytes_per_pixel * 100; 779475e2b97SThomas Zimmermann do_div(bandwidth, divisor); 780475e2b97SThomas Zimmermann 781475e2b97SThomas Zimmermann return (uint32_t)bandwidth; 782475e2b97SThomas Zimmermann } 783475e2b97SThomas Zimmermann 78469340e52SThomas Zimmermann static enum drm_mode_status mgag200_mode_config_mode_valid(struct drm_device *dev, 78569340e52SThomas Zimmermann const struct drm_display_mode *mode) 78669340e52SThomas Zimmermann { 78769340e52SThomas Zimmermann static const unsigned int max_bpp = 4; // DRM_FORMAT_XRGB8888 78869340e52SThomas Zimmermann struct mga_device *mdev = to_mga_device(dev); 78969340e52SThomas Zimmermann unsigned long fbsize, fbpages, max_fbpages; 79098da4b99SThomas Zimmermann const struct mgag200_device_info *info = mdev->info; 79169340e52SThomas Zimmermann 792d45e32c9SThomas Zimmermann max_fbpages = mdev->vram_available >> PAGE_SHIFT; 79369340e52SThomas Zimmermann 79469340e52SThomas Zimmermann fbsize = mode->hdisplay * mode->vdisplay * max_bpp; 79569340e52SThomas Zimmermann fbpages = DIV_ROUND_UP(fbsize, PAGE_SIZE); 79669340e52SThomas Zimmermann 79769340e52SThomas Zimmermann if (fbpages > max_fbpages) 79869340e52SThomas Zimmermann return MODE_MEM; 79969340e52SThomas Zimmermann 80098da4b99SThomas Zimmermann /* 80198da4b99SThomas Zimmermann * Test the mode's required memory bandwidth if the device 80298da4b99SThomas Zimmermann * specifies a maximum. Not all devices do though. 80398da4b99SThomas Zimmermann */ 80498da4b99SThomas Zimmermann if (info->max_mem_bandwidth) { 80598da4b99SThomas Zimmermann uint32_t mode_bandwidth = mgag200_calculate_mode_bandwidth(mode, max_bpp * 8); 806475e2b97SThomas Zimmermann 80798da4b99SThomas Zimmermann if (mode_bandwidth > (info->max_mem_bandwidth * 1024)) 808475e2b97SThomas Zimmermann return MODE_BAD; 809475e2b97SThomas Zimmermann } 810475e2b97SThomas Zimmermann 81169340e52SThomas Zimmermann return MODE_OK; 81269340e52SThomas Zimmermann } 81369340e52SThomas Zimmermann 8145635b7cfSThomas Zimmermann static const struct drm_mode_config_funcs mgag200_mode_config_funcs = { 815913ec479SThomas Zimmermann .fb_create = drm_gem_fb_create_with_dirty, 81669340e52SThomas Zimmermann .mode_valid = mgag200_mode_config_mode_valid, 81788fabb75SThomas Zimmermann .atomic_check = drm_atomic_helper_check, 81888fabb75SThomas Zimmermann .atomic_commit = drm_atomic_helper_commit, 8195635b7cfSThomas Zimmermann }; 8205635b7cfSThomas Zimmermann 821bc835040SThomas Zimmermann int mgag200_mode_config_init(struct mga_device *mdev, resource_size_t vram_available) 822414c4531SDave Airlie { 823832eddf5SThomas Zimmermann struct drm_device *dev = &mdev->base; 82403e44ad1SThomas Zimmermann int ret; 825414c4531SDave Airlie 826d45e32c9SThomas Zimmermann mdev->vram_available = vram_available; 827d45e32c9SThomas Zimmermann 8285635b7cfSThomas Zimmermann ret = drmm_mode_config_init(dev); 8295635b7cfSThomas Zimmermann if (ret) { 83044373151SThomas Zimmermann drm_err(dev, "drmm_mode_config_init() failed: %d\n", ret); 8315635b7cfSThomas Zimmermann return ret; 8325635b7cfSThomas Zimmermann } 8335635b7cfSThomas Zimmermann 834ed5877b6SThomas Zimmermann dev->mode_config.max_width = MGAG200_MAX_FB_WIDTH; 835ed5877b6SThomas Zimmermann dev->mode_config.max_height = MGAG200_MAX_FB_HEIGHT; 83673f54d5dSThomas Zimmermann dev->mode_config.preferred_depth = 24; 8375635b7cfSThomas Zimmermann dev->mode_config.funcs = &mgag200_mode_config_funcs; 8382d70b9a1SThomas Zimmermann dev->mode_config.helper_private = &mgag200_mode_config_helper_funcs; 8395635b7cfSThomas Zimmermann 84044373151SThomas Zimmermann return 0; 84144373151SThomas Zimmermann } 842