blob: 495625914e4a084f01f476d93524bed83db7ac42 [file] [log] [blame]
Daniel Vetter02e792f2009-09-15 22:57:34 +02001/*
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 */
David Howells760285e2012-10-02 18:01:07 +010028#include <drm/drmP.h>
29#include <drm/i915_drm.h>
Daniel Vetter02e792f2009-09-15 22:57:34 +020030#include "i915_drv.h"
31#include "i915_reg.h"
32#include "intel_drv.h"
33
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
43
44/* overlay register definitions */
45/* OCMD register */
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)
Chris Wilsond7961362010-07-13 13:52:17 +010067#define OCMD_BUF_TYPE_MASK (0x1<<5)
Daniel Vetter02e792f2009-09-15 22:57:34 +020068#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)
78
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)
91
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
98
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))
103
104/* overlay flip addr flag */
105#define OFC_UPDATE 0x1
106
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
112#define N_PHASES 17
113#define MAX_TAPS 5
114
115/* memory bufferd overlay registers */
116struct overlay_registers {
Akshay Joshi0206e352011-08-16 15:34:10 -0400117 u32 OBUF_0Y;
118 u32 OBUF_1Y;
119 u32 OBUF_0U;
120 u32 OBUF_0V;
121 u32 OBUF_1U;
122 u32 OBUF_1V;
123 u32 OSTRIDE;
124 u32 YRGB_VPH;
125 u32 UV_VPH;
126 u32 HORZ_PH;
127 u32 INIT_PHS;
128 u32 DWINPOS;
129 u32 DWINSZ;
130 u32 SWIDTH;
131 u32 SWIDTHSW;
132 u32 SHEIGHT;
133 u32 YRGBSCALE;
134 u32 UVSCALE;
135 u32 OCLRC0;
136 u32 OCLRC1;
137 u32 DCLRKV;
138 u32 DCLRKM;
139 u32 SCLRKVH;
140 u32 SCLRKVL;
141 u32 SCLRKEN;
142 u32 OCONFIG;
143 u32 OCMD;
144 u32 RESERVED1; /* 0x6C */
145 u32 OSTART_0Y;
146 u32 OSTART_1Y;
147 u32 OSTART_0U;
148 u32 OSTART_0V;
149 u32 OSTART_1U;
150 u32 OSTART_1V;
151 u32 OTILEOFF_0Y;
152 u32 OTILEOFF_1Y;
153 u32 OTILEOFF_0U;
154 u32 OTILEOFF_0V;
155 u32 OTILEOFF_1U;
156 u32 OTILEOFF_1V;
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];
Daniel Vetter02e792f2009-09-15 22:57:34 +0200168};
169
Chris Wilson23f09ce2010-08-12 13:53:37 +0100170struct intel_overlay {
171 struct drm_device *dev;
172 struct intel_crtc *crtc;
173 struct drm_i915_gem_object *vid_bo;
174 struct drm_i915_gem_object *old_vid_bo;
175 int active;
176 int pfit_active;
177 u32 pfit_vscale_ratio; /* shifted-point number, (1<<12) == 1.0 */
178 u32 color_key;
179 u32 brightness, contrast, saturation;
180 u32 old_xscale, old_yscale;
181 /* register access */
182 u32 flip_addr;
183 struct drm_i915_gem_object *reg_bo;
184 /* flip handling */
185 uint32_t last_flip_req;
Chris Wilsonb303cf92010-08-12 14:03:48 +0100186 void (*flip_tail)(struct intel_overlay *);
Chris Wilson23f09ce2010-08-12 13:53:37 +0100187};
Daniel Vetter02e792f2009-09-15 22:57:34 +0200188
Ben Widawsky75020bc2012-04-16 14:07:43 -0700189static struct overlay_registers __iomem *
Chris Wilson8d74f652010-08-12 10:35:26 +0100190intel_overlay_map_regs(struct intel_overlay *overlay)
Daniel Vetter02e792f2009-09-15 22:57:34 +0200191{
Akshay Joshi0206e352011-08-16 15:34:10 -0400192 drm_i915_private_t *dev_priv = overlay->dev->dev_private;
Ben Widawsky75020bc2012-04-16 14:07:43 -0700193 struct overlay_registers __iomem *regs;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200194
Chris Wilson9bb2ff72010-08-12 12:02:11 +0100195 if (OVERLAY_NEEDS_PHYSICAL(overlay->dev))
Ben Widawsky75020bc2012-04-16 14:07:43 -0700196 regs = (struct overlay_registers __iomem *)overlay->reg_bo->phys_obj->handle->vaddr;
Chris Wilson9bb2ff72010-08-12 12:02:11 +0100197 else
Chris Wilson8d74f652010-08-12 10:35:26 +0100198 regs = io_mapping_map_wc(dev_priv->mm.gtt_mapping,
199 overlay->reg_bo->gtt_offset);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200200
Chris Wilson9bb2ff72010-08-12 12:02:11 +0100201 return regs;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200202}
203
Chris Wilson9bb2ff72010-08-12 12:02:11 +0100204static void intel_overlay_unmap_regs(struct intel_overlay *overlay,
Ben Widawsky75020bc2012-04-16 14:07:43 -0700205 struct overlay_registers __iomem *regs)
Daniel Vetter02e792f2009-09-15 22:57:34 +0200206{
Chris Wilson8d74f652010-08-12 10:35:26 +0100207 if (!OVERLAY_NEEDS_PHYSICAL(overlay->dev))
Chris Wilson9bb2ff72010-08-12 12:02:11 +0100208 io_mapping_unmap(regs);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200209}
Daniel Vetter02e792f2009-09-15 22:57:34 +0200210
Chris Wilsonb6c028e2010-08-12 11:55:08 +0100211static int intel_overlay_do_wait_request(struct intel_overlay *overlay,
Chris Wilsonb303cf92010-08-12 14:03:48 +0100212 void (*tail)(struct intel_overlay *))
Chris Wilsonb6c028e2010-08-12 11:55:08 +0100213{
214 struct drm_device *dev = overlay->dev;
215 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter6d90c952012-04-26 23:28:05 +0200216 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
Chris Wilsonb6c028e2010-08-12 11:55:08 +0100217 int ret;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200218
Chris Wilsonb303cf92010-08-12 14:03:48 +0100219 BUG_ON(overlay->last_flip_req);
Chris Wilsonacb868d2012-09-26 13:47:30 +0100220 ret = i915_add_request(ring, NULL, &overlay->last_flip_req);
221 if (ret)
222 return ret;
223
Chris Wilsonb303cf92010-08-12 14:03:48 +0100224 overlay->flip_tail = tail;
Ben Widawsky199b2bc2012-05-24 15:03:11 -0700225 ret = i915_wait_seqno(ring, overlay->last_flip_req);
Chris Wilsonb6c028e2010-08-12 11:55:08 +0100226 if (ret)
227 return ret;
Ben Widawskyb2da9fe2012-04-26 16:02:58 -0700228 i915_gem_retire_requests(dev);
Chris Wilsonb6c028e2010-08-12 11:55:08 +0100229
Chris Wilsonb6c028e2010-08-12 11:55:08 +0100230 overlay->last_flip_req = 0;
231 return 0;
232}
233
Daniel Vetter02e792f2009-09-15 22:57:34 +0200234/* overlay needs to be disable in OCMD reg */
235static int intel_overlay_on(struct intel_overlay *overlay)
236{
237 struct drm_device *dev = overlay->dev;
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100238 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter6d90c952012-04-26 23:28:05 +0200239 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
Daniel Vetter02e792f2009-09-15 22:57:34 +0200240 int ret;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200241
242 BUG_ON(overlay->active);
Daniel Vetter03f77ea2009-09-15 22:57:37 +0200243 overlay->active = 1;
Daniel Vetter03f77ea2009-09-15 22:57:37 +0200244
Daniel Vetter6306cb42012-08-12 19:27:10 +0200245 WARN_ON(IS_I830(dev) && !(dev_priv->quirks & QUIRK_PIPEA_FORCE));
Chris Wilson106dada2010-07-16 17:13:01 +0100246
Daniel Vetter6d90c952012-04-26 23:28:05 +0200247 ret = intel_ring_begin(ring, 4);
Chris Wilsonacb868d2012-09-26 13:47:30 +0100248 if (ret)
249 return ret;
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100250
Daniel Vetter6d90c952012-04-26 23:28:05 +0200251 intel_ring_emit(ring, MI_OVERLAY_FLIP | MI_OVERLAY_ON);
252 intel_ring_emit(ring, overlay->flip_addr | OFC_UPDATE);
253 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | MI_WAIT_FOR_OVERLAY_FLIP);
254 intel_ring_emit(ring, MI_NOOP);
255 intel_ring_advance(ring);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200256
Chris Wilsonacb868d2012-09-26 13:47:30 +0100257 return intel_overlay_do_wait_request(overlay, NULL);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200258}
259
260/* overlay needs to be enabled in OCMD reg */
Chris Wilson8dc5d142010-08-12 12:36:12 +0100261static int intel_overlay_continue(struct intel_overlay *overlay,
262 bool load_polyphase_filter)
Daniel Vetter02e792f2009-09-15 22:57:34 +0200263{
264 struct drm_device *dev = overlay->dev;
Akshay Joshi0206e352011-08-16 15:34:10 -0400265 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter6d90c952012-04-26 23:28:05 +0200266 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
Daniel Vetter02e792f2009-09-15 22:57:34 +0200267 u32 flip_addr = overlay->flip_addr;
268 u32 tmp;
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100269 int ret;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200270
271 BUG_ON(!overlay->active);
272
273 if (load_polyphase_filter)
274 flip_addr |= OFC_UPDATE;
275
276 /* check for underruns */
277 tmp = I915_READ(DOVSTA);
278 if (tmp & (1 << 17))
279 DRM_DEBUG("overlay underrun, DOVSTA: %x\n", tmp);
280
Daniel Vetter6d90c952012-04-26 23:28:05 +0200281 ret = intel_ring_begin(ring, 2);
Chris Wilsonacb868d2012-09-26 13:47:30 +0100282 if (ret)
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100283 return ret;
Chris Wilsonacb868d2012-09-26 13:47:30 +0100284
Daniel Vetter6d90c952012-04-26 23:28:05 +0200285 intel_ring_emit(ring, MI_OVERLAY_FLIP | MI_OVERLAY_CONTINUE);
286 intel_ring_emit(ring, flip_addr);
287 intel_ring_advance(ring);
Daniel Vetter5a5a0c62009-09-15 22:57:36 +0200288
Chris Wilsonacb868d2012-09-26 13:47:30 +0100289 return i915_add_request(ring, NULL, &overlay->last_flip_req);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200290}
291
Chris Wilsonb303cf92010-08-12 14:03:48 +0100292static void intel_overlay_release_old_vid_tail(struct intel_overlay *overlay)
Daniel Vetter02e792f2009-09-15 22:57:34 +0200293{
Chris Wilson05394f32010-11-08 19:18:58 +0000294 struct drm_i915_gem_object *obj = overlay->old_vid_bo;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200295
Chris Wilsonb303cf92010-08-12 14:03:48 +0100296 i915_gem_object_unpin(obj);
Chris Wilson05394f32010-11-08 19:18:58 +0000297 drm_gem_object_unreference(&obj->base);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200298
Chris Wilsonb303cf92010-08-12 14:03:48 +0100299 overlay->old_vid_bo = NULL;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200300}
301
Daniel Vetter12ca45f2037-04-25 10:08:26 +0200302static void intel_overlay_off_tail(struct intel_overlay *overlay)
303{
Chris Wilson05394f32010-11-08 19:18:58 +0000304 struct drm_i915_gem_object *obj = overlay->vid_bo;
Daniel Vetter12ca45f2037-04-25 10:08:26 +0200305
306 /* never have the overlay hw on without showing a frame */
307 BUG_ON(!overlay->vid_bo);
Daniel Vetter12ca45f2037-04-25 10:08:26 +0200308
309 i915_gem_object_unpin(obj);
Chris Wilson05394f32010-11-08 19:18:58 +0000310 drm_gem_object_unreference(&obj->base);
Daniel Vetter12ca45f2037-04-25 10:08:26 +0200311 overlay->vid_bo = NULL;
312
313 overlay->crtc->overlay = NULL;
314 overlay->crtc = NULL;
315 overlay->active = 0;
316}
317
Daniel Vetter02e792f2009-09-15 22:57:34 +0200318/* overlay needs to be disabled in OCMD reg */
Chris Wilsonce453d82011-02-21 14:43:56 +0000319static int intel_overlay_off(struct intel_overlay *overlay)
Daniel Vetter03f77ea2009-09-15 22:57:37 +0200320{
321 struct drm_device *dev = overlay->dev;
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100322 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter6d90c952012-04-26 23:28:05 +0200323 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
Chris Wilson8dc5d142010-08-12 12:36:12 +0100324 u32 flip_addr = overlay->flip_addr;
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100325 int ret;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200326
327 BUG_ON(!overlay->active);
328
329 /* According to intel docs the overlay hw may hang (when switching
330 * off) without loading the filter coeffs. It is however unclear whether
331 * this applies to the disabling of the overlay or to the switching off
332 * of the hw. Do it in both cases */
333 flip_addr |= OFC_UPDATE;
334
Daniel Vetter6d90c952012-04-26 23:28:05 +0200335 ret = intel_ring_begin(ring, 6);
Chris Wilsonacb868d2012-09-26 13:47:30 +0100336 if (ret)
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100337 return ret;
Chris Wilsonacb868d2012-09-26 13:47:30 +0100338
Daniel Vetter02e792f2009-09-15 22:57:34 +0200339 /* wait for overlay to go idle */
Daniel Vetter6d90c952012-04-26 23:28:05 +0200340 intel_ring_emit(ring, MI_OVERLAY_FLIP | MI_OVERLAY_CONTINUE);
341 intel_ring_emit(ring, flip_addr);
342 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | MI_WAIT_FOR_OVERLAY_FLIP);
Chris Wilson722506f2010-08-12 09:28:50 +0100343 /* turn overlay off */
Daniel Vetter6d90c952012-04-26 23:28:05 +0200344 intel_ring_emit(ring, MI_OVERLAY_FLIP | MI_OVERLAY_OFF);
345 intel_ring_emit(ring, flip_addr);
346 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | MI_WAIT_FOR_OVERLAY_FLIP);
347 intel_ring_advance(ring);
Chris Wilson722506f2010-08-12 09:28:50 +0100348
Chris Wilsonacb868d2012-09-26 13:47:30 +0100349 return intel_overlay_do_wait_request(overlay, intel_overlay_off_tail);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200350}
351
Daniel Vetter03f77ea2009-09-15 22:57:37 +0200352/* recover from an interruption due to a signal
353 * We have to be careful not to repeat work forever an make forward progess. */
Chris Wilsonce453d82011-02-21 14:43:56 +0000354static int intel_overlay_recover_from_interrupt(struct intel_overlay *overlay)
Daniel Vetter03f77ea2009-09-15 22:57:37 +0200355{
356 struct drm_device *dev = overlay->dev;
Zou Nan hai852835f2010-05-21 09:08:56 +0800357 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter6d90c952012-04-26 23:28:05 +0200358 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
Daniel Vetter03f77ea2009-09-15 22:57:37 +0200359 int ret;
Daniel Vetter03f77ea2009-09-15 22:57:37 +0200360
Chris Wilsonb303cf92010-08-12 14:03:48 +0100361 if (overlay->last_flip_req == 0)
362 return 0;
Daniel Vetter03f77ea2009-09-15 22:57:37 +0200363
Ben Widawsky199b2bc2012-05-24 15:03:11 -0700364 ret = i915_wait_seqno(ring, overlay->last_flip_req);
Chris Wilsonb6c028e2010-08-12 11:55:08 +0100365 if (ret)
Daniel Vetter03f77ea2009-09-15 22:57:37 +0200366 return ret;
Ben Widawskyb2da9fe2012-04-26 16:02:58 -0700367 i915_gem_retire_requests(dev);
Daniel Vetter03f77ea2009-09-15 22:57:37 +0200368
Chris Wilsonb303cf92010-08-12 14:03:48 +0100369 if (overlay->flip_tail)
370 overlay->flip_tail(overlay);
Daniel Vetter03f77ea2009-09-15 22:57:37 +0200371
Daniel Vetter03f77ea2009-09-15 22:57:37 +0200372 overlay->last_flip_req = 0;
373 return 0;
374}
375
Daniel Vetter5a5a0c62009-09-15 22:57:36 +0200376/* Wait for pending overlay flip and release old frame.
377 * Needs to be called before the overlay register are changed
Chris Wilson8d74f652010-08-12 10:35:26 +0100378 * via intel_overlay_(un)map_regs
379 */
Daniel Vetter02e792f2009-09-15 22:57:34 +0200380static int intel_overlay_release_old_vid(struct intel_overlay *overlay)
381{
Chris Wilson5cd68c92010-08-12 12:21:54 +0100382 struct drm_device *dev = overlay->dev;
383 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter6d90c952012-04-26 23:28:05 +0200384 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
Daniel Vetter02e792f2009-09-15 22:57:34 +0200385 int ret;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200386
Chris Wilson5cd68c92010-08-12 12:21:54 +0100387 /* Only wait if there is actually an old frame to release to
388 * guarantee forward progress.
389 */
Daniel Vetter03f77ea2009-09-15 22:57:37 +0200390 if (!overlay->old_vid_bo)
391 return 0;
392
Chris Wilson5cd68c92010-08-12 12:21:54 +0100393 if (I915_READ(ISR) & I915_OVERLAY_PLANE_FLIP_PENDING_INTERRUPT) {
394 /* synchronous slowpath */
Daniel Vetter6d90c952012-04-26 23:28:05 +0200395 ret = intel_ring_begin(ring, 2);
Chris Wilsonacb868d2012-09-26 13:47:30 +0100396 if (ret)
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100397 return ret;
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100398
Daniel Vetter6d90c952012-04-26 23:28:05 +0200399 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | MI_WAIT_FOR_OVERLAY_FLIP);
400 intel_ring_emit(ring, MI_NOOP);
401 intel_ring_advance(ring);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200402
Chris Wilsonacb868d2012-09-26 13:47:30 +0100403 ret = intel_overlay_do_wait_request(overlay,
Chris Wilsonb303cf92010-08-12 14:03:48 +0100404 intel_overlay_release_old_vid_tail);
Chris Wilson5cd68c92010-08-12 12:21:54 +0100405 if (ret)
406 return ret;
407 }
Daniel Vetter02e792f2009-09-15 22:57:34 +0200408
Chris Wilson5cd68c92010-08-12 12:21:54 +0100409 intel_overlay_release_old_vid_tail(overlay);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200410 return 0;
411}
412
413struct put_image_params {
414 int format;
415 short dst_x;
416 short dst_y;
417 short dst_w;
418 short dst_h;
419 short src_w;
420 short src_scan_h;
421 short src_scan_w;
422 short src_h;
423 short stride_Y;
424 short stride_UV;
425 int offset_Y;
426 int offset_U;
427 int offset_V;
428};
429
430static int packed_depth_bytes(u32 format)
431{
432 switch (format & I915_OVERLAY_DEPTH_MASK) {
Chris Wilson722506f2010-08-12 09:28:50 +0100433 case I915_OVERLAY_YUV422:
434 return 4;
435 case I915_OVERLAY_YUV411:
436 /* return 6; not implemented */
437 default:
438 return -EINVAL;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200439 }
440}
441
442static int packed_width_bytes(u32 format, short width)
443{
444 switch (format & I915_OVERLAY_DEPTH_MASK) {
Chris Wilson722506f2010-08-12 09:28:50 +0100445 case I915_OVERLAY_YUV422:
446 return width << 1;
447 default:
448 return -EINVAL;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200449 }
450}
451
452static int uv_hsubsampling(u32 format)
453{
454 switch (format & I915_OVERLAY_DEPTH_MASK) {
Chris Wilson722506f2010-08-12 09:28:50 +0100455 case I915_OVERLAY_YUV422:
456 case I915_OVERLAY_YUV420:
457 return 2;
458 case I915_OVERLAY_YUV411:
459 case I915_OVERLAY_YUV410:
460 return 4;
461 default:
462 return -EINVAL;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200463 }
464}
465
466static int uv_vsubsampling(u32 format)
467{
468 switch (format & I915_OVERLAY_DEPTH_MASK) {
Chris Wilson722506f2010-08-12 09:28:50 +0100469 case I915_OVERLAY_YUV420:
470 case I915_OVERLAY_YUV410:
471 return 2;
472 case I915_OVERLAY_YUV422:
473 case I915_OVERLAY_YUV411:
474 return 1;
475 default:
476 return -EINVAL;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200477 }
478}
479
480static u32 calc_swidthsw(struct drm_device *dev, u32 offset, u32 width)
481{
482 u32 mask, shift, ret;
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100483 if (IS_GEN2(dev)) {
Daniel Vetter02e792f2009-09-15 22:57:34 +0200484 mask = 0x1f;
485 shift = 5;
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100486 } else {
487 mask = 0x3f;
488 shift = 6;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200489 }
490 ret = ((offset + width + mask) >> shift) - (offset >> shift);
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100491 if (!IS_GEN2(dev))
Daniel Vetter02e792f2009-09-15 22:57:34 +0200492 ret <<= 1;
Akshay Joshi0206e352011-08-16 15:34:10 -0400493 ret -= 1;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200494 return ret << 2;
495}
496
497static const u16 y_static_hcoeffs[N_HORIZ_Y_TAPS * N_PHASES] = {
498 0x3000, 0xb4a0, 0x1930, 0x1920, 0xb4a0,
499 0x3000, 0xb500, 0x19d0, 0x1880, 0xb440,
500 0x3000, 0xb540, 0x1a88, 0x2f80, 0xb3e0,
501 0x3000, 0xb580, 0x1b30, 0x2e20, 0xb380,
502 0x3000, 0xb5c0, 0x1bd8, 0x2cc0, 0xb320,
503 0x3020, 0xb5e0, 0x1c60, 0x2b80, 0xb2c0,
504 0x3020, 0xb5e0, 0x1cf8, 0x2a20, 0xb260,
505 0x3020, 0xb5e0, 0x1d80, 0x28e0, 0xb200,
506 0x3020, 0xb5c0, 0x1e08, 0x3f40, 0xb1c0,
507 0x3020, 0xb580, 0x1e78, 0x3ce0, 0xb160,
508 0x3040, 0xb520, 0x1ed8, 0x3aa0, 0xb120,
509 0x3040, 0xb4a0, 0x1f30, 0x3880, 0xb0e0,
510 0x3040, 0xb400, 0x1f78, 0x3680, 0xb0a0,
511 0x3020, 0xb340, 0x1fb8, 0x34a0, 0xb060,
512 0x3020, 0xb240, 0x1fe0, 0x32e0, 0xb040,
513 0x3020, 0xb140, 0x1ff8, 0x3160, 0xb020,
Chris Wilson722506f2010-08-12 09:28:50 +0100514 0xb000, 0x3000, 0x0800, 0x3000, 0xb000
515};
516
Daniel Vetter02e792f2009-09-15 22:57:34 +0200517static const u16 uv_static_hcoeffs[N_HORIZ_UV_TAPS * N_PHASES] = {
518 0x3000, 0x1800, 0x1800, 0xb000, 0x18d0, 0x2e60,
519 0xb000, 0x1990, 0x2ce0, 0xb020, 0x1a68, 0x2b40,
520 0xb040, 0x1b20, 0x29e0, 0xb060, 0x1bd8, 0x2880,
521 0xb080, 0x1c88, 0x3e60, 0xb0a0, 0x1d28, 0x3c00,
522 0xb0c0, 0x1db8, 0x39e0, 0xb0e0, 0x1e40, 0x37e0,
523 0xb100, 0x1eb8, 0x3620, 0xb100, 0x1f18, 0x34a0,
524 0xb100, 0x1f68, 0x3360, 0xb0e0, 0x1fa8, 0x3240,
525 0xb0c0, 0x1fe0, 0x3140, 0xb060, 0x1ff0, 0x30a0,
Chris Wilson722506f2010-08-12 09:28:50 +0100526 0x3000, 0x0800, 0x3000
527};
Daniel Vetter02e792f2009-09-15 22:57:34 +0200528
Ben Widawsky75020bc2012-04-16 14:07:43 -0700529static void update_polyphase_filter(struct overlay_registers __iomem *regs)
Daniel Vetter02e792f2009-09-15 22:57:34 +0200530{
Ben Widawsky75020bc2012-04-16 14:07:43 -0700531 memcpy_toio(regs->Y_HCOEFS, y_static_hcoeffs, sizeof(y_static_hcoeffs));
532 memcpy_toio(regs->UV_HCOEFS, uv_static_hcoeffs,
533 sizeof(uv_static_hcoeffs));
Daniel Vetter02e792f2009-09-15 22:57:34 +0200534}
535
536static bool update_scaling_factors(struct intel_overlay *overlay,
Ben Widawsky75020bc2012-04-16 14:07:43 -0700537 struct overlay_registers __iomem *regs,
Daniel Vetter02e792f2009-09-15 22:57:34 +0200538 struct put_image_params *params)
539{
540 /* fixed point with a 12 bit shift */
541 u32 xscale, yscale, xscale_UV, yscale_UV;
542#define FP_SHIFT 12
543#define FRACT_MASK 0xfff
544 bool scale_changed = false;
545 int uv_hscale = uv_hsubsampling(params->format);
546 int uv_vscale = uv_vsubsampling(params->format);
547
548 if (params->dst_w > 1)
549 xscale = ((params->src_scan_w - 1) << FP_SHIFT)
550 /(params->dst_w);
551 else
552 xscale = 1 << FP_SHIFT;
553
554 if (params->dst_h > 1)
555 yscale = ((params->src_scan_h - 1) << FP_SHIFT)
556 /(params->dst_h);
557 else
558 yscale = 1 << FP_SHIFT;
559
560 /*if (params->format & I915_OVERLAY_YUV_PLANAR) {*/
Chris Wilson722506f2010-08-12 09:28:50 +0100561 xscale_UV = xscale/uv_hscale;
562 yscale_UV = yscale/uv_vscale;
563 /* make the Y scale to UV scale ratio an exact multiply */
564 xscale = xscale_UV * uv_hscale;
565 yscale = yscale_UV * uv_vscale;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200566 /*} else {
Chris Wilson722506f2010-08-12 09:28:50 +0100567 xscale_UV = 0;
568 yscale_UV = 0;
569 }*/
Daniel Vetter02e792f2009-09-15 22:57:34 +0200570
571 if (xscale != overlay->old_xscale || yscale != overlay->old_yscale)
572 scale_changed = true;
573 overlay->old_xscale = xscale;
574 overlay->old_yscale = yscale;
575
Ben Widawsky75020bc2012-04-16 14:07:43 -0700576 iowrite32(((yscale & FRACT_MASK) << 20) |
577 ((xscale >> FP_SHIFT) << 16) |
578 ((xscale & FRACT_MASK) << 3),
579 &regs->YRGBSCALE);
Chris Wilson722506f2010-08-12 09:28:50 +0100580
Ben Widawsky75020bc2012-04-16 14:07:43 -0700581 iowrite32(((yscale_UV & FRACT_MASK) << 20) |
582 ((xscale_UV >> FP_SHIFT) << 16) |
583 ((xscale_UV & FRACT_MASK) << 3),
584 &regs->UVSCALE);
Chris Wilson722506f2010-08-12 09:28:50 +0100585
Ben Widawsky75020bc2012-04-16 14:07:43 -0700586 iowrite32((((yscale >> FP_SHIFT) << 16) |
587 ((yscale_UV >> FP_SHIFT) << 0)),
588 &regs->UVSCALEV);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200589
590 if (scale_changed)
591 update_polyphase_filter(regs);
592
593 return scale_changed;
594}
595
596static void update_colorkey(struct intel_overlay *overlay,
Ben Widawsky75020bc2012-04-16 14:07:43 -0700597 struct overlay_registers __iomem *regs)
Daniel Vetter02e792f2009-09-15 22:57:34 +0200598{
599 u32 key = overlay->color_key;
Chris Wilson6ba3ddd2010-08-12 09:30:58 +0100600
Daniel Vetter02e792f2009-09-15 22:57:34 +0200601 switch (overlay->crtc->base.fb->bits_per_pixel) {
Chris Wilson722506f2010-08-12 09:28:50 +0100602 case 8:
Ben Widawsky75020bc2012-04-16 14:07:43 -0700603 iowrite32(0, &regs->DCLRKV);
604 iowrite32(CLK_RGB8I_MASK | DST_KEY_ENABLE, &regs->DCLRKM);
Chris Wilson6ba3ddd2010-08-12 09:30:58 +0100605 break;
606
Chris Wilson722506f2010-08-12 09:28:50 +0100607 case 16:
608 if (overlay->crtc->base.fb->depth == 15) {
Ben Widawsky75020bc2012-04-16 14:07:43 -0700609 iowrite32(RGB15_TO_COLORKEY(key), &regs->DCLRKV);
610 iowrite32(CLK_RGB15_MASK | DST_KEY_ENABLE,
611 &regs->DCLRKM);
Chris Wilson722506f2010-08-12 09:28:50 +0100612 } else {
Ben Widawsky75020bc2012-04-16 14:07:43 -0700613 iowrite32(RGB16_TO_COLORKEY(key), &regs->DCLRKV);
614 iowrite32(CLK_RGB16_MASK | DST_KEY_ENABLE,
615 &regs->DCLRKM);
Chris Wilson722506f2010-08-12 09:28:50 +0100616 }
Chris Wilson6ba3ddd2010-08-12 09:30:58 +0100617 break;
618
Chris Wilson722506f2010-08-12 09:28:50 +0100619 case 24:
620 case 32:
Ben Widawsky75020bc2012-04-16 14:07:43 -0700621 iowrite32(key, &regs->DCLRKV);
622 iowrite32(CLK_RGB24_MASK | DST_KEY_ENABLE, &regs->DCLRKM);
Chris Wilson6ba3ddd2010-08-12 09:30:58 +0100623 break;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200624 }
625}
626
627static u32 overlay_cmd_reg(struct put_image_params *params)
628{
629 u32 cmd = OCMD_ENABLE | OCMD_BUF_TYPE_FRAME | OCMD_BUFFER0;
630
631 if (params->format & I915_OVERLAY_YUV_PLANAR) {
632 switch (params->format & I915_OVERLAY_DEPTH_MASK) {
Chris Wilson722506f2010-08-12 09:28:50 +0100633 case I915_OVERLAY_YUV422:
634 cmd |= OCMD_YUV_422_PLANAR;
635 break;
636 case I915_OVERLAY_YUV420:
637 cmd |= OCMD_YUV_420_PLANAR;
638 break;
639 case I915_OVERLAY_YUV411:
640 case I915_OVERLAY_YUV410:
641 cmd |= OCMD_YUV_410_PLANAR;
642 break;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200643 }
644 } else { /* YUV packed */
645 switch (params->format & I915_OVERLAY_DEPTH_MASK) {
Chris Wilson722506f2010-08-12 09:28:50 +0100646 case I915_OVERLAY_YUV422:
647 cmd |= OCMD_YUV_422_PACKED;
648 break;
649 case I915_OVERLAY_YUV411:
650 cmd |= OCMD_YUV_411_PACKED;
651 break;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200652 }
653
654 switch (params->format & I915_OVERLAY_SWAP_MASK) {
Chris Wilson722506f2010-08-12 09:28:50 +0100655 case I915_OVERLAY_NO_SWAP:
656 break;
657 case I915_OVERLAY_UV_SWAP:
658 cmd |= OCMD_UV_SWAP;
659 break;
660 case I915_OVERLAY_Y_SWAP:
661 cmd |= OCMD_Y_SWAP;
662 break;
663 case I915_OVERLAY_Y_AND_UV_SWAP:
664 cmd |= OCMD_Y_AND_UV_SWAP;
665 break;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200666 }
667 }
668
669 return cmd;
670}
671
Chris Wilson5fe82c52010-08-12 12:38:21 +0100672static int intel_overlay_do_put_image(struct intel_overlay *overlay,
Chris Wilson05394f32010-11-08 19:18:58 +0000673 struct drm_i915_gem_object *new_bo,
Chris Wilson5fe82c52010-08-12 12:38:21 +0100674 struct put_image_params *params)
Daniel Vetter02e792f2009-09-15 22:57:34 +0200675{
676 int ret, tmp_width;
Ben Widawsky75020bc2012-04-16 14:07:43 -0700677 struct overlay_registers __iomem *regs;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200678 bool scale_changed = false;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200679 struct drm_device *dev = overlay->dev;
Ben Widawsky75020bc2012-04-16 14:07:43 -0700680 u32 swidth, swidthsw, sheight, ostride;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200681
682 BUG_ON(!mutex_is_locked(&dev->struct_mutex));
683 BUG_ON(!mutex_is_locked(&dev->mode_config.mutex));
684 BUG_ON(!overlay);
685
Daniel Vetter02e792f2009-09-15 22:57:34 +0200686 ret = intel_overlay_release_old_vid(overlay);
687 if (ret != 0)
688 return ret;
689
Chris Wilson2da3b9b2011-04-14 09:41:17 +0100690 ret = i915_gem_object_pin_to_display_plane(new_bo, 0, NULL);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200691 if (ret != 0)
692 return ret;
693
Chris Wilsond9e86c02010-11-10 16:40:20 +0000694 ret = i915_gem_object_put_fence(new_bo);
695 if (ret)
696 goto out_unpin;
697
Daniel Vetter02e792f2009-09-15 22:57:34 +0200698 if (!overlay->active) {
Ben Widawsky75020bc2012-04-16 14:07:43 -0700699 u32 oconfig;
Chris Wilson8d74f652010-08-12 10:35:26 +0100700 regs = intel_overlay_map_regs(overlay);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200701 if (!regs) {
702 ret = -ENOMEM;
703 goto out_unpin;
704 }
Ben Widawsky75020bc2012-04-16 14:07:43 -0700705 oconfig = OCONF_CC_OUT_8BIT;
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100706 if (IS_GEN4(overlay->dev))
Ben Widawsky75020bc2012-04-16 14:07:43 -0700707 oconfig |= OCONF_CSC_MODE_BT709;
708 oconfig |= overlay->crtc->pipe == 0 ?
Daniel Vetter02e792f2009-09-15 22:57:34 +0200709 OCONF_PIPE_A : OCONF_PIPE_B;
Ben Widawsky75020bc2012-04-16 14:07:43 -0700710 iowrite32(oconfig, &regs->OCONFIG);
Chris Wilson9bb2ff72010-08-12 12:02:11 +0100711 intel_overlay_unmap_regs(overlay, regs);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200712
713 ret = intel_overlay_on(overlay);
714 if (ret != 0)
715 goto out_unpin;
716 }
717
Chris Wilson8d74f652010-08-12 10:35:26 +0100718 regs = intel_overlay_map_regs(overlay);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200719 if (!regs) {
720 ret = -ENOMEM;
721 goto out_unpin;
722 }
723
Ben Widawsky75020bc2012-04-16 14:07:43 -0700724 iowrite32((params->dst_y << 16) | params->dst_x, &regs->DWINPOS);
725 iowrite32((params->dst_h << 16) | params->dst_w, &regs->DWINSZ);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200726
727 if (params->format & I915_OVERLAY_YUV_PACKED)
728 tmp_width = packed_width_bytes(params->format, params->src_w);
729 else
730 tmp_width = params->src_w;
731
Ben Widawsky75020bc2012-04-16 14:07:43 -0700732 swidth = params->src_w;
733 swidthsw = calc_swidthsw(overlay->dev, params->offset_Y, tmp_width);
734 sheight = params->src_h;
735 iowrite32(new_bo->gtt_offset + params->offset_Y, &regs->OBUF_0Y);
736 ostride = params->stride_Y;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200737
738 if (params->format & I915_OVERLAY_YUV_PLANAR) {
739 int uv_hscale = uv_hsubsampling(params->format);
740 int uv_vscale = uv_vsubsampling(params->format);
741 u32 tmp_U, tmp_V;
Ben Widawsky75020bc2012-04-16 14:07:43 -0700742 swidth |= (params->src_w/uv_hscale) << 16;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200743 tmp_U = calc_swidthsw(overlay->dev, params->offset_U,
Chris Wilson722506f2010-08-12 09:28:50 +0100744 params->src_w/uv_hscale);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200745 tmp_V = calc_swidthsw(overlay->dev, params->offset_V,
Chris Wilson722506f2010-08-12 09:28:50 +0100746 params->src_w/uv_hscale);
Ben Widawsky75020bc2012-04-16 14:07:43 -0700747 swidthsw |= max_t(u32, tmp_U, tmp_V) << 16;
748 sheight |= (params->src_h/uv_vscale) << 16;
749 iowrite32(new_bo->gtt_offset + params->offset_U, &regs->OBUF_0U);
750 iowrite32(new_bo->gtt_offset + params->offset_V, &regs->OBUF_0V);
751 ostride |= params->stride_UV << 16;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200752 }
753
Ben Widawsky75020bc2012-04-16 14:07:43 -0700754 iowrite32(swidth, &regs->SWIDTH);
755 iowrite32(swidthsw, &regs->SWIDTHSW);
756 iowrite32(sheight, &regs->SHEIGHT);
757 iowrite32(ostride, &regs->OSTRIDE);
758
Daniel Vetter02e792f2009-09-15 22:57:34 +0200759 scale_changed = update_scaling_factors(overlay, regs, params);
760
761 update_colorkey(overlay, regs);
762
Ben Widawsky75020bc2012-04-16 14:07:43 -0700763 iowrite32(overlay_cmd_reg(params), &regs->OCMD);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200764
Chris Wilson9bb2ff72010-08-12 12:02:11 +0100765 intel_overlay_unmap_regs(overlay, regs);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200766
Chris Wilson8dc5d142010-08-12 12:36:12 +0100767 ret = intel_overlay_continue(overlay, scale_changed);
768 if (ret)
769 goto out_unpin;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200770
771 overlay->old_vid_bo = overlay->vid_bo;
Chris Wilson05394f32010-11-08 19:18:58 +0000772 overlay->vid_bo = new_bo;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200773
774 return 0;
775
776out_unpin:
777 i915_gem_object_unpin(new_bo);
778 return ret;
779}
780
Chris Wilsonce453d82011-02-21 14:43:56 +0000781int intel_overlay_switch_off(struct intel_overlay *overlay)
Daniel Vetter02e792f2009-09-15 22:57:34 +0200782{
Ben Widawsky75020bc2012-04-16 14:07:43 -0700783 struct overlay_registers __iomem *regs;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200784 struct drm_device *dev = overlay->dev;
Chris Wilson5dcdbcb2010-08-12 13:50:28 +0100785 int ret;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200786
787 BUG_ON(!mutex_is_locked(&dev->struct_mutex));
788 BUG_ON(!mutex_is_locked(&dev->mode_config.mutex));
789
Chris Wilsonce453d82011-02-21 14:43:56 +0000790 ret = intel_overlay_recover_from_interrupt(overlay);
Chris Wilsonb303cf92010-08-12 14:03:48 +0100791 if (ret != 0)
792 return ret;
Daniel Vetter9bedb972009-11-30 15:55:49 +0100793
Daniel Vetter02e792f2009-09-15 22:57:34 +0200794 if (!overlay->active)
795 return 0;
796
Daniel Vetter02e792f2009-09-15 22:57:34 +0200797 ret = intel_overlay_release_old_vid(overlay);
798 if (ret != 0)
799 return ret;
800
Chris Wilson8d74f652010-08-12 10:35:26 +0100801 regs = intel_overlay_map_regs(overlay);
Ben Widawsky75020bc2012-04-16 14:07:43 -0700802 iowrite32(0, &regs->OCMD);
Chris Wilson9bb2ff72010-08-12 12:02:11 +0100803 intel_overlay_unmap_regs(overlay, regs);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200804
Chris Wilsonce453d82011-02-21 14:43:56 +0000805 ret = intel_overlay_off(overlay);
Daniel Vetter03f77ea2009-09-15 22:57:37 +0200806 if (ret != 0)
807 return ret;
808
Daniel Vetter12ca45f2037-04-25 10:08:26 +0200809 intel_overlay_off_tail(overlay);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200810 return 0;
811}
812
813static int check_overlay_possible_on_crtc(struct intel_overlay *overlay,
814 struct intel_crtc *crtc)
815{
Chris Wilson722506f2010-08-12 09:28:50 +0100816 drm_i915_private_t *dev_priv = overlay->dev->dev_private;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200817
Chris Wilsonf7abfe82010-09-13 14:19:16 +0100818 if (!crtc->active)
Daniel Vetter02e792f2009-09-15 22:57:34 +0200819 return -EINVAL;
820
Daniel Vetter02e792f2009-09-15 22:57:34 +0200821 /* can't use the overlay with double wide pipe */
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100822 if (INTEL_INFO(overlay->dev)->gen < 4 &&
Chris Wilsonf7abfe82010-09-13 14:19:16 +0100823 (I915_READ(PIPECONF(crtc->pipe)) & (PIPECONF_DOUBLE_WIDE | PIPECONF_ENABLE)) != PIPECONF_ENABLE)
Daniel Vetter02e792f2009-09-15 22:57:34 +0200824 return -EINVAL;
825
826 return 0;
827}
828
829static void update_pfit_vscale_ratio(struct intel_overlay *overlay)
830{
831 struct drm_device *dev = overlay->dev;
Chris Wilson722506f2010-08-12 09:28:50 +0100832 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200833 u32 pfit_control = I915_READ(PFIT_CONTROL);
Chris Wilson446d2182010-08-12 11:15:58 +0100834 u32 ratio;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200835
836 /* XXX: This is not the same logic as in the xorg driver, but more in
Chris Wilson446d2182010-08-12 11:15:58 +0100837 * line with the intel documentation for the i965
838 */
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100839 if (INTEL_INFO(dev)->gen >= 4) {
Akshay Joshi0206e352011-08-16 15:34:10 -0400840 /* on i965 use the PGM reg to read out the autoscaler values */
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100841 ratio = I915_READ(PFIT_PGM_RATIOS) >> PFIT_VERT_SCALE_SHIFT_965;
842 } else {
Chris Wilson446d2182010-08-12 11:15:58 +0100843 if (pfit_control & VERT_AUTO_SCALE)
844 ratio = I915_READ(PFIT_AUTO_RATIOS);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200845 else
Chris Wilson446d2182010-08-12 11:15:58 +0100846 ratio = I915_READ(PFIT_PGM_RATIOS);
847 ratio >>= PFIT_VERT_SCALE_SHIFT;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200848 }
849
850 overlay->pfit_vscale_ratio = ratio;
851}
852
853static int check_overlay_dst(struct intel_overlay *overlay,
854 struct drm_intel_overlay_put_image *rec)
855{
856 struct drm_display_mode *mode = &overlay->crtc->base.mode;
857
Daniel Vetter75c13992012-01-28 23:48:46 +0100858 if (rec->dst_x < mode->hdisplay &&
859 rec->dst_x + rec->dst_width <= mode->hdisplay &&
860 rec->dst_y < mode->vdisplay &&
861 rec->dst_y + rec->dst_height <= mode->vdisplay)
Daniel Vetter02e792f2009-09-15 22:57:34 +0200862 return 0;
863 else
864 return -EINVAL;
865}
866
867static int check_overlay_scaling(struct put_image_params *rec)
868{
869 u32 tmp;
870
871 /* downscaling limit is 8.0 */
872 tmp = ((rec->src_scan_h << 16) / rec->dst_h) >> 16;
873 if (tmp > 7)
874 return -EINVAL;
875 tmp = ((rec->src_scan_w << 16) / rec->dst_w) >> 16;
876 if (tmp > 7)
877 return -EINVAL;
878
879 return 0;
880}
881
882static int check_overlay_src(struct drm_device *dev,
883 struct drm_intel_overlay_put_image *rec,
Chris Wilson05394f32010-11-08 19:18:58 +0000884 struct drm_i915_gem_object *new_bo)
Daniel Vetter02e792f2009-09-15 22:57:34 +0200885{
Daniel Vetter02e792f2009-09-15 22:57:34 +0200886 int uv_hscale = uv_hsubsampling(rec->flags);
887 int uv_vscale = uv_vsubsampling(rec->flags);
Dan Carpenter8f28f542010-10-27 23:17:25 +0200888 u32 stride_mask;
889 int depth;
890 u32 tmp;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200891
892 /* check src dimensions */
893 if (IS_845G(dev) || IS_I830(dev)) {
Chris Wilson722506f2010-08-12 09:28:50 +0100894 if (rec->src_height > IMAGE_MAX_HEIGHT_LEGACY ||
Chris Wilson9f7c3f42010-08-12 11:29:34 +0100895 rec->src_width > IMAGE_MAX_WIDTH_LEGACY)
Daniel Vetter02e792f2009-09-15 22:57:34 +0200896 return -EINVAL;
897 } else {
Chris Wilson722506f2010-08-12 09:28:50 +0100898 if (rec->src_height > IMAGE_MAX_HEIGHT ||
Chris Wilson9f7c3f42010-08-12 11:29:34 +0100899 rec->src_width > IMAGE_MAX_WIDTH)
Daniel Vetter02e792f2009-09-15 22:57:34 +0200900 return -EINVAL;
901 }
Chris Wilson9f7c3f42010-08-12 11:29:34 +0100902
Daniel Vetter02e792f2009-09-15 22:57:34 +0200903 /* better safe than sorry, use 4 as the maximal subsampling ratio */
Chris Wilson722506f2010-08-12 09:28:50 +0100904 if (rec->src_height < N_VERT_Y_TAPS*4 ||
Chris Wilson9f7c3f42010-08-12 11:29:34 +0100905 rec->src_width < N_HORIZ_Y_TAPS*4)
Daniel Vetter02e792f2009-09-15 22:57:34 +0200906 return -EINVAL;
907
Chris Wilsona1efd142010-07-12 19:35:38 +0100908 /* check alignment constraints */
Daniel Vetter02e792f2009-09-15 22:57:34 +0200909 switch (rec->flags & I915_OVERLAY_TYPE_MASK) {
Chris Wilson722506f2010-08-12 09:28:50 +0100910 case I915_OVERLAY_RGB:
911 /* not implemented */
912 return -EINVAL;
Chris Wilson9f7c3f42010-08-12 11:29:34 +0100913
Chris Wilson722506f2010-08-12 09:28:50 +0100914 case I915_OVERLAY_YUV_PACKED:
Chris Wilson722506f2010-08-12 09:28:50 +0100915 if (uv_vscale != 1)
Daniel Vetter02e792f2009-09-15 22:57:34 +0200916 return -EINVAL;
Chris Wilson9f7c3f42010-08-12 11:29:34 +0100917
918 depth = packed_depth_bytes(rec->flags);
Chris Wilson722506f2010-08-12 09:28:50 +0100919 if (depth < 0)
920 return depth;
Chris Wilson9f7c3f42010-08-12 11:29:34 +0100921
Chris Wilson722506f2010-08-12 09:28:50 +0100922 /* ignore UV planes */
923 rec->stride_UV = 0;
924 rec->offset_U = 0;
925 rec->offset_V = 0;
926 /* check pixel alignment */
927 if (rec->offset_Y % depth)
Daniel Vetter02e792f2009-09-15 22:57:34 +0200928 return -EINVAL;
Chris Wilson722506f2010-08-12 09:28:50 +0100929 break;
Chris Wilson9f7c3f42010-08-12 11:29:34 +0100930
Chris Wilson722506f2010-08-12 09:28:50 +0100931 case I915_OVERLAY_YUV_PLANAR:
932 if (uv_vscale < 0 || uv_hscale < 0)
933 return -EINVAL;
934 /* no offset restrictions for planar formats */
935 break;
Chris Wilson9f7c3f42010-08-12 11:29:34 +0100936
Chris Wilson722506f2010-08-12 09:28:50 +0100937 default:
938 return -EINVAL;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200939 }
940
941 if (rec->src_width % uv_hscale)
942 return -EINVAL;
943
944 /* stride checking */
Chris Wilsona1efd142010-07-12 19:35:38 +0100945 if (IS_I830(dev) || IS_845G(dev))
946 stride_mask = 255;
947 else
948 stride_mask = 63;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200949
950 if (rec->stride_Y & stride_mask || rec->stride_UV & stride_mask)
951 return -EINVAL;
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100952 if (IS_GEN4(dev) && rec->stride_Y < 512)
Daniel Vetter02e792f2009-09-15 22:57:34 +0200953 return -EINVAL;
954
955 tmp = (rec->flags & I915_OVERLAY_TYPE_MASK) == I915_OVERLAY_YUV_PLANAR ?
Chris Wilson9f7c3f42010-08-12 11:29:34 +0100956 4096 : 8192;
957 if (rec->stride_Y > tmp || rec->stride_UV > 2*1024)
Daniel Vetter02e792f2009-09-15 22:57:34 +0200958 return -EINVAL;
959
960 /* check buffer dimensions */
961 switch (rec->flags & I915_OVERLAY_TYPE_MASK) {
Chris Wilson722506f2010-08-12 09:28:50 +0100962 case I915_OVERLAY_RGB:
963 case I915_OVERLAY_YUV_PACKED:
964 /* always 4 Y values per depth pixels */
965 if (packed_width_bytes(rec->flags, rec->src_width) > rec->stride_Y)
966 return -EINVAL;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200967
Chris Wilson722506f2010-08-12 09:28:50 +0100968 tmp = rec->stride_Y*rec->src_height;
Chris Wilson05394f32010-11-08 19:18:58 +0000969 if (rec->offset_Y + tmp > new_bo->base.size)
Chris Wilson722506f2010-08-12 09:28:50 +0100970 return -EINVAL;
971 break;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200972
Chris Wilson722506f2010-08-12 09:28:50 +0100973 case I915_OVERLAY_YUV_PLANAR:
974 if (rec->src_width > rec->stride_Y)
975 return -EINVAL;
976 if (rec->src_width/uv_hscale > rec->stride_UV)
977 return -EINVAL;
978
Chris Wilson9f7c3f42010-08-12 11:29:34 +0100979 tmp = rec->stride_Y * rec->src_height;
Chris Wilson05394f32010-11-08 19:18:58 +0000980 if (rec->offset_Y + tmp > new_bo->base.size)
Chris Wilson722506f2010-08-12 09:28:50 +0100981 return -EINVAL;
Chris Wilson9f7c3f42010-08-12 11:29:34 +0100982
983 tmp = rec->stride_UV * (rec->src_height / uv_vscale);
Chris Wilson05394f32010-11-08 19:18:58 +0000984 if (rec->offset_U + tmp > new_bo->base.size ||
985 rec->offset_V + tmp > new_bo->base.size)
Chris Wilson722506f2010-08-12 09:28:50 +0100986 return -EINVAL;
987 break;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200988 }
989
990 return 0;
991}
992
Chris Wilsone9e331a2010-09-13 01:16:10 +0100993/**
994 * Return the pipe currently connected to the panel fitter,
995 * or -1 if the panel fitter is not present or not in use
996 */
997static int intel_panel_fitter_pipe(struct drm_device *dev)
998{
999 struct drm_i915_private *dev_priv = dev->dev_private;
1000 u32 pfit_control;
1001
1002 /* i830 doesn't have a panel fitter */
1003 if (IS_I830(dev))
1004 return -1;
1005
1006 pfit_control = I915_READ(PFIT_CONTROL);
1007
1008 /* See if the panel fitter is in use */
1009 if ((pfit_control & PFIT_ENABLE) == 0)
1010 return -1;
1011
1012 /* 965 can place panel fitter on either pipe */
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001013 if (IS_GEN4(dev))
Chris Wilsone9e331a2010-09-13 01:16:10 +01001014 return (pfit_control >> 29) & 0x3;
1015
1016 /* older chips can only use pipe 1 */
1017 return 1;
1018}
1019
Daniel Vetter02e792f2009-09-15 22:57:34 +02001020int intel_overlay_put_image(struct drm_device *dev, void *data,
Akshay Joshi0206e352011-08-16 15:34:10 -04001021 struct drm_file *file_priv)
Daniel Vetter02e792f2009-09-15 22:57:34 +02001022{
1023 struct drm_intel_overlay_put_image *put_image_rec = data;
1024 drm_i915_private_t *dev_priv = dev->dev_private;
1025 struct intel_overlay *overlay;
1026 struct drm_mode_object *drmmode_obj;
1027 struct intel_crtc *crtc;
Chris Wilson05394f32010-11-08 19:18:58 +00001028 struct drm_i915_gem_object *new_bo;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001029 struct put_image_params *params;
1030 int ret;
1031
Daniel Vetter1cff8f62012-04-24 09:55:08 +02001032 /* No need to check for DRIVER_MODESET - we don't set it up then. */
Daniel Vetter02e792f2009-09-15 22:57:34 +02001033 overlay = dev_priv->overlay;
1034 if (!overlay) {
1035 DRM_DEBUG("userspace bug: no overlay\n");
1036 return -ENODEV;
1037 }
1038
1039 if (!(put_image_rec->flags & I915_OVERLAY_ENABLE)) {
1040 mutex_lock(&dev->mode_config.mutex);
1041 mutex_lock(&dev->struct_mutex);
1042
Chris Wilsonce453d82011-02-21 14:43:56 +00001043 ret = intel_overlay_switch_off(overlay);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001044
1045 mutex_unlock(&dev->struct_mutex);
1046 mutex_unlock(&dev->mode_config.mutex);
1047
1048 return ret;
1049 }
1050
1051 params = kmalloc(sizeof(struct put_image_params), GFP_KERNEL);
1052 if (!params)
1053 return -ENOMEM;
1054
1055 drmmode_obj = drm_mode_object_find(dev, put_image_rec->crtc_id,
Chris Wilson722506f2010-08-12 09:28:50 +01001056 DRM_MODE_OBJECT_CRTC);
Dan Carpenter915a4282010-03-06 14:05:39 +03001057 if (!drmmode_obj) {
1058 ret = -ENOENT;
1059 goto out_free;
1060 }
Daniel Vetter02e792f2009-09-15 22:57:34 +02001061 crtc = to_intel_crtc(obj_to_crtc(drmmode_obj));
1062
Chris Wilson05394f32010-11-08 19:18:58 +00001063 new_bo = to_intel_bo(drm_gem_object_lookup(dev, file_priv,
1064 put_image_rec->bo_handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00001065 if (&new_bo->base == NULL) {
Dan Carpenter915a4282010-03-06 14:05:39 +03001066 ret = -ENOENT;
1067 goto out_free;
1068 }
Daniel Vetter02e792f2009-09-15 22:57:34 +02001069
1070 mutex_lock(&dev->mode_config.mutex);
1071 mutex_lock(&dev->struct_mutex);
1072
Chris Wilsond9e86c02010-11-10 16:40:20 +00001073 if (new_bo->tiling_mode) {
1074 DRM_ERROR("buffer used for overlay image can not be tiled\n");
1075 ret = -EINVAL;
1076 goto out_unlock;
1077 }
1078
Chris Wilsonce453d82011-02-21 14:43:56 +00001079 ret = intel_overlay_recover_from_interrupt(overlay);
Chris Wilsonb303cf92010-08-12 14:03:48 +01001080 if (ret != 0)
1081 goto out_unlock;
Daniel Vetter03f77ea2009-09-15 22:57:37 +02001082
Daniel Vetter02e792f2009-09-15 22:57:34 +02001083 if (overlay->crtc != crtc) {
1084 struct drm_display_mode *mode = &crtc->base.mode;
Chris Wilsonce453d82011-02-21 14:43:56 +00001085 ret = intel_overlay_switch_off(overlay);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001086 if (ret != 0)
1087 goto out_unlock;
1088
1089 ret = check_overlay_possible_on_crtc(overlay, crtc);
1090 if (ret != 0)
1091 goto out_unlock;
1092
1093 overlay->crtc = crtc;
1094 crtc->overlay = overlay;
1095
Chris Wilsone9e331a2010-09-13 01:16:10 +01001096 /* line too wide, i.e. one-line-mode */
1097 if (mode->hdisplay > 1024 &&
1098 intel_panel_fitter_pipe(dev) == crtc->pipe) {
Daniel Vetter02e792f2009-09-15 22:57:34 +02001099 overlay->pfit_active = 1;
1100 update_pfit_vscale_ratio(overlay);
1101 } else
1102 overlay->pfit_active = 0;
1103 }
1104
1105 ret = check_overlay_dst(overlay, put_image_rec);
1106 if (ret != 0)
1107 goto out_unlock;
1108
1109 if (overlay->pfit_active) {
1110 params->dst_y = ((((u32)put_image_rec->dst_y) << 12) /
Chris Wilson722506f2010-08-12 09:28:50 +01001111 overlay->pfit_vscale_ratio);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001112 /* shifting right rounds downwards, so add 1 */
1113 params->dst_h = ((((u32)put_image_rec->dst_height) << 12) /
Chris Wilson722506f2010-08-12 09:28:50 +01001114 overlay->pfit_vscale_ratio) + 1;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001115 } else {
1116 params->dst_y = put_image_rec->dst_y;
1117 params->dst_h = put_image_rec->dst_height;
1118 }
1119 params->dst_x = put_image_rec->dst_x;
1120 params->dst_w = put_image_rec->dst_width;
1121
1122 params->src_w = put_image_rec->src_width;
1123 params->src_h = put_image_rec->src_height;
1124 params->src_scan_w = put_image_rec->src_scan_width;
1125 params->src_scan_h = put_image_rec->src_scan_height;
Chris Wilson722506f2010-08-12 09:28:50 +01001126 if (params->src_scan_h > params->src_h ||
1127 params->src_scan_w > params->src_w) {
Daniel Vetter02e792f2009-09-15 22:57:34 +02001128 ret = -EINVAL;
1129 goto out_unlock;
1130 }
1131
1132 ret = check_overlay_src(dev, put_image_rec, new_bo);
1133 if (ret != 0)
1134 goto out_unlock;
1135 params->format = put_image_rec->flags & ~I915_OVERLAY_FLAGS_MASK;
1136 params->stride_Y = put_image_rec->stride_Y;
1137 params->stride_UV = put_image_rec->stride_UV;
1138 params->offset_Y = put_image_rec->offset_Y;
1139 params->offset_U = put_image_rec->offset_U;
1140 params->offset_V = put_image_rec->offset_V;
1141
1142 /* Check scaling after src size to prevent a divide-by-zero. */
1143 ret = check_overlay_scaling(params);
1144 if (ret != 0)
1145 goto out_unlock;
1146
1147 ret = intel_overlay_do_put_image(overlay, new_bo, params);
1148 if (ret != 0)
1149 goto out_unlock;
1150
1151 mutex_unlock(&dev->struct_mutex);
1152 mutex_unlock(&dev->mode_config.mutex);
1153
1154 kfree(params);
1155
1156 return 0;
1157
1158out_unlock:
1159 mutex_unlock(&dev->struct_mutex);
1160 mutex_unlock(&dev->mode_config.mutex);
Chris Wilson05394f32010-11-08 19:18:58 +00001161 drm_gem_object_unreference_unlocked(&new_bo->base);
Dan Carpenter915a4282010-03-06 14:05:39 +03001162out_free:
Daniel Vetter02e792f2009-09-15 22:57:34 +02001163 kfree(params);
1164
1165 return ret;
1166}
1167
1168static void update_reg_attrs(struct intel_overlay *overlay,
Ben Widawsky75020bc2012-04-16 14:07:43 -07001169 struct overlay_registers __iomem *regs)
Daniel Vetter02e792f2009-09-15 22:57:34 +02001170{
Ben Widawsky75020bc2012-04-16 14:07:43 -07001171 iowrite32((overlay->contrast << 18) | (overlay->brightness & 0xff),
1172 &regs->OCLRC0);
1173 iowrite32(overlay->saturation, &regs->OCLRC1);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001174}
1175
1176static bool check_gamma_bounds(u32 gamma1, u32 gamma2)
1177{
1178 int i;
1179
1180 if (gamma1 & 0xff000000 || gamma2 & 0xff000000)
1181 return false;
1182
1183 for (i = 0; i < 3; i++) {
Chris Wilson722506f2010-08-12 09:28:50 +01001184 if (((gamma1 >> i*8) & 0xff) >= ((gamma2 >> i*8) & 0xff))
Daniel Vetter02e792f2009-09-15 22:57:34 +02001185 return false;
1186 }
1187
1188 return true;
1189}
1190
1191static bool check_gamma5_errata(u32 gamma5)
1192{
1193 int i;
1194
1195 for (i = 0; i < 3; i++) {
1196 if (((gamma5 >> i*8) & 0xff) == 0x80)
1197 return false;
1198 }
1199
1200 return true;
1201}
1202
1203static int check_gamma(struct drm_intel_overlay_attrs *attrs)
1204{
Chris Wilson722506f2010-08-12 09:28:50 +01001205 if (!check_gamma_bounds(0, attrs->gamma0) ||
1206 !check_gamma_bounds(attrs->gamma0, attrs->gamma1) ||
1207 !check_gamma_bounds(attrs->gamma1, attrs->gamma2) ||
1208 !check_gamma_bounds(attrs->gamma2, attrs->gamma3) ||
1209 !check_gamma_bounds(attrs->gamma3, attrs->gamma4) ||
1210 !check_gamma_bounds(attrs->gamma4, attrs->gamma5) ||
1211 !check_gamma_bounds(attrs->gamma5, 0x00ffffff))
Daniel Vetter02e792f2009-09-15 22:57:34 +02001212 return -EINVAL;
Chris Wilson722506f2010-08-12 09:28:50 +01001213
Daniel Vetter02e792f2009-09-15 22:57:34 +02001214 if (!check_gamma5_errata(attrs->gamma5))
1215 return -EINVAL;
Chris Wilson722506f2010-08-12 09:28:50 +01001216
Daniel Vetter02e792f2009-09-15 22:57:34 +02001217 return 0;
1218}
1219
1220int intel_overlay_attrs(struct drm_device *dev, void *data,
Akshay Joshi0206e352011-08-16 15:34:10 -04001221 struct drm_file *file_priv)
Daniel Vetter02e792f2009-09-15 22:57:34 +02001222{
1223 struct drm_intel_overlay_attrs *attrs = data;
Akshay Joshi0206e352011-08-16 15:34:10 -04001224 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001225 struct intel_overlay *overlay;
Ben Widawsky75020bc2012-04-16 14:07:43 -07001226 struct overlay_registers __iomem *regs;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001227 int ret;
1228
Daniel Vetter1cff8f62012-04-24 09:55:08 +02001229 /* No need to check for DRIVER_MODESET - we don't set it up then. */
Daniel Vetter02e792f2009-09-15 22:57:34 +02001230 overlay = dev_priv->overlay;
1231 if (!overlay) {
1232 DRM_DEBUG("userspace bug: no overlay\n");
1233 return -ENODEV;
1234 }
1235
1236 mutex_lock(&dev->mode_config.mutex);
1237 mutex_lock(&dev->struct_mutex);
1238
Chris Wilson60fc3322010-08-12 10:44:45 +01001239 ret = -EINVAL;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001240 if (!(attrs->flags & I915_OVERLAY_UPDATE_ATTRS)) {
Chris Wilson60fc3322010-08-12 10:44:45 +01001241 attrs->color_key = overlay->color_key;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001242 attrs->brightness = overlay->brightness;
Chris Wilson60fc3322010-08-12 10:44:45 +01001243 attrs->contrast = overlay->contrast;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001244 attrs->saturation = overlay->saturation;
1245
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001246 if (!IS_GEN2(dev)) {
Daniel Vetter02e792f2009-09-15 22:57:34 +02001247 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);
1253 }
Daniel Vetter02e792f2009-09-15 22:57:34 +02001254 } else {
Chris Wilson60fc3322010-08-12 10:44:45 +01001255 if (attrs->brightness < -128 || attrs->brightness > 127)
Daniel Vetter02e792f2009-09-15 22:57:34 +02001256 goto out_unlock;
Chris Wilson60fc3322010-08-12 10:44:45 +01001257 if (attrs->contrast > 255)
Daniel Vetter02e792f2009-09-15 22:57:34 +02001258 goto out_unlock;
Chris Wilson60fc3322010-08-12 10:44:45 +01001259 if (attrs->saturation > 1023)
Daniel Vetter02e792f2009-09-15 22:57:34 +02001260 goto out_unlock;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001261
Chris Wilson60fc3322010-08-12 10:44:45 +01001262 overlay->color_key = attrs->color_key;
1263 overlay->brightness = attrs->brightness;
1264 overlay->contrast = attrs->contrast;
1265 overlay->saturation = attrs->saturation;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001266
Chris Wilson8d74f652010-08-12 10:35:26 +01001267 regs = intel_overlay_map_regs(overlay);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001268 if (!regs) {
1269 ret = -ENOMEM;
1270 goto out_unlock;
1271 }
1272
1273 update_reg_attrs(overlay, regs);
1274
Chris Wilson9bb2ff72010-08-12 12:02:11 +01001275 intel_overlay_unmap_regs(overlay, regs);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001276
1277 if (attrs->flags & I915_OVERLAY_UPDATE_GAMMA) {
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001278 if (IS_GEN2(dev))
Daniel Vetter02e792f2009-09-15 22:57:34 +02001279 goto out_unlock;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001280
1281 if (overlay->active) {
1282 ret = -EBUSY;
1283 goto out_unlock;
1284 }
1285
1286 ret = check_gamma(attrs);
Chris Wilson60fc3322010-08-12 10:44:45 +01001287 if (ret)
Daniel Vetter02e792f2009-09-15 22:57:34 +02001288 goto out_unlock;
1289
1290 I915_WRITE(OGAMC0, attrs->gamma0);
1291 I915_WRITE(OGAMC1, attrs->gamma1);
1292 I915_WRITE(OGAMC2, attrs->gamma2);
1293 I915_WRITE(OGAMC3, attrs->gamma3);
1294 I915_WRITE(OGAMC4, attrs->gamma4);
1295 I915_WRITE(OGAMC5, attrs->gamma5);
1296 }
Daniel Vetter02e792f2009-09-15 22:57:34 +02001297 }
1298
Chris Wilson60fc3322010-08-12 10:44:45 +01001299 ret = 0;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001300out_unlock:
1301 mutex_unlock(&dev->struct_mutex);
1302 mutex_unlock(&dev->mode_config.mutex);
1303
1304 return ret;
1305}
1306
1307void intel_setup_overlay(struct drm_device *dev)
1308{
Akshay Joshi0206e352011-08-16 15:34:10 -04001309 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001310 struct intel_overlay *overlay;
Chris Wilson05394f32010-11-08 19:18:58 +00001311 struct drm_i915_gem_object *reg_bo;
Ben Widawsky75020bc2012-04-16 14:07:43 -07001312 struct overlay_registers __iomem *regs;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001313 int ret;
1314
Chris Wilson315781482010-08-12 09:42:51 +01001315 if (!HAS_OVERLAY(dev))
Daniel Vetter02e792f2009-09-15 22:57:34 +02001316 return;
1317
1318 overlay = kzalloc(sizeof(struct intel_overlay), GFP_KERNEL);
1319 if (!overlay)
1320 return;
Chris Wilson79d24272011-06-28 11:27:47 +01001321
1322 mutex_lock(&dev->struct_mutex);
1323 if (WARN_ON(dev_priv->overlay))
1324 goto out_free;
1325
Daniel Vetter02e792f2009-09-15 22:57:34 +02001326 overlay->dev = dev;
1327
Daniel Vetterac52bc52010-04-09 19:05:06 +00001328 reg_bo = i915_gem_alloc_object(dev, PAGE_SIZE);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001329 if (!reg_bo)
1330 goto out_free;
Chris Wilson05394f32010-11-08 19:18:58 +00001331 overlay->reg_bo = reg_bo;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001332
Chris Wilson315781482010-08-12 09:42:51 +01001333 if (OVERLAY_NEEDS_PHYSICAL(dev)) {
1334 ret = i915_gem_attach_phys_object(dev, reg_bo,
1335 I915_GEM_PHYS_OVERLAY_REGS,
Chris Wilsona2930122010-08-12 10:47:56 +01001336 PAGE_SIZE);
Akshay Joshi0206e352011-08-16 15:34:10 -04001337 if (ret) {
1338 DRM_ERROR("failed to attach phys overlay regs\n");
1339 goto out_free_bo;
1340 }
Chris Wilson05394f32010-11-08 19:18:58 +00001341 overlay->flip_addr = reg_bo->phys_obj->handle->busaddr;
Chris Wilson315781482010-08-12 09:42:51 +01001342 } else {
Chris Wilson86a1ee22012-08-11 15:41:04 +01001343 ret = i915_gem_object_pin(reg_bo, PAGE_SIZE, true, false);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001344 if (ret) {
Akshay Joshi0206e352011-08-16 15:34:10 -04001345 DRM_ERROR("failed to pin overlay register bo\n");
1346 goto out_free_bo;
1347 }
Chris Wilson05394f32010-11-08 19:18:58 +00001348 overlay->flip_addr = reg_bo->gtt_offset;
Chris Wilson0ddc1282010-08-12 09:35:00 +01001349
1350 ret = i915_gem_object_set_to_gtt_domain(reg_bo, true);
1351 if (ret) {
Akshay Joshi0206e352011-08-16 15:34:10 -04001352 DRM_ERROR("failed to move overlay register bo into the GTT\n");
1353 goto out_unpin_bo;
1354 }
Daniel Vetter02e792f2009-09-15 22:57:34 +02001355 }
1356
1357 /* init all values */
1358 overlay->color_key = 0x0101fe;
1359 overlay->brightness = -19;
1360 overlay->contrast = 75;
1361 overlay->saturation = 146;
1362
Chris Wilson8d74f652010-08-12 10:35:26 +01001363 regs = intel_overlay_map_regs(overlay);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001364 if (!regs)
Chris Wilson79d24272011-06-28 11:27:47 +01001365 goto out_unpin_bo;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001366
Ben Widawsky75020bc2012-04-16 14:07:43 -07001367 memset_io(regs, 0, sizeof(struct overlay_registers));
Daniel Vetter02e792f2009-09-15 22:57:34 +02001368 update_polyphase_filter(regs);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001369 update_reg_attrs(overlay, regs);
1370
Chris Wilson9bb2ff72010-08-12 12:02:11 +01001371 intel_overlay_unmap_regs(overlay, regs);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001372
1373 dev_priv->overlay = overlay;
Chris Wilson79d24272011-06-28 11:27:47 +01001374 mutex_unlock(&dev->struct_mutex);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001375 DRM_INFO("initialized overlay support\n");
1376 return;
1377
Chris Wilson0ddc1282010-08-12 09:35:00 +01001378out_unpin_bo:
Chris Wilson79d24272011-06-28 11:27:47 +01001379 if (!OVERLAY_NEEDS_PHYSICAL(dev))
1380 i915_gem_object_unpin(reg_bo);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001381out_free_bo:
Chris Wilson05394f32010-11-08 19:18:58 +00001382 drm_gem_object_unreference(&reg_bo->base);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001383out_free:
Chris Wilson79d24272011-06-28 11:27:47 +01001384 mutex_unlock(&dev->struct_mutex);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001385 kfree(overlay);
1386 return;
1387}
1388
1389void intel_cleanup_overlay(struct drm_device *dev)
1390{
Chris Wilson722506f2010-08-12 09:28:50 +01001391 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001392
Chris Wilson62cf4e62010-08-12 10:50:36 +01001393 if (!dev_priv->overlay)
1394 return;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001395
Chris Wilson62cf4e62010-08-12 10:50:36 +01001396 /* 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 */
1399 BUG_ON(dev_priv->overlay->active);
1400
1401 drm_gem_object_unreference_unlocked(&dev_priv->overlay->reg_bo->base);
1402 kfree(dev_priv->overlay);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001403}
Chris Wilson6ef3d422010-08-04 20:26:07 +01001404
Chris Wilson3bd3c932010-08-19 08:19:30 +01001405#ifdef CONFIG_DEBUG_FS
1406#include <linux/seq_file.h>
1407
Chris Wilson6ef3d422010-08-04 20:26:07 +01001408struct intel_overlay_error_state {
1409 struct overlay_registers regs;
1410 unsigned long base;
1411 u32 dovsta;
1412 u32 isr;
1413};
1414
Ben Widawsky75020bc2012-04-16 14:07:43 -07001415static struct overlay_registers __iomem *
Linus Torvaldsc48c43e2010-10-26 18:57:59 -07001416intel_overlay_map_regs_atomic(struct intel_overlay *overlay)
Chris Wilson3bd3c932010-08-19 08:19:30 +01001417{
Linus Torvaldsc48c43e2010-10-26 18:57:59 -07001418 drm_i915_private_t *dev_priv = overlay->dev->dev_private;
Ben Widawsky75020bc2012-04-16 14:07:43 -07001419 struct overlay_registers __iomem *regs;
Chris Wilson3bd3c932010-08-19 08:19:30 +01001420
1421 if (OVERLAY_NEEDS_PHYSICAL(overlay->dev))
Ben Widawsky75020bc2012-04-16 14:07:43 -07001422 /* Cast to make sparse happy, but it's wc memory anyway, so
1423 * equivalent to the wc io mapping on X86. */
1424 regs = (struct overlay_registers __iomem *)
1425 overlay->reg_bo->phys_obj->handle->vaddr;
Chris Wilson3bd3c932010-08-19 08:19:30 +01001426 else
1427 regs = io_mapping_map_atomic_wc(dev_priv->mm.gtt_mapping,
Linus Torvaldsc48c43e2010-10-26 18:57:59 -07001428 overlay->reg_bo->gtt_offset);
Chris Wilson3bd3c932010-08-19 08:19:30 +01001429
1430 return regs;
1431}
1432
1433static void intel_overlay_unmap_regs_atomic(struct intel_overlay *overlay,
Ben Widawsky75020bc2012-04-16 14:07:43 -07001434 struct overlay_registers __iomem *regs)
Chris Wilson3bd3c932010-08-19 08:19:30 +01001435{
1436 if (!OVERLAY_NEEDS_PHYSICAL(overlay->dev))
Linus Torvaldsc48c43e2010-10-26 18:57:59 -07001437 io_mapping_unmap_atomic(regs);
Chris Wilson3bd3c932010-08-19 08:19:30 +01001438}
1439
1440
Chris Wilson6ef3d422010-08-04 20:26:07 +01001441struct intel_overlay_error_state *
1442intel_overlay_capture_error_state(struct drm_device *dev)
1443{
Akshay Joshi0206e352011-08-16 15:34:10 -04001444 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson6ef3d422010-08-04 20:26:07 +01001445 struct intel_overlay *overlay = dev_priv->overlay;
1446 struct intel_overlay_error_state *error;
1447 struct overlay_registers __iomem *regs;
1448
1449 if (!overlay || !overlay->active)
1450 return NULL;
1451
1452 error = kmalloc(sizeof(*error), GFP_ATOMIC);
1453 if (error == NULL)
1454 return NULL;
1455
1456 error->dovsta = I915_READ(DOVSTA);
1457 error->isr = I915_READ(ISR);
Chris Wilson315781482010-08-12 09:42:51 +01001458 if (OVERLAY_NEEDS_PHYSICAL(overlay->dev))
Ben Widawsky75020bc2012-04-16 14:07:43 -07001459 error->base = (__force long)overlay->reg_bo->phys_obj->handle->vaddr;
Chris Wilson315781482010-08-12 09:42:51 +01001460 else
Ben Widawsky75020bc2012-04-16 14:07:43 -07001461 error->base = overlay->reg_bo->gtt_offset;
Chris Wilson6ef3d422010-08-04 20:26:07 +01001462
1463 regs = intel_overlay_map_regs_atomic(overlay);
1464 if (!regs)
1465 goto err;
1466
1467 memcpy_fromio(&error->regs, regs, sizeof(struct overlay_registers));
Linus Torvaldsc48c43e2010-10-26 18:57:59 -07001468 intel_overlay_unmap_regs_atomic(overlay, regs);
Chris Wilson6ef3d422010-08-04 20:26:07 +01001469
1470 return error;
1471
1472err:
1473 kfree(error);
1474 return NULL;
1475}
1476
1477void
1478intel_overlay_print_error_state(struct seq_file *m, struct intel_overlay_error_state *error)
1479{
1480 seq_printf(m, "Overlay, status: 0x%08x, interrupt: 0x%08x\n",
1481 error->dovsta, error->isr);
1482 seq_printf(m, " Register file at 0x%08lx:\n",
1483 error->base);
1484
1485#define P(x) seq_printf(m, " " #x ": 0x%08x\n", error->regs.x)
1486 P(OBUF_0Y);
1487 P(OBUF_1Y);
1488 P(OBUF_0U);
1489 P(OBUF_0V);
1490 P(OBUF_1U);
1491 P(OBUF_1V);
1492 P(OSTRIDE);
1493 P(YRGB_VPH);
1494 P(UV_VPH);
1495 P(HORZ_PH);
1496 P(INIT_PHS);
1497 P(DWINPOS);
1498 P(DWINSZ);
1499 P(SWIDTH);
1500 P(SWIDTHSW);
1501 P(SHEIGHT);
1502 P(YRGBSCALE);
1503 P(UVSCALE);
1504 P(OCLRC0);
1505 P(OCLRC1);
1506 P(DCLRKV);
1507 P(DCLRKM);
1508 P(SCLRKVH);
1509 P(SCLRKVL);
1510 P(SCLRKEN);
1511 P(OCONFIG);
1512 P(OCMD);
1513 P(OSTART_0Y);
1514 P(OSTART_1Y);
1515 P(OSTART_0U);
1516 P(OSTART_0V);
1517 P(OSTART_1U);
1518 P(OSTART_1V);
1519 P(OTILEOFF_0Y);
1520 P(OTILEOFF_1Y);
1521 P(OTILEOFF_0U);
1522 P(OTILEOFF_0V);
1523 P(OTILEOFF_1U);
1524 P(OTILEOFF_1V);
1525 P(FASTHSCALE);
1526 P(UVSCALEV);
1527#undef P
1528}
Chris Wilson3bd3c932010-08-19 08:19:30 +01001529#endif