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:
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
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
26 * Derived from Xorg ddx, xf86-video-intel, src/i830_video.c
29 #include <drm/drm_fourcc.h>
30 #include <drm/i915_drm.h>
34 #include "intel_drv.h"
35 #include "intel_frontbuffer.h"
36 #include "intel_overlay.h"
38 /* Limits for overlay size. According to intel doc, the real limits are:
39 * Y width: 4095, UV width (planar): 2047, Y height: 2047,
40 * UV width (planar): * 1023. But the xorg thinks 2048 for height and width. Use
41 * the mininum of both. */
42 #define IMAGE_MAX_WIDTH 2048
43 #define IMAGE_MAX_HEIGHT 2046 /* 2 * 1023 */
44 /* on 830 and 845 these large limits result in the card hanging */
45 #define IMAGE_MAX_WIDTH_LEGACY 1024
46 #define IMAGE_MAX_HEIGHT_LEGACY 1088
48 /* overlay register definitions */
50 #define OCMD_TILED_SURFACE (0x1<<19)
51 #define OCMD_MIRROR_MASK (0x3<<17)
52 #define OCMD_MIRROR_MODE (0x3<<17)
53 #define OCMD_MIRROR_HORIZONTAL (0x1<<17)
54 #define OCMD_MIRROR_VERTICAL (0x2<<17)
55 #define OCMD_MIRROR_BOTH (0x3<<17)
56 #define OCMD_BYTEORDER_MASK (0x3<<14) /* zero for YUYV or FOURCC YUY2 */
57 #define OCMD_UV_SWAP (0x1<<14) /* YVYU */
58 #define OCMD_Y_SWAP (0x2<<14) /* UYVY or FOURCC UYVY */
59 #define OCMD_Y_AND_UV_SWAP (0x3<<14) /* VYUY */
60 #define OCMD_SOURCE_FORMAT_MASK (0xf<<10)
61 #define OCMD_RGB_888 (0x1<<10) /* not in i965 Intel docs */
62 #define OCMD_RGB_555 (0x2<<10) /* not in i965 Intel docs */
63 #define OCMD_RGB_565 (0x3<<10) /* not in i965 Intel docs */
64 #define OCMD_YUV_422_PACKED (0x8<<10)
65 #define OCMD_YUV_411_PACKED (0x9<<10) /* not in i965 Intel docs */
66 #define OCMD_YUV_420_PLANAR (0xc<<10)
67 #define OCMD_YUV_422_PLANAR (0xd<<10)
68 #define OCMD_YUV_410_PLANAR (0xe<<10) /* also 411 */
69 #define OCMD_TVSYNCFLIP_PARITY (0x1<<9)
70 #define OCMD_TVSYNCFLIP_ENABLE (0x1<<7)
71 #define OCMD_BUF_TYPE_MASK (0x1<<5)
72 #define OCMD_BUF_TYPE_FRAME (0x0<<5)
73 #define OCMD_BUF_TYPE_FIELD (0x1<<5)
74 #define OCMD_TEST_MODE (0x1<<4)
75 #define OCMD_BUFFER_SELECT (0x3<<2)
76 #define OCMD_BUFFER0 (0x0<<2)
77 #define OCMD_BUFFER1 (0x1<<2)
78 #define OCMD_FIELD_SELECT (0x1<<2)
79 #define OCMD_FIELD0 (0x0<<1)
80 #define OCMD_FIELD1 (0x1<<1)
81 #define OCMD_ENABLE (0x1<<0)
83 /* OCONFIG register */
84 #define OCONF_PIPE_MASK (0x1<<18)
85 #define OCONF_PIPE_A (0x0<<18)
86 #define OCONF_PIPE_B (0x1<<18)
87 #define OCONF_GAMMA2_ENABLE (0x1<<16)
88 #define OCONF_CSC_MODE_BT601 (0x0<<5)
89 #define OCONF_CSC_MODE_BT709 (0x1<<5)
90 #define OCONF_CSC_BYPASS (0x1<<4)
91 #define OCONF_CC_OUT_8BIT (0x1<<3)
92 #define OCONF_TEST_MODE (0x1<<2)
93 #define OCONF_THREE_LINE_BUFFER (0x1<<0)
94 #define OCONF_TWO_LINE_BUFFER (0x0<<0)
96 /* DCLRKM (dst-key) register */
97 #define DST_KEY_ENABLE (0x1<<31)
98 #define CLK_RGB24_MASK 0x0
99 #define CLK_RGB16_MASK 0x070307
100 #define CLK_RGB15_MASK 0x070707
101 #define CLK_RGB8I_MASK 0xffffff
103 #define RGB16_TO_COLORKEY(c) \
104 (((c & 0xF800) << 8) | ((c & 0x07E0) << 5) | ((c & 0x001F) << 3))
105 #define RGB15_TO_COLORKEY(c) \
106 (((c & 0x7c00) << 9) | ((c & 0x03E0) << 6) | ((c & 0x001F) << 3))
108 /* overlay flip addr flag */
109 #define OFC_UPDATE 0x1
111 /* polyphase filter coefficients */
112 #define N_HORIZ_Y_TAPS 5
113 #define N_VERT_Y_TAPS 3
114 #define N_HORIZ_UV_TAPS 3
115 #define N_VERT_UV_TAPS 3
119 /* memory bufferd overlay registers */
120 struct overlay_registers {
148 u32 RESERVED1; /* 0x6C */
161 u32 FASTHSCALE; /* 0xA0 */
162 u32 UVSCALEV; /* 0xA4 */
163 u32 RESERVEDC[(0x200 - 0xA8) / 4]; /* 0xA8 - 0x1FC */
164 u16 Y_VCOEFS[N_VERT_Y_TAPS * N_PHASES]; /* 0x200 */
165 u16 RESERVEDD[0x100 / 2 - N_VERT_Y_TAPS * N_PHASES];
166 u16 Y_HCOEFS[N_HORIZ_Y_TAPS * N_PHASES]; /* 0x300 */
167 u16 RESERVEDE[0x200 / 2 - N_HORIZ_Y_TAPS * N_PHASES];
168 u16 UV_VCOEFS[N_VERT_UV_TAPS * N_PHASES]; /* 0x500 */
169 u16 RESERVEDF[0x100 / 2 - N_VERT_UV_TAPS * N_PHASES];
170 u16 UV_HCOEFS[N_HORIZ_UV_TAPS * N_PHASES]; /* 0x600 */
171 u16 RESERVEDG[0x100 / 2 - N_HORIZ_UV_TAPS * N_PHASES];
174 struct intel_overlay {
175 struct drm_i915_private *i915;
176 struct intel_crtc *crtc;
177 struct i915_vma *vma;
178 struct i915_vma *old_vma;
181 u32 pfit_vscale_ratio; /* shifted-point number, (1<<12) == 1.0 */
183 u32 color_key_enabled:1;
184 u32 brightness, contrast, saturation;
185 u32 old_xscale, old_yscale;
186 /* register access */
187 struct drm_i915_gem_object *reg_bo;
188 struct overlay_registers __iomem *regs;
191 struct i915_active_request last_flip;
194 static void i830_overlay_clock_gating(struct drm_i915_private *dev_priv,
197 struct pci_dev *pdev = dev_priv->drm.pdev;
200 /* WA_OVERLAY_CLKGATE:alm */
202 I915_WRITE(DSPCLK_GATE_D, 0);
204 I915_WRITE(DSPCLK_GATE_D, OVRUNIT_CLOCK_GATE_DISABLE);
206 /* WA_DISABLE_L2CACHE_CLOCK_GATING:alm */
207 pci_bus_read_config_byte(pdev->bus,
208 PCI_DEVFN(0, 0), I830_CLOCK_GATE, &val);
210 val &= ~I830_L2_CACHE_CLOCK_GATE_DISABLE;
212 val |= I830_L2_CACHE_CLOCK_GATE_DISABLE;
213 pci_bus_write_config_byte(pdev->bus,
214 PCI_DEVFN(0, 0), I830_CLOCK_GATE, val);
217 static void intel_overlay_submit_request(struct intel_overlay *overlay,
218 struct i915_request *rq,
219 i915_active_retire_fn retire)
221 GEM_BUG_ON(i915_active_request_peek(&overlay->last_flip,
222 &overlay->i915->drm.struct_mutex));
223 i915_active_request_set_retire_fn(&overlay->last_flip, retire,
224 &overlay->i915->drm.struct_mutex);
225 __i915_active_request_set(&overlay->last_flip, rq);
226 i915_request_add(rq);
229 static int intel_overlay_do_wait_request(struct intel_overlay *overlay,
230 struct i915_request *rq,
231 i915_active_retire_fn retire)
233 intel_overlay_submit_request(overlay, rq, retire);
234 return i915_active_request_retire(&overlay->last_flip,
235 &overlay->i915->drm.struct_mutex);
238 static struct i915_request *alloc_request(struct intel_overlay *overlay)
240 struct intel_engine_cs *engine = overlay->i915->engine[RCS0];
242 return i915_request_create(engine->kernel_context);
245 /* overlay needs to be disable in OCMD reg */
246 static int intel_overlay_on(struct intel_overlay *overlay)
248 struct drm_i915_private *dev_priv = overlay->i915;
249 struct i915_request *rq;
252 WARN_ON(overlay->active);
254 rq = alloc_request(overlay);
258 cs = intel_ring_begin(rq, 4);
260 i915_request_add(rq);
264 overlay->active = true;
266 if (IS_I830(dev_priv))
267 i830_overlay_clock_gating(dev_priv, false);
269 *cs++ = MI_OVERLAY_FLIP | MI_OVERLAY_ON;
270 *cs++ = overlay->flip_addr | OFC_UPDATE;
271 *cs++ = MI_WAIT_FOR_EVENT | MI_WAIT_FOR_OVERLAY_FLIP;
273 intel_ring_advance(rq, cs);
275 return intel_overlay_do_wait_request(overlay, rq, NULL);
278 static void intel_overlay_flip_prepare(struct intel_overlay *overlay,
279 struct i915_vma *vma)
281 enum pipe pipe = overlay->crtc->pipe;
283 WARN_ON(overlay->old_vma);
285 i915_gem_track_fb(overlay->vma ? overlay->vma->obj : NULL,
286 vma ? vma->obj : NULL,
287 INTEL_FRONTBUFFER_OVERLAY(pipe));
289 intel_frontbuffer_flip_prepare(overlay->i915,
290 INTEL_FRONTBUFFER_OVERLAY(pipe));
292 overlay->old_vma = overlay->vma;
294 overlay->vma = i915_vma_get(vma);
299 /* overlay needs to be enabled in OCMD reg */
300 static int intel_overlay_continue(struct intel_overlay *overlay,
301 struct i915_vma *vma,
302 bool load_polyphase_filter)
304 struct drm_i915_private *dev_priv = overlay->i915;
305 struct i915_request *rq;
306 u32 flip_addr = overlay->flip_addr;
309 WARN_ON(!overlay->active);
311 if (load_polyphase_filter)
312 flip_addr |= OFC_UPDATE;
314 /* check for underruns */
315 tmp = I915_READ(DOVSTA);
317 DRM_DEBUG("overlay underrun, DOVSTA: %x\n", tmp);
319 rq = alloc_request(overlay);
323 cs = intel_ring_begin(rq, 2);
325 i915_request_add(rq);
329 *cs++ = MI_OVERLAY_FLIP | MI_OVERLAY_CONTINUE;
331 intel_ring_advance(rq, cs);
333 intel_overlay_flip_prepare(overlay, vma);
335 intel_overlay_submit_request(overlay, rq, NULL);
340 static void intel_overlay_release_old_vma(struct intel_overlay *overlay)
342 struct i915_vma *vma;
344 vma = fetch_and_zero(&overlay->old_vma);
348 intel_frontbuffer_flip_complete(overlay->i915,
349 INTEL_FRONTBUFFER_OVERLAY(overlay->crtc->pipe));
351 i915_gem_object_unpin_from_display_plane(vma);
356 intel_overlay_release_old_vid_tail(struct i915_active_request *active,
357 struct i915_request *rq)
359 struct intel_overlay *overlay =
360 container_of(active, typeof(*overlay), last_flip);
362 intel_overlay_release_old_vma(overlay);
365 static void intel_overlay_off_tail(struct i915_active_request *active,
366 struct i915_request *rq)
368 struct intel_overlay *overlay =
369 container_of(active, typeof(*overlay), last_flip);
370 struct drm_i915_private *dev_priv = overlay->i915;
372 intel_overlay_release_old_vma(overlay);
374 overlay->crtc->overlay = NULL;
375 overlay->crtc = NULL;
376 overlay->active = false;
378 if (IS_I830(dev_priv))
379 i830_overlay_clock_gating(dev_priv, true);
382 /* overlay needs to be disabled in OCMD reg */
383 static int intel_overlay_off(struct intel_overlay *overlay)
385 struct i915_request *rq;
386 u32 *cs, flip_addr = overlay->flip_addr;
388 WARN_ON(!overlay->active);
390 /* According to intel docs the overlay hw may hang (when switching
391 * off) without loading the filter coeffs. It is however unclear whether
392 * this applies to the disabling of the overlay or to the switching off
393 * of the hw. Do it in both cases */
394 flip_addr |= OFC_UPDATE;
396 rq = alloc_request(overlay);
400 cs = intel_ring_begin(rq, 6);
402 i915_request_add(rq);
406 /* wait for overlay to go idle */
407 *cs++ = MI_OVERLAY_FLIP | MI_OVERLAY_CONTINUE;
409 *cs++ = MI_WAIT_FOR_EVENT | MI_WAIT_FOR_OVERLAY_FLIP;
411 /* turn overlay off */
412 *cs++ = MI_OVERLAY_FLIP | MI_OVERLAY_OFF;
414 *cs++ = MI_WAIT_FOR_EVENT | MI_WAIT_FOR_OVERLAY_FLIP;
416 intel_ring_advance(rq, cs);
418 intel_overlay_flip_prepare(overlay, NULL);
420 return intel_overlay_do_wait_request(overlay, rq,
421 intel_overlay_off_tail);
424 /* recover from an interruption due to a signal
425 * We have to be careful not to repeat work forever an make forward progess. */
426 static int intel_overlay_recover_from_interrupt(struct intel_overlay *overlay)
428 return i915_active_request_retire(&overlay->last_flip,
429 &overlay->i915->drm.struct_mutex);
432 /* Wait for pending overlay flip and release old frame.
433 * Needs to be called before the overlay register are changed
434 * via intel_overlay_(un)map_regs
436 static int intel_overlay_release_old_vid(struct intel_overlay *overlay)
438 struct drm_i915_private *dev_priv = overlay->i915;
442 lockdep_assert_held(&dev_priv->drm.struct_mutex);
444 /* Only wait if there is actually an old frame to release to
445 * guarantee forward progress.
447 if (!overlay->old_vma)
450 if (I915_READ(GEN2_ISR) & I915_OVERLAY_PLANE_FLIP_PENDING_INTERRUPT) {
451 /* synchronous slowpath */
452 struct i915_request *rq;
454 rq = alloc_request(overlay);
458 cs = intel_ring_begin(rq, 2);
460 i915_request_add(rq);
464 *cs++ = MI_WAIT_FOR_EVENT | MI_WAIT_FOR_OVERLAY_FLIP;
466 intel_ring_advance(rq, cs);
468 ret = intel_overlay_do_wait_request(overlay, rq,
469 intel_overlay_release_old_vid_tail);
473 intel_overlay_release_old_vid_tail(&overlay->last_flip, NULL);
478 void intel_overlay_reset(struct drm_i915_private *dev_priv)
480 struct intel_overlay *overlay = dev_priv->overlay;
485 overlay->old_xscale = 0;
486 overlay->old_yscale = 0;
487 overlay->crtc = NULL;
488 overlay->active = false;
491 static int packed_depth_bytes(u32 format)
493 switch (format & I915_OVERLAY_DEPTH_MASK) {
494 case I915_OVERLAY_YUV422:
496 case I915_OVERLAY_YUV411:
497 /* return 6; not implemented */
503 static int packed_width_bytes(u32 format, short width)
505 switch (format & I915_OVERLAY_DEPTH_MASK) {
506 case I915_OVERLAY_YUV422:
513 static int uv_hsubsampling(u32 format)
515 switch (format & I915_OVERLAY_DEPTH_MASK) {
516 case I915_OVERLAY_YUV422:
517 case I915_OVERLAY_YUV420:
519 case I915_OVERLAY_YUV411:
520 case I915_OVERLAY_YUV410:
527 static int uv_vsubsampling(u32 format)
529 switch (format & I915_OVERLAY_DEPTH_MASK) {
530 case I915_OVERLAY_YUV420:
531 case I915_OVERLAY_YUV410:
533 case I915_OVERLAY_YUV422:
534 case I915_OVERLAY_YUV411:
541 static u32 calc_swidthsw(struct drm_i915_private *dev_priv, u32 offset, u32 width)
545 if (IS_GEN(dev_priv, 2))
546 sw = ALIGN((offset & 31) + width, 32);
548 sw = ALIGN((offset & 63) + width, 64);
553 return (sw - 32) >> 3;
556 static const u16 y_static_hcoeffs[N_PHASES][N_HORIZ_Y_TAPS] = {
557 [ 0] = { 0x3000, 0xb4a0, 0x1930, 0x1920, 0xb4a0, },
558 [ 1] = { 0x3000, 0xb500, 0x19d0, 0x1880, 0xb440, },
559 [ 2] = { 0x3000, 0xb540, 0x1a88, 0x2f80, 0xb3e0, },
560 [ 3] = { 0x3000, 0xb580, 0x1b30, 0x2e20, 0xb380, },
561 [ 4] = { 0x3000, 0xb5c0, 0x1bd8, 0x2cc0, 0xb320, },
562 [ 5] = { 0x3020, 0xb5e0, 0x1c60, 0x2b80, 0xb2c0, },
563 [ 6] = { 0x3020, 0xb5e0, 0x1cf8, 0x2a20, 0xb260, },
564 [ 7] = { 0x3020, 0xb5e0, 0x1d80, 0x28e0, 0xb200, },
565 [ 8] = { 0x3020, 0xb5c0, 0x1e08, 0x3f40, 0xb1c0, },
566 [ 9] = { 0x3020, 0xb580, 0x1e78, 0x3ce0, 0xb160, },
567 [10] = { 0x3040, 0xb520, 0x1ed8, 0x3aa0, 0xb120, },
568 [11] = { 0x3040, 0xb4a0, 0x1f30, 0x3880, 0xb0e0, },
569 [12] = { 0x3040, 0xb400, 0x1f78, 0x3680, 0xb0a0, },
570 [13] = { 0x3020, 0xb340, 0x1fb8, 0x34a0, 0xb060, },
571 [14] = { 0x3020, 0xb240, 0x1fe0, 0x32e0, 0xb040, },
572 [15] = { 0x3020, 0xb140, 0x1ff8, 0x3160, 0xb020, },
573 [16] = { 0xb000, 0x3000, 0x0800, 0x3000, 0xb000, },
576 static const u16 uv_static_hcoeffs[N_PHASES][N_HORIZ_UV_TAPS] = {
577 [ 0] = { 0x3000, 0x1800, 0x1800, },
578 [ 1] = { 0xb000, 0x18d0, 0x2e60, },
579 [ 2] = { 0xb000, 0x1990, 0x2ce0, },
580 [ 3] = { 0xb020, 0x1a68, 0x2b40, },
581 [ 4] = { 0xb040, 0x1b20, 0x29e0, },
582 [ 5] = { 0xb060, 0x1bd8, 0x2880, },
583 [ 6] = { 0xb080, 0x1c88, 0x3e60, },
584 [ 7] = { 0xb0a0, 0x1d28, 0x3c00, },
585 [ 8] = { 0xb0c0, 0x1db8, 0x39e0, },
586 [ 9] = { 0xb0e0, 0x1e40, 0x37e0, },
587 [10] = { 0xb100, 0x1eb8, 0x3620, },
588 [11] = { 0xb100, 0x1f18, 0x34a0, },
589 [12] = { 0xb100, 0x1f68, 0x3360, },
590 [13] = { 0xb0e0, 0x1fa8, 0x3240, },
591 [14] = { 0xb0c0, 0x1fe0, 0x3140, },
592 [15] = { 0xb060, 0x1ff0, 0x30a0, },
593 [16] = { 0x3000, 0x0800, 0x3000, },
596 static void update_polyphase_filter(struct overlay_registers __iomem *regs)
598 memcpy_toio(regs->Y_HCOEFS, y_static_hcoeffs, sizeof(y_static_hcoeffs));
599 memcpy_toio(regs->UV_HCOEFS, uv_static_hcoeffs,
600 sizeof(uv_static_hcoeffs));
603 static bool update_scaling_factors(struct intel_overlay *overlay,
604 struct overlay_registers __iomem *regs,
605 struct drm_intel_overlay_put_image *params)
607 /* fixed point with a 12 bit shift */
608 u32 xscale, yscale, xscale_UV, yscale_UV;
610 #define FRACT_MASK 0xfff
611 bool scale_changed = false;
612 int uv_hscale = uv_hsubsampling(params->flags);
613 int uv_vscale = uv_vsubsampling(params->flags);
615 if (params->dst_width > 1)
616 xscale = ((params->src_scan_width - 1) << FP_SHIFT) /
619 xscale = 1 << FP_SHIFT;
621 if (params->dst_height > 1)
622 yscale = ((params->src_scan_height - 1) << FP_SHIFT) /
625 yscale = 1 << FP_SHIFT;
627 /*if (params->format & I915_OVERLAY_YUV_PLANAR) {*/
628 xscale_UV = xscale/uv_hscale;
629 yscale_UV = yscale/uv_vscale;
630 /* make the Y scale to UV scale ratio an exact multiply */
631 xscale = xscale_UV * uv_hscale;
632 yscale = yscale_UV * uv_vscale;
638 if (xscale != overlay->old_xscale || yscale != overlay->old_yscale)
639 scale_changed = true;
640 overlay->old_xscale = xscale;
641 overlay->old_yscale = yscale;
643 iowrite32(((yscale & FRACT_MASK) << 20) |
644 ((xscale >> FP_SHIFT) << 16) |
645 ((xscale & FRACT_MASK) << 3),
648 iowrite32(((yscale_UV & FRACT_MASK) << 20) |
649 ((xscale_UV >> FP_SHIFT) << 16) |
650 ((xscale_UV & FRACT_MASK) << 3),
653 iowrite32((((yscale >> FP_SHIFT) << 16) |
654 ((yscale_UV >> FP_SHIFT) << 0)),
658 update_polyphase_filter(regs);
660 return scale_changed;
663 static void update_colorkey(struct intel_overlay *overlay,
664 struct overlay_registers __iomem *regs)
666 const struct intel_plane_state *state =
667 to_intel_plane_state(overlay->crtc->base.primary->state);
668 u32 key = overlay->color_key;
672 if (overlay->color_key_enabled)
673 flags |= DST_KEY_ENABLE;
675 if (state->base.visible)
676 format = state->base.fb->format->format;
681 flags |= CLK_RGB8I_MASK;
683 case DRM_FORMAT_XRGB1555:
684 key = RGB15_TO_COLORKEY(key);
685 flags |= CLK_RGB15_MASK;
687 case DRM_FORMAT_RGB565:
688 key = RGB16_TO_COLORKEY(key);
689 flags |= CLK_RGB16_MASK;
692 flags |= CLK_RGB24_MASK;
696 iowrite32(key, ®s->DCLRKV);
697 iowrite32(flags, ®s->DCLRKM);
700 static u32 overlay_cmd_reg(struct drm_intel_overlay_put_image *params)
702 u32 cmd = OCMD_ENABLE | OCMD_BUF_TYPE_FRAME | OCMD_BUFFER0;
704 if (params->flags & I915_OVERLAY_YUV_PLANAR) {
705 switch (params->flags & I915_OVERLAY_DEPTH_MASK) {
706 case I915_OVERLAY_YUV422:
707 cmd |= OCMD_YUV_422_PLANAR;
709 case I915_OVERLAY_YUV420:
710 cmd |= OCMD_YUV_420_PLANAR;
712 case I915_OVERLAY_YUV411:
713 case I915_OVERLAY_YUV410:
714 cmd |= OCMD_YUV_410_PLANAR;
717 } else { /* YUV packed */
718 switch (params->flags & I915_OVERLAY_DEPTH_MASK) {
719 case I915_OVERLAY_YUV422:
720 cmd |= OCMD_YUV_422_PACKED;
722 case I915_OVERLAY_YUV411:
723 cmd |= OCMD_YUV_411_PACKED;
727 switch (params->flags & I915_OVERLAY_SWAP_MASK) {
728 case I915_OVERLAY_NO_SWAP:
730 case I915_OVERLAY_UV_SWAP:
733 case I915_OVERLAY_Y_SWAP:
736 case I915_OVERLAY_Y_AND_UV_SWAP:
737 cmd |= OCMD_Y_AND_UV_SWAP;
745 static int intel_overlay_do_put_image(struct intel_overlay *overlay,
746 struct drm_i915_gem_object *new_bo,
747 struct drm_intel_overlay_put_image *params)
749 struct overlay_registers __iomem *regs = overlay->regs;
750 struct drm_i915_private *dev_priv = overlay->i915;
751 u32 swidth, swidthsw, sheight, ostride;
752 enum pipe pipe = overlay->crtc->pipe;
753 bool scale_changed = false;
754 struct i915_vma *vma;
757 lockdep_assert_held(&dev_priv->drm.struct_mutex);
758 WARN_ON(!drm_modeset_is_locked(&dev_priv->drm.mode_config.connection_mutex));
760 ret = intel_overlay_release_old_vid(overlay);
764 atomic_inc(&dev_priv->gpu_error.pending_fb_pin);
766 vma = i915_gem_object_pin_to_display_plane(new_bo,
767 0, NULL, PIN_MAPPABLE);
770 goto out_pin_section;
772 intel_fb_obj_flush(new_bo, ORIGIN_DIRTYFB);
774 ret = i915_vma_put_fence(vma);
778 if (!overlay->active) {
781 oconfig = OCONF_CC_OUT_8BIT;
782 if (IS_GEN(dev_priv, 4))
783 oconfig |= OCONF_CSC_MODE_BT709;
784 oconfig |= pipe == 0 ?
785 OCONF_PIPE_A : OCONF_PIPE_B;
786 iowrite32(oconfig, ®s->OCONFIG);
788 ret = intel_overlay_on(overlay);
793 iowrite32(params->dst_y << 16 | params->dst_x, ®s->DWINPOS);
794 iowrite32(params->dst_height << 16 | params->dst_width, ®s->DWINSZ);
796 if (params->flags & I915_OVERLAY_YUV_PACKED)
797 tmp_width = packed_width_bytes(params->flags,
800 tmp_width = params->src_width;
802 swidth = params->src_width;
803 swidthsw = calc_swidthsw(dev_priv, params->offset_Y, tmp_width);
804 sheight = params->src_height;
805 iowrite32(i915_ggtt_offset(vma) + params->offset_Y, ®s->OBUF_0Y);
806 ostride = params->stride_Y;
808 if (params->flags & I915_OVERLAY_YUV_PLANAR) {
809 int uv_hscale = uv_hsubsampling(params->flags);
810 int uv_vscale = uv_vsubsampling(params->flags);
813 swidth |= (params->src_width / uv_hscale) << 16;
814 sheight |= (params->src_height / uv_vscale) << 16;
816 tmp_U = calc_swidthsw(dev_priv, params->offset_U,
817 params->src_width / uv_hscale);
818 tmp_V = calc_swidthsw(dev_priv, params->offset_V,
819 params->src_width / uv_hscale);
820 swidthsw |= max(tmp_U, tmp_V) << 16;
822 iowrite32(i915_ggtt_offset(vma) + params->offset_U,
824 iowrite32(i915_ggtt_offset(vma) + params->offset_V,
827 ostride |= params->stride_UV << 16;
830 iowrite32(swidth, ®s->SWIDTH);
831 iowrite32(swidthsw, ®s->SWIDTHSW);
832 iowrite32(sheight, ®s->SHEIGHT);
833 iowrite32(ostride, ®s->OSTRIDE);
835 scale_changed = update_scaling_factors(overlay, regs, params);
837 update_colorkey(overlay, regs);
839 iowrite32(overlay_cmd_reg(params), ®s->OCMD);
841 ret = intel_overlay_continue(overlay, vma, scale_changed);
848 i915_gem_object_unpin_from_display_plane(vma);
850 atomic_dec(&dev_priv->gpu_error.pending_fb_pin);
855 int intel_overlay_switch_off(struct intel_overlay *overlay)
857 struct drm_i915_private *dev_priv = overlay->i915;
860 lockdep_assert_held(&dev_priv->drm.struct_mutex);
861 WARN_ON(!drm_modeset_is_locked(&dev_priv->drm.mode_config.connection_mutex));
863 ret = intel_overlay_recover_from_interrupt(overlay);
867 if (!overlay->active)
870 ret = intel_overlay_release_old_vid(overlay);
874 iowrite32(0, &overlay->regs->OCMD);
876 return intel_overlay_off(overlay);
879 static int check_overlay_possible_on_crtc(struct intel_overlay *overlay,
880 struct intel_crtc *crtc)
885 /* can't use the overlay with double wide pipe */
886 if (crtc->config->double_wide)
892 static void update_pfit_vscale_ratio(struct intel_overlay *overlay)
894 struct drm_i915_private *dev_priv = overlay->i915;
895 u32 pfit_control = I915_READ(PFIT_CONTROL);
898 /* XXX: This is not the same logic as in the xorg driver, but more in
899 * line with the intel documentation for the i965
901 if (INTEL_GEN(dev_priv) >= 4) {
902 /* on i965 use the PGM reg to read out the autoscaler values */
903 ratio = I915_READ(PFIT_PGM_RATIOS) >> PFIT_VERT_SCALE_SHIFT_965;
905 if (pfit_control & VERT_AUTO_SCALE)
906 ratio = I915_READ(PFIT_AUTO_RATIOS);
908 ratio = I915_READ(PFIT_PGM_RATIOS);
909 ratio >>= PFIT_VERT_SCALE_SHIFT;
912 overlay->pfit_vscale_ratio = ratio;
915 static int check_overlay_dst(struct intel_overlay *overlay,
916 struct drm_intel_overlay_put_image *rec)
918 const struct intel_crtc_state *pipe_config =
919 overlay->crtc->config;
921 if (rec->dst_x < pipe_config->pipe_src_w &&
922 rec->dst_x + rec->dst_width <= pipe_config->pipe_src_w &&
923 rec->dst_y < pipe_config->pipe_src_h &&
924 rec->dst_y + rec->dst_height <= pipe_config->pipe_src_h)
930 static int check_overlay_scaling(struct drm_intel_overlay_put_image *rec)
934 /* downscaling limit is 8.0 */
935 tmp = ((rec->src_scan_height << 16) / rec->dst_height) >> 16;
939 tmp = ((rec->src_scan_width << 16) / rec->dst_width) >> 16;
946 static int check_overlay_src(struct drm_i915_private *dev_priv,
947 struct drm_intel_overlay_put_image *rec,
948 struct drm_i915_gem_object *new_bo)
950 int uv_hscale = uv_hsubsampling(rec->flags);
951 int uv_vscale = uv_vsubsampling(rec->flags);
956 /* check src dimensions */
957 if (IS_I845G(dev_priv) || IS_I830(dev_priv)) {
958 if (rec->src_height > IMAGE_MAX_HEIGHT_LEGACY ||
959 rec->src_width > IMAGE_MAX_WIDTH_LEGACY)
962 if (rec->src_height > IMAGE_MAX_HEIGHT ||
963 rec->src_width > IMAGE_MAX_WIDTH)
967 /* better safe than sorry, use 4 as the maximal subsampling ratio */
968 if (rec->src_height < N_VERT_Y_TAPS*4 ||
969 rec->src_width < N_HORIZ_Y_TAPS*4)
972 /* check alignment constraints */
973 switch (rec->flags & I915_OVERLAY_TYPE_MASK) {
974 case I915_OVERLAY_RGB:
975 /* not implemented */
978 case I915_OVERLAY_YUV_PACKED:
982 depth = packed_depth_bytes(rec->flags);
986 /* ignore UV planes */
990 /* check pixel alignment */
991 if (rec->offset_Y % depth)
995 case I915_OVERLAY_YUV_PLANAR:
996 if (uv_vscale < 0 || uv_hscale < 0)
998 /* no offset restrictions for planar formats */
1005 if (rec->src_width % uv_hscale)
1008 /* stride checking */
1009 if (IS_I830(dev_priv) || IS_I845G(dev_priv))
1014 if (rec->stride_Y & stride_mask || rec->stride_UV & stride_mask)
1016 if (IS_GEN(dev_priv, 4) && rec->stride_Y < 512)
1019 tmp = (rec->flags & I915_OVERLAY_TYPE_MASK) == I915_OVERLAY_YUV_PLANAR ?
1021 if (rec->stride_Y > tmp || rec->stride_UV > 2*1024)
1024 /* check buffer dimensions */
1025 switch (rec->flags & I915_OVERLAY_TYPE_MASK) {
1026 case I915_OVERLAY_RGB:
1027 case I915_OVERLAY_YUV_PACKED:
1028 /* always 4 Y values per depth pixels */
1029 if (packed_width_bytes(rec->flags, rec->src_width) > rec->stride_Y)
1032 tmp = rec->stride_Y*rec->src_height;
1033 if (rec->offset_Y + tmp > new_bo->base.size)
1037 case I915_OVERLAY_YUV_PLANAR:
1038 if (rec->src_width > rec->stride_Y)
1040 if (rec->src_width/uv_hscale > rec->stride_UV)
1043 tmp = rec->stride_Y * rec->src_height;
1044 if (rec->offset_Y + tmp > new_bo->base.size)
1047 tmp = rec->stride_UV * (rec->src_height / uv_vscale);
1048 if (rec->offset_U + tmp > new_bo->base.size ||
1049 rec->offset_V + tmp > new_bo->base.size)
1057 int intel_overlay_put_image_ioctl(struct drm_device *dev, void *data,
1058 struct drm_file *file_priv)
1060 struct drm_intel_overlay_put_image *params = data;
1061 struct drm_i915_private *dev_priv = to_i915(dev);
1062 struct intel_overlay *overlay;
1063 struct drm_crtc *drmmode_crtc;
1064 struct intel_crtc *crtc;
1065 struct drm_i915_gem_object *new_bo;
1068 overlay = dev_priv->overlay;
1070 DRM_DEBUG("userspace bug: no overlay\n");
1074 if (!(params->flags & I915_OVERLAY_ENABLE)) {
1075 drm_modeset_lock_all(dev);
1076 mutex_lock(&dev->struct_mutex);
1078 ret = intel_overlay_switch_off(overlay);
1080 mutex_unlock(&dev->struct_mutex);
1081 drm_modeset_unlock_all(dev);
1086 drmmode_crtc = drm_crtc_find(dev, file_priv, params->crtc_id);
1089 crtc = to_intel_crtc(drmmode_crtc);
1091 new_bo = i915_gem_object_lookup(file_priv, params->bo_handle);
1095 drm_modeset_lock_all(dev);
1096 mutex_lock(&dev->struct_mutex);
1098 if (i915_gem_object_is_tiled(new_bo)) {
1099 DRM_DEBUG_KMS("buffer used for overlay image can not be tiled\n");
1104 ret = intel_overlay_recover_from_interrupt(overlay);
1108 if (overlay->crtc != crtc) {
1109 ret = intel_overlay_switch_off(overlay);
1113 ret = check_overlay_possible_on_crtc(overlay, crtc);
1117 overlay->crtc = crtc;
1118 crtc->overlay = overlay;
1120 /* line too wide, i.e. one-line-mode */
1121 if (crtc->config->pipe_src_w > 1024 &&
1122 crtc->config->gmch_pfit.control & PFIT_ENABLE) {
1123 overlay->pfit_active = true;
1124 update_pfit_vscale_ratio(overlay);
1126 overlay->pfit_active = false;
1129 ret = check_overlay_dst(overlay, params);
1133 if (overlay->pfit_active) {
1134 params->dst_y = (((u32)params->dst_y << 12) /
1135 overlay->pfit_vscale_ratio);
1136 /* shifting right rounds downwards, so add 1 */
1137 params->dst_height = (((u32)params->dst_height << 12) /
1138 overlay->pfit_vscale_ratio) + 1;
1141 if (params->src_scan_height > params->src_height ||
1142 params->src_scan_width > params->src_width) {
1147 ret = check_overlay_src(dev_priv, params, new_bo);
1151 /* Check scaling after src size to prevent a divide-by-zero. */
1152 ret = check_overlay_scaling(params);
1156 ret = intel_overlay_do_put_image(overlay, new_bo, params);
1160 mutex_unlock(&dev->struct_mutex);
1161 drm_modeset_unlock_all(dev);
1162 i915_gem_object_put(new_bo);
1167 mutex_unlock(&dev->struct_mutex);
1168 drm_modeset_unlock_all(dev);
1169 i915_gem_object_put(new_bo);
1174 static void update_reg_attrs(struct intel_overlay *overlay,
1175 struct overlay_registers __iomem *regs)
1177 iowrite32((overlay->contrast << 18) | (overlay->brightness & 0xff),
1179 iowrite32(overlay->saturation, ®s->OCLRC1);
1182 static bool check_gamma_bounds(u32 gamma1, u32 gamma2)
1186 if (gamma1 & 0xff000000 || gamma2 & 0xff000000)
1189 for (i = 0; i < 3; i++) {
1190 if (((gamma1 >> i*8) & 0xff) >= ((gamma2 >> i*8) & 0xff))
1197 static bool check_gamma5_errata(u32 gamma5)
1201 for (i = 0; i < 3; i++) {
1202 if (((gamma5 >> i*8) & 0xff) == 0x80)
1209 static int check_gamma(struct drm_intel_overlay_attrs *attrs)
1211 if (!check_gamma_bounds(0, attrs->gamma0) ||
1212 !check_gamma_bounds(attrs->gamma0, attrs->gamma1) ||
1213 !check_gamma_bounds(attrs->gamma1, attrs->gamma2) ||
1214 !check_gamma_bounds(attrs->gamma2, attrs->gamma3) ||
1215 !check_gamma_bounds(attrs->gamma3, attrs->gamma4) ||
1216 !check_gamma_bounds(attrs->gamma4, attrs->gamma5) ||
1217 !check_gamma_bounds(attrs->gamma5, 0x00ffffff))
1220 if (!check_gamma5_errata(attrs->gamma5))
1226 int intel_overlay_attrs_ioctl(struct drm_device *dev, void *data,
1227 struct drm_file *file_priv)
1229 struct drm_intel_overlay_attrs *attrs = data;
1230 struct drm_i915_private *dev_priv = to_i915(dev);
1231 struct intel_overlay *overlay;
1234 overlay = dev_priv->overlay;
1236 DRM_DEBUG("userspace bug: no overlay\n");
1240 drm_modeset_lock_all(dev);
1241 mutex_lock(&dev->struct_mutex);
1244 if (!(attrs->flags & I915_OVERLAY_UPDATE_ATTRS)) {
1245 attrs->color_key = overlay->color_key;
1246 attrs->brightness = overlay->brightness;
1247 attrs->contrast = overlay->contrast;
1248 attrs->saturation = overlay->saturation;
1250 if (!IS_GEN(dev_priv, 2)) {
1251 attrs->gamma0 = I915_READ(OGAMC0);
1252 attrs->gamma1 = I915_READ(OGAMC1);
1253 attrs->gamma2 = I915_READ(OGAMC2);
1254 attrs->gamma3 = I915_READ(OGAMC3);
1255 attrs->gamma4 = I915_READ(OGAMC4);
1256 attrs->gamma5 = I915_READ(OGAMC5);
1259 if (attrs->brightness < -128 || attrs->brightness > 127)
1261 if (attrs->contrast > 255)
1263 if (attrs->saturation > 1023)
1266 overlay->color_key = attrs->color_key;
1267 overlay->brightness = attrs->brightness;
1268 overlay->contrast = attrs->contrast;
1269 overlay->saturation = attrs->saturation;
1271 update_reg_attrs(overlay, overlay->regs);
1273 if (attrs->flags & I915_OVERLAY_UPDATE_GAMMA) {
1274 if (IS_GEN(dev_priv, 2))
1277 if (overlay->active) {
1282 ret = check_gamma(attrs);
1286 I915_WRITE(OGAMC0, attrs->gamma0);
1287 I915_WRITE(OGAMC1, attrs->gamma1);
1288 I915_WRITE(OGAMC2, attrs->gamma2);
1289 I915_WRITE(OGAMC3, attrs->gamma3);
1290 I915_WRITE(OGAMC4, attrs->gamma4);
1291 I915_WRITE(OGAMC5, attrs->gamma5);
1294 overlay->color_key_enabled = (attrs->flags & I915_OVERLAY_DISABLE_DEST_COLORKEY) == 0;
1298 mutex_unlock(&dev->struct_mutex);
1299 drm_modeset_unlock_all(dev);
1304 static int get_registers(struct intel_overlay *overlay, bool use_phys)
1306 struct drm_i915_gem_object *obj;
1307 struct i915_vma *vma;
1310 obj = i915_gem_object_create_stolen(overlay->i915, PAGE_SIZE);
1312 obj = i915_gem_object_create_internal(overlay->i915, PAGE_SIZE);
1314 return PTR_ERR(obj);
1316 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0, PIN_MAPPABLE);
1323 overlay->flip_addr = sg_dma_address(obj->mm.pages->sgl);
1325 overlay->flip_addr = i915_ggtt_offset(vma);
1326 overlay->regs = i915_vma_pin_iomap(vma);
1327 i915_vma_unpin(vma);
1329 if (IS_ERR(overlay->regs)) {
1330 err = PTR_ERR(overlay->regs);
1334 overlay->reg_bo = obj;
1338 i915_gem_object_put(obj);
1342 void intel_overlay_setup(struct drm_i915_private *dev_priv)
1344 struct intel_overlay *overlay;
1347 if (!HAS_OVERLAY(dev_priv))
1350 overlay = kzalloc(sizeof(*overlay), GFP_KERNEL);
1354 overlay->i915 = dev_priv;
1356 overlay->color_key = 0x0101fe;
1357 overlay->color_key_enabled = true;
1358 overlay->brightness = -19;
1359 overlay->contrast = 75;
1360 overlay->saturation = 146;
1362 INIT_ACTIVE_REQUEST(&overlay->last_flip);
1364 mutex_lock(&dev_priv->drm.struct_mutex);
1366 ret = get_registers(overlay, OVERLAY_NEEDS_PHYSICAL(dev_priv));
1370 ret = i915_gem_object_set_to_gtt_domain(overlay->reg_bo, true);
1374 mutex_unlock(&dev_priv->drm.struct_mutex);
1376 memset_io(overlay->regs, 0, sizeof(struct overlay_registers));
1377 update_polyphase_filter(overlay->regs);
1378 update_reg_attrs(overlay, overlay->regs);
1380 dev_priv->overlay = overlay;
1381 DRM_INFO("Initialized overlay support.\n");
1385 i915_gem_object_put(overlay->reg_bo);
1387 mutex_unlock(&dev_priv->drm.struct_mutex);
1391 void intel_overlay_cleanup(struct drm_i915_private *dev_priv)
1393 struct intel_overlay *overlay;
1395 overlay = fetch_and_zero(&dev_priv->overlay);
1400 * The bo's should be free'd by the generic code already.
1401 * Furthermore modesetting teardown happens beforehand so the
1402 * hardware should be off already.
1404 WARN_ON(overlay->active);
1406 i915_gem_object_put(overlay->reg_bo);
1411 #if IS_ENABLED(CONFIG_DRM_I915_CAPTURE_ERROR)
1413 struct intel_overlay_error_state {
1414 struct overlay_registers regs;
1420 struct intel_overlay_error_state *
1421 intel_overlay_capture_error_state(struct drm_i915_private *dev_priv)
1423 struct intel_overlay *overlay = dev_priv->overlay;
1424 struct intel_overlay_error_state *error;
1426 if (!overlay || !overlay->active)
1429 error = kmalloc(sizeof(*error), GFP_ATOMIC);
1433 error->dovsta = I915_READ(DOVSTA);
1434 error->isr = I915_READ(GEN2_ISR);
1435 error->base = overlay->flip_addr;
1437 memcpy_fromio(&error->regs, overlay->regs, sizeof(error->regs));
1443 intel_overlay_print_error_state(struct drm_i915_error_state_buf *m,
1444 struct intel_overlay_error_state *error)
1446 i915_error_printf(m, "Overlay, status: 0x%08x, interrupt: 0x%08x\n",
1447 error->dovsta, error->isr);
1448 i915_error_printf(m, " Register file at 0x%08lx:\n",
1451 #define P(x) i915_error_printf(m, " " #x ": 0x%08x\n", error->regs.x)