1 /* 2 * Copyright 2012 Red Hat Inc. 3 * Parts based on xf86-video-ast 4 * Copyright (c) 2005 ASPEED Technology Inc. 5 * 6 * Permission is hereby granted, free of charge, to any person obtaining a 7 * copy of this software and associated documentation files (the 8 * "Software"), to deal in the Software without restriction, including 9 * without limitation the rights to use, copy, modify, merge, publish, 10 * distribute, sub license, and/or sell copies of the Software, and to 11 * permit persons to whom the Software is furnished to do so, subject to 12 * the following conditions: 13 * 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL 17 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, 18 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 19 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 20 * USE OR OTHER DEALINGS IN THE SOFTWARE. 21 * 22 * The above copyright notice and this permission notice (including the 23 * next paragraph) shall be included in all copies or substantial portions 24 * of the Software. 25 * 26 */ 27 /* 28 * Authors: Dave Airlie <airlied@redhat.com> 29 */ 30 31 #include <linux/delay.h> 32 #include <linux/pci.h> 33 34 #include <drm/drm_atomic.h> 35 #include <drm/drm_atomic_helper.h> 36 #include <drm/drm_color_mgmt.h> 37 #include <drm/drm_crtc.h> 38 #include <drm/drm_damage_helper.h> 39 #include <drm/drm_format_helper.h> 40 #include <drm/drm_fourcc.h> 41 #include <drm/drm_gem_atomic_helper.h> 42 #include <drm/drm_gem_framebuffer_helper.h> 43 #include <drm/drm_gem_shmem_helper.h> 44 #include <drm/drm_managed.h> 45 #include <drm/drm_panic.h> 46 #include <drm/drm_panic_helper.h> 47 #include <drm/drm_print.h> 48 #include <drm/drm_probe_helper.h> 49 50 #include "ast_drv.h" 51 #include "ast_tables.h" 52 #include "ast_vbios.h" 53 54 #define AST_LUT_SIZE 256 55 56 #define AST_PRIMARY_PLANE_MAX_OFFSET (BIT(16) - 1) 57 58 static unsigned long ast_fb_vram_offset(void) 59 { 60 return 0; // with shmem, the primary plane is always at offset 0 61 } 62 63 static unsigned long ast_fb_vram_size(struct ast_device *ast) 64 { 65 struct drm_device *dev = &ast->base; 66 unsigned long offset = ast_fb_vram_offset(); // starts at offset 67 long cursor_offset = ast_cursor_vram_offset(ast); // ends at cursor offset 68 69 if (cursor_offset < 0) 70 cursor_offset = ast->vram_size; // no cursor; it's all ours 71 if (drm_WARN_ON_ONCE(dev, offset > cursor_offset)) 72 return 0; // cannot legally happen; signal error 73 return cursor_offset - offset; 74 } 75 76 static void ast_set_gamma_lut(struct drm_crtc *crtc, unsigned int index, 77 u16 red, u16 green, u16 blue) 78 { 79 struct drm_device *dev = crtc->dev; 80 struct ast_device *ast = to_ast_device(dev); 81 u8 i8 = index & 0xff; 82 u8 r8 = red >> 8; 83 u8 g8 = green >> 8; 84 u8 b8 = blue >> 8; 85 86 if (drm_WARN_ON_ONCE(dev, index != i8)) 87 return; /* driver bug */ 88 89 ast_io_write8(ast, AST_IO_VGADWR, i8); 90 ast_io_read8(ast, AST_IO_VGASRI); 91 ast_io_write8(ast, AST_IO_VGAPDR, r8); 92 ast_io_read8(ast, AST_IO_VGASRI); 93 ast_io_write8(ast, AST_IO_VGAPDR, g8); 94 ast_io_read8(ast, AST_IO_VGASRI); 95 ast_io_write8(ast, AST_IO_VGAPDR, b8); 96 ast_io_read8(ast, AST_IO_VGASRI); 97 } 98 99 static void ast_crtc_fill_gamma(struct ast_device *ast, 100 const struct drm_format_info *format) 101 { 102 struct drm_crtc *crtc = &ast->crtc; 103 104 switch (format->format) { 105 case DRM_FORMAT_C8: 106 /* gamma table is used as color palette */ 107 drm_crtc_fill_palette_8(crtc, ast_set_gamma_lut); 108 break; 109 case DRM_FORMAT_XRGB1555: 110 case DRM_FORMAT_RGB565: 111 /* also uses 24-bit gamma correction on high-color modes */ 112 fallthrough; 113 case DRM_FORMAT_XRGB8888: 114 drm_crtc_fill_gamma_888(crtc, ast_set_gamma_lut); 115 break; 116 default: 117 drm_warn_once(&ast->base, "Unsupported format %p4cc for gamma correction\n", 118 &format->format); 119 break; 120 } 121 } 122 123 static void ast_crtc_load_gamma(struct ast_device *ast, 124 const struct drm_format_info *format, 125 struct drm_color_lut *lut) 126 { 127 struct drm_crtc *crtc = &ast->crtc; 128 129 switch (format->format) { 130 case DRM_FORMAT_C8: 131 /* gamma table is used as color palette */ 132 drm_crtc_load_palette_8(crtc, lut, ast_set_gamma_lut); 133 break; 134 case DRM_FORMAT_XRGB1555: 135 case DRM_FORMAT_RGB565: 136 /* also uses 24-bit gamma correction on high-color modes */ 137 fallthrough; 138 case DRM_FORMAT_XRGB8888: 139 drm_crtc_load_gamma_888(crtc, lut, ast_set_gamma_lut); 140 break; 141 default: 142 drm_warn_once(&ast->base, "Unsupported format %p4cc for gamma correction\n", 143 &format->format); 144 break; 145 } 146 } 147 148 static void ast_set_vbios_color_reg(struct ast_device *ast, 149 const struct drm_format_info *format, 150 const struct ast_vbios_enhtable *vmode) 151 { 152 u8 vgacr8c = 0x00; 153 u8 vgacr92 = 0x00; 154 155 switch (format->format) { 156 case DRM_FORMAT_C8: 157 vgacr8c |= AST_IO_VGACR8C_CUR_MODE_VGA; 158 vgacr92 = 8; 159 break; 160 case DRM_FORMAT_XRGB1555: 161 vgacr8c |= AST_IO_VGACR8C_CUR_MODE_15_BPP; 162 vgacr92 = 15; 163 break; 164 case DRM_FORMAT_RGB565: 165 vgacr8c |= AST_IO_VGACR8C_CUR_MODE_16_BPP; 166 vgacr92 = 16; 167 break; 168 case DRM_FORMAT_XRGB8888: 169 vgacr8c |= AST_IO_VGACR8C_CUR_MODE_32_BPP; 170 vgacr92 = 32; 171 break; 172 } 173 174 ast_set_index_reg(ast, AST_IO_VGACRI, 0x8c, vgacr8c); 175 176 ast_set_index_reg(ast, AST_IO_VGACRI, 0x91, 0x00); 177 178 if (vmode->flags & NewModeInfo) { 179 ast_set_index_reg(ast, AST_IO_VGACRI, 0x91, AST_IO_VGACR91_PASSWORD); 180 ast_set_index_reg(ast, AST_IO_VGACRI, 0x92, vgacr92); 181 } 182 } 183 184 static void ast_set_vbios_mode_reg(struct ast_device *ast, 185 const struct drm_display_mode *adjusted_mode, 186 const struct ast_vbios_enhtable *vmode) 187 { 188 u32 refresh_rate_index, mode_id; 189 190 refresh_rate_index = vmode->refresh_rate_index; 191 mode_id = vmode->mode_id; 192 193 ast_set_index_reg(ast, AST_IO_VGACRI, 0x8d, refresh_rate_index & 0xff); 194 ast_set_index_reg(ast, AST_IO_VGACRI, 0x8e, mode_id & 0xff); 195 196 ast_set_index_reg(ast, AST_IO_VGACRI, 0x91, 0x00); 197 198 if (vmode->flags & NewModeInfo) { 199 ast_set_index_reg(ast, AST_IO_VGACRI, 0x91, AST_IO_VGACR91_PASSWORD); 200 ast_set_index_reg(ast, AST_IO_VGACRI, 0x93, adjusted_mode->clock / 1000); 201 ast_set_index_reg(ast, AST_IO_VGACRI, 0x94, adjusted_mode->crtc_hdisplay); 202 ast_set_index_reg(ast, AST_IO_VGACRI, 0x95, adjusted_mode->crtc_hdisplay >> 8); 203 ast_set_index_reg(ast, AST_IO_VGACRI, 0x96, adjusted_mode->crtc_vdisplay); 204 ast_set_index_reg(ast, AST_IO_VGACRI, 0x97, adjusted_mode->crtc_vdisplay >> 8); 205 } 206 } 207 208 static void ast_set_std_reg(struct ast_device *ast, 209 struct drm_display_mode *mode, 210 const struct ast_vbios_stdtable *stdtable) 211 { 212 u32 i; 213 u8 jreg; 214 215 jreg = stdtable->misc; 216 ast_io_write8(ast, AST_IO_VGAMR_W, jreg); 217 218 /* Set SEQ; except Screen Disable field */ 219 ast_set_index_reg(ast, AST_IO_VGASRI, 0x00, 0x03); 220 ast_set_index_reg_mask(ast, AST_IO_VGASRI, 0x01, 0x20, stdtable->seq[0]); 221 for (i = 1; i < 4; i++) { 222 jreg = stdtable->seq[i]; 223 ast_set_index_reg(ast, AST_IO_VGASRI, (i + 1), jreg); 224 } 225 226 /* Set CRTC; except base address and offset */ 227 ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0x11, 0x7f, 0x00); 228 for (i = 0; i < 12; i++) 229 ast_set_index_reg(ast, AST_IO_VGACRI, i, stdtable->crtc[i]); 230 for (i = 14; i < 19; i++) 231 ast_set_index_reg(ast, AST_IO_VGACRI, i, stdtable->crtc[i]); 232 for (i = 20; i < 25; i++) 233 ast_set_index_reg(ast, AST_IO_VGACRI, i, stdtable->crtc[i]); 234 235 /* set AR */ 236 jreg = ast_io_read8(ast, AST_IO_VGAIR1_R); 237 for (i = 0; i < 20; i++) { 238 jreg = stdtable->ar[i]; 239 ast_io_write8(ast, AST_IO_VGAARI_W, (u8)i); 240 ast_io_write8(ast, AST_IO_VGAARI_W, jreg); 241 } 242 ast_io_write8(ast, AST_IO_VGAARI_W, 0x14); 243 ast_io_write8(ast, AST_IO_VGAARI_W, 0x00); 244 245 jreg = ast_io_read8(ast, AST_IO_VGAIR1_R); 246 ast_io_write8(ast, AST_IO_VGAARI_W, 0x20); 247 248 /* Set GR */ 249 for (i = 0; i < 9; i++) 250 ast_set_index_reg(ast, AST_IO_VGAGRI, i, stdtable->gr[i]); 251 } 252 253 static void ast_set_crtc_reg(struct ast_device *ast, struct drm_display_mode *mode, 254 const struct ast_vbios_enhtable *vmode) 255 { 256 u8 jreg05 = 0, jreg07 = 0, jreg09 = 0, jregAC = 0, jregAD = 0, jregAE = 0; 257 u16 temp; 258 unsigned char crtc_hsync_precatch = 0; 259 260 if (ast->quirks->crtc_hsync_precatch_needed && (vmode->flags & AST2500PreCatchCRT)) 261 crtc_hsync_precatch = 40; 262 263 ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0x11, 0x7f, 0x00); 264 265 temp = (mode->crtc_htotal >> 3) - 5; 266 if (temp & 0x100) 267 jregAC |= 0x01; /* HT D[8] */ 268 ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0x00, 0x00, temp); 269 270 temp = (mode->crtc_hdisplay >> 3) - 1; 271 if (temp & 0x100) 272 jregAC |= 0x04; /* HDE D[8] */ 273 ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0x01, 0x00, temp); 274 275 temp = (mode->crtc_hblank_start >> 3) - 1; 276 if (temp & 0x100) 277 jregAC |= 0x10; /* HBS D[8] */ 278 ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0x02, 0x00, temp); 279 280 temp = ((mode->crtc_hblank_end >> 3) - 1) & 0x7f; 281 if (temp & 0x20) 282 jreg05 |= 0x80; /* HBE D[5] */ 283 if (temp & 0x40) 284 jregAD |= 0x01; /* HBE D[5] */ 285 ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0x03, 0xE0, (temp & 0x1f)); 286 287 temp = ((mode->crtc_hsync_start - crtc_hsync_precatch) >> 3) - 1; 288 if (temp & 0x100) 289 jregAC |= 0x40; /* HRS D[5] */ 290 ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0x04, 0x00, temp); 291 292 temp = (((mode->crtc_hsync_end - crtc_hsync_precatch) >> 3) - 1) & 0x3f; 293 if (temp & 0x20) 294 jregAD |= 0x04; /* HRE D[5] */ 295 ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0x05, 0x60, (u8)((temp & 0x1f) | jreg05)); 296 297 ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0xAC, 0x00, jregAC); 298 ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0xAD, 0x00, jregAD); 299 300 if (ast->quirks->crtc_hsync_add4_needed && mode->crtc_vdisplay == 1080) 301 ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0xFC, 0xFD, 0x02); 302 else 303 ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0xFC, 0xFD, 0x00); 304 305 /* vert timings */ 306 temp = (mode->crtc_vtotal) - 2; 307 if (temp & 0x100) 308 jreg07 |= 0x01; 309 if (temp & 0x200) 310 jreg07 |= 0x20; 311 if (temp & 0x400) 312 jregAE |= 0x01; 313 ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0x06, 0x00, temp); 314 315 temp = (mode->crtc_vsync_start) - 1; 316 if (temp & 0x100) 317 jreg07 |= 0x04; 318 if (temp & 0x200) 319 jreg07 |= 0x80; 320 if (temp & 0x400) 321 jregAE |= 0x08; 322 ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0x10, 0x00, temp); 323 324 temp = (mode->crtc_vsync_end - 1) & 0x3f; 325 if (temp & 0x10) 326 jregAE |= 0x20; 327 if (temp & 0x20) 328 jregAE |= 0x40; 329 ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0x11, 0x70, temp & 0xf); 330 331 temp = mode->crtc_vdisplay - 1; 332 if (temp & 0x100) 333 jreg07 |= 0x02; 334 if (temp & 0x200) 335 jreg07 |= 0x40; 336 if (temp & 0x400) 337 jregAE |= 0x02; 338 ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0x12, 0x00, temp); 339 340 temp = mode->crtc_vblank_start - 1; 341 if (temp & 0x100) 342 jreg07 |= 0x08; 343 if (temp & 0x200) 344 jreg09 |= 0x20; 345 if (temp & 0x400) 346 jregAE |= 0x04; 347 ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0x15, 0x00, temp); 348 349 temp = mode->crtc_vblank_end - 1; 350 if (temp & 0x100) 351 jregAE |= 0x10; 352 ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0x16, 0x00, temp); 353 354 ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0x07, 0x00, jreg07); 355 ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0x09, 0xdf, jreg09); 356 ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0xAE, 0x00, (jregAE | 0x80)); 357 358 if (crtc_hsync_precatch) 359 ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0xb6, 0x3f, 0x80); 360 else 361 ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0xb6, 0x3f, 0x00); 362 363 ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0x11, 0x7f, 0x80); 364 } 365 366 static void ast_set_offset_reg(struct ast_device *ast, 367 struct drm_framebuffer *fb) 368 { 369 u16 offset; 370 371 offset = fb->pitches[0] >> 3; 372 ast_set_index_reg(ast, AST_IO_VGACRI, 0x13, (offset & 0xff)); 373 ast_set_index_reg(ast, AST_IO_VGACRI, 0xb0, (offset >> 8) & 0x3f); 374 } 375 376 static void ast_set_dclk_reg(struct ast_device *ast, 377 struct drm_display_mode *mode, 378 const struct ast_vbios_enhtable *vmode) 379 { 380 const struct ast_vbios_dclk_info *clk_info = &ast->dclk_table[vmode->dclk_index]; 381 382 ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0xc0, 0x00, clk_info->param1); 383 ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0xc1, 0x00, clk_info->param2); 384 ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0xbb, 0x0f, 385 (clk_info->param3 & 0xc0) | 386 ((clk_info->param3 & 0x3) << 4)); 387 } 388 389 static void ast_set_color_reg(struct ast_device *ast, 390 const struct drm_format_info *format) 391 { 392 u8 vgacra0 = 0x00; 393 u8 vgacra3 = 0x00; 394 u8 vgacra8 = 0x00; 395 396 vgacra0 |= AST_IO_VGACRA0_MEMORY_CHAIN4_MODE | 397 AST_IO_VGACRA0_LINEAR_EXT_ACCESS | 398 AST_IO_VGACRA0_SEGMENTED_EXT_ACCESS; 399 400 switch (format->format) { 401 case DRM_FORMAT_C8: 402 vgacra3 |= AST_IO_VGACRA3_256_COLORS; 403 vgacra8 &= ~AST_IO_VGACRA8_GAMMA_CORRECTION_ENABLED; 404 break; 405 case DRM_FORMAT_XRGB1555: 406 vgacra3 |= AST_IO_VGACRA3_15_BPP; 407 vgacra8 |= AST_IO_VGACRA8_GAMMA_CORRECTION_ENABLED; 408 break; 409 case DRM_FORMAT_RGB565: 410 vgacra3 |= AST_IO_VGACRA3_16_BPP; 411 vgacra8 |= AST_IO_VGACRA8_GAMMA_CORRECTION_ENABLED; 412 break; 413 case DRM_FORMAT_XRGB8888: 414 vgacra3 |= AST_IO_VGACRA3_32_BPP; 415 vgacra8 |= AST_IO_VGACRA8_GAMMA_CORRECTION_ENABLED; 416 break; 417 } 418 419 ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0xa0, 0x8f, vgacra0); 420 ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0xa3, 0xf0, vgacra3); 421 ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0xa8, 0xfd, vgacra8); 422 } 423 424 static void ast_set_crtthd_reg(struct ast_device *ast) 425 { 426 u8 vgacra6 = ast->quirks->crtc_mem_req_threshold_low; 427 u8 vgacra7 = ast->quirks->crtc_mem_req_threshold_high; 428 429 ast_set_index_reg(ast, AST_IO_VGACRI, 0xa7, vgacra7); 430 ast_set_index_reg(ast, AST_IO_VGACRI, 0xa6, vgacra6); 431 } 432 433 static void ast_set_sync_reg(struct ast_device *ast, 434 struct drm_display_mode *mode, 435 const struct ast_vbios_enhtable *vmode) 436 { 437 u8 jreg; 438 439 jreg = ast_io_read8(ast, AST_IO_VGAMR_R); 440 jreg &= ~0xC0; 441 if (vmode->flags & NVSync) 442 jreg |= 0x80; 443 if (vmode->flags & NHSync) 444 jreg |= 0x40; 445 ast_io_write8(ast, AST_IO_VGAMR_W, jreg); 446 } 447 448 static void ast_set_start_address_crt1(struct ast_device *ast, 449 unsigned int offset) 450 { 451 u32 addr; 452 453 addr = offset >> 2; 454 ast_set_index_reg(ast, AST_IO_VGACRI, 0x0d, (u8)(addr & 0xff)); 455 ast_set_index_reg(ast, AST_IO_VGACRI, 0x0c, (u8)((addr >> 8) & 0xff)); 456 ast_set_index_reg(ast, AST_IO_VGACRI, 0xaf, (u8)((addr >> 16) & 0xff)); 457 458 } 459 460 static void ast_wait_for_vretrace(struct ast_device *ast) 461 { 462 unsigned long timeout = jiffies + HZ; 463 u8 vgair1; 464 465 do { 466 vgair1 = ast_io_read8(ast, AST_IO_VGAIR1_R); 467 } while (!(vgair1 & AST_IO_VGAIR1_VREFRESH) && time_before(jiffies, timeout)); 468 } 469 470 /* 471 * Planes 472 */ 473 474 int ast_plane_init(struct drm_device *dev, struct ast_plane *ast_plane, 475 u64 offset, unsigned long size, 476 uint32_t possible_crtcs, 477 const struct drm_plane_funcs *funcs, 478 const uint32_t *formats, unsigned int format_count, 479 const uint64_t *format_modifiers, 480 enum drm_plane_type type) 481 { 482 struct drm_plane *plane = &ast_plane->base; 483 484 ast_plane->offset = offset; 485 ast_plane->size = size; 486 487 return drm_universal_plane_init(dev, plane, possible_crtcs, funcs, 488 formats, format_count, format_modifiers, 489 type, NULL); 490 } 491 492 void __iomem *ast_plane_vaddr(struct ast_plane *ast_plane) 493 { 494 struct ast_device *ast = to_ast_device(ast_plane->base.dev); 495 496 return ast->vram + ast_plane->offset; 497 } 498 499 /* 500 * Primary plane 501 */ 502 503 static const uint32_t ast_primary_plane_formats[] = { 504 DRM_FORMAT_XRGB8888, 505 DRM_FORMAT_RGB565, 506 DRM_FORMAT_XRGB1555, 507 DRM_FORMAT_C8, 508 }; 509 510 static int ast_primary_plane_helper_atomic_check(struct drm_plane *plane, 511 struct drm_atomic_commit *state) 512 { 513 struct drm_device *dev = plane->dev; 514 struct drm_plane_state *new_plane_state = drm_atomic_get_new_plane_state(state, plane); 515 struct drm_crtc_state *new_crtc_state = NULL; 516 struct ast_crtc_state *new_ast_crtc_state; 517 int ret; 518 519 if (new_plane_state->crtc) 520 new_crtc_state = drm_atomic_get_new_crtc_state(state, new_plane_state->crtc); 521 522 ret = drm_atomic_helper_check_plane_state(new_plane_state, new_crtc_state, 523 DRM_PLANE_NO_SCALING, 524 DRM_PLANE_NO_SCALING, 525 false, true); 526 if (ret) { 527 return ret; 528 } else if (!new_plane_state->visible) { 529 if (drm_WARN_ON(dev, new_plane_state->crtc)) /* cannot legally happen */ 530 return -EINVAL; 531 else 532 return 0; 533 } 534 535 new_ast_crtc_state = to_ast_crtc_state(new_crtc_state); 536 537 new_ast_crtc_state->format = new_plane_state->fb->format; 538 539 return 0; 540 } 541 542 static void ast_handle_damage(struct ast_plane *ast_plane, struct iosys_map *src, 543 struct drm_framebuffer *fb, 544 const struct drm_rect *clip, 545 struct drm_format_conv_state *fmtcnv_state) 546 { 547 struct iosys_map dst = IOSYS_MAP_INIT_VADDR_IOMEM(ast_plane_vaddr(ast_plane)); 548 549 iosys_map_incr(&dst, drm_fb_clip_offset(fb->pitches[0], fb->format, clip)); 550 551 #if defined(__BIG_ENDIAN) 552 drm_fb_swab(&dst, fb->pitches, src, fb, clip, !src[0].is_iomem, fmtcnv_state); 553 #else 554 drm_fb_memcpy(&dst, fb->pitches, src, fb, clip); 555 #endif 556 } 557 558 static void ast_primary_plane_helper_atomic_update(struct drm_plane *plane, 559 struct drm_atomic_commit *state) 560 { 561 struct drm_device *dev = plane->dev; 562 struct ast_device *ast = to_ast_device(dev); 563 struct drm_plane_state *plane_state = drm_atomic_get_new_plane_state(state, plane); 564 struct drm_shadow_plane_state *shadow_plane_state = to_drm_shadow_plane_state(plane_state); 565 struct drm_framebuffer *fb = plane_state->fb; 566 struct drm_plane_state *old_plane_state = drm_atomic_get_old_plane_state(state, plane); 567 struct drm_framebuffer *old_fb = old_plane_state->fb; 568 struct ast_plane *ast_plane = to_ast_plane(plane); 569 struct drm_crtc *crtc = plane_state->crtc; 570 struct drm_crtc_state *crtc_state = drm_atomic_get_new_crtc_state(state, crtc); 571 struct drm_rect damage; 572 struct drm_atomic_helper_damage_iter iter; 573 574 if (!old_fb || (fb->format != old_fb->format) || crtc_state->mode_changed) { 575 struct ast_crtc_state *ast_crtc_state = to_ast_crtc_state(crtc_state); 576 577 ast_set_color_reg(ast, fb->format); 578 ast_set_vbios_color_reg(ast, fb->format, ast_crtc_state->vmode); 579 } 580 581 /* if the buffer comes from another device */ 582 if (drm_gem_fb_begin_cpu_access(fb, DMA_FROM_DEVICE) == 0) { 583 drm_atomic_helper_damage_iter_init(&iter, old_plane_state, plane_state); 584 drm_atomic_for_each_plane_damage(&iter, &damage) { 585 ast_handle_damage(ast_plane, shadow_plane_state->data, fb, &damage, 586 &shadow_plane_state->fmtcnv_state); 587 } 588 589 drm_gem_fb_end_cpu_access(fb, DMA_FROM_DEVICE); 590 } 591 592 /* 593 * Some BMCs stop scanning out the video signal after the driver 594 * reprogrammed the offset. This stalls display output for several 595 * seconds and makes the display unusable. Therefore only update 596 * the offset if it changes. 597 */ 598 if (!old_fb || old_fb->pitches[0] != fb->pitches[0]) 599 ast_set_offset_reg(ast, fb); 600 } 601 602 static void ast_primary_plane_helper_atomic_enable(struct drm_plane *plane, 603 struct drm_atomic_commit *state) 604 { 605 struct ast_device *ast = to_ast_device(plane->dev); 606 struct ast_plane *ast_plane = to_ast_plane(plane); 607 608 /* 609 * Some BMCs stop scanning out the video signal after the driver 610 * reprogrammed the scanout address. This stalls display 611 * output for several seconds and makes the display unusable. 612 * Therefore only reprogram the address after enabling the plane. 613 */ 614 ast_set_start_address_crt1(ast, (u32)ast_plane->offset); 615 } 616 617 static void ast_primary_plane_helper_atomic_disable(struct drm_plane *plane, 618 struct drm_atomic_commit *state) 619 { 620 /* 621 * Keep this empty function to avoid calling 622 * atomic_update when disabling the plane. 623 */ 624 } 625 626 static int ast_primary_plane_helper_get_scanout_buffer(struct drm_plane *plane, 627 struct drm_scanout_buffer *sb) 628 { 629 struct ast_plane *ast_plane = to_ast_plane(plane); 630 631 if (plane->state && plane->state->fb) { 632 sb->format = plane->state->fb->format; 633 sb->width = plane->state->fb->width; 634 sb->height = plane->state->fb->height; 635 sb->pitch[0] = plane->state->fb->pitches[0]; 636 iosys_map_set_vaddr_iomem(&sb->map[0], ast_plane_vaddr(ast_plane)); 637 return 0; 638 } 639 return -ENODEV; 640 } 641 642 static const struct drm_plane_helper_funcs ast_primary_plane_helper_funcs = { 643 DRM_GEM_SHADOW_PLANE_HELPER_FUNCS, 644 .atomic_check = ast_primary_plane_helper_atomic_check, 645 .atomic_update = ast_primary_plane_helper_atomic_update, 646 .atomic_enable = ast_primary_plane_helper_atomic_enable, 647 .atomic_disable = ast_primary_plane_helper_atomic_disable, 648 .get_scanout_buffer = ast_primary_plane_helper_get_scanout_buffer, 649 }; 650 651 static const struct drm_plane_funcs ast_primary_plane_funcs = { 652 .update_plane = drm_atomic_helper_update_plane, 653 .disable_plane = drm_atomic_helper_disable_plane, 654 .destroy = drm_plane_cleanup, 655 DRM_GEM_SHADOW_PLANE_FUNCS, 656 DRM_PANIC_PLANE_FUNCS, 657 }; 658 659 static int ast_primary_plane_init(struct ast_device *ast) 660 { 661 struct drm_device *dev = &ast->base; 662 struct ast_plane *ast_primary_plane = &ast->primary_plane; 663 struct drm_plane *primary_plane = &ast_primary_plane->base; 664 u64 offset = ast_fb_vram_offset(); 665 unsigned long size = ast_fb_vram_size(ast); 666 int ret; 667 668 ret = ast_plane_init(dev, ast_primary_plane, offset, size, 669 0x01, &ast_primary_plane_funcs, 670 ast_primary_plane_formats, ARRAY_SIZE(ast_primary_plane_formats), 671 NULL, DRM_PLANE_TYPE_PRIMARY); 672 if (ret) { 673 drm_err(dev, "ast_plane_init() failed: %d\n", ret); 674 return ret; 675 } 676 drm_plane_helper_add(primary_plane, &ast_primary_plane_helper_funcs); 677 drm_plane_enable_fb_damage_clips(primary_plane); 678 679 return 0; 680 } 681 682 /* 683 * CRTC 684 */ 685 686 static enum drm_mode_status 687 ast_crtc_helper_mode_valid(struct drm_crtc *crtc, const struct drm_display_mode *mode) 688 { 689 struct ast_device *ast = to_ast_device(crtc->dev); 690 const struct ast_vbios_enhtable *vmode; 691 692 vmode = ast_vbios_find_mode(ast, mode); 693 if (!vmode) 694 return MODE_NOMODE; 695 696 return MODE_OK; 697 } 698 699 static void ast_crtc_helper_mode_set_nofb(struct drm_crtc *crtc) 700 { 701 struct drm_device *dev = crtc->dev; 702 struct ast_device *ast = to_ast_device(dev); 703 struct drm_crtc_state *crtc_state = crtc->state; 704 struct ast_crtc_state *ast_crtc_state = to_ast_crtc_state(crtc_state); 705 const struct ast_vbios_stdtable *std_table = ast_crtc_state->std_table; 706 const struct ast_vbios_enhtable *vmode = ast_crtc_state->vmode; 707 struct drm_display_mode *adjusted_mode = &crtc_state->adjusted_mode; 708 709 /* 710 * Ensure that no scanout takes place before reprogramming mode 711 * and format registers. 712 * 713 * TODO: Get vblank interrupts working and remove this line. 714 */ 715 ast_wait_for_vretrace(ast); 716 717 ast_set_vbios_mode_reg(ast, adjusted_mode, vmode); 718 ast_set_index_reg(ast, AST_IO_VGACRI, 0xa1, 0x06); 719 ast_set_std_reg(ast, adjusted_mode, std_table); 720 ast_set_crtc_reg(ast, adjusted_mode, vmode); 721 ast_set_dclk_reg(ast, adjusted_mode, vmode); 722 ast_set_crtthd_reg(ast); 723 ast_set_sync_reg(ast, adjusted_mode, vmode); 724 } 725 726 static int ast_crtc_helper_atomic_check(struct drm_crtc *crtc, 727 struct drm_atomic_commit *state) 728 { 729 struct drm_crtc_state *crtc_state = drm_atomic_get_new_crtc_state(state, crtc); 730 struct drm_display_mode *adjusted_mode = &crtc_state->adjusted_mode; 731 struct drm_crtc_state *old_crtc_state = drm_atomic_get_old_crtc_state(state, crtc); 732 struct ast_crtc_state *old_ast_crtc_state = to_ast_crtc_state(old_crtc_state); 733 struct drm_device *dev = crtc->dev; 734 struct ast_device *ast = to_ast_device(dev); 735 struct ast_crtc_state *ast_state; 736 const struct drm_format_info *format; 737 const struct ast_vbios_enhtable *vmode; 738 unsigned int hborder = 0; 739 unsigned int vborder = 0; 740 int ret; 741 742 if (!crtc_state->enable) 743 return 0; 744 745 ret = drm_atomic_helper_check_crtc_primary_plane(crtc_state); 746 if (ret) 747 return ret; 748 749 ast_state = to_ast_crtc_state(crtc_state); 750 751 format = ast_state->format; 752 if (drm_WARN_ON_ONCE(dev, !format)) 753 return -EINVAL; /* BUG: We didn't set format in primary check(). */ 754 755 /* 756 * The gamma LUT has to be reloaded after changing the primary 757 * plane's color format. 758 */ 759 if (old_ast_crtc_state->format != format) 760 crtc_state->color_mgmt_changed = true; 761 762 if (crtc_state->color_mgmt_changed && crtc_state->gamma_lut) { 763 if (crtc_state->gamma_lut->length != 764 AST_LUT_SIZE * sizeof(struct drm_color_lut)) { 765 drm_err(dev, "Wrong size for gamma_lut %zu\n", 766 crtc_state->gamma_lut->length); 767 return -EINVAL; 768 } 769 } 770 771 /* 772 * Set register tables. 773 * 774 * TODO: These tables mix all kinds of fields and should 775 * probably be resolved into various helper functions. 776 */ 777 switch (format->format) { 778 case DRM_FORMAT_C8: 779 ast_state->std_table = &vbios_stdtable[VGAModeIndex]; 780 break; 781 case DRM_FORMAT_XRGB1555: 782 case DRM_FORMAT_RGB565: 783 ast_state->std_table = &vbios_stdtable[HiCModeIndex]; 784 break; 785 case DRM_FORMAT_XRGB8888: 786 ast_state->std_table = &vbios_stdtable[TrueCModeIndex]; 787 break; 788 default: 789 return -EINVAL; 790 } 791 792 /* 793 * Find the VBIOS mode and adjust the DRM display mode accordingly 794 * if a full modeset is required. Otherwise keep the existing values. 795 */ 796 if (drm_atomic_crtc_needs_modeset(crtc_state)) { 797 vmode = ast_vbios_find_mode(ast, &crtc_state->mode); 798 if (!vmode) 799 return -EINVAL; 800 ast_state->vmode = vmode; 801 802 if (vmode->flags & HBorder) 803 hborder = 8; 804 if (vmode->flags & VBorder) 805 vborder = 8; 806 807 adjusted_mode->crtc_hdisplay = vmode->hde; 808 adjusted_mode->crtc_hblank_start = vmode->hde + hborder; 809 adjusted_mode->crtc_hblank_end = vmode->ht - hborder; 810 adjusted_mode->crtc_hsync_start = vmode->hde + hborder + vmode->hfp; 811 adjusted_mode->crtc_hsync_end = vmode->hde + hborder + vmode->hfp + vmode->hsync; 812 adjusted_mode->crtc_htotal = vmode->ht; 813 814 adjusted_mode->crtc_vdisplay = vmode->vde; 815 adjusted_mode->crtc_vblank_start = vmode->vde + vborder; 816 adjusted_mode->crtc_vblank_end = vmode->vt - vborder; 817 adjusted_mode->crtc_vsync_start = vmode->vde + vborder + vmode->vfp; 818 adjusted_mode->crtc_vsync_end = vmode->vde + vborder + vmode->vfp + vmode->vsync; 819 adjusted_mode->crtc_vtotal = vmode->vt; 820 } 821 822 return 0; 823 } 824 825 static void 826 ast_crtc_helper_atomic_flush(struct drm_crtc *crtc, 827 struct drm_atomic_commit *state) 828 { 829 struct drm_crtc_state *crtc_state = drm_atomic_get_new_crtc_state(state, 830 crtc); 831 struct drm_device *dev = crtc->dev; 832 struct ast_device *ast = to_ast_device(dev); 833 struct ast_crtc_state *ast_crtc_state = to_ast_crtc_state(crtc_state); 834 835 /* 836 * The gamma LUT has to be reloaded after changing the primary 837 * plane's color format. 838 */ 839 if (crtc_state->enable && crtc_state->color_mgmt_changed) { 840 if (crtc_state->gamma_lut) 841 ast_crtc_load_gamma(ast, 842 ast_crtc_state->format, 843 crtc_state->gamma_lut->data); 844 else 845 ast_crtc_fill_gamma(ast, ast_crtc_state->format); 846 } 847 } 848 849 static void ast_crtc_helper_atomic_enable(struct drm_crtc *crtc, struct drm_atomic_commit *state) 850 { 851 struct ast_device *ast = to_ast_device(crtc->dev); 852 u8 vgacr17 = 0x00; 853 u8 vgacrb6 = 0xff; 854 855 vgacr17 |= AST_IO_VGACR17_SYNC_ENABLE; 856 vgacrb6 &= ~(AST_IO_VGACRB6_VSYNC_OFF | AST_IO_VGACRB6_HSYNC_OFF); 857 858 ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0x17, 0x7f, vgacr17); 859 ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0xb6, 0xfc, vgacrb6); 860 } 861 862 static void ast_crtc_helper_atomic_disable(struct drm_crtc *crtc, struct drm_atomic_commit *state) 863 { 864 struct drm_crtc_state *old_crtc_state = drm_atomic_get_old_crtc_state(state, crtc); 865 struct ast_device *ast = to_ast_device(crtc->dev); 866 u8 vgacr17 = 0xff; 867 868 vgacr17 &= ~AST_IO_VGACR17_SYNC_ENABLE; 869 ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0x17, 0x7f, vgacr17); 870 871 /* 872 * HW cursors require the underlying primary plane and CRTC to 873 * display a valid mode and image. This is not the case during 874 * full modeset operations. So we temporarily disable any active 875 * plane, including the HW cursor. Each plane's atomic_update() 876 * helper will re-enable it if necessary. 877 * 878 * We only do this during *full* modesets. It does not affect 879 * simple pageflips on the planes. 880 */ 881 drm_atomic_helper_disable_planes_on_crtc(old_crtc_state, false); 882 } 883 884 static const struct drm_crtc_helper_funcs ast_crtc_helper_funcs = { 885 .mode_valid = ast_crtc_helper_mode_valid, 886 .mode_set_nofb = ast_crtc_helper_mode_set_nofb, 887 .atomic_check = ast_crtc_helper_atomic_check, 888 .atomic_flush = ast_crtc_helper_atomic_flush, 889 .atomic_enable = ast_crtc_helper_atomic_enable, 890 .atomic_disable = ast_crtc_helper_atomic_disable, 891 }; 892 893 static struct drm_crtc_state *ast_crtc_create_state(struct drm_crtc *crtc) 894 { 895 struct ast_crtc_state *ast_state; 896 897 ast_state = kzalloc_obj(*ast_state); 898 if (!ast_state) 899 return ERR_PTR(-ENOMEM); 900 901 __drm_atomic_helper_crtc_state_init(&ast_state->base, crtc); 902 903 return &ast_state->base; 904 } 905 906 static struct drm_crtc_state * 907 ast_crtc_atomic_duplicate_state(struct drm_crtc *crtc) 908 { 909 struct ast_crtc_state *new_ast_state, *ast_state; 910 struct drm_device *dev = crtc->dev; 911 912 if (drm_WARN_ON(dev, !crtc->state)) 913 return NULL; 914 915 new_ast_state = kmalloc_obj(*new_ast_state); 916 if (!new_ast_state) 917 return NULL; 918 __drm_atomic_helper_crtc_duplicate_state(crtc, &new_ast_state->base); 919 920 ast_state = to_ast_crtc_state(crtc->state); 921 922 new_ast_state->format = ast_state->format; 923 new_ast_state->std_table = ast_state->std_table; 924 new_ast_state->vmode = ast_state->vmode; 925 926 return &new_ast_state->base; 927 } 928 929 static void ast_crtc_atomic_destroy_state(struct drm_crtc *crtc, 930 struct drm_crtc_state *state) 931 { 932 struct ast_crtc_state *ast_state = to_ast_crtc_state(state); 933 934 __drm_atomic_helper_crtc_destroy_state(&ast_state->base); 935 kfree(ast_state); 936 } 937 938 static const struct drm_crtc_funcs ast_crtc_funcs = { 939 .atomic_create_state = ast_crtc_create_state, 940 .destroy = drm_crtc_cleanup, 941 .set_config = drm_atomic_helper_set_config, 942 .page_flip = drm_atomic_helper_page_flip, 943 .atomic_duplicate_state = ast_crtc_atomic_duplicate_state, 944 .atomic_destroy_state = ast_crtc_atomic_destroy_state, 945 }; 946 947 static int ast_crtc_init(struct ast_device *ast) 948 { 949 struct drm_device *dev = &ast->base; 950 struct drm_crtc *crtc = &ast->crtc; 951 int ret; 952 953 ret = drm_crtc_init_with_planes(dev, crtc, &ast->primary_plane.base, 954 &ast->cursor_plane.base.base, &ast_crtc_funcs, 955 NULL); 956 if (ret) 957 return ret; 958 959 drm_mode_crtc_set_gamma_size(crtc, AST_LUT_SIZE); 960 drm_crtc_enable_color_mgmt(crtc, 0, false, AST_LUT_SIZE); 961 962 drm_crtc_helper_add(crtc, &ast_crtc_helper_funcs); 963 964 return 0; 965 } 966 967 /* 968 * Mode config 969 */ 970 971 static void ast_mode_config_helper_atomic_commit_tail(struct drm_atomic_commit *state) 972 { 973 struct ast_device *ast = to_ast_device(state->dev); 974 975 /* 976 * Concurrent operations could possibly trigger a call to 977 * drm_connector_helper_funcs.get_modes by reading the display 978 * modes. Protect access to registers by acquiring the modeset 979 * lock. 980 */ 981 mutex_lock(&ast->modeset_lock); 982 drm_atomic_helper_commit_tail(state); 983 mutex_unlock(&ast->modeset_lock); 984 } 985 986 static const struct drm_mode_config_helper_funcs ast_mode_config_helper_funcs = { 987 .atomic_commit_tail = ast_mode_config_helper_atomic_commit_tail, 988 }; 989 990 static enum drm_mode_status ast_mode_config_mode_valid(struct drm_device *dev, 991 const struct drm_display_mode *mode) 992 { 993 const struct drm_format_info *info = drm_format_info(DRM_FORMAT_XRGB8888); 994 struct ast_device *ast = to_ast_device(dev); 995 unsigned long max_fb_size = ast_fb_vram_size(ast); 996 u64 pitch; 997 998 if (drm_WARN_ON_ONCE(dev, !info)) 999 return MODE_ERROR; /* driver bug */ 1000 1001 pitch = drm_format_info_min_pitch(info, 0, mode->hdisplay); 1002 if (!pitch) 1003 return MODE_BAD_WIDTH; 1004 if (pitch > AST_PRIMARY_PLANE_MAX_OFFSET) 1005 return MODE_BAD_WIDTH; /* maximum programmable pitch */ 1006 if (pitch > max_fb_size / mode->vdisplay) 1007 return MODE_MEM; 1008 1009 return MODE_OK; 1010 } 1011 1012 static const struct drm_mode_config_funcs ast_mode_config_funcs = { 1013 .fb_create = drm_gem_fb_create_with_dirty, 1014 .mode_valid = ast_mode_config_mode_valid, 1015 .atomic_check = drm_atomic_helper_check, 1016 .atomic_commit = drm_atomic_helper_commit, 1017 }; 1018 1019 int ast_mode_config_init(struct ast_device *ast) 1020 { 1021 struct drm_device *dev = &ast->base; 1022 int ret; 1023 1024 ret = drmm_mutex_init(dev, &ast->modeset_lock); 1025 if (ret) 1026 return ret; 1027 1028 ret = drmm_mode_config_init(dev); 1029 if (ret) 1030 return ret; 1031 1032 dev->mode_config.funcs = &ast_mode_config_funcs; 1033 dev->mode_config.min_width = 0; 1034 dev->mode_config.min_height = 0; 1035 dev->mode_config.preferred_depth = 24; 1036 1037 if (ast->support_fullhd) { 1038 dev->mode_config.max_width = 1920; 1039 dev->mode_config.max_height = 2048; 1040 } else { 1041 dev->mode_config.max_width = 1600; 1042 dev->mode_config.max_height = 1200; 1043 } 1044 1045 dev->mode_config.helper_private = &ast_mode_config_helper_funcs; 1046 1047 ret = ast_primary_plane_init(ast); 1048 if (ret) 1049 return ret; 1050 1051 ret = ast_cursor_plane_init(ast); 1052 if (ret) 1053 return ret; 1054 1055 ret = ast_crtc_init(ast); 1056 if (ret) 1057 return ret; 1058 1059 switch (ast->tx_chip) { 1060 case AST_TX_NONE: 1061 ret = ast_vga_output_init(ast); 1062 break; 1063 case AST_TX_SIL164: 1064 ret = ast_sil164_output_init(ast); 1065 break; 1066 case AST_TX_DP501: 1067 ret = ast_dp501_output_init(ast); 1068 break; 1069 case AST_TX_ASTDP: 1070 ret = ast_astdp_output_init(ast); 1071 break; 1072 } 1073 if (ret) 1074 return ret; 1075 1076 drm_mode_config_reset(dev); 1077 drmm_kms_helper_poll_init(dev); 1078 1079 return 0; 1080 } 1081