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
24 * Daniel Vetter <daniel@ffwll.ch>
26 * Derived from Xorg ddx, xf86-video-intel, src/i830_video.c
28 #include <drm/i915_drm.h>
31 #include "intel_drv.h"
32 #include "intel_frontbuffer.h"
34 /* Limits for overlay size. According to intel doc, the real limits are:
35 * Y width: 4095, UV width (planar): 2047, Y height: 2047,
36 * UV width (planar): * 1023. But the xorg thinks 2048 for height and width. Use
37 * the mininum of both. */
38 #define IMAGE_MAX_WIDTH 2048
39 #define IMAGE_MAX_HEIGHT 2046 /* 2 * 1023 */
40 /* on 830 and 845 these large limits result in the card hanging */
41 #define IMAGE_MAX_WIDTH_LEGACY 1024
42 #define IMAGE_MAX_HEIGHT_LEGACY 1088
44 /* overlay register definitions */
46 #define OCMD_TILED_SURFACE (0x1<<19)
47 #define OCMD_MIRROR_MASK (0x3<<17)
48 #define OCMD_MIRROR_MODE (0x3<<17)
49 #define OCMD_MIRROR_HORIZONTAL (0x1<<17)
50 #define OCMD_MIRROR_VERTICAL (0x2<<17)
51 #define OCMD_MIRROR_BOTH (0x3<<17)
52 #define OCMD_BYTEORDER_MASK (0x3<<14) /* zero for YUYV or FOURCC YUY2 */
53 #define OCMD_UV_SWAP (0x1<<14) /* YVYU */
54 #define OCMD_Y_SWAP (0x2<<14) /* UYVY or FOURCC UYVY */
55 #define OCMD_Y_AND_UV_SWAP (0x3<<14) /* VYUY */
56 #define OCMD_SOURCE_FORMAT_MASK (0xf<<10)
57 #define OCMD_RGB_888 (0x1<<10) /* not in i965 Intel docs */
58 #define OCMD_RGB_555 (0x2<<10) /* not in i965 Intel docs */
59 #define OCMD_RGB_565 (0x3<<10) /* not in i965 Intel docs */
60 #define OCMD_YUV_422_PACKED (0x8<<10)
61 #define OCMD_YUV_411_PACKED (0x9<<10) /* not in i965 Intel docs */
62 #define OCMD_YUV_420_PLANAR (0xc<<10)
63 #define OCMD_YUV_422_PLANAR (0xd<<10)
64 #define OCMD_YUV_410_PLANAR (0xe<<10) /* also 411 */
65 #define OCMD_TVSYNCFLIP_PARITY (0x1<<9)
66 #define OCMD_TVSYNCFLIP_ENABLE (0x1<<7)
67 #define OCMD_BUF_TYPE_MASK (0x1<<5)
68 #define OCMD_BUF_TYPE_FRAME (0x0<<5)
69 #define OCMD_BUF_TYPE_FIELD (0x1<<5)
70 #define OCMD_TEST_MODE (0x1<<4)
71 #define OCMD_BUFFER_SELECT (0x3<<2)
72 #define OCMD_BUFFER0 (0x0<<2)
73 #define OCMD_BUFFER1 (0x1<<2)
74 #define OCMD_FIELD_SELECT (0x1<<2)
75 #define OCMD_FIELD0 (0x0<<1)
76 #define OCMD_FIELD1 (0x1<<1)
77 #define OCMD_ENABLE (0x1<<0)
79 /* OCONFIG register */
80 #define OCONF_PIPE_MASK (0x1<<18)
81 #define OCONF_PIPE_A (0x0<<18)
82 #define OCONF_PIPE_B (0x1<<18)
83 #define OCONF_GAMMA2_ENABLE (0x1<<16)
84 #define OCONF_CSC_MODE_BT601 (0x0<<5)
85 #define OCONF_CSC_MODE_BT709 (0x1<<5)
86 #define OCONF_CSC_BYPASS (0x1<<4)
87 #define OCONF_CC_OUT_8BIT (0x1<<3)
88 #define OCONF_TEST_MODE (0x1<<2)
89 #define OCONF_THREE_LINE_BUFFER (0x1<<0)
90 #define OCONF_TWO_LINE_BUFFER (0x0<<0)
92 /* DCLRKM (dst-key) register */
93 #define DST_KEY_ENABLE (0x1<<31)
94 #define CLK_RGB24_MASK 0x0
95 #define CLK_RGB16_MASK 0x070307
96 #define CLK_RGB15_MASK 0x070707
97 #define CLK_RGB8I_MASK 0xffffff
99 #define RGB16_TO_COLORKEY(c) \
100 (((c & 0xF800) << 8) | ((c & 0x07E0) << 5) | ((c & 0x001F) << 3))
101 #define RGB15_TO_COLORKEY(c) \
102 (((c & 0x7c00) << 9) | ((c & 0x03E0) << 6) | ((c & 0x001F) << 3))
104 /* overlay flip addr flag */
105 #define OFC_UPDATE 0x1
107 /* polyphase filter coefficients */
108 #define N_HORIZ_Y_TAPS 5
109 #define N_VERT_Y_TAPS 3
110 #define N_HORIZ_UV_TAPS 3
111 #define N_VERT_UV_TAPS 3
115 /* memory bufferd overlay registers */
116 struct overlay_registers {
144 u32 RESERVED1; /* 0x6C */
157 u32 FASTHSCALE; /* 0xA0 */
158 u32 UVSCALEV; /* 0xA4 */
159 u32 RESERVEDC[(0x200 - 0xA8) / 4]; /* 0xA8 - 0x1FC */
160 u16 Y_VCOEFS[N_VERT_Y_TAPS * N_PHASES]; /* 0x200 */
161 u16 RESERVEDD[0x100 / 2 - N_VERT_Y_TAPS * N_PHASES];
162 u16 Y_HCOEFS[N_HORIZ_Y_TAPS * N_PHASES]; /* 0x300 */
163 u16 RESERVEDE[0x200 / 2 - N_HORIZ_Y_TAPS * N_PHASES];
164 u16 UV_VCOEFS[N_VERT_UV_TAPS * N_PHASES]; /* 0x500 */
165 u16 RESERVEDF[0x100 / 2 - N_VERT_UV_TAPS * N_PHASES];
166 u16 UV_HCOEFS[N_HORIZ_UV_TAPS * N_PHASES]; /* 0x600 */
167 u16 RESERVEDG[0x100 / 2 - N_HORIZ_UV_TAPS * N_PHASES];
170 struct intel_overlay {
171 struct drm_i915_private *i915;
172 struct intel_crtc *crtc;
173 struct i915_vma *vma;
174 struct i915_vma *old_vma;
177 u32 pfit_vscale_ratio; /* shifted-point number, (1<<12) == 1.0 */
179 u32 color_key_enabled:1;
180 u32 brightness, contrast, saturation;
181 u32 old_xscale, old_yscale;
182 /* register access */
183 struct drm_i915_gem_object *reg_bo;
184 struct overlay_registers __iomem *regs;
187 struct i915_gem_active last_flip;
190 static void i830_overlay_clock_gating(struct drm_i915_private *dev_priv,
193 struct pci_dev *pdev = dev_priv->drm.pdev;
196 /* WA_OVERLAY_CLKGATE:alm */
198 I915_WRITE(DSPCLK_GATE_D, 0);
200 I915_WRITE(DSPCLK_GATE_D, OVRUNIT_CLOCK_GATE_DISABLE);
202 /* WA_DISABLE_L2CACHE_CLOCK_GATING:alm */
203 pci_bus_read_config_byte(pdev->bus,
204 PCI_DEVFN(0, 0), I830_CLOCK_GATE, &val);
206 val &= ~I830_L2_CACHE_CLOCK_GATE_DISABLE;
208 val |= I830_L2_CACHE_CLOCK_GATE_DISABLE;
209 pci_bus_write_config_byte(pdev->bus,
210 PCI_DEVFN(0, 0), I830_CLOCK_GATE, val);
213 static void intel_overlay_submit_request(struct intel_overlay *overlay,
214 struct i915_request *rq,
215 i915_gem_retire_fn retire)
217 GEM_BUG_ON(i915_gem_active_peek(&overlay->last_flip,
218 &overlay->i915->drm.struct_mutex));
219 i915_gem_active_set_retire_fn(&overlay->last_flip, retire,
220 &overlay->i915->drm.struct_mutex);
221 i915_gem_active_set(&overlay->last_flip, rq);
222 i915_request_add(rq);
225 static int intel_overlay_do_wait_request(struct intel_overlay *overlay,
226 struct i915_request *rq,
227 i915_gem_retire_fn retire)
229 intel_overlay_submit_request(overlay, rq, retire);
230 return i915_gem_active_retire(&overlay->last_flip,
231 &overlay->i915->drm.struct_mutex);
234 static struct i915_request *alloc_request(struct intel_overlay *overlay)
236 struct drm_i915_private *dev_priv = overlay->i915;
237 struct intel_engine_cs *engine = dev_priv->engine[RCS];
239 return i915_request_alloc(engine, dev_priv->kernel_context);
242 /* overlay needs to be disable in OCMD reg */
243 static int intel_overlay_on(struct intel_overlay *overlay)
245 struct drm_i915_private *dev_priv = overlay->i915;
246 struct i915_request *rq;
249 WARN_ON(overlay->active);
251 rq = alloc_request(overlay);
255 cs = intel_ring_begin(rq, 4);
257 i915_request_add(rq);
261 overlay->active = true;
263 if (IS_I830(dev_priv))
264 i830_overlay_clock_gating(dev_priv, false);
266 *cs++ = MI_OVERLAY_FLIP | MI_OVERLAY_ON;
267 *cs++ = overlay->flip_addr | OFC_UPDATE;
268 *cs++ = MI_WAIT_FOR_EVENT | MI_WAIT_FOR_OVERLAY_FLIP;
270 intel_ring_advance(rq, cs);
272 return intel_overlay_do_wait_request(overlay, rq, NULL);
275 static void intel_overlay_flip_prepare(struct intel_overlay *overlay,
276 struct i915_vma *vma)
278 enum pipe pipe = overlay->crtc->pipe;
280 WARN_ON(overlay->old_vma);
282 i915_gem_track_fb(overlay->vma ? overlay->vma->obj : NULL,
283 vma ? vma->obj : NULL,
284 INTEL_FRONTBUFFER_OVERLAY(pipe));
286 intel_frontbuffer_flip_prepare(overlay->i915,
287 INTEL_FRONTBUFFER_OVERLAY(pipe));
289 overlay->old_vma = overlay->vma;
291 overlay->vma = i915_vma_get(vma);
296 /* overlay needs to be enabled in OCMD reg */
297 static int intel_overlay_continue(struct intel_overlay *overlay,
298 struct i915_vma *vma,
299 bool load_polyphase_filter)
301 struct drm_i915_private *dev_priv = overlay->i915;
302 struct i915_request *rq;
303 u32 flip_addr = overlay->flip_addr;
306 WARN_ON(!overlay->active);
308 if (load_polyphase_filter)
309 flip_addr |= OFC_UPDATE;
311 /* check for underruns */
312 tmp = I915_READ(DOVSTA);
314 DRM_DEBUG("overlay underrun, DOVSTA: %x\n", tmp);
316 rq = alloc_request(overlay);
320 cs = intel_ring_begin(rq, 2);
322 i915_request_add(rq);
326 *cs++ = MI_OVERLAY_FLIP | MI_OVERLAY_CONTINUE;
328 intel_ring_advance(rq, cs);
330 intel_overlay_flip_prepare(overlay, vma);
332 intel_overlay_submit_request(overlay, rq, NULL);
337 static void intel_overlay_release_old_vma(struct intel_overlay *overlay)
339 struct i915_vma *vma;
341 vma = fetch_and_zero(&overlay->old_vma);
345 intel_frontbuffer_flip_complete(overlay->i915,
346 INTEL_FRONTBUFFER_OVERLAY(overlay->crtc->pipe));
348 i915_gem_object_unpin_from_display_plane(vma);
352 static void intel_overlay_release_old_vid_tail(struct i915_gem_active *active,
353 struct i915_request *rq)
355 struct intel_overlay *overlay =
356 container_of(active, typeof(*overlay), last_flip);
358 intel_overlay_release_old_vma(overlay);
361 static void intel_overlay_off_tail(struct i915_gem_active *active,
362 struct i915_request *rq)
364 struct intel_overlay *overlay =
365 container_of(active, typeof(*overlay), last_flip);
366 struct drm_i915_private *dev_priv = overlay->i915;
368 intel_overlay_release_old_vma(overlay);
370 overlay->crtc->overlay = NULL;
371 overlay->crtc = NULL;
372 overlay->active = false;
374 if (IS_I830(dev_priv))
375 i830_overlay_clock_gating(dev_priv, true);
378 /* overlay needs to be disabled in OCMD reg */
379 static int intel_overlay_off(struct intel_overlay *overlay)
381 struct i915_request *rq;
382 u32 *cs, flip_addr = overlay->flip_addr;
384 WARN_ON(!overlay->active);
386 /* According to intel docs the overlay hw may hang (when switching
387 * off) without loading the filter coeffs. It is however unclear whether
388 * this applies to the disabling of the overlay or to the switching off
389 * of the hw. Do it in both cases */
390 flip_addr |= OFC_UPDATE;
392 rq = alloc_request(overlay);
396 cs = intel_ring_begin(rq, 6);
398 i915_request_add(rq);
402 /* wait for overlay to go idle */
403 *cs++ = MI_OVERLAY_FLIP | MI_OVERLAY_CONTINUE;
405 *cs++ = MI_WAIT_FOR_EVENT | MI_WAIT_FOR_OVERLAY_FLIP;
407 /* turn overlay off */
408 *cs++ = MI_OVERLAY_FLIP | MI_OVERLAY_OFF;
410 *cs++ = MI_WAIT_FOR_EVENT | MI_WAIT_FOR_OVERLAY_FLIP;
412 intel_ring_advance(rq, cs);
414 intel_overlay_flip_prepare(overlay, NULL);
416 return intel_overlay_do_wait_request(overlay, rq,
417 intel_overlay_off_tail);
420 /* recover from an interruption due to a signal
421 * We have to be careful not to repeat work forever an make forward progess. */
422 static int intel_overlay_recover_from_interrupt(struct intel_overlay *overlay)
424 return i915_gem_active_retire(&overlay->last_flip,
425 &overlay->i915->drm.struct_mutex);
428 /* Wait for pending overlay flip and release old frame.
429 * Needs to be called before the overlay register are changed
430 * via intel_overlay_(un)map_regs
432 static int intel_overlay_release_old_vid(struct intel_overlay *overlay)
434 struct drm_i915_private *dev_priv = overlay->i915;
438 lockdep_assert_held(&dev_priv->drm.struct_mutex);
440 /* Only wait if there is actually an old frame to release to
441 * guarantee forward progress.
443 if (!overlay->old_vma)
446 if (I915_READ(ISR) & I915_OVERLAY_PLANE_FLIP_PENDING_INTERRUPT) {
447 /* synchronous slowpath */
448 struct i915_request *rq;
450 rq = alloc_request(overlay);
454 cs = intel_ring_begin(rq, 2);
456 i915_request_add(rq);
460 *cs++ = MI_WAIT_FOR_EVENT | MI_WAIT_FOR_OVERLAY_FLIP;
462 intel_ring_advance(rq, cs);
464 ret = intel_overlay_do_wait_request(overlay, rq,
465 intel_overlay_release_old_vid_tail);
469 intel_overlay_release_old_vid_tail(&overlay->last_flip, NULL);
474 void intel_overlay_reset(struct drm_i915_private *dev_priv)
476 struct intel_overlay *overlay = dev_priv->overlay;
481 overlay->old_xscale = 0;
482 overlay->old_yscale = 0;
483 overlay->crtc = NULL;
484 overlay->active = false;
487 static int packed_depth_bytes(u32 format)
489 switch (format & I915_OVERLAY_DEPTH_MASK) {
490 case I915_OVERLAY_YUV422:
492 case I915_OVERLAY_YUV411:
493 /* return 6; not implemented */
499 static int packed_width_bytes(u32 format, short width)
501 switch (format & I915_OVERLAY_DEPTH_MASK) {
502 case I915_OVERLAY_YUV422:
509 static int uv_hsubsampling(u32 format)
511 switch (format & I915_OVERLAY_DEPTH_MASK) {
512 case I915_OVERLAY_YUV422:
513 case I915_OVERLAY_YUV420:
515 case I915_OVERLAY_YUV411:
516 case I915_OVERLAY_YUV410:
523 static int uv_vsubsampling(u32 format)
525 switch (format & I915_OVERLAY_DEPTH_MASK) {
526 case I915_OVERLAY_YUV420:
527 case I915_OVERLAY_YUV410:
529 case I915_OVERLAY_YUV422:
530 case I915_OVERLAY_YUV411:
537 static u32 calc_swidthsw(struct drm_i915_private *dev_priv, u32 offset, u32 width)
541 if (IS_GEN(dev_priv, 2))
542 sw = ALIGN((offset & 31) + width, 32);
544 sw = ALIGN((offset & 63) + width, 64);
549 return (sw - 32) >> 3;
552 static const u16 y_static_hcoeffs[N_PHASES][N_HORIZ_Y_TAPS] = {
553 [ 0] = { 0x3000, 0xb4a0, 0x1930, 0x1920, 0xb4a0, },
554 [ 1] = { 0x3000, 0xb500, 0x19d0, 0x1880, 0xb440, },
555 [ 2] = { 0x3000, 0xb540, 0x1a88, 0x2f80, 0xb3e0, },
556 [ 3] = { 0x3000, 0xb580, 0x1b30, 0x2e20, 0xb380, },
557 [ 4] = { 0x3000, 0xb5c0, 0x1bd8, 0x2cc0, 0xb320, },
558 [ 5] = { 0x3020, 0xb5e0, 0x1c60, 0x2b80, 0xb2c0, },
559 [ 6] = { 0x3020, 0xb5e0, 0x1cf8, 0x2a20, 0xb260, },
560 [ 7] = { 0x3020, 0xb5e0, 0x1d80, 0x28e0, 0xb200, },
561 [ 8] = { 0x3020, 0xb5c0, 0x1e08, 0x3f40, 0xb1c0, },
562 [ 9] = { 0x3020, 0xb580, 0x1e78, 0x3ce0, 0xb160, },
563 [10] = { 0x3040, 0xb520, 0x1ed8, 0x3aa0, 0xb120, },
564 [11] = { 0x3040, 0xb4a0, 0x1f30, 0x3880, 0xb0e0, },
565 [12] = { 0x3040, 0xb400, 0x1f78, 0x3680, 0xb0a0, },
566 [13] = { 0x3020, 0xb340, 0x1fb8, 0x34a0, 0xb060, },
567 [14] = { 0x3020, 0xb240, 0x1fe0, 0x32e0, 0xb040, },
568 [15] = { 0x3020, 0xb140, 0x1ff8, 0x3160, 0xb020, },
569 [16] = { 0xb000, 0x3000, 0x0800, 0x3000, 0xb000, },
572 static const u16 uv_static_hcoeffs[N_PHASES][N_HORIZ_UV_TAPS] = {
573 [ 0] = { 0x3000, 0x1800, 0x1800, },
574 [ 1] = { 0xb000, 0x18d0, 0x2e60, },
575 [ 2] = { 0xb000, 0x1990, 0x2ce0, },
576 [ 3] = { 0xb020, 0x1a68, 0x2b40, },
577 [ 4] = { 0xb040, 0x1b20, 0x29e0, },
578 [ 5] = { 0xb060, 0x1bd8, 0x2880, },
579 [ 6] = { 0xb080, 0x1c88, 0x3e60, },
580 [ 7] = { 0xb0a0, 0x1d28, 0x3c00, },
581 [ 8] = { 0xb0c0, 0x1db8, 0x39e0, },
582 [ 9] = { 0xb0e0, 0x1e40, 0x37e0, },
583 [10] = { 0xb100, 0x1eb8, 0x3620, },
584 [11] = { 0xb100, 0x1f18, 0x34a0, },
585 [12] = { 0xb100, 0x1f68, 0x3360, },
586 [13] = { 0xb0e0, 0x1fa8, 0x3240, },
587 [14] = { 0xb0c0, 0x1fe0, 0x3140, },
588 [15] = { 0xb060, 0x1ff0, 0x30a0, },
589 [16] = { 0x3000, 0x0800, 0x3000, },
592 static void update_polyphase_filter(struct overlay_registers __iomem *regs)
594 memcpy_toio(regs->Y_HCOEFS, y_static_hcoeffs, sizeof(y_static_hcoeffs));
595 memcpy_toio(regs->UV_HCOEFS, uv_static_hcoeffs,
596 sizeof(uv_static_hcoeffs));
599 static bool update_scaling_factors(struct intel_overlay *overlay,
600 struct overlay_registers __iomem *regs,
601 struct drm_intel_overlay_put_image *params)
603 /* fixed point with a 12 bit shift */
604 u32 xscale, yscale, xscale_UV, yscale_UV;
606 #define FRACT_MASK 0xfff
607 bool scale_changed = false;
608 int uv_hscale = uv_hsubsampling(params->flags);
609 int uv_vscale = uv_vsubsampling(params->flags);
611 if (params->dst_width > 1)
612 xscale = ((params->src_scan_width - 1) << FP_SHIFT) /
615 xscale = 1 << FP_SHIFT;
617 if (params->dst_height > 1)
618 yscale = ((params->src_scan_height - 1) << FP_SHIFT) /
621 yscale = 1 << FP_SHIFT;
623 /*if (params->format & I915_OVERLAY_YUV_PLANAR) {*/
624 xscale_UV = xscale/uv_hscale;
625 yscale_UV = yscale/uv_vscale;
626 /* make the Y scale to UV scale ratio an exact multiply */
627 xscale = xscale_UV * uv_hscale;
628 yscale = yscale_UV * uv_vscale;
634 if (xscale != overlay->old_xscale || yscale != overlay->old_yscale)
635 scale_changed = true;
636 overlay->old_xscale = xscale;
637 overlay->old_yscale = yscale;
639 iowrite32(((yscale & FRACT_MASK) << 20) |
640 ((xscale >> FP_SHIFT) << 16) |
641 ((xscale & FRACT_MASK) << 3),
644 iowrite32(((yscale_UV & FRACT_MASK) << 20) |
645 ((xscale_UV >> FP_SHIFT) << 16) |
646 ((xscale_UV & FRACT_MASK) << 3),
649 iowrite32((((yscale >> FP_SHIFT) << 16) |
650 ((yscale_UV >> FP_SHIFT) << 0)),
654 update_polyphase_filter(regs);
656 return scale_changed;
659 static void update_colorkey(struct intel_overlay *overlay,
660 struct overlay_registers __iomem *regs)
662 const struct intel_plane_state *state =
663 to_intel_plane_state(overlay->crtc->base.primary->state);
664 u32 key = overlay->color_key;
668 if (overlay->color_key_enabled)
669 flags |= DST_KEY_ENABLE;
671 if (state->base.visible)
672 format = state->base.fb->format->format;
677 flags |= CLK_RGB8I_MASK;
679 case DRM_FORMAT_XRGB1555:
680 key = RGB15_TO_COLORKEY(key);
681 flags |= CLK_RGB15_MASK;
683 case DRM_FORMAT_RGB565:
684 key = RGB16_TO_COLORKEY(key);
685 flags |= CLK_RGB16_MASK;
688 flags |= CLK_RGB24_MASK;
692 iowrite32(key, ®s->DCLRKV);
693 iowrite32(flags, ®s->DCLRKM);
696 static u32 overlay_cmd_reg(struct drm_intel_overlay_put_image *params)
698 u32 cmd = OCMD_ENABLE | OCMD_BUF_TYPE_FRAME | OCMD_BUFFER0;
700 if (params->flags & I915_OVERLAY_YUV_PLANAR) {
701 switch (params->flags & I915_OVERLAY_DEPTH_MASK) {
702 case I915_OVERLAY_YUV422:
703 cmd |= OCMD_YUV_422_PLANAR;
705 case I915_OVERLAY_YUV420:
706 cmd |= OCMD_YUV_420_PLANAR;
708 case I915_OVERLAY_YUV411:
709 case I915_OVERLAY_YUV410:
710 cmd |= OCMD_YUV_410_PLANAR;
713 } else { /* YUV packed */
714 switch (params->flags & I915_OVERLAY_DEPTH_MASK) {
715 case I915_OVERLAY_YUV422:
716 cmd |= OCMD_YUV_422_PACKED;
718 case I915_OVERLAY_YUV411:
719 cmd |= OCMD_YUV_411_PACKED;
723 switch (params->flags & I915_OVERLAY_SWAP_MASK) {
724 case I915_OVERLAY_NO_SWAP:
726 case I915_OVERLAY_UV_SWAP:
729 case I915_OVERLAY_Y_SWAP:
732 case I915_OVERLAY_Y_AND_UV_SWAP:
733 cmd |= OCMD_Y_AND_UV_SWAP;
741 static int intel_overlay_do_put_image(struct intel_overlay *overlay,
742 struct drm_i915_gem_object *new_bo,
743 struct drm_intel_overlay_put_image *params)
745 struct overlay_registers __iomem *regs = overlay->regs;
746 struct drm_i915_private *dev_priv = overlay->i915;
747 u32 swidth, swidthsw, sheight, ostride;
748 enum pipe pipe = overlay->crtc->pipe;
749 bool scale_changed = false;
750 struct i915_vma *vma;
753 lockdep_assert_held(&dev_priv->drm.struct_mutex);
754 WARN_ON(!drm_modeset_is_locked(&dev_priv->drm.mode_config.connection_mutex));
756 ret = intel_overlay_release_old_vid(overlay);
760 atomic_inc(&dev_priv->gpu_error.pending_fb_pin);
762 vma = i915_gem_object_pin_to_display_plane(new_bo,
763 0, NULL, PIN_MAPPABLE);
766 goto out_pin_section;
768 intel_fb_obj_flush(new_bo, ORIGIN_DIRTYFB);
770 ret = i915_vma_put_fence(vma);
774 if (!overlay->active) {
777 oconfig = OCONF_CC_OUT_8BIT;
778 if (IS_GEN(dev_priv, 4))
779 oconfig |= OCONF_CSC_MODE_BT709;
780 oconfig |= pipe == 0 ?
781 OCONF_PIPE_A : OCONF_PIPE_B;
782 iowrite32(oconfig, ®s->OCONFIG);
784 ret = intel_overlay_on(overlay);
789 iowrite32(params->dst_y << 16 | params->dst_x, ®s->DWINPOS);
790 iowrite32(params->dst_height << 16 | params->dst_width, ®s->DWINSZ);
792 if (params->flags & I915_OVERLAY_YUV_PACKED)
793 tmp_width = packed_width_bytes(params->flags,
796 tmp_width = params->src_width;
798 swidth = params->src_width;
799 swidthsw = calc_swidthsw(dev_priv, params->offset_Y, tmp_width);
800 sheight = params->src_height;
801 iowrite32(i915_ggtt_offset(vma) + params->offset_Y, ®s->OBUF_0Y);
802 ostride = params->stride_Y;
804 if (params->flags & I915_OVERLAY_YUV_PLANAR) {
805 int uv_hscale = uv_hsubsampling(params->flags);
806 int uv_vscale = uv_vsubsampling(params->flags);
809 swidth |= (params->src_width / uv_hscale) << 16;
810 sheight |= (params->src_height / uv_vscale) << 16;
812 tmp_U = calc_swidthsw(dev_priv, params->offset_U,
813 params->src_width / uv_hscale);
814 tmp_V = calc_swidthsw(dev_priv, params->offset_V,
815 params->src_width / uv_hscale);
816 swidthsw |= max(tmp_U, tmp_V) << 16;
818 iowrite32(i915_ggtt_offset(vma) + params->offset_U,
820 iowrite32(i915_ggtt_offset(vma) + params->offset_V,
823 ostride |= params->stride_UV << 16;
826 iowrite32(swidth, ®s->SWIDTH);
827 iowrite32(swidthsw, ®s->SWIDTHSW);
828 iowrite32(sheight, ®s->SHEIGHT);
829 iowrite32(ostride, ®s->OSTRIDE);
831 scale_changed = update_scaling_factors(overlay, regs, params);
833 update_colorkey(overlay, regs);
835 iowrite32(overlay_cmd_reg(params), ®s->OCMD);
837 ret = intel_overlay_continue(overlay, vma, scale_changed);
844 i915_gem_object_unpin_from_display_plane(vma);
846 atomic_dec(&dev_priv->gpu_error.pending_fb_pin);
851 int intel_overlay_switch_off(struct intel_overlay *overlay)
853 struct drm_i915_private *dev_priv = overlay->i915;
856 lockdep_assert_held(&dev_priv->drm.struct_mutex);
857 WARN_ON(!drm_modeset_is_locked(&dev_priv->drm.mode_config.connection_mutex));
859 ret = intel_overlay_recover_from_interrupt(overlay);
863 if (!overlay->active)
866 ret = intel_overlay_release_old_vid(overlay);
870 iowrite32(0, &overlay->regs->OCMD);
872 return intel_overlay_off(overlay);
875 static int check_overlay_possible_on_crtc(struct intel_overlay *overlay,
876 struct intel_crtc *crtc)
881 /* can't use the overlay with double wide pipe */
882 if (crtc->config->double_wide)
888 static void update_pfit_vscale_ratio(struct intel_overlay *overlay)
890 struct drm_i915_private *dev_priv = overlay->i915;
891 u32 pfit_control = I915_READ(PFIT_CONTROL);
894 /* XXX: This is not the same logic as in the xorg driver, but more in
895 * line with the intel documentation for the i965
897 if (INTEL_GEN(dev_priv) >= 4) {
898 /* on i965 use the PGM reg to read out the autoscaler values */
899 ratio = I915_READ(PFIT_PGM_RATIOS) >> PFIT_VERT_SCALE_SHIFT_965;
901 if (pfit_control & VERT_AUTO_SCALE)
902 ratio = I915_READ(PFIT_AUTO_RATIOS);
904 ratio = I915_READ(PFIT_PGM_RATIOS);
905 ratio >>= PFIT_VERT_SCALE_SHIFT;
908 overlay->pfit_vscale_ratio = ratio;
911 static int check_overlay_dst(struct intel_overlay *overlay,
912 struct drm_intel_overlay_put_image *rec)
914 const struct intel_crtc_state *pipe_config =
915 overlay->crtc->config;
917 if (rec->dst_x < pipe_config->pipe_src_w &&
918 rec->dst_x + rec->dst_width <= pipe_config->pipe_src_w &&
919 rec->dst_y < pipe_config->pipe_src_h &&
920 rec->dst_y + rec->dst_height <= pipe_config->pipe_src_h)
926 static int check_overlay_scaling(struct drm_intel_overlay_put_image *rec)
930 /* downscaling limit is 8.0 */
931 tmp = ((rec->src_scan_height << 16) / rec->dst_height) >> 16;
935 tmp = ((rec->src_scan_width << 16) / rec->dst_width) >> 16;
942 static int check_overlay_src(struct drm_i915_private *dev_priv,
943 struct drm_intel_overlay_put_image *rec,
944 struct drm_i915_gem_object *new_bo)
946 int uv_hscale = uv_hsubsampling(rec->flags);
947 int uv_vscale = uv_vsubsampling(rec->flags);
952 /* check src dimensions */
953 if (IS_I845G(dev_priv) || IS_I830(dev_priv)) {
954 if (rec->src_height > IMAGE_MAX_HEIGHT_LEGACY ||
955 rec->src_width > IMAGE_MAX_WIDTH_LEGACY)
958 if (rec->src_height > IMAGE_MAX_HEIGHT ||
959 rec->src_width > IMAGE_MAX_WIDTH)
963 /* better safe than sorry, use 4 as the maximal subsampling ratio */
964 if (rec->src_height < N_VERT_Y_TAPS*4 ||
965 rec->src_width < N_HORIZ_Y_TAPS*4)
968 /* check alignment constraints */
969 switch (rec->flags & I915_OVERLAY_TYPE_MASK) {
970 case I915_OVERLAY_RGB:
971 /* not implemented */
974 case I915_OVERLAY_YUV_PACKED:
978 depth = packed_depth_bytes(rec->flags);
982 /* ignore UV planes */
986 /* check pixel alignment */
987 if (rec->offset_Y % depth)
991 case I915_OVERLAY_YUV_PLANAR:
992 if (uv_vscale < 0 || uv_hscale < 0)
994 /* no offset restrictions for planar formats */
1001 if (rec->src_width % uv_hscale)
1004 /* stride checking */
1005 if (IS_I830(dev_priv) || IS_I845G(dev_priv))
1010 if (rec->stride_Y & stride_mask || rec->stride_UV & stride_mask)
1012 if (IS_GEN(dev_priv, 4) && rec->stride_Y < 512)
1015 tmp = (rec->flags & I915_OVERLAY_TYPE_MASK) == I915_OVERLAY_YUV_PLANAR ?
1017 if (rec->stride_Y > tmp || rec->stride_UV > 2*1024)
1020 /* check buffer dimensions */
1021 switch (rec->flags & I915_OVERLAY_TYPE_MASK) {
1022 case I915_OVERLAY_RGB:
1023 case I915_OVERLAY_YUV_PACKED:
1024 /* always 4 Y values per depth pixels */
1025 if (packed_width_bytes(rec->flags, rec->src_width) > rec->stride_Y)
1028 tmp = rec->stride_Y*rec->src_height;
1029 if (rec->offset_Y + tmp > new_bo->base.size)
1033 case I915_OVERLAY_YUV_PLANAR:
1034 if (rec->src_width > rec->stride_Y)
1036 if (rec->src_width/uv_hscale > rec->stride_UV)
1039 tmp = rec->stride_Y * rec->src_height;
1040 if (rec->offset_Y + tmp > new_bo->base.size)
1043 tmp = rec->stride_UV * (rec->src_height / uv_vscale);
1044 if (rec->offset_U + tmp > new_bo->base.size ||
1045 rec->offset_V + tmp > new_bo->base.size)
1053 int intel_overlay_put_image_ioctl(struct drm_device *dev, void *data,
1054 struct drm_file *file_priv)
1056 struct drm_intel_overlay_put_image *params = data;
1057 struct drm_i915_private *dev_priv = to_i915(dev);
1058 struct intel_overlay *overlay;
1059 struct drm_crtc *drmmode_crtc;
1060 struct intel_crtc *crtc;
1061 struct drm_i915_gem_object *new_bo;
1064 overlay = dev_priv->overlay;
1066 DRM_DEBUG("userspace bug: no overlay\n");
1070 if (!(params->flags & I915_OVERLAY_ENABLE)) {
1071 drm_modeset_lock_all(dev);
1072 mutex_lock(&dev->struct_mutex);
1074 ret = intel_overlay_switch_off(overlay);
1076 mutex_unlock(&dev->struct_mutex);
1077 drm_modeset_unlock_all(dev);
1082 drmmode_crtc = drm_crtc_find(dev, file_priv, params->crtc_id);
1085 crtc = to_intel_crtc(drmmode_crtc);
1087 new_bo = i915_gem_object_lookup(file_priv, params->bo_handle);
1091 drm_modeset_lock_all(dev);
1092 mutex_lock(&dev->struct_mutex);
1094 if (i915_gem_object_is_tiled(new_bo)) {
1095 DRM_DEBUG_KMS("buffer used for overlay image can not be tiled\n");
1100 ret = intel_overlay_recover_from_interrupt(overlay);
1104 if (overlay->crtc != crtc) {
1105 ret = intel_overlay_switch_off(overlay);
1109 ret = check_overlay_possible_on_crtc(overlay, crtc);
1113 overlay->crtc = crtc;
1114 crtc->overlay = overlay;
1116 /* line too wide, i.e. one-line-mode */
1117 if (crtc->config->pipe_src_w > 1024 &&
1118 crtc->config->gmch_pfit.control & PFIT_ENABLE) {
1119 overlay->pfit_active = true;
1120 update_pfit_vscale_ratio(overlay);
1122 overlay->pfit_active = false;
1125 ret = check_overlay_dst(overlay, params);
1129 if (overlay->pfit_active) {
1130 params->dst_y = (((u32)params->dst_y << 12) /
1131 overlay->pfit_vscale_ratio);
1132 /* shifting right rounds downwards, so add 1 */
1133 params->dst_height = (((u32)params->dst_height << 12) /
1134 overlay->pfit_vscale_ratio) + 1;
1137 if (params->src_scan_height > params->src_height ||
1138 params->src_scan_width > params->src_width) {
1143 ret = check_overlay_src(dev_priv, params, new_bo);
1147 /* Check scaling after src size to prevent a divide-by-zero. */
1148 ret = check_overlay_scaling(params);
1152 ret = intel_overlay_do_put_image(overlay, new_bo, params);
1156 mutex_unlock(&dev->struct_mutex);
1157 drm_modeset_unlock_all(dev);
1158 i915_gem_object_put(new_bo);
1163 mutex_unlock(&dev->struct_mutex);
1164 drm_modeset_unlock_all(dev);
1165 i915_gem_object_put(new_bo);
1170 static void update_reg_attrs(struct intel_overlay *overlay,
1171 struct overlay_registers __iomem *regs)
1173 iowrite32((overlay->contrast << 18) | (overlay->brightness & 0xff),
1175 iowrite32(overlay->saturation, ®s->OCLRC1);
1178 static bool check_gamma_bounds(u32 gamma1, u32 gamma2)
1182 if (gamma1 & 0xff000000 || gamma2 & 0xff000000)
1185 for (i = 0; i < 3; i++) {
1186 if (((gamma1 >> i*8) & 0xff) >= ((gamma2 >> i*8) & 0xff))
1193 static bool check_gamma5_errata(u32 gamma5)
1197 for (i = 0; i < 3; i++) {
1198 if (((gamma5 >> i*8) & 0xff) == 0x80)
1205 static int check_gamma(struct drm_intel_overlay_attrs *attrs)
1207 if (!check_gamma_bounds(0, attrs->gamma0) ||
1208 !check_gamma_bounds(attrs->gamma0, attrs->gamma1) ||
1209 !check_gamma_bounds(attrs->gamma1, attrs->gamma2) ||
1210 !check_gamma_bounds(attrs->gamma2, attrs->gamma3) ||
1211 !check_gamma_bounds(attrs->gamma3, attrs->gamma4) ||
1212 !check_gamma_bounds(attrs->gamma4, attrs->gamma5) ||
1213 !check_gamma_bounds(attrs->gamma5, 0x00ffffff))
1216 if (!check_gamma5_errata(attrs->gamma5))
1222 int intel_overlay_attrs_ioctl(struct drm_device *dev, void *data,
1223 struct drm_file *file_priv)
1225 struct drm_intel_overlay_attrs *attrs = data;
1226 struct drm_i915_private *dev_priv = to_i915(dev);
1227 struct intel_overlay *overlay;
1230 overlay = dev_priv->overlay;
1232 DRM_DEBUG("userspace bug: no overlay\n");
1236 drm_modeset_lock_all(dev);
1237 mutex_lock(&dev->struct_mutex);
1240 if (!(attrs->flags & I915_OVERLAY_UPDATE_ATTRS)) {
1241 attrs->color_key = overlay->color_key;
1242 attrs->brightness = overlay->brightness;
1243 attrs->contrast = overlay->contrast;
1244 attrs->saturation = overlay->saturation;
1246 if (!IS_GEN(dev_priv, 2)) {
1247 attrs->gamma0 = I915_READ(OGAMC0);
1248 attrs->gamma1 = I915_READ(OGAMC1);
1249 attrs->gamma2 = I915_READ(OGAMC2);
1250 attrs->gamma3 = I915_READ(OGAMC3);
1251 attrs->gamma4 = I915_READ(OGAMC4);
1252 attrs->gamma5 = I915_READ(OGAMC5);
1255 if (attrs->brightness < -128 || attrs->brightness > 127)
1257 if (attrs->contrast > 255)
1259 if (attrs->saturation > 1023)
1262 overlay->color_key = attrs->color_key;
1263 overlay->brightness = attrs->brightness;
1264 overlay->contrast = attrs->contrast;
1265 overlay->saturation = attrs->saturation;
1267 update_reg_attrs(overlay, overlay->regs);
1269 if (attrs->flags & I915_OVERLAY_UPDATE_GAMMA) {
1270 if (IS_GEN(dev_priv, 2))
1273 if (overlay->active) {
1278 ret = check_gamma(attrs);
1282 I915_WRITE(OGAMC0, attrs->gamma0);
1283 I915_WRITE(OGAMC1, attrs->gamma1);
1284 I915_WRITE(OGAMC2, attrs->gamma2);
1285 I915_WRITE(OGAMC3, attrs->gamma3);
1286 I915_WRITE(OGAMC4, attrs->gamma4);
1287 I915_WRITE(OGAMC5, attrs->gamma5);
1290 overlay->color_key_enabled = (attrs->flags & I915_OVERLAY_DISABLE_DEST_COLORKEY) == 0;
1294 mutex_unlock(&dev->struct_mutex);
1295 drm_modeset_unlock_all(dev);
1300 static int get_registers(struct intel_overlay *overlay, bool use_phys)
1302 struct drm_i915_gem_object *obj;
1303 struct i915_vma *vma;
1306 obj = i915_gem_object_create_stolen(overlay->i915, PAGE_SIZE);
1308 obj = i915_gem_object_create_internal(overlay->i915, PAGE_SIZE);
1310 return PTR_ERR(obj);
1312 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0, PIN_MAPPABLE);
1319 overlay->flip_addr = sg_dma_address(obj->mm.pages->sgl);
1321 overlay->flip_addr = i915_ggtt_offset(vma);
1322 overlay->regs = i915_vma_pin_iomap(vma);
1323 i915_vma_unpin(vma);
1325 if (IS_ERR(overlay->regs)) {
1326 err = PTR_ERR(overlay->regs);
1330 overlay->reg_bo = obj;
1334 i915_gem_object_put(obj);
1338 void intel_overlay_setup(struct drm_i915_private *dev_priv)
1340 struct intel_overlay *overlay;
1343 if (!HAS_OVERLAY(dev_priv))
1346 overlay = kzalloc(sizeof(*overlay), GFP_KERNEL);
1350 overlay->i915 = dev_priv;
1352 overlay->color_key = 0x0101fe;
1353 overlay->color_key_enabled = true;
1354 overlay->brightness = -19;
1355 overlay->contrast = 75;
1356 overlay->saturation = 146;
1358 init_request_active(&overlay->last_flip, NULL);
1360 mutex_lock(&dev_priv->drm.struct_mutex);
1362 ret = get_registers(overlay, OVERLAY_NEEDS_PHYSICAL(dev_priv));
1366 ret = i915_gem_object_set_to_gtt_domain(overlay->reg_bo, true);
1370 mutex_unlock(&dev_priv->drm.struct_mutex);
1372 memset_io(overlay->regs, 0, sizeof(struct overlay_registers));
1373 update_polyphase_filter(overlay->regs);
1374 update_reg_attrs(overlay, overlay->regs);
1376 dev_priv->overlay = overlay;
1377 DRM_INFO("Initialized overlay support.\n");
1381 i915_gem_object_put(overlay->reg_bo);
1383 mutex_unlock(&dev_priv->drm.struct_mutex);
1387 void intel_overlay_cleanup(struct drm_i915_private *dev_priv)
1389 struct intel_overlay *overlay;
1391 overlay = fetch_and_zero(&dev_priv->overlay);
1396 * The bo's should be free'd by the generic code already.
1397 * Furthermore modesetting teardown happens beforehand so the
1398 * hardware should be off already.
1400 WARN_ON(overlay->active);
1402 i915_gem_object_put(overlay->reg_bo);
1407 #if IS_ENABLED(CONFIG_DRM_I915_CAPTURE_ERROR)
1409 struct intel_overlay_error_state {
1410 struct overlay_registers regs;
1416 struct intel_overlay_error_state *
1417 intel_overlay_capture_error_state(struct drm_i915_private *dev_priv)
1419 struct intel_overlay *overlay = dev_priv->overlay;
1420 struct intel_overlay_error_state *error;
1422 if (!overlay || !overlay->active)
1425 error = kmalloc(sizeof(*error), GFP_ATOMIC);
1429 error->dovsta = I915_READ(DOVSTA);
1430 error->isr = I915_READ(ISR);
1431 error->base = overlay->flip_addr;
1433 memcpy_fromio(&error->regs, overlay->regs, sizeof(error->regs));
1439 intel_overlay_print_error_state(struct drm_i915_error_state_buf *m,
1440 struct intel_overlay_error_state *error)
1442 i915_error_printf(m, "Overlay, status: 0x%08x, interrupt: 0x%08x\n",
1443 error->dovsta, error->isr);
1444 i915_error_printf(m, " Register file at 0x%08lx:\n",
1447 #define P(x) i915_error_printf(m, " " #x ": 0x%08x\n", error->regs.x)