blob: 484058216e9d10635302aef4cd1c6ec4b18a6e55 [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 */
Andrew Mortone1679762010-08-24 16:35:52 -070028
29#include <linux/seq_file.h>
Daniel Vetter02e792f2009-09-15 22:57:34 +020030#include "drmP.h"
31#include "drm.h"
32#include "i915_drm.h"
33#include "i915_drv.h"
34#include "i915_reg.h"
35#include "intel_drv.h"
36
37/* Limits for overlay size. According to intel doc, the real limits are:
38 * Y width: 4095, UV width (planar): 2047, Y height: 2047,
39 * UV width (planar): * 1023. But the xorg thinks 2048 for height and width. Use
40 * the mininum of both. */
41#define IMAGE_MAX_WIDTH 2048
42#define IMAGE_MAX_HEIGHT 2046 /* 2 * 1023 */
43/* on 830 and 845 these large limits result in the card hanging */
44#define IMAGE_MAX_WIDTH_LEGACY 1024
45#define IMAGE_MAX_HEIGHT_LEGACY 1088
46
47/* overlay register definitions */
48/* OCMD register */
49#define OCMD_TILED_SURFACE (0x1<<19)
50#define OCMD_MIRROR_MASK (0x3<<17)
51#define OCMD_MIRROR_MODE (0x3<<17)
52#define OCMD_MIRROR_HORIZONTAL (0x1<<17)
53#define OCMD_MIRROR_VERTICAL (0x2<<17)
54#define OCMD_MIRROR_BOTH (0x3<<17)
55#define OCMD_BYTEORDER_MASK (0x3<<14) /* zero for YUYV or FOURCC YUY2 */
56#define OCMD_UV_SWAP (0x1<<14) /* YVYU */
57#define OCMD_Y_SWAP (0x2<<14) /* UYVY or FOURCC UYVY */
58#define OCMD_Y_AND_UV_SWAP (0x3<<14) /* VYUY */
59#define OCMD_SOURCE_FORMAT_MASK (0xf<<10)
60#define OCMD_RGB_888 (0x1<<10) /* not in i965 Intel docs */
61#define OCMD_RGB_555 (0x2<<10) /* not in i965 Intel docs */
62#define OCMD_RGB_565 (0x3<<10) /* not in i965 Intel docs */
63#define OCMD_YUV_422_PACKED (0x8<<10)
64#define OCMD_YUV_411_PACKED (0x9<<10) /* not in i965 Intel docs */
65#define OCMD_YUV_420_PLANAR (0xc<<10)
66#define OCMD_YUV_422_PLANAR (0xd<<10)
67#define OCMD_YUV_410_PLANAR (0xe<<10) /* also 411 */
68#define OCMD_TVSYNCFLIP_PARITY (0x1<<9)
69#define OCMD_TVSYNCFLIP_ENABLE (0x1<<7)
Chris Wilsond7961362010-07-13 13:52:17 +010070#define OCMD_BUF_TYPE_MASK (0x1<<5)
Daniel Vetter02e792f2009-09-15 22:57:34 +020071#define OCMD_BUF_TYPE_FRAME (0x0<<5)
72#define OCMD_BUF_TYPE_FIELD (0x1<<5)
73#define OCMD_TEST_MODE (0x1<<4)
74#define OCMD_BUFFER_SELECT (0x3<<2)
75#define OCMD_BUFFER0 (0x0<<2)
76#define OCMD_BUFFER1 (0x1<<2)
77#define OCMD_FIELD_SELECT (0x1<<2)
78#define OCMD_FIELD0 (0x0<<1)
79#define OCMD_FIELD1 (0x1<<1)
80#define OCMD_ENABLE (0x1<<0)
81
82/* OCONFIG register */
83#define OCONF_PIPE_MASK (0x1<<18)
84#define OCONF_PIPE_A (0x0<<18)
85#define OCONF_PIPE_B (0x1<<18)
86#define OCONF_GAMMA2_ENABLE (0x1<<16)
87#define OCONF_CSC_MODE_BT601 (0x0<<5)
88#define OCONF_CSC_MODE_BT709 (0x1<<5)
89#define OCONF_CSC_BYPASS (0x1<<4)
90#define OCONF_CC_OUT_8BIT (0x1<<3)
91#define OCONF_TEST_MODE (0x1<<2)
92#define OCONF_THREE_LINE_BUFFER (0x1<<0)
93#define OCONF_TWO_LINE_BUFFER (0x0<<0)
94
95/* DCLRKM (dst-key) register */
96#define DST_KEY_ENABLE (0x1<<31)
97#define CLK_RGB24_MASK 0x0
98#define CLK_RGB16_MASK 0x070307
99#define CLK_RGB15_MASK 0x070707
100#define CLK_RGB8I_MASK 0xffffff
101
102#define RGB16_TO_COLORKEY(c) \
103 (((c & 0xF800) << 8) | ((c & 0x07E0) << 5) | ((c & 0x001F) << 3))
104#define RGB15_TO_COLORKEY(c) \
105 (((c & 0x7c00) << 9) | ((c & 0x03E0) << 6) | ((c & 0x001F) << 3))
106
107/* overlay flip addr flag */
108#define OFC_UPDATE 0x1
109
110/* polyphase filter coefficients */
111#define N_HORIZ_Y_TAPS 5
112#define N_VERT_Y_TAPS 3
113#define N_HORIZ_UV_TAPS 3
114#define N_VERT_UV_TAPS 3
115#define N_PHASES 17
116#define MAX_TAPS 5
117
118/* memory bufferd overlay registers */
119struct overlay_registers {
120 u32 OBUF_0Y;
121 u32 OBUF_1Y;
122 u32 OBUF_0U;
123 u32 OBUF_0V;
124 u32 OBUF_1U;
125 u32 OBUF_1V;
126 u32 OSTRIDE;
127 u32 YRGB_VPH;
128 u32 UV_VPH;
129 u32 HORZ_PH;
130 u32 INIT_PHS;
131 u32 DWINPOS;
132 u32 DWINSZ;
133 u32 SWIDTH;
134 u32 SWIDTHSW;
135 u32 SHEIGHT;
136 u32 YRGBSCALE;
137 u32 UVSCALE;
138 u32 OCLRC0;
139 u32 OCLRC1;
140 u32 DCLRKV;
141 u32 DCLRKM;
142 u32 SCLRKVH;
143 u32 SCLRKVL;
144 u32 SCLRKEN;
145 u32 OCONFIG;
146 u32 OCMD;
147 u32 RESERVED1; /* 0x6C */
148 u32 OSTART_0Y;
149 u32 OSTART_1Y;
150 u32 OSTART_0U;
151 u32 OSTART_0V;
152 u32 OSTART_1U;
153 u32 OSTART_1V;
154 u32 OTILEOFF_0Y;
155 u32 OTILEOFF_1Y;
156 u32 OTILEOFF_0U;
157 u32 OTILEOFF_0V;
158 u32 OTILEOFF_1U;
159 u32 OTILEOFF_1V;
160 u32 FASTHSCALE; /* 0xA0 */
161 u32 UVSCALEV; /* 0xA4 */
162 u32 RESERVEDC[(0x200 - 0xA8) / 4]; /* 0xA8 - 0x1FC */
163 u16 Y_VCOEFS[N_VERT_Y_TAPS * N_PHASES]; /* 0x200 */
164 u16 RESERVEDD[0x100 / 2 - N_VERT_Y_TAPS * N_PHASES];
165 u16 Y_HCOEFS[N_HORIZ_Y_TAPS * N_PHASES]; /* 0x300 */
166 u16 RESERVEDE[0x200 / 2 - N_HORIZ_Y_TAPS * N_PHASES];
167 u16 UV_VCOEFS[N_VERT_UV_TAPS * N_PHASES]; /* 0x500 */
168 u16 RESERVEDF[0x100 / 2 - N_VERT_UV_TAPS * N_PHASES];
169 u16 UV_HCOEFS[N_HORIZ_UV_TAPS * N_PHASES]; /* 0x600 */
170 u16 RESERVEDG[0x100 / 2 - N_HORIZ_UV_TAPS * N_PHASES];
171};
172
Chris Wilson8d74f652010-08-12 10:35:26 +0100173static struct overlay_registers *
174intel_overlay_map_regs_atomic(struct intel_overlay *overlay,
175 int slot)
Daniel Vetter02e792f2009-09-15 22:57:34 +0200176{
177 drm_i915_private_t *dev_priv = overlay->dev->dev_private;
178 struct overlay_registers *regs;
179
180 /* no recursive mappings */
181 BUG_ON(overlay->virt_addr);
182
Chris Wilson315781482010-08-12 09:42:51 +0100183 if (OVERLAY_NEEDS_PHYSICAL(overlay->dev)) {
184 regs = overlay->reg_bo->phys_obj->handle->vaddr;
185 } else {
Daniel Vetter02e792f2009-09-15 22:57:34 +0200186 regs = io_mapping_map_atomic_wc(dev_priv->mm.gtt_mapping,
Chris Wilsonfca3ec02010-08-04 14:34:24 +0100187 overlay->reg_bo->gtt_offset,
Chris Wilson8d74f652010-08-12 10:35:26 +0100188 slot);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200189
190 if (!regs) {
191 DRM_ERROR("failed to map overlay regs in GTT\n");
192 return NULL;
193 }
Chris Wilson315781482010-08-12 09:42:51 +0100194 }
Daniel Vetter02e792f2009-09-15 22:57:34 +0200195
196 return overlay->virt_addr = regs;
197}
198
Chris Wilson8d74f652010-08-12 10:35:26 +0100199static void intel_overlay_unmap_regs_atomic(struct intel_overlay *overlay,
200 int slot)
Daniel Vetter02e792f2009-09-15 22:57:34 +0200201{
Chris Wilson315781482010-08-12 09:42:51 +0100202 if (!OVERLAY_NEEDS_PHYSICAL(overlay->dev))
Chris Wilson8d74f652010-08-12 10:35:26 +0100203 io_mapping_unmap_atomic(overlay->virt_addr, slot);
204
205 overlay->virt_addr = NULL;
206
207 return;
208}
209
210static struct overlay_registers *
211intel_overlay_map_regs(struct intel_overlay *overlay)
212{
213 drm_i915_private_t *dev_priv = overlay->dev->dev_private;
214 struct overlay_registers *regs;
215
216 /* no recursive mappings */
217 BUG_ON(overlay->virt_addr);
218
219 if (OVERLAY_NEEDS_PHYSICAL(overlay->dev)) {
220 regs = overlay->reg_bo->phys_obj->handle->vaddr;
221 } else {
222 regs = io_mapping_map_wc(dev_priv->mm.gtt_mapping,
223 overlay->reg_bo->gtt_offset);
224
225 if (!regs) {
226 DRM_ERROR("failed to map overlay regs in GTT\n");
227 return NULL;
228 }
229 }
230
231 return overlay->virt_addr = regs;
232}
233
234static void intel_overlay_unmap_regs(struct intel_overlay *overlay)
235{
236 if (!OVERLAY_NEEDS_PHYSICAL(overlay->dev))
237 io_mapping_unmap(overlay->virt_addr);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200238
239 overlay->virt_addr = NULL;
240
Daniel Vetter02e792f2009-09-15 22:57:34 +0200241 return;
242}
243
244/* overlay needs to be disable in OCMD reg */
245static int intel_overlay_on(struct intel_overlay *overlay)
246{
247 struct drm_device *dev = overlay->dev;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200248 int ret;
Zou Nan hai852835f2010-05-21 09:08:56 +0800249 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200250
251 BUG_ON(overlay->active);
252
Daniel Vetter03f77ea2009-09-15 22:57:37 +0200253 overlay->active = 1;
254 overlay->hw_wedged = NEEDS_WAIT_FOR_FLIP;
255
Daniel Vetter4f8a5672010-02-11 14:14:43 +0100256 BEGIN_LP_RING(4);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200257 OUT_RING(MI_OVERLAY_FLIP | MI_OVERLAY_ON);
258 OUT_RING(overlay->flip_addr | OFC_UPDATE);
259 OUT_RING(MI_WAIT_FOR_EVENT | MI_WAIT_FOR_OVERLAY_FLIP);
260 OUT_RING(MI_NOOP);
261 ADVANCE_LP_RING();
262
Zou Nan hai852835f2010-05-21 09:08:56 +0800263 overlay->last_flip_req =
Daniel Vetter8a1a49f2010-02-11 22:29:04 +0100264 i915_add_request(dev, NULL, &dev_priv->render_ring);
Daniel Vetter03f77ea2009-09-15 22:57:37 +0200265 if (overlay->last_flip_req == 0)
266 return -ENOMEM;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200267
Zou Nan hai852835f2010-05-21 09:08:56 +0800268 ret = i915_do_wait_request(dev,
Chris Wilson722506f2010-08-12 09:28:50 +0100269 overlay->last_flip_req, true,
270 &dev_priv->render_ring);
Daniel Vetter03f77ea2009-09-15 22:57:37 +0200271 if (ret != 0)
272 return ret;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200273
Daniel Vetter03f77ea2009-09-15 22:57:37 +0200274 overlay->hw_wedged = 0;
275 overlay->last_flip_req = 0;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200276 return 0;
277}
278
279/* overlay needs to be enabled in OCMD reg */
280static void intel_overlay_continue(struct intel_overlay *overlay,
Chris Wilson722506f2010-08-12 09:28:50 +0100281 bool load_polyphase_filter)
Daniel Vetter02e792f2009-09-15 22:57:34 +0200282{
283 struct drm_device *dev = overlay->dev;
284 drm_i915_private_t *dev_priv = dev->dev_private;
285 u32 flip_addr = overlay->flip_addr;
286 u32 tmp;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200287
288 BUG_ON(!overlay->active);
289
290 if (load_polyphase_filter)
291 flip_addr |= OFC_UPDATE;
292
293 /* check for underruns */
294 tmp = I915_READ(DOVSTA);
295 if (tmp & (1 << 17))
296 DRM_DEBUG("overlay underrun, DOVSTA: %x\n", tmp);
297
Daniel Vetter4f8a5672010-02-11 14:14:43 +0100298 BEGIN_LP_RING(2);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200299 OUT_RING(MI_OVERLAY_FLIP | MI_OVERLAY_CONTINUE);
300 OUT_RING(flip_addr);
Daniel Vetter5a5a0c62009-09-15 22:57:36 +0200301 ADVANCE_LP_RING();
302
Zou Nan hai852835f2010-05-21 09:08:56 +0800303 overlay->last_flip_req =
Daniel Vetter8a1a49f2010-02-11 22:29:04 +0100304 i915_add_request(dev, NULL, &dev_priv->render_ring);
Daniel Vetter5a5a0c62009-09-15 22:57:36 +0200305}
306
307static int intel_overlay_wait_flip(struct intel_overlay *overlay)
308{
309 struct drm_device *dev = overlay->dev;
Chris Wilson722506f2010-08-12 09:28:50 +0100310 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter5a5a0c62009-09-15 22:57:36 +0200311 int ret;
312 u32 tmp;
Daniel Vetter5a5a0c62009-09-15 22:57:36 +0200313
314 if (overlay->last_flip_req != 0) {
Chris Wilson722506f2010-08-12 09:28:50 +0100315 ret = i915_do_wait_request(dev,
316 overlay->last_flip_req, true,
317 &dev_priv->render_ring);
Daniel Vetter5c5a4352009-10-04 15:00:36 +0200318 if (ret == 0) {
319 overlay->last_flip_req = 0;
Daniel Vetter5a5a0c62009-09-15 22:57:36 +0200320
Daniel Vetter5c5a4352009-10-04 15:00:36 +0200321 tmp = I915_READ(ISR);
Daniel Vetter5a5a0c62009-09-15 22:57:36 +0200322
Daniel Vetter5c5a4352009-10-04 15:00:36 +0200323 if (!(tmp & I915_OVERLAY_PLANE_FLIP_PENDING_INTERRUPT))
324 return 0;
325 }
Daniel Vetter5a5a0c62009-09-15 22:57:36 +0200326 }
327
328 /* synchronous slowpath */
Daniel Vetter03f77ea2009-09-15 22:57:37 +0200329 overlay->hw_wedged = RELEASE_OLD_VID;
330
Daniel Vetter5a5a0c62009-09-15 22:57:36 +0200331 BEGIN_LP_RING(2);
Chris Wilson722506f2010-08-12 09:28:50 +0100332 OUT_RING(MI_WAIT_FOR_EVENT | MI_WAIT_FOR_OVERLAY_FLIP);
333 OUT_RING(MI_NOOP);
334 ADVANCE_LP_RING();
Daniel Vetter02e792f2009-09-15 22:57:34 +0200335
Zou Nan hai852835f2010-05-21 09:08:56 +0800336 overlay->last_flip_req =
Daniel Vetter8a1a49f2010-02-11 22:29:04 +0100337 i915_add_request(dev, NULL, &dev_priv->render_ring);
Daniel Vetter03f77ea2009-09-15 22:57:37 +0200338 if (overlay->last_flip_req == 0)
339 return -ENOMEM;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200340
Chris Wilson722506f2010-08-12 09:28:50 +0100341 ret = i915_do_wait_request(dev,
342 overlay->last_flip_req, true,
343 &dev_priv->render_ring);
Daniel Vetter03f77ea2009-09-15 22:57:37 +0200344 if (ret != 0)
345 return ret;
346
347 overlay->hw_wedged = 0;
348 overlay->last_flip_req = 0;
349 return 0;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200350}
351
352/* overlay needs to be disabled in OCMD reg */
353static int intel_overlay_off(struct intel_overlay *overlay)
354{
355 u32 flip_addr = overlay->flip_addr;
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 Vetter02e792f2009-09-15 22:57:34 +0200358 int ret;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200359
360 BUG_ON(!overlay->active);
361
362 /* According to intel docs the overlay hw may hang (when switching
363 * off) without loading the filter coeffs. It is however unclear whether
364 * this applies to the disabling of the overlay or to the switching off
365 * of the hw. Do it in both cases */
366 flip_addr |= OFC_UPDATE;
367
368 /* wait for overlay to go idle */
Daniel Vetter03f77ea2009-09-15 22:57:37 +0200369 overlay->hw_wedged = SWITCH_OFF_STAGE_1;
370
Daniel Vetter4f8a5672010-02-11 14:14:43 +0100371 BEGIN_LP_RING(4);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200372 OUT_RING(MI_OVERLAY_FLIP | MI_OVERLAY_CONTINUE);
373 OUT_RING(flip_addr);
Chris Wilson722506f2010-08-12 09:28:50 +0100374 OUT_RING(MI_WAIT_FOR_EVENT | MI_WAIT_FOR_OVERLAY_FLIP);
375 OUT_RING(MI_NOOP);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200376 ADVANCE_LP_RING();
377
Zou Nan hai852835f2010-05-21 09:08:56 +0800378 overlay->last_flip_req =
Daniel Vetter8a1a49f2010-02-11 22:29:04 +0100379 i915_add_request(dev, NULL, &dev_priv->render_ring);
Daniel Vetter03f77ea2009-09-15 22:57:37 +0200380 if (overlay->last_flip_req == 0)
381 return -ENOMEM;
382
Chris Wilson722506f2010-08-12 09:28:50 +0100383 ret = i915_do_wait_request(dev,
384 overlay->last_flip_req, true,
385 &dev_priv->render_ring);
386 if (ret != 0)
387 return ret;
388
389 /* turn overlay off */
390 overlay->hw_wedged = SWITCH_OFF_STAGE_2;
391
392 BEGIN_LP_RING(4);
393 OUT_RING(MI_OVERLAY_FLIP | MI_OVERLAY_OFF);
394 OUT_RING(flip_addr);
395 OUT_RING(MI_WAIT_FOR_EVENT | MI_WAIT_FOR_OVERLAY_FLIP);
396 OUT_RING(MI_NOOP);
397 ADVANCE_LP_RING();
398
399 overlay->last_flip_req =
400 i915_add_request(dev, NULL, &dev_priv->render_ring);
401 if (overlay->last_flip_req == 0)
402 return -ENOMEM;
403
404 ret = i915_do_wait_request(dev,
405 overlay->last_flip_req, true,
406 &dev_priv->render_ring);
Daniel Vetter03f77ea2009-09-15 22:57:37 +0200407 if (ret != 0)
Daniel Vetter02e792f2009-09-15 22:57:34 +0200408 return ret;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200409
Daniel Vetter03f77ea2009-09-15 22:57:37 +0200410 overlay->hw_wedged = 0;
411 overlay->last_flip_req = 0;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200412 return ret;
413}
414
Daniel Vetter12ca45f2037-04-25 10:08:26 +0200415static void intel_overlay_off_tail(struct intel_overlay *overlay)
416{
417 struct drm_gem_object *obj;
418
419 /* never have the overlay hw on without showing a frame */
420 BUG_ON(!overlay->vid_bo);
Daniel Vettera8089e82010-04-09 19:05:09 +0000421 obj = &overlay->vid_bo->base;
Daniel Vetter12ca45f2037-04-25 10:08:26 +0200422
423 i915_gem_object_unpin(obj);
424 drm_gem_object_unreference(obj);
425 overlay->vid_bo = NULL;
426
427 overlay->crtc->overlay = NULL;
428 overlay->crtc = NULL;
429 overlay->active = 0;
430}
431
Daniel Vetter03f77ea2009-09-15 22:57:37 +0200432/* recover from an interruption due to a signal
433 * We have to be careful not to repeat work forever an make forward progess. */
434int intel_overlay_recover_from_interrupt(struct intel_overlay *overlay,
Chris Wilson722506f2010-08-12 09:28:50 +0100435 bool interruptible)
Daniel Vetter03f77ea2009-09-15 22:57:37 +0200436{
437 struct drm_device *dev = overlay->dev;
Daniel Vetter03f77ea2009-09-15 22:57:37 +0200438 struct drm_gem_object *obj;
Zou Nan hai852835f2010-05-21 09:08:56 +0800439 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter03f77ea2009-09-15 22:57:37 +0200440 u32 flip_addr;
441 int ret;
Daniel Vetter03f77ea2009-09-15 22:57:37 +0200442
443 if (overlay->hw_wedged == HW_WEDGED)
444 return -EIO;
445
446 if (overlay->last_flip_req == 0) {
Zou Nan hai852835f2010-05-21 09:08:56 +0800447 overlay->last_flip_req =
Daniel Vetter8a1a49f2010-02-11 22:29:04 +0100448 i915_add_request(dev, NULL, &dev_priv->render_ring);
Daniel Vetter03f77ea2009-09-15 22:57:37 +0200449 if (overlay->last_flip_req == 0)
450 return -ENOMEM;
451 }
452
Zou Nan hai852835f2010-05-21 09:08:56 +0800453 ret = i915_do_wait_request(dev, overlay->last_flip_req,
Chris Wilson722506f2010-08-12 09:28:50 +0100454 interruptible, &dev_priv->render_ring);
Daniel Vetter03f77ea2009-09-15 22:57:37 +0200455 if (ret != 0)
456 return ret;
457
458 switch (overlay->hw_wedged) {
Chris Wilson722506f2010-08-12 09:28:50 +0100459 case RELEASE_OLD_VID:
460 obj = &overlay->old_vid_bo->base;
461 i915_gem_object_unpin(obj);
462 drm_gem_object_unreference(obj);
463 overlay->old_vid_bo = NULL;
464 break;
465 case SWITCH_OFF_STAGE_1:
466 flip_addr = overlay->flip_addr;
467 flip_addr |= OFC_UPDATE;
Daniel Vetter03f77ea2009-09-15 22:57:37 +0200468
Chris Wilson722506f2010-08-12 09:28:50 +0100469 overlay->hw_wedged = SWITCH_OFF_STAGE_2;
Daniel Vetter03f77ea2009-09-15 22:57:37 +0200470
Chris Wilson722506f2010-08-12 09:28:50 +0100471 BEGIN_LP_RING(4);
472 OUT_RING(MI_OVERLAY_FLIP | MI_OVERLAY_OFF);
473 OUT_RING(flip_addr);
474 OUT_RING(MI_WAIT_FOR_EVENT | MI_WAIT_FOR_OVERLAY_FLIP);
475 OUT_RING(MI_NOOP);
476 ADVANCE_LP_RING();
Daniel Vetter03f77ea2009-09-15 22:57:37 +0200477
Chris Wilson722506f2010-08-12 09:28:50 +0100478 overlay->last_flip_req =
479 i915_add_request(dev, NULL,
480 &dev_priv->render_ring);
481 if (overlay->last_flip_req == 0)
482 return -ENOMEM;
Daniel Vetter03f77ea2009-09-15 22:57:37 +0200483
Chris Wilson722506f2010-08-12 09:28:50 +0100484 ret = i915_do_wait_request(dev, overlay->last_flip_req,
485 interruptible,
486 &dev_priv->render_ring);
487 if (ret != 0)
488 return ret;
Daniel Vetter03f77ea2009-09-15 22:57:37 +0200489
Chris Wilson722506f2010-08-12 09:28:50 +0100490 case SWITCH_OFF_STAGE_2:
491 intel_overlay_off_tail(overlay);
492 break;
493 default:
494 BUG_ON(overlay->hw_wedged != NEEDS_WAIT_FOR_FLIP);
Daniel Vetter03f77ea2009-09-15 22:57:37 +0200495 }
496
497 overlay->hw_wedged = 0;
498 overlay->last_flip_req = 0;
499 return 0;
500}
501
Daniel Vetter5a5a0c62009-09-15 22:57:36 +0200502/* Wait for pending overlay flip and release old frame.
503 * Needs to be called before the overlay register are changed
Chris Wilson8d74f652010-08-12 10:35:26 +0100504 * via intel_overlay_(un)map_regs
505 */
Daniel Vetter02e792f2009-09-15 22:57:34 +0200506static int intel_overlay_release_old_vid(struct intel_overlay *overlay)
507{
508 int ret;
509 struct drm_gem_object *obj;
510
Daniel Vetter03f77ea2009-09-15 22:57:37 +0200511 /* only wait if there is actually an old frame to release to
512 * guarantee forward progress */
513 if (!overlay->old_vid_bo)
514 return 0;
515
Daniel Vetter02e792f2009-09-15 22:57:34 +0200516 ret = intel_overlay_wait_flip(overlay);
517 if (ret != 0)
518 return ret;
519
Daniel Vettera8089e82010-04-09 19:05:09 +0000520 obj = &overlay->old_vid_bo->base;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200521 i915_gem_object_unpin(obj);
522 drm_gem_object_unreference(obj);
523 overlay->old_vid_bo = NULL;
524
525 return 0;
526}
527
528struct put_image_params {
529 int format;
530 short dst_x;
531 short dst_y;
532 short dst_w;
533 short dst_h;
534 short src_w;
535 short src_scan_h;
536 short src_scan_w;
537 short src_h;
538 short stride_Y;
539 short stride_UV;
540 int offset_Y;
541 int offset_U;
542 int offset_V;
543};
544
545static int packed_depth_bytes(u32 format)
546{
547 switch (format & I915_OVERLAY_DEPTH_MASK) {
Chris Wilson722506f2010-08-12 09:28:50 +0100548 case I915_OVERLAY_YUV422:
549 return 4;
550 case I915_OVERLAY_YUV411:
551 /* return 6; not implemented */
552 default:
553 return -EINVAL;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200554 }
555}
556
557static int packed_width_bytes(u32 format, short width)
558{
559 switch (format & I915_OVERLAY_DEPTH_MASK) {
Chris Wilson722506f2010-08-12 09:28:50 +0100560 case I915_OVERLAY_YUV422:
561 return width << 1;
562 default:
563 return -EINVAL;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200564 }
565}
566
567static int uv_hsubsampling(u32 format)
568{
569 switch (format & I915_OVERLAY_DEPTH_MASK) {
Chris Wilson722506f2010-08-12 09:28:50 +0100570 case I915_OVERLAY_YUV422:
571 case I915_OVERLAY_YUV420:
572 return 2;
573 case I915_OVERLAY_YUV411:
574 case I915_OVERLAY_YUV410:
575 return 4;
576 default:
577 return -EINVAL;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200578 }
579}
580
581static int uv_vsubsampling(u32 format)
582{
583 switch (format & I915_OVERLAY_DEPTH_MASK) {
Chris Wilson722506f2010-08-12 09:28:50 +0100584 case I915_OVERLAY_YUV420:
585 case I915_OVERLAY_YUV410:
586 return 2;
587 case I915_OVERLAY_YUV422:
588 case I915_OVERLAY_YUV411:
589 return 1;
590 default:
591 return -EINVAL;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200592 }
593}
594
595static u32 calc_swidthsw(struct drm_device *dev, u32 offset, u32 width)
596{
597 u32 mask, shift, ret;
598 if (IS_I9XX(dev)) {
599 mask = 0x3f;
600 shift = 6;
601 } else {
602 mask = 0x1f;
603 shift = 5;
604 }
605 ret = ((offset + width + mask) >> shift) - (offset >> shift);
606 if (IS_I9XX(dev))
607 ret <<= 1;
608 ret -=1;
609 return ret << 2;
610}
611
612static const u16 y_static_hcoeffs[N_HORIZ_Y_TAPS * N_PHASES] = {
613 0x3000, 0xb4a0, 0x1930, 0x1920, 0xb4a0,
614 0x3000, 0xb500, 0x19d0, 0x1880, 0xb440,
615 0x3000, 0xb540, 0x1a88, 0x2f80, 0xb3e0,
616 0x3000, 0xb580, 0x1b30, 0x2e20, 0xb380,
617 0x3000, 0xb5c0, 0x1bd8, 0x2cc0, 0xb320,
618 0x3020, 0xb5e0, 0x1c60, 0x2b80, 0xb2c0,
619 0x3020, 0xb5e0, 0x1cf8, 0x2a20, 0xb260,
620 0x3020, 0xb5e0, 0x1d80, 0x28e0, 0xb200,
621 0x3020, 0xb5c0, 0x1e08, 0x3f40, 0xb1c0,
622 0x3020, 0xb580, 0x1e78, 0x3ce0, 0xb160,
623 0x3040, 0xb520, 0x1ed8, 0x3aa0, 0xb120,
624 0x3040, 0xb4a0, 0x1f30, 0x3880, 0xb0e0,
625 0x3040, 0xb400, 0x1f78, 0x3680, 0xb0a0,
626 0x3020, 0xb340, 0x1fb8, 0x34a0, 0xb060,
627 0x3020, 0xb240, 0x1fe0, 0x32e0, 0xb040,
628 0x3020, 0xb140, 0x1ff8, 0x3160, 0xb020,
Chris Wilson722506f2010-08-12 09:28:50 +0100629 0xb000, 0x3000, 0x0800, 0x3000, 0xb000
630};
631
Daniel Vetter02e792f2009-09-15 22:57:34 +0200632static const u16 uv_static_hcoeffs[N_HORIZ_UV_TAPS * N_PHASES] = {
633 0x3000, 0x1800, 0x1800, 0xb000, 0x18d0, 0x2e60,
634 0xb000, 0x1990, 0x2ce0, 0xb020, 0x1a68, 0x2b40,
635 0xb040, 0x1b20, 0x29e0, 0xb060, 0x1bd8, 0x2880,
636 0xb080, 0x1c88, 0x3e60, 0xb0a0, 0x1d28, 0x3c00,
637 0xb0c0, 0x1db8, 0x39e0, 0xb0e0, 0x1e40, 0x37e0,
638 0xb100, 0x1eb8, 0x3620, 0xb100, 0x1f18, 0x34a0,
639 0xb100, 0x1f68, 0x3360, 0xb0e0, 0x1fa8, 0x3240,
640 0xb0c0, 0x1fe0, 0x3140, 0xb060, 0x1ff0, 0x30a0,
Chris Wilson722506f2010-08-12 09:28:50 +0100641 0x3000, 0x0800, 0x3000
642};
Daniel Vetter02e792f2009-09-15 22:57:34 +0200643
644static void update_polyphase_filter(struct overlay_registers *regs)
645{
646 memcpy(regs->Y_HCOEFS, y_static_hcoeffs, sizeof(y_static_hcoeffs));
647 memcpy(regs->UV_HCOEFS, uv_static_hcoeffs, sizeof(uv_static_hcoeffs));
648}
649
650static bool update_scaling_factors(struct intel_overlay *overlay,
651 struct overlay_registers *regs,
652 struct put_image_params *params)
653{
654 /* fixed point with a 12 bit shift */
655 u32 xscale, yscale, xscale_UV, yscale_UV;
656#define FP_SHIFT 12
657#define FRACT_MASK 0xfff
658 bool scale_changed = false;
659 int uv_hscale = uv_hsubsampling(params->format);
660 int uv_vscale = uv_vsubsampling(params->format);
661
662 if (params->dst_w > 1)
663 xscale = ((params->src_scan_w - 1) << FP_SHIFT)
664 /(params->dst_w);
665 else
666 xscale = 1 << FP_SHIFT;
667
668 if (params->dst_h > 1)
669 yscale = ((params->src_scan_h - 1) << FP_SHIFT)
670 /(params->dst_h);
671 else
672 yscale = 1 << FP_SHIFT;
673
674 /*if (params->format & I915_OVERLAY_YUV_PLANAR) {*/
Chris Wilson722506f2010-08-12 09:28:50 +0100675 xscale_UV = xscale/uv_hscale;
676 yscale_UV = yscale/uv_vscale;
677 /* make the Y scale to UV scale ratio an exact multiply */
678 xscale = xscale_UV * uv_hscale;
679 yscale = yscale_UV * uv_vscale;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200680 /*} else {
Chris Wilson722506f2010-08-12 09:28:50 +0100681 xscale_UV = 0;
682 yscale_UV = 0;
683 }*/
Daniel Vetter02e792f2009-09-15 22:57:34 +0200684
685 if (xscale != overlay->old_xscale || yscale != overlay->old_yscale)
686 scale_changed = true;
687 overlay->old_xscale = xscale;
688 overlay->old_yscale = yscale;
689
Chris Wilson722506f2010-08-12 09:28:50 +0100690 regs->YRGBSCALE = (((yscale & FRACT_MASK) << 20) |
691 ((xscale >> FP_SHIFT) << 16) |
692 ((xscale & FRACT_MASK) << 3));
693
694 regs->UVSCALE = (((yscale_UV & FRACT_MASK) << 20) |
695 ((xscale_UV >> FP_SHIFT) << 16) |
696 ((xscale_UV & FRACT_MASK) << 3));
697
698 regs->UVSCALEV = ((((yscale >> FP_SHIFT) << 16) |
699 ((yscale_UV >> FP_SHIFT) << 0)));
Daniel Vetter02e792f2009-09-15 22:57:34 +0200700
701 if (scale_changed)
702 update_polyphase_filter(regs);
703
704 return scale_changed;
705}
706
707static void update_colorkey(struct intel_overlay *overlay,
708 struct overlay_registers *regs)
709{
710 u32 key = overlay->color_key;
Chris Wilson6ba3ddd2010-08-12 09:30:58 +0100711
Daniel Vetter02e792f2009-09-15 22:57:34 +0200712 switch (overlay->crtc->base.fb->bits_per_pixel) {
Chris Wilson722506f2010-08-12 09:28:50 +0100713 case 8:
714 regs->DCLRKV = 0;
715 regs->DCLRKM = CLK_RGB8I_MASK | DST_KEY_ENABLE;
Chris Wilson6ba3ddd2010-08-12 09:30:58 +0100716 break;
717
Chris Wilson722506f2010-08-12 09:28:50 +0100718 case 16:
719 if (overlay->crtc->base.fb->depth == 15) {
720 regs->DCLRKV = RGB15_TO_COLORKEY(key);
721 regs->DCLRKM = CLK_RGB15_MASK | DST_KEY_ENABLE;
722 } else {
723 regs->DCLRKV = RGB16_TO_COLORKEY(key);
724 regs->DCLRKM = CLK_RGB16_MASK | DST_KEY_ENABLE;
725 }
Chris Wilson6ba3ddd2010-08-12 09:30:58 +0100726 break;
727
Chris Wilson722506f2010-08-12 09:28:50 +0100728 case 24:
729 case 32:
730 regs->DCLRKV = key;
731 regs->DCLRKM = CLK_RGB24_MASK | DST_KEY_ENABLE;
Chris Wilson6ba3ddd2010-08-12 09:30:58 +0100732 break;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200733 }
734}
735
736static u32 overlay_cmd_reg(struct put_image_params *params)
737{
738 u32 cmd = OCMD_ENABLE | OCMD_BUF_TYPE_FRAME | OCMD_BUFFER0;
739
740 if (params->format & I915_OVERLAY_YUV_PLANAR) {
741 switch (params->format & I915_OVERLAY_DEPTH_MASK) {
Chris Wilson722506f2010-08-12 09:28:50 +0100742 case I915_OVERLAY_YUV422:
743 cmd |= OCMD_YUV_422_PLANAR;
744 break;
745 case I915_OVERLAY_YUV420:
746 cmd |= OCMD_YUV_420_PLANAR;
747 break;
748 case I915_OVERLAY_YUV411:
749 case I915_OVERLAY_YUV410:
750 cmd |= OCMD_YUV_410_PLANAR;
751 break;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200752 }
753 } else { /* YUV packed */
754 switch (params->format & I915_OVERLAY_DEPTH_MASK) {
Chris Wilson722506f2010-08-12 09:28:50 +0100755 case I915_OVERLAY_YUV422:
756 cmd |= OCMD_YUV_422_PACKED;
757 break;
758 case I915_OVERLAY_YUV411:
759 cmd |= OCMD_YUV_411_PACKED;
760 break;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200761 }
762
763 switch (params->format & I915_OVERLAY_SWAP_MASK) {
Chris Wilson722506f2010-08-12 09:28:50 +0100764 case I915_OVERLAY_NO_SWAP:
765 break;
766 case I915_OVERLAY_UV_SWAP:
767 cmd |= OCMD_UV_SWAP;
768 break;
769 case I915_OVERLAY_Y_SWAP:
770 cmd |= OCMD_Y_SWAP;
771 break;
772 case I915_OVERLAY_Y_AND_UV_SWAP:
773 cmd |= OCMD_Y_AND_UV_SWAP;
774 break;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200775 }
776 }
777
778 return cmd;
779}
780
781int intel_overlay_do_put_image(struct intel_overlay *overlay,
782 struct drm_gem_object *new_bo,
783 struct put_image_params *params)
784{
785 int ret, tmp_width;
786 struct overlay_registers *regs;
787 bool scale_changed = false;
Daniel Vetter23010e42010-03-08 13:35:02 +0100788 struct drm_i915_gem_object *bo_priv = to_intel_bo(new_bo);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200789 struct drm_device *dev = overlay->dev;
790
791 BUG_ON(!mutex_is_locked(&dev->struct_mutex));
792 BUG_ON(!mutex_is_locked(&dev->mode_config.mutex));
793 BUG_ON(!overlay);
794
Daniel Vetter02e792f2009-09-15 22:57:34 +0200795 ret = intel_overlay_release_old_vid(overlay);
796 if (ret != 0)
797 return ret;
798
799 ret = i915_gem_object_pin(new_bo, PAGE_SIZE);
800 if (ret != 0)
801 return ret;
802
803 ret = i915_gem_object_set_to_gtt_domain(new_bo, 0);
804 if (ret != 0)
805 goto out_unpin;
806
807 if (!overlay->active) {
Chris Wilson8d74f652010-08-12 10:35:26 +0100808 regs = intel_overlay_map_regs(overlay);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200809 if (!regs) {
810 ret = -ENOMEM;
811 goto out_unpin;
812 }
813 regs->OCONFIG = OCONF_CC_OUT_8BIT;
814 if (IS_I965GM(overlay->dev))
815 regs->OCONFIG |= OCONF_CSC_MODE_BT709;
816 regs->OCONFIG |= overlay->crtc->pipe == 0 ?
817 OCONF_PIPE_A : OCONF_PIPE_B;
Chris Wilson8d74f652010-08-12 10:35:26 +0100818 intel_overlay_unmap_regs(overlay);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200819
820 ret = intel_overlay_on(overlay);
821 if (ret != 0)
822 goto out_unpin;
823 }
824
Chris Wilson8d74f652010-08-12 10:35:26 +0100825 regs = intel_overlay_map_regs(overlay);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200826 if (!regs) {
827 ret = -ENOMEM;
828 goto out_unpin;
829 }
830
831 regs->DWINPOS = (params->dst_y << 16) | params->dst_x;
832 regs->DWINSZ = (params->dst_h << 16) | params->dst_w;
833
834 if (params->format & I915_OVERLAY_YUV_PACKED)
835 tmp_width = packed_width_bytes(params->format, params->src_w);
836 else
837 tmp_width = params->src_w;
838
839 regs->SWIDTH = params->src_w;
840 regs->SWIDTHSW = calc_swidthsw(overlay->dev,
Chris Wilson722506f2010-08-12 09:28:50 +0100841 params->offset_Y, tmp_width);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200842 regs->SHEIGHT = params->src_h;
843 regs->OBUF_0Y = bo_priv->gtt_offset + params-> offset_Y;
844 regs->OSTRIDE = params->stride_Y;
845
846 if (params->format & I915_OVERLAY_YUV_PLANAR) {
847 int uv_hscale = uv_hsubsampling(params->format);
848 int uv_vscale = uv_vsubsampling(params->format);
849 u32 tmp_U, tmp_V;
850 regs->SWIDTH |= (params->src_w/uv_hscale) << 16;
851 tmp_U = calc_swidthsw(overlay->dev, params->offset_U,
Chris Wilson722506f2010-08-12 09:28:50 +0100852 params->src_w/uv_hscale);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200853 tmp_V = calc_swidthsw(overlay->dev, params->offset_V,
Chris Wilson722506f2010-08-12 09:28:50 +0100854 params->src_w/uv_hscale);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200855 regs->SWIDTHSW |= max_t(u32, tmp_U, tmp_V) << 16;
856 regs->SHEIGHT |= (params->src_h/uv_vscale) << 16;
857 regs->OBUF_0U = bo_priv->gtt_offset + params->offset_U;
858 regs->OBUF_0V = bo_priv->gtt_offset + params->offset_V;
859 regs->OSTRIDE |= params->stride_UV << 16;
860 }
861
862 scale_changed = update_scaling_factors(overlay, regs, params);
863
864 update_colorkey(overlay, regs);
865
866 regs->OCMD = overlay_cmd_reg(params);
867
Chris Wilson8d74f652010-08-12 10:35:26 +0100868 intel_overlay_unmap_regs(overlay);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200869
870 intel_overlay_continue(overlay, scale_changed);
871
872 overlay->old_vid_bo = overlay->vid_bo;
Daniel Vetter23010e42010-03-08 13:35:02 +0100873 overlay->vid_bo = to_intel_bo(new_bo);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200874
875 return 0;
876
877out_unpin:
878 i915_gem_object_unpin(new_bo);
879 return ret;
880}
881
882int intel_overlay_switch_off(struct intel_overlay *overlay)
883{
884 int ret;
885 struct overlay_registers *regs;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200886 struct drm_device *dev = overlay->dev;
887
888 BUG_ON(!mutex_is_locked(&dev->struct_mutex));
889 BUG_ON(!mutex_is_locked(&dev->mode_config.mutex));
890
Daniel Vetter9bedb972009-11-30 15:55:49 +0100891 if (overlay->hw_wedged) {
892 ret = intel_overlay_recover_from_interrupt(overlay, 1);
893 if (ret != 0)
894 return ret;
895 }
896
Daniel Vetter02e792f2009-09-15 22:57:34 +0200897 if (!overlay->active)
898 return 0;
899
Daniel Vetter02e792f2009-09-15 22:57:34 +0200900 ret = intel_overlay_release_old_vid(overlay);
901 if (ret != 0)
902 return ret;
903
Chris Wilson8d74f652010-08-12 10:35:26 +0100904 regs = intel_overlay_map_regs(overlay);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200905 regs->OCMD = 0;
Chris Wilson8d74f652010-08-12 10:35:26 +0100906 intel_overlay_unmap_regs(overlay);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200907
908 ret = intel_overlay_off(overlay);
Daniel Vetter03f77ea2009-09-15 22:57:37 +0200909 if (ret != 0)
910 return ret;
911
Daniel Vetter12ca45f2037-04-25 10:08:26 +0200912 intel_overlay_off_tail(overlay);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200913
914 return 0;
915}
916
917static int check_overlay_possible_on_crtc(struct intel_overlay *overlay,
918 struct intel_crtc *crtc)
919{
Chris Wilson722506f2010-08-12 09:28:50 +0100920 drm_i915_private_t *dev_priv = overlay->dev->dev_private;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200921 u32 pipeconf;
922 int pipeconf_reg = (crtc->pipe == 0) ? PIPEACONF : PIPEBCONF;
923
924 if (!crtc->base.enabled || crtc->dpms_mode != DRM_MODE_DPMS_ON)
925 return -EINVAL;
926
927 pipeconf = I915_READ(pipeconf_reg);
928
929 /* can't use the overlay with double wide pipe */
930 if (!IS_I965G(overlay->dev) && pipeconf & PIPEACONF_DOUBLE_WIDE)
931 return -EINVAL;
932
933 return 0;
934}
935
936static void update_pfit_vscale_ratio(struct intel_overlay *overlay)
937{
938 struct drm_device *dev = overlay->dev;
Chris Wilson722506f2010-08-12 09:28:50 +0100939 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200940 u32 pfit_control = I915_READ(PFIT_CONTROL);
Chris Wilson446d2182010-08-12 11:15:58 +0100941 u32 ratio;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200942
943 /* XXX: This is not the same logic as in the xorg driver, but more in
Chris Wilson446d2182010-08-12 11:15:58 +0100944 * line with the intel documentation for the i965
945 */
946 if (!IS_I965G(dev)) {
947 if (pfit_control & VERT_AUTO_SCALE)
948 ratio = I915_READ(PFIT_AUTO_RATIOS);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200949 else
Chris Wilson446d2182010-08-12 11:15:58 +0100950 ratio = I915_READ(PFIT_PGM_RATIOS);
951 ratio >>= PFIT_VERT_SCALE_SHIFT;
952 } else { /* on i965 use the PGM reg to read out the autoscaler values */
953 ratio = I915_READ(PFIT_PGM_RATIOS) >> PFIT_VERT_SCALE_SHIFT_965;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200954 }
955
956 overlay->pfit_vscale_ratio = ratio;
957}
958
959static int check_overlay_dst(struct intel_overlay *overlay,
960 struct drm_intel_overlay_put_image *rec)
961{
962 struct drm_display_mode *mode = &overlay->crtc->base.mode;
963
Chris Wilson722506f2010-08-12 09:28:50 +0100964 if (rec->dst_x < mode->crtc_hdisplay &&
965 rec->dst_x + rec->dst_width <= mode->crtc_hdisplay &&
966 rec->dst_y < mode->crtc_vdisplay &&
967 rec->dst_y + rec->dst_height <= mode->crtc_vdisplay)
Daniel Vetter02e792f2009-09-15 22:57:34 +0200968 return 0;
969 else
970 return -EINVAL;
971}
972
973static int check_overlay_scaling(struct put_image_params *rec)
974{
975 u32 tmp;
976
977 /* downscaling limit is 8.0 */
978 tmp = ((rec->src_scan_h << 16) / rec->dst_h) >> 16;
979 if (tmp > 7)
980 return -EINVAL;
981 tmp = ((rec->src_scan_w << 16) / rec->dst_w) >> 16;
982 if (tmp > 7)
983 return -EINVAL;
984
985 return 0;
986}
987
988static int check_overlay_src(struct drm_device *dev,
989 struct drm_intel_overlay_put_image *rec,
990 struct drm_gem_object *new_bo)
991{
992 u32 stride_mask;
993 int depth;
994 int uv_hscale = uv_hsubsampling(rec->flags);
995 int uv_vscale = uv_vsubsampling(rec->flags);
996 size_t tmp;
997
998 /* check src dimensions */
999 if (IS_845G(dev) || IS_I830(dev)) {
Chris Wilson722506f2010-08-12 09:28:50 +01001000 if (rec->src_height > IMAGE_MAX_HEIGHT_LEGACY ||
1001 rec->src_width > IMAGE_MAX_WIDTH_LEGACY)
Daniel Vetter02e792f2009-09-15 22:57:34 +02001002 return -EINVAL;
1003 } else {
Chris Wilson722506f2010-08-12 09:28:50 +01001004 if (rec->src_height > IMAGE_MAX_HEIGHT ||
1005 rec->src_width > IMAGE_MAX_WIDTH)
Daniel Vetter02e792f2009-09-15 22:57:34 +02001006 return -EINVAL;
1007 }
1008 /* better safe than sorry, use 4 as the maximal subsampling ratio */
Chris Wilson722506f2010-08-12 09:28:50 +01001009 if (rec->src_height < N_VERT_Y_TAPS*4 ||
1010 rec->src_width < N_HORIZ_Y_TAPS*4)
Daniel Vetter02e792f2009-09-15 22:57:34 +02001011 return -EINVAL;
1012
Chris Wilsona1efd142010-07-12 19:35:38 +01001013 /* check alignment constraints */
Daniel Vetter02e792f2009-09-15 22:57:34 +02001014 switch (rec->flags & I915_OVERLAY_TYPE_MASK) {
Chris Wilson722506f2010-08-12 09:28:50 +01001015 case I915_OVERLAY_RGB:
1016 /* not implemented */
1017 return -EINVAL;
1018 case I915_OVERLAY_YUV_PACKED:
1019 depth = packed_depth_bytes(rec->flags);
1020 if (uv_vscale != 1)
Daniel Vetter02e792f2009-09-15 22:57:34 +02001021 return -EINVAL;
Chris Wilson722506f2010-08-12 09:28:50 +01001022 if (depth < 0)
1023 return depth;
1024 /* ignore UV planes */
1025 rec->stride_UV = 0;
1026 rec->offset_U = 0;
1027 rec->offset_V = 0;
1028 /* check pixel alignment */
1029 if (rec->offset_Y % depth)
Daniel Vetter02e792f2009-09-15 22:57:34 +02001030 return -EINVAL;
Chris Wilson722506f2010-08-12 09:28:50 +01001031 break;
1032 case I915_OVERLAY_YUV_PLANAR:
1033 if (uv_vscale < 0 || uv_hscale < 0)
1034 return -EINVAL;
1035 /* no offset restrictions for planar formats */
1036 break;
1037 default:
1038 return -EINVAL;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001039 }
1040
1041 if (rec->src_width % uv_hscale)
1042 return -EINVAL;
1043
1044 /* stride checking */
Chris Wilsona1efd142010-07-12 19:35:38 +01001045 if (IS_I830(dev) || IS_845G(dev))
1046 stride_mask = 255;
1047 else
1048 stride_mask = 63;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001049
1050 if (rec->stride_Y & stride_mask || rec->stride_UV & stride_mask)
1051 return -EINVAL;
1052 if (IS_I965G(dev) && rec->stride_Y < 512)
1053 return -EINVAL;
1054
1055 tmp = (rec->flags & I915_OVERLAY_TYPE_MASK) == I915_OVERLAY_YUV_PLANAR ?
1056 4 : 8;
1057 if (rec->stride_Y > tmp*1024 || rec->stride_UV > 2*1024)
1058 return -EINVAL;
1059
1060 /* check buffer dimensions */
1061 switch (rec->flags & I915_OVERLAY_TYPE_MASK) {
Chris Wilson722506f2010-08-12 09:28:50 +01001062 case I915_OVERLAY_RGB:
1063 case I915_OVERLAY_YUV_PACKED:
1064 /* always 4 Y values per depth pixels */
1065 if (packed_width_bytes(rec->flags, rec->src_width) > rec->stride_Y)
1066 return -EINVAL;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001067
Chris Wilson722506f2010-08-12 09:28:50 +01001068 tmp = rec->stride_Y*rec->src_height;
1069 if (rec->offset_Y + tmp > new_bo->size)
1070 return -EINVAL;
1071 break;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001072
Chris Wilson722506f2010-08-12 09:28:50 +01001073 case I915_OVERLAY_YUV_PLANAR:
1074 if (rec->src_width > rec->stride_Y)
1075 return -EINVAL;
1076 if (rec->src_width/uv_hscale > rec->stride_UV)
1077 return -EINVAL;
1078
1079 tmp = rec->stride_Y*rec->src_height;
1080 if (rec->offset_Y + tmp > new_bo->size)
1081 return -EINVAL;
1082 tmp = rec->stride_UV*rec->src_height;
1083 tmp /= uv_vscale;
1084 if (rec->offset_U + tmp > new_bo->size ||
1085 rec->offset_V + tmp > new_bo->size)
1086 return -EINVAL;
1087 break;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001088 }
1089
1090 return 0;
1091}
1092
1093int intel_overlay_put_image(struct drm_device *dev, void *data,
1094 struct drm_file *file_priv)
1095{
1096 struct drm_intel_overlay_put_image *put_image_rec = data;
1097 drm_i915_private_t *dev_priv = dev->dev_private;
1098 struct intel_overlay *overlay;
1099 struct drm_mode_object *drmmode_obj;
1100 struct intel_crtc *crtc;
1101 struct drm_gem_object *new_bo;
1102 struct put_image_params *params;
1103 int ret;
1104
1105 if (!dev_priv) {
1106 DRM_ERROR("called with no initialization\n");
1107 return -EINVAL;
1108 }
1109
1110 overlay = dev_priv->overlay;
1111 if (!overlay) {
1112 DRM_DEBUG("userspace bug: no overlay\n");
1113 return -ENODEV;
1114 }
1115
1116 if (!(put_image_rec->flags & I915_OVERLAY_ENABLE)) {
1117 mutex_lock(&dev->mode_config.mutex);
1118 mutex_lock(&dev->struct_mutex);
1119
1120 ret = intel_overlay_switch_off(overlay);
1121
1122 mutex_unlock(&dev->struct_mutex);
1123 mutex_unlock(&dev->mode_config.mutex);
1124
1125 return ret;
1126 }
1127
1128 params = kmalloc(sizeof(struct put_image_params), GFP_KERNEL);
1129 if (!params)
1130 return -ENOMEM;
1131
1132 drmmode_obj = drm_mode_object_find(dev, put_image_rec->crtc_id,
Chris Wilson722506f2010-08-12 09:28:50 +01001133 DRM_MODE_OBJECT_CRTC);
Dan Carpenter915a4282010-03-06 14:05:39 +03001134 if (!drmmode_obj) {
1135 ret = -ENOENT;
1136 goto out_free;
1137 }
Daniel Vetter02e792f2009-09-15 22:57:34 +02001138 crtc = to_intel_crtc(obj_to_crtc(drmmode_obj));
1139
1140 new_bo = drm_gem_object_lookup(dev, file_priv,
Chris Wilson722506f2010-08-12 09:28:50 +01001141 put_image_rec->bo_handle);
Dan Carpenter915a4282010-03-06 14:05:39 +03001142 if (!new_bo) {
1143 ret = -ENOENT;
1144 goto out_free;
1145 }
Daniel Vetter02e792f2009-09-15 22:57:34 +02001146
1147 mutex_lock(&dev->mode_config.mutex);
1148 mutex_lock(&dev->struct_mutex);
1149
Daniel Vetter03f77ea2009-09-15 22:57:37 +02001150 if (overlay->hw_wedged) {
1151 ret = intel_overlay_recover_from_interrupt(overlay, 1);
1152 if (ret != 0)
1153 goto out_unlock;
1154 }
1155
Daniel Vetter02e792f2009-09-15 22:57:34 +02001156 if (overlay->crtc != crtc) {
1157 struct drm_display_mode *mode = &crtc->base.mode;
1158 ret = intel_overlay_switch_off(overlay);
1159 if (ret != 0)
1160 goto out_unlock;
1161
1162 ret = check_overlay_possible_on_crtc(overlay, crtc);
1163 if (ret != 0)
1164 goto out_unlock;
1165
1166 overlay->crtc = crtc;
1167 crtc->overlay = overlay;
1168
1169 if (intel_panel_fitter_pipe(dev) == crtc->pipe
1170 /* and line to wide, i.e. one-line-mode */
1171 && mode->hdisplay > 1024) {
1172 overlay->pfit_active = 1;
1173 update_pfit_vscale_ratio(overlay);
1174 } else
1175 overlay->pfit_active = 0;
1176 }
1177
1178 ret = check_overlay_dst(overlay, put_image_rec);
1179 if (ret != 0)
1180 goto out_unlock;
1181
1182 if (overlay->pfit_active) {
1183 params->dst_y = ((((u32)put_image_rec->dst_y) << 12) /
Chris Wilson722506f2010-08-12 09:28:50 +01001184 overlay->pfit_vscale_ratio);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001185 /* shifting right rounds downwards, so add 1 */
1186 params->dst_h = ((((u32)put_image_rec->dst_height) << 12) /
Chris Wilson722506f2010-08-12 09:28:50 +01001187 overlay->pfit_vscale_ratio) + 1;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001188 } else {
1189 params->dst_y = put_image_rec->dst_y;
1190 params->dst_h = put_image_rec->dst_height;
1191 }
1192 params->dst_x = put_image_rec->dst_x;
1193 params->dst_w = put_image_rec->dst_width;
1194
1195 params->src_w = put_image_rec->src_width;
1196 params->src_h = put_image_rec->src_height;
1197 params->src_scan_w = put_image_rec->src_scan_width;
1198 params->src_scan_h = put_image_rec->src_scan_height;
Chris Wilson722506f2010-08-12 09:28:50 +01001199 if (params->src_scan_h > params->src_h ||
1200 params->src_scan_w > params->src_w) {
Daniel Vetter02e792f2009-09-15 22:57:34 +02001201 ret = -EINVAL;
1202 goto out_unlock;
1203 }
1204
1205 ret = check_overlay_src(dev, put_image_rec, new_bo);
1206 if (ret != 0)
1207 goto out_unlock;
1208 params->format = put_image_rec->flags & ~I915_OVERLAY_FLAGS_MASK;
1209 params->stride_Y = put_image_rec->stride_Y;
1210 params->stride_UV = put_image_rec->stride_UV;
1211 params->offset_Y = put_image_rec->offset_Y;
1212 params->offset_U = put_image_rec->offset_U;
1213 params->offset_V = put_image_rec->offset_V;
1214
1215 /* Check scaling after src size to prevent a divide-by-zero. */
1216 ret = check_overlay_scaling(params);
1217 if (ret != 0)
1218 goto out_unlock;
1219
1220 ret = intel_overlay_do_put_image(overlay, new_bo, params);
1221 if (ret != 0)
1222 goto out_unlock;
1223
1224 mutex_unlock(&dev->struct_mutex);
1225 mutex_unlock(&dev->mode_config.mutex);
1226
1227 kfree(params);
1228
1229 return 0;
1230
1231out_unlock:
1232 mutex_unlock(&dev->struct_mutex);
1233 mutex_unlock(&dev->mode_config.mutex);
Luca Barbieribc9025b2010-02-09 05:49:12 +00001234 drm_gem_object_unreference_unlocked(new_bo);
Dan Carpenter915a4282010-03-06 14:05:39 +03001235out_free:
Daniel Vetter02e792f2009-09-15 22:57:34 +02001236 kfree(params);
1237
1238 return ret;
1239}
1240
1241static void update_reg_attrs(struct intel_overlay *overlay,
1242 struct overlay_registers *regs)
1243{
1244 regs->OCLRC0 = (overlay->contrast << 18) | (overlay->brightness & 0xff);
1245 regs->OCLRC1 = overlay->saturation;
1246}
1247
1248static bool check_gamma_bounds(u32 gamma1, u32 gamma2)
1249{
1250 int i;
1251
1252 if (gamma1 & 0xff000000 || gamma2 & 0xff000000)
1253 return false;
1254
1255 for (i = 0; i < 3; i++) {
Chris Wilson722506f2010-08-12 09:28:50 +01001256 if (((gamma1 >> i*8) & 0xff) >= ((gamma2 >> i*8) & 0xff))
Daniel Vetter02e792f2009-09-15 22:57:34 +02001257 return false;
1258 }
1259
1260 return true;
1261}
1262
1263static bool check_gamma5_errata(u32 gamma5)
1264{
1265 int i;
1266
1267 for (i = 0; i < 3; i++) {
1268 if (((gamma5 >> i*8) & 0xff) == 0x80)
1269 return false;
1270 }
1271
1272 return true;
1273}
1274
1275static int check_gamma(struct drm_intel_overlay_attrs *attrs)
1276{
Chris Wilson722506f2010-08-12 09:28:50 +01001277 if (!check_gamma_bounds(0, attrs->gamma0) ||
1278 !check_gamma_bounds(attrs->gamma0, attrs->gamma1) ||
1279 !check_gamma_bounds(attrs->gamma1, attrs->gamma2) ||
1280 !check_gamma_bounds(attrs->gamma2, attrs->gamma3) ||
1281 !check_gamma_bounds(attrs->gamma3, attrs->gamma4) ||
1282 !check_gamma_bounds(attrs->gamma4, attrs->gamma5) ||
1283 !check_gamma_bounds(attrs->gamma5, 0x00ffffff))
Daniel Vetter02e792f2009-09-15 22:57:34 +02001284 return -EINVAL;
Chris Wilson722506f2010-08-12 09:28:50 +01001285
Daniel Vetter02e792f2009-09-15 22:57:34 +02001286 if (!check_gamma5_errata(attrs->gamma5))
1287 return -EINVAL;
Chris Wilson722506f2010-08-12 09:28:50 +01001288
Daniel Vetter02e792f2009-09-15 22:57:34 +02001289 return 0;
1290}
1291
1292int intel_overlay_attrs(struct drm_device *dev, void *data,
1293 struct drm_file *file_priv)
1294{
1295 struct drm_intel_overlay_attrs *attrs = data;
1296 drm_i915_private_t *dev_priv = dev->dev_private;
1297 struct intel_overlay *overlay;
1298 struct overlay_registers *regs;
1299 int ret;
1300
1301 if (!dev_priv) {
1302 DRM_ERROR("called with no initialization\n");
1303 return -EINVAL;
1304 }
1305
1306 overlay = dev_priv->overlay;
1307 if (!overlay) {
1308 DRM_DEBUG("userspace bug: no overlay\n");
1309 return -ENODEV;
1310 }
1311
1312 mutex_lock(&dev->mode_config.mutex);
1313 mutex_lock(&dev->struct_mutex);
1314
Chris Wilson60fc3322010-08-12 10:44:45 +01001315 ret = -EINVAL;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001316 if (!(attrs->flags & I915_OVERLAY_UPDATE_ATTRS)) {
Chris Wilson60fc3322010-08-12 10:44:45 +01001317 attrs->color_key = overlay->color_key;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001318 attrs->brightness = overlay->brightness;
Chris Wilson60fc3322010-08-12 10:44:45 +01001319 attrs->contrast = overlay->contrast;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001320 attrs->saturation = overlay->saturation;
1321
1322 if (IS_I9XX(dev)) {
1323 attrs->gamma0 = I915_READ(OGAMC0);
1324 attrs->gamma1 = I915_READ(OGAMC1);
1325 attrs->gamma2 = I915_READ(OGAMC2);
1326 attrs->gamma3 = I915_READ(OGAMC3);
1327 attrs->gamma4 = I915_READ(OGAMC4);
1328 attrs->gamma5 = I915_READ(OGAMC5);
1329 }
Daniel Vetter02e792f2009-09-15 22:57:34 +02001330 } else {
Chris Wilson60fc3322010-08-12 10:44:45 +01001331 if (attrs->brightness < -128 || attrs->brightness > 127)
Daniel Vetter02e792f2009-09-15 22:57:34 +02001332 goto out_unlock;
Chris Wilson60fc3322010-08-12 10:44:45 +01001333 if (attrs->contrast > 255)
1334 goto out_unlock;
1335 if (attrs->saturation > 1023)
1336 goto out_unlock;
Chris Wilson722506f2010-08-12 09:28:50 +01001337
Chris Wilson60fc3322010-08-12 10:44:45 +01001338 overlay->color_key = attrs->color_key;
1339 overlay->brightness = attrs->brightness;
1340 overlay->contrast = attrs->contrast;
1341 overlay->saturation = attrs->saturation;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001342
Chris Wilson8d74f652010-08-12 10:35:26 +01001343 regs = intel_overlay_map_regs(overlay);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001344 if (!regs) {
1345 ret = -ENOMEM;
1346 goto out_unlock;
1347 }
1348
1349 update_reg_attrs(overlay, regs);
1350
Chris Wilson8d74f652010-08-12 10:35:26 +01001351 intel_overlay_unmap_regs(overlay);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001352
1353 if (attrs->flags & I915_OVERLAY_UPDATE_GAMMA) {
Chris Wilson60fc3322010-08-12 10:44:45 +01001354 if (!IS_I9XX(dev))
Daniel Vetter02e792f2009-09-15 22:57:34 +02001355 goto out_unlock;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001356
1357 if (overlay->active) {
1358 ret = -EBUSY;
1359 goto out_unlock;
1360 }
1361
1362 ret = check_gamma(attrs);
Chris Wilson60fc3322010-08-12 10:44:45 +01001363 if (ret)
Daniel Vetter02e792f2009-09-15 22:57:34 +02001364 goto out_unlock;
1365
1366 I915_WRITE(OGAMC0, attrs->gamma0);
1367 I915_WRITE(OGAMC1, attrs->gamma1);
1368 I915_WRITE(OGAMC2, attrs->gamma2);
1369 I915_WRITE(OGAMC3, attrs->gamma3);
1370 I915_WRITE(OGAMC4, attrs->gamma4);
1371 I915_WRITE(OGAMC5, attrs->gamma5);
1372 }
Daniel Vetter02e792f2009-09-15 22:57:34 +02001373 }
1374
Chris Wilson60fc3322010-08-12 10:44:45 +01001375 ret = 0;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001376out_unlock:
1377 mutex_unlock(&dev->struct_mutex);
1378 mutex_unlock(&dev->mode_config.mutex);
1379
1380 return ret;
1381}
1382
1383void intel_setup_overlay(struct drm_device *dev)
1384{
1385 drm_i915_private_t *dev_priv = dev->dev_private;
1386 struct intel_overlay *overlay;
1387 struct drm_gem_object *reg_bo;
1388 struct overlay_registers *regs;
1389 int ret;
1390
Chris Wilson315781482010-08-12 09:42:51 +01001391 if (!HAS_OVERLAY(dev))
Daniel Vetter02e792f2009-09-15 22:57:34 +02001392 return;
1393
1394 overlay = kzalloc(sizeof(struct intel_overlay), GFP_KERNEL);
1395 if (!overlay)
1396 return;
1397 overlay->dev = dev;
1398
Daniel Vetterac52bc52010-04-09 19:05:06 +00001399 reg_bo = i915_gem_alloc_object(dev, PAGE_SIZE);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001400 if (!reg_bo)
1401 goto out_free;
Daniel Vetter23010e42010-03-08 13:35:02 +01001402 overlay->reg_bo = to_intel_bo(reg_bo);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001403
Chris Wilson315781482010-08-12 09:42:51 +01001404 if (OVERLAY_NEEDS_PHYSICAL(dev)) {
1405 ret = i915_gem_attach_phys_object(dev, reg_bo,
1406 I915_GEM_PHYS_OVERLAY_REGS,
Chris Wilsona2930122010-08-12 10:47:56 +01001407 PAGE_SIZE);
Chris Wilson315781482010-08-12 09:42:51 +01001408 if (ret) {
1409 DRM_ERROR("failed to attach phys overlay regs\n");
1410 goto out_free_bo;
1411 }
1412 overlay->flip_addr = overlay->reg_bo->phys_obj->handle->busaddr;
1413 } else {
Daniel Vetter02e792f2009-09-15 22:57:34 +02001414 ret = i915_gem_object_pin(reg_bo, PAGE_SIZE);
1415 if (ret) {
1416 DRM_ERROR("failed to pin overlay register bo\n");
1417 goto out_free_bo;
1418 }
1419 overlay->flip_addr = overlay->reg_bo->gtt_offset;
Chris Wilson0ddc1282010-08-12 09:35:00 +01001420
1421 ret = i915_gem_object_set_to_gtt_domain(reg_bo, true);
1422 if (ret) {
1423 DRM_ERROR("failed to move overlay register bo into the GTT\n");
1424 goto out_unpin_bo;
1425 }
Daniel Vetter02e792f2009-09-15 22:57:34 +02001426 }
1427
1428 /* init all values */
1429 overlay->color_key = 0x0101fe;
1430 overlay->brightness = -19;
1431 overlay->contrast = 75;
1432 overlay->saturation = 146;
1433
Chris Wilson8d74f652010-08-12 10:35:26 +01001434 regs = intel_overlay_map_regs(overlay);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001435 if (!regs)
1436 goto out_free_bo;
1437
1438 memset(regs, 0, sizeof(struct overlay_registers));
1439 update_polyphase_filter(regs);
1440
1441 update_reg_attrs(overlay, regs);
1442
Chris Wilson8d74f652010-08-12 10:35:26 +01001443 intel_overlay_unmap_regs(overlay);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001444
1445 dev_priv->overlay = overlay;
1446 DRM_INFO("initialized overlay support\n");
1447 return;
1448
Chris Wilson0ddc1282010-08-12 09:35:00 +01001449out_unpin_bo:
1450 i915_gem_object_unpin(reg_bo);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001451out_free_bo:
1452 drm_gem_object_unreference(reg_bo);
1453out_free:
1454 kfree(overlay);
1455 return;
1456}
1457
1458void intel_cleanup_overlay(struct drm_device *dev)
1459{
Chris Wilson722506f2010-08-12 09:28:50 +01001460 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001461
Chris Wilson62cf4e62010-08-12 10:50:36 +01001462 if (!dev_priv->overlay)
1463 return;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001464
Chris Wilson62cf4e62010-08-12 10:50:36 +01001465 /* The bo's should be free'd by the generic code already.
1466 * Furthermore modesetting teardown happens beforehand so the
1467 * hardware should be off already */
1468 BUG_ON(dev_priv->overlay->active);
1469
1470 drm_gem_object_unreference_unlocked(&dev_priv->overlay->reg_bo->base);
1471 kfree(dev_priv->overlay);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001472}
Chris Wilson6ef3d422010-08-04 20:26:07 +01001473
1474struct intel_overlay_error_state {
1475 struct overlay_registers regs;
1476 unsigned long base;
1477 u32 dovsta;
1478 u32 isr;
1479};
1480
1481struct intel_overlay_error_state *
1482intel_overlay_capture_error_state(struct drm_device *dev)
1483{
1484 drm_i915_private_t *dev_priv = dev->dev_private;
1485 struct intel_overlay *overlay = dev_priv->overlay;
1486 struct intel_overlay_error_state *error;
1487 struct overlay_registers __iomem *regs;
1488
1489 if (!overlay || !overlay->active)
1490 return NULL;
1491
1492 error = kmalloc(sizeof(*error), GFP_ATOMIC);
1493 if (error == NULL)
1494 return NULL;
1495
1496 error->dovsta = I915_READ(DOVSTA);
1497 error->isr = I915_READ(ISR);
Chris Wilson315781482010-08-12 09:42:51 +01001498 if (OVERLAY_NEEDS_PHYSICAL(overlay->dev))
Chris Wilson6ef3d422010-08-04 20:26:07 +01001499 error->base = (long) overlay->reg_bo->phys_obj->handle->vaddr;
Chris Wilson315781482010-08-12 09:42:51 +01001500 else
1501 error->base = (long) overlay->reg_bo->gtt_offset;
Chris Wilson6ef3d422010-08-04 20:26:07 +01001502
Chris Wilson8d74f652010-08-12 10:35:26 +01001503 regs = intel_overlay_map_regs_atomic(overlay, KM_IRQ0);
Chris Wilson6ef3d422010-08-04 20:26:07 +01001504 if (!regs)
1505 goto err;
1506
1507 memcpy_fromio(&error->regs, regs, sizeof(struct overlay_registers));
Chris Wilson8d74f652010-08-12 10:35:26 +01001508 intel_overlay_unmap_regs_atomic(overlay, KM_IRQ0);
Chris Wilson6ef3d422010-08-04 20:26:07 +01001509
1510 return error;
1511
1512err:
1513 kfree(error);
1514 return NULL;
1515}
1516
1517void
1518intel_overlay_print_error_state(struct seq_file *m, struct intel_overlay_error_state *error)
1519{
1520 seq_printf(m, "Overlay, status: 0x%08x, interrupt: 0x%08x\n",
1521 error->dovsta, error->isr);
1522 seq_printf(m, " Register file at 0x%08lx:\n",
1523 error->base);
1524
1525#define P(x) seq_printf(m, " " #x ": 0x%08x\n", error->regs.x)
1526 P(OBUF_0Y);
1527 P(OBUF_1Y);
1528 P(OBUF_0U);
1529 P(OBUF_0V);
1530 P(OBUF_1U);
1531 P(OBUF_1V);
1532 P(OSTRIDE);
1533 P(YRGB_VPH);
1534 P(UV_VPH);
1535 P(HORZ_PH);
1536 P(INIT_PHS);
1537 P(DWINPOS);
1538 P(DWINSZ);
1539 P(SWIDTH);
1540 P(SWIDTHSW);
1541 P(SHEIGHT);
1542 P(YRGBSCALE);
1543 P(UVSCALE);
1544 P(OCLRC0);
1545 P(OCLRC1);
1546 P(DCLRKV);
1547 P(DCLRKM);
1548 P(SCLRKVH);
1549 P(SCLRKVL);
1550 P(SCLRKEN);
1551 P(OCONFIG);
1552 P(OCMD);
1553 P(OSTART_0Y);
1554 P(OSTART_1Y);
1555 P(OSTART_0U);
1556 P(OSTART_0V);
1557 P(OSTART_1U);
1558 P(OSTART_1V);
1559 P(OTILEOFF_0Y);
1560 P(OTILEOFF_1Y);
1561 P(OTILEOFF_0U);
1562 P(OTILEOFF_0V);
1563 P(OTILEOFF_1U);
1564 P(OTILEOFF_1V);
1565 P(FASTHSCALE);
1566 P(UVSCALEV);
1567#undef P
1568}