1 /* 2 * Copyright © 2009 3 * 4 * Permission is hereby granted, free of charge, to any person obtaining a 5 * copy of this software and associated documentation files (the "Software"), 6 * to deal in the Software without restriction, including without limitation 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8 * and/or sell copies of the Software, and to permit persons to whom the 9 * Software is furnished to do so, subject to the following conditions: 10 * 11 * The above copyright notice and this permission notice (including the next 12 * paragraph) shall be included in all copies or substantial portions of the 13 * Software. 14 * 15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 * SOFTWARE. 22 * 23 * Authors: 24 * Daniel Vetter <daniel@ffwll.ch> 25 * 26 * Derived from Xorg ddx, xf86-video-intel, src/i830_video.c 27 */ 28 29 #include <drm/drm_fourcc.h> 30 31 #include "gem/i915_gem_pm.h" 32 #include "gt/intel_gpu_commands.h" 33 #include "gt/intel_ring.h" 34 35 #include "i915_drv.h" 36 #include "i915_reg.h" 37 #include "intel_de.h" 38 #include "intel_display_types.h" 39 #include "intel_frontbuffer.h" 40 #include "intel_overlay.h" 41 #include "intel_pci_config.h" 42 43 /* Limits for overlay size. According to intel doc, the real limits are: 44 * Y width: 4095, UV width (planar): 2047, Y height: 2047, 45 * UV width (planar): * 1023. But the xorg thinks 2048 for height and width. Use 46 * the mininum of both. */ 47 #define IMAGE_MAX_WIDTH 2048 48 #define IMAGE_MAX_HEIGHT 2046 /* 2 * 1023 */ 49 /* on 830 and 845 these large limits result in the card hanging */ 50 #define IMAGE_MAX_WIDTH_LEGACY 1024 51 #define IMAGE_MAX_HEIGHT_LEGACY 1088 52 53 /* overlay register definitions */ 54 /* OCMD register */ 55 #define OCMD_TILED_SURFACE (0x1<<19) 56 #define OCMD_MIRROR_MASK (0x3<<17) 57 #define OCMD_MIRROR_MODE (0x3<<17) 58 #define OCMD_MIRROR_HORIZONTAL (0x1<<17) 59 #define OCMD_MIRROR_VERTICAL (0x2<<17) 60 #define OCMD_MIRROR_BOTH (0x3<<17) 61 #define OCMD_BYTEORDER_MASK (0x3<<14) /* zero for YUYV or FOURCC YUY2 */ 62 #define OCMD_UV_SWAP (0x1<<14) /* YVYU */ 63 #define OCMD_Y_SWAP (0x2<<14) /* UYVY or FOURCC UYVY */ 64 #define OCMD_Y_AND_UV_SWAP (0x3<<14) /* VYUY */ 65 #define OCMD_SOURCE_FORMAT_MASK (0xf<<10) 66 #define OCMD_RGB_888 (0x1<<10) /* not in i965 Intel docs */ 67 #define OCMD_RGB_555 (0x2<<10) /* not in i965 Intel docs */ 68 #define OCMD_RGB_565 (0x3<<10) /* not in i965 Intel docs */ 69 #define OCMD_YUV_422_PACKED (0x8<<10) 70 #define OCMD_YUV_411_PACKED (0x9<<10) /* not in i965 Intel docs */ 71 #define OCMD_YUV_420_PLANAR (0xc<<10) 72 #define OCMD_YUV_422_PLANAR (0xd<<10) 73 #define OCMD_YUV_410_PLANAR (0xe<<10) /* also 411 */ 74 #define OCMD_TVSYNCFLIP_PARITY (0x1<<9) 75 #define OCMD_TVSYNCFLIP_ENABLE (0x1<<7) 76 #define OCMD_BUF_TYPE_MASK (0x1<<5) 77 #define OCMD_BUF_TYPE_FRAME (0x0<<5) 78 #define OCMD_BUF_TYPE_FIELD (0x1<<5) 79 #define OCMD_TEST_MODE (0x1<<4) 80 #define OCMD_BUFFER_SELECT (0x3<<2) 81 #define OCMD_BUFFER0 (0x0<<2) 82 #define OCMD_BUFFER1 (0x1<<2) 83 #define OCMD_FIELD_SELECT (0x1<<2) 84 #define OCMD_FIELD0 (0x0<<1) 85 #define OCMD_FIELD1 (0x1<<1) 86 #define OCMD_ENABLE (0x1<<0) 87 88 /* OCONFIG register */ 89 #define OCONF_PIPE_MASK (0x1<<18) 90 #define OCONF_PIPE_A (0x0<<18) 91 #define OCONF_PIPE_B (0x1<<18) 92 #define OCONF_GAMMA2_ENABLE (0x1<<16) 93 #define OCONF_CSC_MODE_BT601 (0x0<<5) 94 #define OCONF_CSC_MODE_BT709 (0x1<<5) 95 #define OCONF_CSC_BYPASS (0x1<<4) 96 #define OCONF_CC_OUT_8BIT (0x1<<3) 97 #define OCONF_TEST_MODE (0x1<<2) 98 #define OCONF_THREE_LINE_BUFFER (0x1<<0) 99 #define OCONF_TWO_LINE_BUFFER (0x0<<0) 100 101 /* DCLRKM (dst-key) register */ 102 #define DST_KEY_ENABLE (0x1<<31) 103 #define CLK_RGB24_MASK 0x0 104 #define CLK_RGB16_MASK 0x070307 105 #define CLK_RGB15_MASK 0x070707 106 107 #define RGB30_TO_COLORKEY(c) \ 108 ((((c) & 0x3fc00000) >> 6) | (((c) & 0x000ff000) >> 4) | (((c) & 0x000003fc) >> 2)) 109 #define RGB16_TO_COLORKEY(c) \ 110 ((((c) & 0xf800) << 8) | (((c) & 0x07e0) << 5) | (((c) & 0x001f) << 3)) 111 #define RGB15_TO_COLORKEY(c) \ 112 ((((c) & 0x7c00) << 9) | (((c) & 0x03e0) << 6) | (((c) & 0x001f) << 3)) 113 #define RGB8I_TO_COLORKEY(c) \ 114 ((((c) & 0xff) << 16) | (((c) & 0xff) << 8) | (((c) & 0xff) << 0)) 115 116 /* overlay flip addr flag */ 117 #define OFC_UPDATE 0x1 118 119 /* polyphase filter coefficients */ 120 #define N_HORIZ_Y_TAPS 5 121 #define N_VERT_Y_TAPS 3 122 #define N_HORIZ_UV_TAPS 3 123 #define N_VERT_UV_TAPS 3 124 #define N_PHASES 17 125 #define MAX_TAPS 5 126 127 /* memory bufferd overlay registers */ 128 struct overlay_registers { 129 u32 OBUF_0Y; 130 u32 OBUF_1Y; 131 u32 OBUF_0U; 132 u32 OBUF_0V; 133 u32 OBUF_1U; 134 u32 OBUF_1V; 135 u32 OSTRIDE; 136 u32 YRGB_VPH; 137 u32 UV_VPH; 138 u32 HORZ_PH; 139 u32 INIT_PHS; 140 u32 DWINPOS; 141 u32 DWINSZ; 142 u32 SWIDTH; 143 u32 SWIDTHSW; 144 u32 SHEIGHT; 145 u32 YRGBSCALE; 146 u32 UVSCALE; 147 u32 OCLRC0; 148 u32 OCLRC1; 149 u32 DCLRKV; 150 u32 DCLRKM; 151 u32 SCLRKVH; 152 u32 SCLRKVL; 153 u32 SCLRKEN; 154 u32 OCONFIG; 155 u32 OCMD; 156 u32 RESERVED1; /* 0x6C */ 157 u32 OSTART_0Y; 158 u32 OSTART_1Y; 159 u32 OSTART_0U; 160 u32 OSTART_0V; 161 u32 OSTART_1U; 162 u32 OSTART_1V; 163 u32 OTILEOFF_0Y; 164 u32 OTILEOFF_1Y; 165 u32 OTILEOFF_0U; 166 u32 OTILEOFF_0V; 167 u32 OTILEOFF_1U; 168 u32 OTILEOFF_1V; 169 u32 FASTHSCALE; /* 0xA0 */ 170 u32 UVSCALEV; /* 0xA4 */ 171 u32 RESERVEDC[(0x200 - 0xA8) / 4]; /* 0xA8 - 0x1FC */ 172 u16 Y_VCOEFS[N_VERT_Y_TAPS * N_PHASES]; /* 0x200 */ 173 u16 RESERVEDD[0x100 / 2 - N_VERT_Y_TAPS * N_PHASES]; 174 u16 Y_HCOEFS[N_HORIZ_Y_TAPS * N_PHASES]; /* 0x300 */ 175 u16 RESERVEDE[0x200 / 2 - N_HORIZ_Y_TAPS * N_PHASES]; 176 u16 UV_VCOEFS[N_VERT_UV_TAPS * N_PHASES]; /* 0x500 */ 177 u16 RESERVEDF[0x100 / 2 - N_VERT_UV_TAPS * N_PHASES]; 178 u16 UV_HCOEFS[N_HORIZ_UV_TAPS * N_PHASES]; /* 0x600 */ 179 u16 RESERVEDG[0x100 / 2 - N_HORIZ_UV_TAPS * N_PHASES]; 180 }; 181 182 struct intel_overlay { 183 struct drm_i915_private *i915; 184 struct intel_context *context; 185 struct intel_crtc *crtc; 186 struct i915_vma *vma; 187 struct i915_vma *old_vma; 188 struct intel_frontbuffer *frontbuffer; 189 bool active; 190 bool pfit_active; 191 u32 pfit_vscale_ratio; /* shifted-point number, (1<<12) == 1.0 */ 192 u32 color_key:24; 193 u32 color_key_enabled:1; 194 u32 brightness, contrast, saturation; 195 u32 old_xscale, old_yscale; 196 /* register access */ 197 struct drm_i915_gem_object *reg_bo; 198 struct overlay_registers __iomem *regs; 199 u32 flip_addr; 200 /* flip handling */ 201 struct i915_active last_flip; 202 void (*flip_complete)(struct intel_overlay *ovl); 203 }; 204 205 static void i830_overlay_clock_gating(struct drm_i915_private *dev_priv, 206 bool enable) 207 { 208 struct pci_dev *pdev = to_pci_dev(dev_priv->drm.dev); 209 u8 val; 210 211 /* WA_OVERLAY_CLKGATE:alm */ 212 if (enable) 213 intel_de_write(dev_priv, DSPCLK_GATE_D, 0); 214 else 215 intel_de_write(dev_priv, DSPCLK_GATE_D, 216 OVRUNIT_CLOCK_GATE_DISABLE); 217 218 /* WA_DISABLE_L2CACHE_CLOCK_GATING:alm */ 219 pci_bus_read_config_byte(pdev->bus, 220 PCI_DEVFN(0, 0), I830_CLOCK_GATE, &val); 221 if (enable) 222 val &= ~I830_L2_CACHE_CLOCK_GATE_DISABLE; 223 else 224 val |= I830_L2_CACHE_CLOCK_GATE_DISABLE; 225 pci_bus_write_config_byte(pdev->bus, 226 PCI_DEVFN(0, 0), I830_CLOCK_GATE, val); 227 } 228 229 static struct i915_request * 230 alloc_request(struct intel_overlay *overlay, void (*fn)(struct intel_overlay *)) 231 { 232 struct i915_request *rq; 233 int err; 234 235 overlay->flip_complete = fn; 236 237 rq = i915_request_create(overlay->context); 238 if (IS_ERR(rq)) 239 return rq; 240 241 err = i915_active_add_request(&overlay->last_flip, rq); 242 if (err) { 243 i915_request_add(rq); 244 return ERR_PTR(err); 245 } 246 247 return rq; 248 } 249 250 /* overlay needs to be disable in OCMD reg */ 251 static int intel_overlay_on(struct intel_overlay *overlay) 252 { 253 struct drm_i915_private *dev_priv = overlay->i915; 254 struct i915_request *rq; 255 u32 *cs; 256 257 drm_WARN_ON(&dev_priv->drm, overlay->active); 258 259 rq = alloc_request(overlay, NULL); 260 if (IS_ERR(rq)) 261 return PTR_ERR(rq); 262 263 cs = intel_ring_begin(rq, 4); 264 if (IS_ERR(cs)) { 265 i915_request_add(rq); 266 return PTR_ERR(cs); 267 } 268 269 overlay->active = true; 270 271 if (IS_I830(dev_priv)) 272 i830_overlay_clock_gating(dev_priv, false); 273 274 *cs++ = MI_OVERLAY_FLIP | MI_OVERLAY_ON; 275 *cs++ = overlay->flip_addr | OFC_UPDATE; 276 *cs++ = MI_WAIT_FOR_EVENT | MI_WAIT_FOR_OVERLAY_FLIP; 277 *cs++ = MI_NOOP; 278 intel_ring_advance(rq, cs); 279 280 i915_request_add(rq); 281 282 return i915_active_wait(&overlay->last_flip); 283 } 284 285 static void intel_overlay_flip_prepare(struct intel_overlay *overlay, 286 struct i915_vma *vma) 287 { 288 enum pipe pipe = overlay->crtc->pipe; 289 struct intel_frontbuffer *frontbuffer = NULL; 290 291 drm_WARN_ON(&overlay->i915->drm, overlay->old_vma); 292 293 if (vma) 294 frontbuffer = intel_frontbuffer_get(vma->obj); 295 296 intel_frontbuffer_track(overlay->frontbuffer, frontbuffer, 297 INTEL_FRONTBUFFER_OVERLAY(pipe)); 298 299 if (overlay->frontbuffer) 300 intel_frontbuffer_put(overlay->frontbuffer); 301 overlay->frontbuffer = frontbuffer; 302 303 intel_frontbuffer_flip_prepare(overlay->i915, 304 INTEL_FRONTBUFFER_OVERLAY(pipe)); 305 306 overlay->old_vma = overlay->vma; 307 if (vma) 308 overlay->vma = i915_vma_get(vma); 309 else 310 overlay->vma = NULL; 311 } 312 313 /* overlay needs to be enabled in OCMD reg */ 314 static int intel_overlay_continue(struct intel_overlay *overlay, 315 struct i915_vma *vma, 316 bool load_polyphase_filter) 317 { 318 struct drm_i915_private *dev_priv = overlay->i915; 319 struct i915_request *rq; 320 u32 flip_addr = overlay->flip_addr; 321 u32 tmp, *cs; 322 323 drm_WARN_ON(&dev_priv->drm, !overlay->active); 324 325 if (load_polyphase_filter) 326 flip_addr |= OFC_UPDATE; 327 328 /* check for underruns */ 329 tmp = intel_de_read(dev_priv, DOVSTA); 330 if (tmp & (1 << 17)) 331 drm_dbg(&dev_priv->drm, "overlay underrun, DOVSTA: %x\n", tmp); 332 333 rq = alloc_request(overlay, NULL); 334 if (IS_ERR(rq)) 335 return PTR_ERR(rq); 336 337 cs = intel_ring_begin(rq, 2); 338 if (IS_ERR(cs)) { 339 i915_request_add(rq); 340 return PTR_ERR(cs); 341 } 342 343 *cs++ = MI_OVERLAY_FLIP | MI_OVERLAY_CONTINUE; 344 *cs++ = flip_addr; 345 intel_ring_advance(rq, cs); 346 347 intel_overlay_flip_prepare(overlay, vma); 348 i915_request_add(rq); 349 350 return 0; 351 } 352 353 static void intel_overlay_release_old_vma(struct intel_overlay *overlay) 354 { 355 struct i915_vma *vma; 356 357 vma = fetch_and_zero(&overlay->old_vma); 358 if (drm_WARN_ON(&overlay->i915->drm, !vma)) 359 return; 360 361 intel_frontbuffer_flip_complete(overlay->i915, 362 INTEL_FRONTBUFFER_OVERLAY(overlay->crtc->pipe)); 363 364 i915_vma_unpin(vma); 365 i915_vma_put(vma); 366 } 367 368 static void 369 intel_overlay_release_old_vid_tail(struct intel_overlay *overlay) 370 { 371 intel_overlay_release_old_vma(overlay); 372 } 373 374 static void intel_overlay_off_tail(struct intel_overlay *overlay) 375 { 376 struct drm_i915_private *dev_priv = overlay->i915; 377 378 intel_overlay_release_old_vma(overlay); 379 380 overlay->crtc->overlay = NULL; 381 overlay->crtc = NULL; 382 overlay->active = false; 383 384 if (IS_I830(dev_priv)) 385 i830_overlay_clock_gating(dev_priv, true); 386 } 387 388 static void intel_overlay_last_flip_retire(struct i915_active *active) 389 { 390 struct intel_overlay *overlay = 391 container_of(active, typeof(*overlay), last_flip); 392 393 if (overlay->flip_complete) 394 overlay->flip_complete(overlay); 395 } 396 397 /* overlay needs to be disabled in OCMD reg */ 398 static int intel_overlay_off(struct intel_overlay *overlay) 399 { 400 struct i915_request *rq; 401 u32 *cs, flip_addr = overlay->flip_addr; 402 403 drm_WARN_ON(&overlay->i915->drm, !overlay->active); 404 405 /* According to intel docs the overlay hw may hang (when switching 406 * off) without loading the filter coeffs. It is however unclear whether 407 * this applies to the disabling of the overlay or to the switching off 408 * of the hw. Do it in both cases */ 409 flip_addr |= OFC_UPDATE; 410 411 rq = alloc_request(overlay, intel_overlay_off_tail); 412 if (IS_ERR(rq)) 413 return PTR_ERR(rq); 414 415 cs = intel_ring_begin(rq, 6); 416 if (IS_ERR(cs)) { 417 i915_request_add(rq); 418 return PTR_ERR(cs); 419 } 420 421 /* wait for overlay to go idle */ 422 *cs++ = MI_OVERLAY_FLIP | MI_OVERLAY_CONTINUE; 423 *cs++ = flip_addr; 424 *cs++ = MI_WAIT_FOR_EVENT | MI_WAIT_FOR_OVERLAY_FLIP; 425 426 /* turn overlay off */ 427 *cs++ = MI_OVERLAY_FLIP | MI_OVERLAY_OFF; 428 *cs++ = flip_addr; 429 *cs++ = MI_WAIT_FOR_EVENT | MI_WAIT_FOR_OVERLAY_FLIP; 430 431 intel_ring_advance(rq, cs); 432 433 intel_overlay_flip_prepare(overlay, NULL); 434 i915_request_add(rq); 435 436 return i915_active_wait(&overlay->last_flip); 437 } 438 439 /* recover from an interruption due to a signal 440 * We have to be careful not to repeat work forever an make forward progess. */ 441 static int intel_overlay_recover_from_interrupt(struct intel_overlay *overlay) 442 { 443 return i915_active_wait(&overlay->last_flip); 444 } 445 446 /* Wait for pending overlay flip and release old frame. 447 * Needs to be called before the overlay register are changed 448 * via intel_overlay_(un)map_regs 449 */ 450 static int intel_overlay_release_old_vid(struct intel_overlay *overlay) 451 { 452 struct drm_i915_private *dev_priv = overlay->i915; 453 struct i915_request *rq; 454 u32 *cs; 455 456 /* 457 * Only wait if there is actually an old frame to release to 458 * guarantee forward progress. 459 */ 460 if (!overlay->old_vma) 461 return 0; 462 463 if (!(intel_de_read(dev_priv, GEN2_ISR) & I915_OVERLAY_PLANE_FLIP_PENDING_INTERRUPT)) { 464 intel_overlay_release_old_vid_tail(overlay); 465 return 0; 466 } 467 468 rq = alloc_request(overlay, intel_overlay_release_old_vid_tail); 469 if (IS_ERR(rq)) 470 return PTR_ERR(rq); 471 472 cs = intel_ring_begin(rq, 2); 473 if (IS_ERR(cs)) { 474 i915_request_add(rq); 475 return PTR_ERR(cs); 476 } 477 478 *cs++ = MI_WAIT_FOR_EVENT | MI_WAIT_FOR_OVERLAY_FLIP; 479 *cs++ = MI_NOOP; 480 intel_ring_advance(rq, cs); 481 482 i915_request_add(rq); 483 484 return i915_active_wait(&overlay->last_flip); 485 } 486 487 void intel_overlay_reset(struct drm_i915_private *dev_priv) 488 { 489 struct intel_overlay *overlay = dev_priv->overlay; 490 491 if (!overlay) 492 return; 493 494 overlay->old_xscale = 0; 495 overlay->old_yscale = 0; 496 overlay->crtc = NULL; 497 overlay->active = false; 498 } 499 500 static int packed_depth_bytes(u32 format) 501 { 502 switch (format & I915_OVERLAY_DEPTH_MASK) { 503 case I915_OVERLAY_YUV422: 504 return 4; 505 case I915_OVERLAY_YUV411: 506 /* return 6; not implemented */ 507 default: 508 return -EINVAL; 509 } 510 } 511 512 static int packed_width_bytes(u32 format, short width) 513 { 514 switch (format & I915_OVERLAY_DEPTH_MASK) { 515 case I915_OVERLAY_YUV422: 516 return width << 1; 517 default: 518 return -EINVAL; 519 } 520 } 521 522 static int uv_hsubsampling(u32 format) 523 { 524 switch (format & I915_OVERLAY_DEPTH_MASK) { 525 case I915_OVERLAY_YUV422: 526 case I915_OVERLAY_YUV420: 527 return 2; 528 case I915_OVERLAY_YUV411: 529 case I915_OVERLAY_YUV410: 530 return 4; 531 default: 532 return -EINVAL; 533 } 534 } 535 536 static int uv_vsubsampling(u32 format) 537 { 538 switch (format & I915_OVERLAY_DEPTH_MASK) { 539 case I915_OVERLAY_YUV420: 540 case I915_OVERLAY_YUV410: 541 return 2; 542 case I915_OVERLAY_YUV422: 543 case I915_OVERLAY_YUV411: 544 return 1; 545 default: 546 return -EINVAL; 547 } 548 } 549 550 static u32 calc_swidthsw(struct drm_i915_private *dev_priv, u32 offset, u32 width) 551 { 552 u32 sw; 553 554 if (DISPLAY_VER(dev_priv) == 2) 555 sw = ALIGN((offset & 31) + width, 32); 556 else 557 sw = ALIGN((offset & 63) + width, 64); 558 559 if (sw == 0) 560 return 0; 561 562 return (sw - 32) >> 3; 563 } 564 565 static const u16 y_static_hcoeffs[N_PHASES][N_HORIZ_Y_TAPS] = { 566 [ 0] = { 0x3000, 0xb4a0, 0x1930, 0x1920, 0xb4a0, }, 567 [ 1] = { 0x3000, 0xb500, 0x19d0, 0x1880, 0xb440, }, 568 [ 2] = { 0x3000, 0xb540, 0x1a88, 0x2f80, 0xb3e0, }, 569 [ 3] = { 0x3000, 0xb580, 0x1b30, 0x2e20, 0xb380, }, 570 [ 4] = { 0x3000, 0xb5c0, 0x1bd8, 0x2cc0, 0xb320, }, 571 [ 5] = { 0x3020, 0xb5e0, 0x1c60, 0x2b80, 0xb2c0, }, 572 [ 6] = { 0x3020, 0xb5e0, 0x1cf8, 0x2a20, 0xb260, }, 573 [ 7] = { 0x3020, 0xb5e0, 0x1d80, 0x28e0, 0xb200, }, 574 [ 8] = { 0x3020, 0xb5c0, 0x1e08, 0x3f40, 0xb1c0, }, 575 [ 9] = { 0x3020, 0xb580, 0x1e78, 0x3ce0, 0xb160, }, 576 [10] = { 0x3040, 0xb520, 0x1ed8, 0x3aa0, 0xb120, }, 577 [11] = { 0x3040, 0xb4a0, 0x1f30, 0x3880, 0xb0e0, }, 578 [12] = { 0x3040, 0xb400, 0x1f78, 0x3680, 0xb0a0, }, 579 [13] = { 0x3020, 0xb340, 0x1fb8, 0x34a0, 0xb060, }, 580 [14] = { 0x3020, 0xb240, 0x1fe0, 0x32e0, 0xb040, }, 581 [15] = { 0x3020, 0xb140, 0x1ff8, 0x3160, 0xb020, }, 582 [16] = { 0xb000, 0x3000, 0x0800, 0x3000, 0xb000, }, 583 }; 584 585 static const u16 uv_static_hcoeffs[N_PHASES][N_HORIZ_UV_TAPS] = { 586 [ 0] = { 0x3000, 0x1800, 0x1800, }, 587 [ 1] = { 0xb000, 0x18d0, 0x2e60, }, 588 [ 2] = { 0xb000, 0x1990, 0x2ce0, }, 589 [ 3] = { 0xb020, 0x1a68, 0x2b40, }, 590 [ 4] = { 0xb040, 0x1b20, 0x29e0, }, 591 [ 5] = { 0xb060, 0x1bd8, 0x2880, }, 592 [ 6] = { 0xb080, 0x1c88, 0x3e60, }, 593 [ 7] = { 0xb0a0, 0x1d28, 0x3c00, }, 594 [ 8] = { 0xb0c0, 0x1db8, 0x39e0, }, 595 [ 9] = { 0xb0e0, 0x1e40, 0x37e0, }, 596 [10] = { 0xb100, 0x1eb8, 0x3620, }, 597 [11] = { 0xb100, 0x1f18, 0x34a0, }, 598 [12] = { 0xb100, 0x1f68, 0x3360, }, 599 [13] = { 0xb0e0, 0x1fa8, 0x3240, }, 600 [14] = { 0xb0c0, 0x1fe0, 0x3140, }, 601 [15] = { 0xb060, 0x1ff0, 0x30a0, }, 602 [16] = { 0x3000, 0x0800, 0x3000, }, 603 }; 604 605 static void update_polyphase_filter(struct overlay_registers __iomem *regs) 606 { 607 memcpy_toio(regs->Y_HCOEFS, y_static_hcoeffs, sizeof(y_static_hcoeffs)); 608 memcpy_toio(regs->UV_HCOEFS, uv_static_hcoeffs, 609 sizeof(uv_static_hcoeffs)); 610 } 611 612 static bool update_scaling_factors(struct intel_overlay *overlay, 613 struct overlay_registers __iomem *regs, 614 struct drm_intel_overlay_put_image *params) 615 { 616 /* fixed point with a 12 bit shift */ 617 u32 xscale, yscale, xscale_UV, yscale_UV; 618 #define FP_SHIFT 12 619 #define FRACT_MASK 0xfff 620 bool scale_changed = false; 621 int uv_hscale = uv_hsubsampling(params->flags); 622 int uv_vscale = uv_vsubsampling(params->flags); 623 624 if (params->dst_width > 1) 625 xscale = ((params->src_scan_width - 1) << FP_SHIFT) / 626 params->dst_width; 627 else 628 xscale = 1 << FP_SHIFT; 629 630 if (params->dst_height > 1) 631 yscale = ((params->src_scan_height - 1) << FP_SHIFT) / 632 params->dst_height; 633 else 634 yscale = 1 << FP_SHIFT; 635 636 /*if (params->format & I915_OVERLAY_YUV_PLANAR) {*/ 637 xscale_UV = xscale/uv_hscale; 638 yscale_UV = yscale/uv_vscale; 639 /* make the Y scale to UV scale ratio an exact multiply */ 640 xscale = xscale_UV * uv_hscale; 641 yscale = yscale_UV * uv_vscale; 642 /*} else { 643 xscale_UV = 0; 644 yscale_UV = 0; 645 }*/ 646 647 if (xscale != overlay->old_xscale || yscale != overlay->old_yscale) 648 scale_changed = true; 649 overlay->old_xscale = xscale; 650 overlay->old_yscale = yscale; 651 652 iowrite32(((yscale & FRACT_MASK) << 20) | 653 ((xscale >> FP_SHIFT) << 16) | 654 ((xscale & FRACT_MASK) << 3), 655 ®s->YRGBSCALE); 656 657 iowrite32(((yscale_UV & FRACT_MASK) << 20) | 658 ((xscale_UV >> FP_SHIFT) << 16) | 659 ((xscale_UV & FRACT_MASK) << 3), 660 ®s->UVSCALE); 661 662 iowrite32((((yscale >> FP_SHIFT) << 16) | 663 ((yscale_UV >> FP_SHIFT) << 0)), 664 ®s->UVSCALEV); 665 666 if (scale_changed) 667 update_polyphase_filter(regs); 668 669 return scale_changed; 670 } 671 672 static void update_colorkey(struct intel_overlay *overlay, 673 struct overlay_registers __iomem *regs) 674 { 675 const struct intel_plane_state *state = 676 to_intel_plane_state(overlay->crtc->base.primary->state); 677 u32 key = overlay->color_key; 678 u32 format = 0; 679 u32 flags = 0; 680 681 if (overlay->color_key_enabled) 682 flags |= DST_KEY_ENABLE; 683 684 if (state->uapi.visible) 685 format = state->hw.fb->format->format; 686 687 switch (format) { 688 case DRM_FORMAT_C8: 689 key = RGB8I_TO_COLORKEY(key); 690 flags |= CLK_RGB24_MASK; 691 break; 692 case DRM_FORMAT_XRGB1555: 693 key = RGB15_TO_COLORKEY(key); 694 flags |= CLK_RGB15_MASK; 695 break; 696 case DRM_FORMAT_RGB565: 697 key = RGB16_TO_COLORKEY(key); 698 flags |= CLK_RGB16_MASK; 699 break; 700 case DRM_FORMAT_XRGB2101010: 701 case DRM_FORMAT_XBGR2101010: 702 key = RGB30_TO_COLORKEY(key); 703 flags |= CLK_RGB24_MASK; 704 break; 705 default: 706 flags |= CLK_RGB24_MASK; 707 break; 708 } 709 710 iowrite32(key, ®s->DCLRKV); 711 iowrite32(flags, ®s->DCLRKM); 712 } 713 714 static u32 overlay_cmd_reg(struct drm_intel_overlay_put_image *params) 715 { 716 u32 cmd = OCMD_ENABLE | OCMD_BUF_TYPE_FRAME | OCMD_BUFFER0; 717 718 if (params->flags & I915_OVERLAY_YUV_PLANAR) { 719 switch (params->flags & I915_OVERLAY_DEPTH_MASK) { 720 case I915_OVERLAY_YUV422: 721 cmd |= OCMD_YUV_422_PLANAR; 722 break; 723 case I915_OVERLAY_YUV420: 724 cmd |= OCMD_YUV_420_PLANAR; 725 break; 726 case I915_OVERLAY_YUV411: 727 case I915_OVERLAY_YUV410: 728 cmd |= OCMD_YUV_410_PLANAR; 729 break; 730 } 731 } else { /* YUV packed */ 732 switch (params->flags & I915_OVERLAY_DEPTH_MASK) { 733 case I915_OVERLAY_YUV422: 734 cmd |= OCMD_YUV_422_PACKED; 735 break; 736 case I915_OVERLAY_YUV411: 737 cmd |= OCMD_YUV_411_PACKED; 738 break; 739 } 740 741 switch (params->flags & I915_OVERLAY_SWAP_MASK) { 742 case I915_OVERLAY_NO_SWAP: 743 break; 744 case I915_OVERLAY_UV_SWAP: 745 cmd |= OCMD_UV_SWAP; 746 break; 747 case I915_OVERLAY_Y_SWAP: 748 cmd |= OCMD_Y_SWAP; 749 break; 750 case I915_OVERLAY_Y_AND_UV_SWAP: 751 cmd |= OCMD_Y_AND_UV_SWAP; 752 break; 753 } 754 } 755 756 return cmd; 757 } 758 759 static struct i915_vma *intel_overlay_pin_fb(struct drm_i915_gem_object *new_bo) 760 { 761 struct i915_gem_ww_ctx ww; 762 struct i915_vma *vma; 763 int ret; 764 765 i915_gem_ww_ctx_init(&ww, true); 766 retry: 767 ret = i915_gem_object_lock(new_bo, &ww); 768 if (!ret) { 769 vma = i915_gem_object_pin_to_display_plane(new_bo, &ww, 0, 770 NULL, PIN_MAPPABLE); 771 ret = PTR_ERR_OR_ZERO(vma); 772 } 773 if (ret == -EDEADLK) { 774 ret = i915_gem_ww_ctx_backoff(&ww); 775 if (!ret) 776 goto retry; 777 } 778 i915_gem_ww_ctx_fini(&ww); 779 if (ret) 780 return ERR_PTR(ret); 781 782 return vma; 783 } 784 785 static int intel_overlay_do_put_image(struct intel_overlay *overlay, 786 struct drm_i915_gem_object *new_bo, 787 struct drm_intel_overlay_put_image *params) 788 { 789 struct overlay_registers __iomem *regs = overlay->regs; 790 struct drm_i915_private *dev_priv = overlay->i915; 791 u32 swidth, swidthsw, sheight, ostride; 792 enum pipe pipe = overlay->crtc->pipe; 793 bool scale_changed = false; 794 struct i915_vma *vma; 795 int ret, tmp_width; 796 797 drm_WARN_ON(&dev_priv->drm, 798 !drm_modeset_is_locked(&dev_priv->drm.mode_config.connection_mutex)); 799 800 ret = intel_overlay_release_old_vid(overlay); 801 if (ret != 0) 802 return ret; 803 804 atomic_inc(&dev_priv->gpu_error.pending_fb_pin); 805 806 vma = intel_overlay_pin_fb(new_bo); 807 if (IS_ERR(vma)) { 808 ret = PTR_ERR(vma); 809 goto out_pin_section; 810 } 811 812 i915_gem_object_flush_frontbuffer(new_bo, ORIGIN_DIRTYFB); 813 814 if (!overlay->active) { 815 const struct intel_crtc_state *crtc_state = 816 overlay->crtc->config; 817 u32 oconfig = 0; 818 819 if (crtc_state->gamma_enable && 820 crtc_state->gamma_mode == GAMMA_MODE_MODE_8BIT) 821 oconfig |= OCONF_CC_OUT_8BIT; 822 if (crtc_state->gamma_enable) 823 oconfig |= OCONF_GAMMA2_ENABLE; 824 if (DISPLAY_VER(dev_priv) == 4) 825 oconfig |= OCONF_CSC_MODE_BT709; 826 oconfig |= pipe == 0 ? 827 OCONF_PIPE_A : OCONF_PIPE_B; 828 iowrite32(oconfig, ®s->OCONFIG); 829 830 ret = intel_overlay_on(overlay); 831 if (ret != 0) 832 goto out_unpin; 833 } 834 835 iowrite32(params->dst_y << 16 | params->dst_x, ®s->DWINPOS); 836 iowrite32(params->dst_height << 16 | params->dst_width, ®s->DWINSZ); 837 838 if (params->flags & I915_OVERLAY_YUV_PACKED) 839 tmp_width = packed_width_bytes(params->flags, 840 params->src_width); 841 else 842 tmp_width = params->src_width; 843 844 swidth = params->src_width; 845 swidthsw = calc_swidthsw(dev_priv, params->offset_Y, tmp_width); 846 sheight = params->src_height; 847 iowrite32(i915_ggtt_offset(vma) + params->offset_Y, ®s->OBUF_0Y); 848 ostride = params->stride_Y; 849 850 if (params->flags & I915_OVERLAY_YUV_PLANAR) { 851 int uv_hscale = uv_hsubsampling(params->flags); 852 int uv_vscale = uv_vsubsampling(params->flags); 853 u32 tmp_U, tmp_V; 854 855 swidth |= (params->src_width / uv_hscale) << 16; 856 sheight |= (params->src_height / uv_vscale) << 16; 857 858 tmp_U = calc_swidthsw(dev_priv, params->offset_U, 859 params->src_width / uv_hscale); 860 tmp_V = calc_swidthsw(dev_priv, params->offset_V, 861 params->src_width / uv_hscale); 862 swidthsw |= max(tmp_U, tmp_V) << 16; 863 864 iowrite32(i915_ggtt_offset(vma) + params->offset_U, 865 ®s->OBUF_0U); 866 iowrite32(i915_ggtt_offset(vma) + params->offset_V, 867 ®s->OBUF_0V); 868 869 ostride |= params->stride_UV << 16; 870 } 871 872 iowrite32(swidth, ®s->SWIDTH); 873 iowrite32(swidthsw, ®s->SWIDTHSW); 874 iowrite32(sheight, ®s->SHEIGHT); 875 iowrite32(ostride, ®s->OSTRIDE); 876 877 scale_changed = update_scaling_factors(overlay, regs, params); 878 879 update_colorkey(overlay, regs); 880 881 iowrite32(overlay_cmd_reg(params), ®s->OCMD); 882 883 ret = intel_overlay_continue(overlay, vma, scale_changed); 884 if (ret) 885 goto out_unpin; 886 887 return 0; 888 889 out_unpin: 890 i915_vma_unpin(vma); 891 out_pin_section: 892 atomic_dec(&dev_priv->gpu_error.pending_fb_pin); 893 894 return ret; 895 } 896 897 int intel_overlay_switch_off(struct intel_overlay *overlay) 898 { 899 struct drm_i915_private *dev_priv = overlay->i915; 900 int ret; 901 902 drm_WARN_ON(&dev_priv->drm, 903 !drm_modeset_is_locked(&dev_priv->drm.mode_config.connection_mutex)); 904 905 ret = intel_overlay_recover_from_interrupt(overlay); 906 if (ret != 0) 907 return ret; 908 909 if (!overlay->active) 910 return 0; 911 912 ret = intel_overlay_release_old_vid(overlay); 913 if (ret != 0) 914 return ret; 915 916 iowrite32(0, &overlay->regs->OCMD); 917 918 return intel_overlay_off(overlay); 919 } 920 921 static int check_overlay_possible_on_crtc(struct intel_overlay *overlay, 922 struct intel_crtc *crtc) 923 { 924 if (!crtc->active) 925 return -EINVAL; 926 927 /* can't use the overlay with double wide pipe */ 928 if (crtc->config->double_wide) 929 return -EINVAL; 930 931 return 0; 932 } 933 934 static void update_pfit_vscale_ratio(struct intel_overlay *overlay) 935 { 936 struct drm_i915_private *dev_priv = overlay->i915; 937 u32 pfit_control = intel_de_read(dev_priv, PFIT_CONTROL); 938 u32 ratio; 939 940 /* XXX: This is not the same logic as in the xorg driver, but more in 941 * line with the intel documentation for the i965 942 */ 943 if (DISPLAY_VER(dev_priv) >= 4) { 944 /* on i965 use the PGM reg to read out the autoscaler values */ 945 ratio = intel_de_read(dev_priv, PFIT_PGM_RATIOS) >> PFIT_VERT_SCALE_SHIFT_965; 946 } else { 947 if (pfit_control & VERT_AUTO_SCALE) 948 ratio = intel_de_read(dev_priv, PFIT_AUTO_RATIOS); 949 else 950 ratio = intel_de_read(dev_priv, PFIT_PGM_RATIOS); 951 ratio >>= PFIT_VERT_SCALE_SHIFT; 952 } 953 954 overlay->pfit_vscale_ratio = ratio; 955 } 956 957 static int check_overlay_dst(struct intel_overlay *overlay, 958 struct drm_intel_overlay_put_image *rec) 959 { 960 const struct intel_crtc_state *pipe_config = 961 overlay->crtc->config; 962 963 if (rec->dst_height == 0 || rec->dst_width == 0) 964 return -EINVAL; 965 966 if (rec->dst_x < pipe_config->pipe_src_w && 967 rec->dst_x + rec->dst_width <= pipe_config->pipe_src_w && 968 rec->dst_y < pipe_config->pipe_src_h && 969 rec->dst_y + rec->dst_height <= pipe_config->pipe_src_h) 970 return 0; 971 else 972 return -EINVAL; 973 } 974 975 static int check_overlay_scaling(struct drm_intel_overlay_put_image *rec) 976 { 977 u32 tmp; 978 979 /* downscaling limit is 8.0 */ 980 tmp = ((rec->src_scan_height << 16) / rec->dst_height) >> 16; 981 if (tmp > 7) 982 return -EINVAL; 983 984 tmp = ((rec->src_scan_width << 16) / rec->dst_width) >> 16; 985 if (tmp > 7) 986 return -EINVAL; 987 988 return 0; 989 } 990 991 static int check_overlay_src(struct drm_i915_private *dev_priv, 992 struct drm_intel_overlay_put_image *rec, 993 struct drm_i915_gem_object *new_bo) 994 { 995 int uv_hscale = uv_hsubsampling(rec->flags); 996 int uv_vscale = uv_vsubsampling(rec->flags); 997 u32 stride_mask; 998 int depth; 999 u32 tmp; 1000 1001 /* check src dimensions */ 1002 if (IS_I845G(dev_priv) || IS_I830(dev_priv)) { 1003 if (rec->src_height > IMAGE_MAX_HEIGHT_LEGACY || 1004 rec->src_width > IMAGE_MAX_WIDTH_LEGACY) 1005 return -EINVAL; 1006 } else { 1007 if (rec->src_height > IMAGE_MAX_HEIGHT || 1008 rec->src_width > IMAGE_MAX_WIDTH) 1009 return -EINVAL; 1010 } 1011 1012 /* better safe than sorry, use 4 as the maximal subsampling ratio */ 1013 if (rec->src_height < N_VERT_Y_TAPS*4 || 1014 rec->src_width < N_HORIZ_Y_TAPS*4) 1015 return -EINVAL; 1016 1017 /* check alignment constraints */ 1018 switch (rec->flags & I915_OVERLAY_TYPE_MASK) { 1019 case I915_OVERLAY_RGB: 1020 /* not implemented */ 1021 return -EINVAL; 1022 1023 case I915_OVERLAY_YUV_PACKED: 1024 if (uv_vscale != 1) 1025 return -EINVAL; 1026 1027 depth = packed_depth_bytes(rec->flags); 1028 if (depth < 0) 1029 return depth; 1030 1031 /* ignore UV planes */ 1032 rec->stride_UV = 0; 1033 rec->offset_U = 0; 1034 rec->offset_V = 0; 1035 /* check pixel alignment */ 1036 if (rec->offset_Y % depth) 1037 return -EINVAL; 1038 break; 1039 1040 case I915_OVERLAY_YUV_PLANAR: 1041 if (uv_vscale < 0 || uv_hscale < 0) 1042 return -EINVAL; 1043 /* no offset restrictions for planar formats */ 1044 break; 1045 1046 default: 1047 return -EINVAL; 1048 } 1049 1050 if (rec->src_width % uv_hscale) 1051 return -EINVAL; 1052 1053 /* stride checking */ 1054 if (IS_I830(dev_priv) || IS_I845G(dev_priv)) 1055 stride_mask = 255; 1056 else 1057 stride_mask = 63; 1058 1059 if (rec->stride_Y & stride_mask || rec->stride_UV & stride_mask) 1060 return -EINVAL; 1061 if (DISPLAY_VER(dev_priv) == 4 && rec->stride_Y < 512) 1062 return -EINVAL; 1063 1064 tmp = (rec->flags & I915_OVERLAY_TYPE_MASK) == I915_OVERLAY_YUV_PLANAR ? 1065 4096 : 8192; 1066 if (rec->stride_Y > tmp || rec->stride_UV > 2*1024) 1067 return -EINVAL; 1068 1069 /* check buffer dimensions */ 1070 switch (rec->flags & I915_OVERLAY_TYPE_MASK) { 1071 case I915_OVERLAY_RGB: 1072 case I915_OVERLAY_YUV_PACKED: 1073 /* always 4 Y values per depth pixels */ 1074 if (packed_width_bytes(rec->flags, rec->src_width) > rec->stride_Y) 1075 return -EINVAL; 1076 1077 tmp = rec->stride_Y*rec->src_height; 1078 if (rec->offset_Y + tmp > new_bo->base.size) 1079 return -EINVAL; 1080 break; 1081 1082 case I915_OVERLAY_YUV_PLANAR: 1083 if (rec->src_width > rec->stride_Y) 1084 return -EINVAL; 1085 if (rec->src_width/uv_hscale > rec->stride_UV) 1086 return -EINVAL; 1087 1088 tmp = rec->stride_Y * rec->src_height; 1089 if (rec->offset_Y + tmp > new_bo->base.size) 1090 return -EINVAL; 1091 1092 tmp = rec->stride_UV * (rec->src_height / uv_vscale); 1093 if (rec->offset_U + tmp > new_bo->base.size || 1094 rec->offset_V + tmp > new_bo->base.size) 1095 return -EINVAL; 1096 break; 1097 } 1098 1099 return 0; 1100 } 1101 1102 int intel_overlay_put_image_ioctl(struct drm_device *dev, void *data, 1103 struct drm_file *file_priv) 1104 { 1105 struct drm_intel_overlay_put_image *params = data; 1106 struct drm_i915_private *dev_priv = to_i915(dev); 1107 struct intel_overlay *overlay; 1108 struct drm_crtc *drmmode_crtc; 1109 struct intel_crtc *crtc; 1110 struct drm_i915_gem_object *new_bo; 1111 int ret; 1112 1113 overlay = dev_priv->overlay; 1114 if (!overlay) { 1115 drm_dbg(&dev_priv->drm, "userspace bug: no overlay\n"); 1116 return -ENODEV; 1117 } 1118 1119 if (!(params->flags & I915_OVERLAY_ENABLE)) { 1120 drm_modeset_lock_all(dev); 1121 ret = intel_overlay_switch_off(overlay); 1122 drm_modeset_unlock_all(dev); 1123 1124 return ret; 1125 } 1126 1127 drmmode_crtc = drm_crtc_find(dev, file_priv, params->crtc_id); 1128 if (!drmmode_crtc) 1129 return -ENOENT; 1130 crtc = to_intel_crtc(drmmode_crtc); 1131 1132 new_bo = i915_gem_object_lookup(file_priv, params->bo_handle); 1133 if (!new_bo) 1134 return -ENOENT; 1135 1136 drm_modeset_lock_all(dev); 1137 1138 if (i915_gem_object_is_tiled(new_bo)) { 1139 drm_dbg_kms(&dev_priv->drm, 1140 "buffer used for overlay image can not be tiled\n"); 1141 ret = -EINVAL; 1142 goto out_unlock; 1143 } 1144 1145 ret = intel_overlay_recover_from_interrupt(overlay); 1146 if (ret != 0) 1147 goto out_unlock; 1148 1149 if (overlay->crtc != crtc) { 1150 ret = intel_overlay_switch_off(overlay); 1151 if (ret != 0) 1152 goto out_unlock; 1153 1154 ret = check_overlay_possible_on_crtc(overlay, crtc); 1155 if (ret != 0) 1156 goto out_unlock; 1157 1158 overlay->crtc = crtc; 1159 crtc->overlay = overlay; 1160 1161 /* line too wide, i.e. one-line-mode */ 1162 if (crtc->config->pipe_src_w > 1024 && 1163 crtc->config->gmch_pfit.control & PFIT_ENABLE) { 1164 overlay->pfit_active = true; 1165 update_pfit_vscale_ratio(overlay); 1166 } else 1167 overlay->pfit_active = false; 1168 } 1169 1170 ret = check_overlay_dst(overlay, params); 1171 if (ret != 0) 1172 goto out_unlock; 1173 1174 if (overlay->pfit_active) { 1175 params->dst_y = (((u32)params->dst_y << 12) / 1176 overlay->pfit_vscale_ratio); 1177 /* shifting right rounds downwards, so add 1 */ 1178 params->dst_height = (((u32)params->dst_height << 12) / 1179 overlay->pfit_vscale_ratio) + 1; 1180 } 1181 1182 if (params->src_scan_height > params->src_height || 1183 params->src_scan_width > params->src_width) { 1184 ret = -EINVAL; 1185 goto out_unlock; 1186 } 1187 1188 ret = check_overlay_src(dev_priv, params, new_bo); 1189 if (ret != 0) 1190 goto out_unlock; 1191 1192 /* Check scaling after src size to prevent a divide-by-zero. */ 1193 ret = check_overlay_scaling(params); 1194 if (ret != 0) 1195 goto out_unlock; 1196 1197 ret = intel_overlay_do_put_image(overlay, new_bo, params); 1198 if (ret != 0) 1199 goto out_unlock; 1200 1201 drm_modeset_unlock_all(dev); 1202 i915_gem_object_put(new_bo); 1203 1204 return 0; 1205 1206 out_unlock: 1207 drm_modeset_unlock_all(dev); 1208 i915_gem_object_put(new_bo); 1209 1210 return ret; 1211 } 1212 1213 static void update_reg_attrs(struct intel_overlay *overlay, 1214 struct overlay_registers __iomem *regs) 1215 { 1216 iowrite32((overlay->contrast << 18) | (overlay->brightness & 0xff), 1217 ®s->OCLRC0); 1218 iowrite32(overlay->saturation, ®s->OCLRC1); 1219 } 1220 1221 static bool check_gamma_bounds(u32 gamma1, u32 gamma2) 1222 { 1223 int i; 1224 1225 if (gamma1 & 0xff000000 || gamma2 & 0xff000000) 1226 return false; 1227 1228 for (i = 0; i < 3; i++) { 1229 if (((gamma1 >> i*8) & 0xff) >= ((gamma2 >> i*8) & 0xff)) 1230 return false; 1231 } 1232 1233 return true; 1234 } 1235 1236 static bool check_gamma5_errata(u32 gamma5) 1237 { 1238 int i; 1239 1240 for (i = 0; i < 3; i++) { 1241 if (((gamma5 >> i*8) & 0xff) == 0x80) 1242 return false; 1243 } 1244 1245 return true; 1246 } 1247 1248 static int check_gamma(struct drm_intel_overlay_attrs *attrs) 1249 { 1250 if (!check_gamma_bounds(0, attrs->gamma0) || 1251 !check_gamma_bounds(attrs->gamma0, attrs->gamma1) || 1252 !check_gamma_bounds(attrs->gamma1, attrs->gamma2) || 1253 !check_gamma_bounds(attrs->gamma2, attrs->gamma3) || 1254 !check_gamma_bounds(attrs->gamma3, attrs->gamma4) || 1255 !check_gamma_bounds(attrs->gamma4, attrs->gamma5) || 1256 !check_gamma_bounds(attrs->gamma5, 0x00ffffff)) 1257 return -EINVAL; 1258 1259 if (!check_gamma5_errata(attrs->gamma5)) 1260 return -EINVAL; 1261 1262 return 0; 1263 } 1264 1265 int intel_overlay_attrs_ioctl(struct drm_device *dev, void *data, 1266 struct drm_file *file_priv) 1267 { 1268 struct drm_intel_overlay_attrs *attrs = data; 1269 struct drm_i915_private *dev_priv = to_i915(dev); 1270 struct intel_overlay *overlay; 1271 int ret; 1272 1273 overlay = dev_priv->overlay; 1274 if (!overlay) { 1275 drm_dbg(&dev_priv->drm, "userspace bug: no overlay\n"); 1276 return -ENODEV; 1277 } 1278 1279 drm_modeset_lock_all(dev); 1280 1281 ret = -EINVAL; 1282 if (!(attrs->flags & I915_OVERLAY_UPDATE_ATTRS)) { 1283 attrs->color_key = overlay->color_key; 1284 attrs->brightness = overlay->brightness; 1285 attrs->contrast = overlay->contrast; 1286 attrs->saturation = overlay->saturation; 1287 1288 if (DISPLAY_VER(dev_priv) != 2) { 1289 attrs->gamma0 = intel_de_read(dev_priv, OGAMC0); 1290 attrs->gamma1 = intel_de_read(dev_priv, OGAMC1); 1291 attrs->gamma2 = intel_de_read(dev_priv, OGAMC2); 1292 attrs->gamma3 = intel_de_read(dev_priv, OGAMC3); 1293 attrs->gamma4 = intel_de_read(dev_priv, OGAMC4); 1294 attrs->gamma5 = intel_de_read(dev_priv, OGAMC5); 1295 } 1296 } else { 1297 if (attrs->brightness < -128 || attrs->brightness > 127) 1298 goto out_unlock; 1299 if (attrs->contrast > 255) 1300 goto out_unlock; 1301 if (attrs->saturation > 1023) 1302 goto out_unlock; 1303 1304 overlay->color_key = attrs->color_key; 1305 overlay->brightness = attrs->brightness; 1306 overlay->contrast = attrs->contrast; 1307 overlay->saturation = attrs->saturation; 1308 1309 update_reg_attrs(overlay, overlay->regs); 1310 1311 if (attrs->flags & I915_OVERLAY_UPDATE_GAMMA) { 1312 if (DISPLAY_VER(dev_priv) == 2) 1313 goto out_unlock; 1314 1315 if (overlay->active) { 1316 ret = -EBUSY; 1317 goto out_unlock; 1318 } 1319 1320 ret = check_gamma(attrs); 1321 if (ret) 1322 goto out_unlock; 1323 1324 intel_de_write(dev_priv, OGAMC0, attrs->gamma0); 1325 intel_de_write(dev_priv, OGAMC1, attrs->gamma1); 1326 intel_de_write(dev_priv, OGAMC2, attrs->gamma2); 1327 intel_de_write(dev_priv, OGAMC3, attrs->gamma3); 1328 intel_de_write(dev_priv, OGAMC4, attrs->gamma4); 1329 intel_de_write(dev_priv, OGAMC5, attrs->gamma5); 1330 } 1331 } 1332 overlay->color_key_enabled = (attrs->flags & I915_OVERLAY_DISABLE_DEST_COLORKEY) == 0; 1333 1334 ret = 0; 1335 out_unlock: 1336 drm_modeset_unlock_all(dev); 1337 1338 return ret; 1339 } 1340 1341 static int get_registers(struct intel_overlay *overlay, bool use_phys) 1342 { 1343 struct drm_i915_private *i915 = overlay->i915; 1344 struct drm_i915_gem_object *obj; 1345 struct i915_vma *vma; 1346 int err; 1347 1348 obj = i915_gem_object_create_stolen(i915, PAGE_SIZE); 1349 if (IS_ERR(obj)) 1350 obj = i915_gem_object_create_internal(i915, PAGE_SIZE); 1351 if (IS_ERR(obj)) 1352 return PTR_ERR(obj); 1353 1354 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0, PIN_MAPPABLE); 1355 if (IS_ERR(vma)) { 1356 err = PTR_ERR(vma); 1357 goto err_put_bo; 1358 } 1359 1360 if (use_phys) 1361 overlay->flip_addr = sg_dma_address(obj->mm.pages->sgl); 1362 else 1363 overlay->flip_addr = i915_ggtt_offset(vma); 1364 overlay->regs = i915_vma_pin_iomap(vma); 1365 i915_vma_unpin(vma); 1366 1367 if (IS_ERR(overlay->regs)) { 1368 err = PTR_ERR(overlay->regs); 1369 goto err_put_bo; 1370 } 1371 1372 overlay->reg_bo = obj; 1373 return 0; 1374 1375 err_put_bo: 1376 i915_gem_object_put(obj); 1377 return err; 1378 } 1379 1380 void intel_overlay_setup(struct drm_i915_private *dev_priv) 1381 { 1382 struct intel_overlay *overlay; 1383 struct intel_engine_cs *engine; 1384 int ret; 1385 1386 if (!HAS_OVERLAY(dev_priv)) 1387 return; 1388 1389 engine = to_gt(dev_priv)->engine[RCS0]; 1390 if (!engine || !engine->kernel_context) 1391 return; 1392 1393 overlay = kzalloc(sizeof(*overlay), GFP_KERNEL); 1394 if (!overlay) 1395 return; 1396 1397 overlay->i915 = dev_priv; 1398 overlay->context = engine->kernel_context; 1399 GEM_BUG_ON(!overlay->context); 1400 1401 overlay->color_key = 0x0101fe; 1402 overlay->color_key_enabled = true; 1403 overlay->brightness = -19; 1404 overlay->contrast = 75; 1405 overlay->saturation = 146; 1406 1407 i915_active_init(&overlay->last_flip, 1408 NULL, intel_overlay_last_flip_retire, 0); 1409 1410 ret = get_registers(overlay, OVERLAY_NEEDS_PHYSICAL(dev_priv)); 1411 if (ret) 1412 goto out_free; 1413 1414 memset_io(overlay->regs, 0, sizeof(struct overlay_registers)); 1415 update_polyphase_filter(overlay->regs); 1416 update_reg_attrs(overlay, overlay->regs); 1417 1418 dev_priv->overlay = overlay; 1419 drm_info(&dev_priv->drm, "Initialized overlay support.\n"); 1420 return; 1421 1422 out_free: 1423 kfree(overlay); 1424 } 1425 1426 void intel_overlay_cleanup(struct drm_i915_private *dev_priv) 1427 { 1428 struct intel_overlay *overlay; 1429 1430 overlay = fetch_and_zero(&dev_priv->overlay); 1431 if (!overlay) 1432 return; 1433 1434 /* 1435 * The bo's should be free'd by the generic code already. 1436 * Furthermore modesetting teardown happens beforehand so the 1437 * hardware should be off already. 1438 */ 1439 drm_WARN_ON(&dev_priv->drm, overlay->active); 1440 1441 i915_gem_object_put(overlay->reg_bo); 1442 i915_active_fini(&overlay->last_flip); 1443 1444 kfree(overlay); 1445 } 1446 1447 #if IS_ENABLED(CONFIG_DRM_I915_CAPTURE_ERROR) 1448 1449 struct intel_overlay_error_state { 1450 struct overlay_registers regs; 1451 unsigned long base; 1452 u32 dovsta; 1453 u32 isr; 1454 }; 1455 1456 struct intel_overlay_error_state * 1457 intel_overlay_capture_error_state(struct drm_i915_private *dev_priv) 1458 { 1459 struct intel_overlay *overlay = dev_priv->overlay; 1460 struct intel_overlay_error_state *error; 1461 1462 if (!overlay || !overlay->active) 1463 return NULL; 1464 1465 error = kmalloc(sizeof(*error), GFP_ATOMIC); 1466 if (error == NULL) 1467 return NULL; 1468 1469 error->dovsta = intel_de_read(dev_priv, DOVSTA); 1470 error->isr = intel_de_read(dev_priv, GEN2_ISR); 1471 error->base = overlay->flip_addr; 1472 1473 memcpy_fromio(&error->regs, overlay->regs, sizeof(error->regs)); 1474 1475 return error; 1476 } 1477 1478 void 1479 intel_overlay_print_error_state(struct drm_i915_error_state_buf *m, 1480 struct intel_overlay_error_state *error) 1481 { 1482 i915_error_printf(m, "Overlay, status: 0x%08x, interrupt: 0x%08x\n", 1483 error->dovsta, error->isr); 1484 i915_error_printf(m, " Register file at 0x%08lx:\n", 1485 error->base); 1486 1487 #define P(x) i915_error_printf(m, " " #x ": 0x%08x\n", error->regs.x) 1488 P(OBUF_0Y); 1489 P(OBUF_1Y); 1490 P(OBUF_0U); 1491 P(OBUF_0V); 1492 P(OBUF_1U); 1493 P(OBUF_1V); 1494 P(OSTRIDE); 1495 P(YRGB_VPH); 1496 P(UV_VPH); 1497 P(HORZ_PH); 1498 P(INIT_PHS); 1499 P(DWINPOS); 1500 P(DWINSZ); 1501 P(SWIDTH); 1502 P(SWIDTHSW); 1503 P(SHEIGHT); 1504 P(YRGBSCALE); 1505 P(UVSCALE); 1506 P(OCLRC0); 1507 P(OCLRC1); 1508 P(DCLRKV); 1509 P(DCLRKM); 1510 P(SCLRKVH); 1511 P(SCLRKVL); 1512 P(SCLRKEN); 1513 P(OCONFIG); 1514 P(OCMD); 1515 P(OSTART_0Y); 1516 P(OSTART_1Y); 1517 P(OSTART_0U); 1518 P(OSTART_0V); 1519 P(OSTART_1U); 1520 P(OSTART_1V); 1521 P(OTILEOFF_0Y); 1522 P(OTILEOFF_1Y); 1523 P(OTILEOFF_0U); 1524 P(OTILEOFF_0V); 1525 P(OTILEOFF_1U); 1526 P(OTILEOFF_1V); 1527 P(FASTHSCALE); 1528 P(UVSCALEV); 1529 #undef P 1530 } 1531 1532 #endif 1533