blob: 389690d36b59df0a1c3e6db7d56ead1b2581f981 [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
Chris Wilson9bb2ff72010-08-12 12:02:11 +0100180 if (OVERLAY_NEEDS_PHYSICAL(overlay->dev))
Chris Wilson315781482010-08-12 09:42:51 +0100181 regs = overlay->reg_bo->phys_obj->handle->vaddr;
Chris Wilson9bb2ff72010-08-12 12:02:11 +0100182 else
Daniel Vetter02e792f2009-09-15 22:57:34 +0200183 regs = io_mapping_map_atomic_wc(dev_priv->mm.gtt_mapping,
Chris Wilsonfca3ec02010-08-04 14:34:24 +0100184 overlay->reg_bo->gtt_offset,
Chris Wilson8d74f652010-08-12 10:35:26 +0100185 slot);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200186
Chris Wilson9bb2ff72010-08-12 12:02:11 +0100187 return regs;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200188}
189
Chris Wilson8d74f652010-08-12 10:35:26 +0100190static void intel_overlay_unmap_regs_atomic(struct intel_overlay *overlay,
Chris Wilson9bb2ff72010-08-12 12:02:11 +0100191 int slot,
192 struct overlay_registers *regs)
Daniel Vetter02e792f2009-09-15 22:57:34 +0200193{
Chris Wilson315781482010-08-12 09:42:51 +0100194 if (!OVERLAY_NEEDS_PHYSICAL(overlay->dev))
Chris Wilson9bb2ff72010-08-12 12:02:11 +0100195 io_mapping_unmap_atomic(regs, slot);
Chris Wilson8d74f652010-08-12 10:35:26 +0100196}
197
198static struct overlay_registers *
199intel_overlay_map_regs(struct intel_overlay *overlay)
200{
201 drm_i915_private_t *dev_priv = overlay->dev->dev_private;
202 struct overlay_registers *regs;
203
Chris Wilson9bb2ff72010-08-12 12:02:11 +0100204 if (OVERLAY_NEEDS_PHYSICAL(overlay->dev))
Chris Wilson8d74f652010-08-12 10:35:26 +0100205 regs = overlay->reg_bo->phys_obj->handle->vaddr;
Chris Wilson9bb2ff72010-08-12 12:02:11 +0100206 else
Chris Wilson8d74f652010-08-12 10:35:26 +0100207 regs = io_mapping_map_wc(dev_priv->mm.gtt_mapping,
208 overlay->reg_bo->gtt_offset);
209
Chris Wilson9bb2ff72010-08-12 12:02:11 +0100210 return regs;
Chris Wilson8d74f652010-08-12 10:35:26 +0100211}
212
Chris Wilson9bb2ff72010-08-12 12:02:11 +0100213static void intel_overlay_unmap_regs(struct intel_overlay *overlay,
214 struct overlay_registers *regs)
Chris Wilson8d74f652010-08-12 10:35:26 +0100215{
216 if (!OVERLAY_NEEDS_PHYSICAL(overlay->dev))
Chris Wilson9bb2ff72010-08-12 12:02:11 +0100217 io_mapping_unmap(regs);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200218}
219
Chris Wilsonb6c028e2010-08-12 11:55:08 +0100220static int intel_overlay_do_wait_request(struct intel_overlay *overlay,
Chris Wilson8dc5d142010-08-12 12:36:12 +0100221 struct drm_i915_gem_request *request,
Chris Wilsonb6c028e2010-08-12 11:55:08 +0100222 bool interruptible,
223 int stage)
224{
225 struct drm_device *dev = overlay->dev;
226 drm_i915_private_t *dev_priv = dev->dev_private;
227 int ret;
228
229 overlay->last_flip_req =
Chris Wilson8dc5d142010-08-12 12:36:12 +0100230 i915_add_request(dev, NULL, request, &dev_priv->render_ring);
Chris Wilsonb6c028e2010-08-12 11:55:08 +0100231 if (overlay->last_flip_req == 0)
232 return -ENOMEM;
233
234 overlay->hw_wedged = stage;
235 ret = i915_do_wait_request(dev,
236 overlay->last_flip_req, true,
237 &dev_priv->render_ring);
238 if (ret)
239 return ret;
240
241 overlay->hw_wedged = 0;
242 overlay->last_flip_req = 0;
243 return 0;
244}
245
Chris Wilson106dada2010-07-16 17:13:01 +0100246/* Workaround for i830 bug where pipe a must be enable to change control regs */
247static int
248i830_activate_pipe_a(struct drm_device *dev)
249{
250 drm_i915_private_t *dev_priv = dev->dev_private;
251 struct intel_crtc *crtc;
252 struct drm_crtc_helper_funcs *crtc_funcs;
253 struct drm_display_mode vesa_640x480 = {
254 DRM_MODE("640x480", DRM_MODE_TYPE_DRIVER, 25175, 640, 656,
255 752, 800, 0, 480, 489, 492, 525, 0,
256 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC)
257 }, *mode;
258
259 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[0]);
260 if (crtc->dpms_mode == DRM_MODE_DPMS_ON)
261 return 0;
262
263 /* most i8xx have pipe a forced on, so don't trust dpms mode */
264 if (I915_READ(PIPEACONF) & PIPEACONF_ENABLE)
265 return 0;
266
267 crtc_funcs = crtc->base.helper_private;
268 if (crtc_funcs->dpms == NULL)
269 return 0;
270
271 DRM_DEBUG_DRIVER("Enabling pipe A in order to enable overlay\n");
272
273 mode = drm_mode_duplicate(dev, &vesa_640x480);
274 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
275 if(!drm_crtc_helper_set_mode(&crtc->base, mode,
276 crtc->base.x, crtc->base.y,
277 crtc->base.fb))
278 return 0;
279
280 crtc_funcs->dpms(&crtc->base, DRM_MODE_DPMS_ON);
281 return 1;
282}
283
284static void
285i830_deactivate_pipe_a(struct drm_device *dev)
286{
287 drm_i915_private_t *dev_priv = dev->dev_private;
288 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[0];
289 struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
290
291 crtc_funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
292}
293
Daniel Vetter02e792f2009-09-15 22:57:34 +0200294/* overlay needs to be disable in OCMD reg */
295static int intel_overlay_on(struct intel_overlay *overlay)
296{
297 struct drm_device *dev = overlay->dev;
Chris Wilson8dc5d142010-08-12 12:36:12 +0100298 struct drm_i915_gem_request *request;
Chris Wilson106dada2010-07-16 17:13:01 +0100299 int pipe_a_quirk = 0;
300 int ret;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200301
302 BUG_ON(overlay->active);
Daniel Vetter03f77ea2009-09-15 22:57:37 +0200303 overlay->active = 1;
Daniel Vetter03f77ea2009-09-15 22:57:37 +0200304
Chris Wilson106dada2010-07-16 17:13:01 +0100305 if (IS_I830(dev)) {
306 pipe_a_quirk = i830_activate_pipe_a(dev);
307 if (pipe_a_quirk < 0)
308 return pipe_a_quirk;
309 }
310
Chris Wilson8dc5d142010-08-12 12:36:12 +0100311 request = kzalloc(sizeof(*request), GFP_KERNEL);
Chris Wilson106dada2010-07-16 17:13:01 +0100312 if (request == NULL) {
313 ret = -ENOMEM;
314 goto out;
315 }
Chris Wilson8dc5d142010-08-12 12:36:12 +0100316
Daniel Vetter4f8a5672010-02-11 14:14:43 +0100317 BEGIN_LP_RING(4);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200318 OUT_RING(MI_OVERLAY_FLIP | MI_OVERLAY_ON);
319 OUT_RING(overlay->flip_addr | OFC_UPDATE);
320 OUT_RING(MI_WAIT_FOR_EVENT | MI_WAIT_FOR_OVERLAY_FLIP);
321 OUT_RING(MI_NOOP);
322 ADVANCE_LP_RING();
323
Chris Wilson106dada2010-07-16 17:13:01 +0100324 ret = intel_overlay_do_wait_request(overlay, request, true,
325 NEEDS_WAIT_FOR_FLIP);
326out:
327 if (pipe_a_quirk)
328 i830_deactivate_pipe_a(dev);
329
330 return ret;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200331}
332
333/* overlay needs to be enabled in OCMD reg */
Chris Wilson8dc5d142010-08-12 12:36:12 +0100334static int intel_overlay_continue(struct intel_overlay *overlay,
335 bool load_polyphase_filter)
Daniel Vetter02e792f2009-09-15 22:57:34 +0200336{
337 struct drm_device *dev = overlay->dev;
338 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson8dc5d142010-08-12 12:36:12 +0100339 struct drm_i915_gem_request *request;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200340 u32 flip_addr = overlay->flip_addr;
341 u32 tmp;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200342
343 BUG_ON(!overlay->active);
344
Chris Wilson8dc5d142010-08-12 12:36:12 +0100345 request = kzalloc(sizeof(*request), GFP_KERNEL);
346 if (request == NULL)
347 return -ENOMEM;
348
Daniel Vetter02e792f2009-09-15 22:57:34 +0200349 if (load_polyphase_filter)
350 flip_addr |= OFC_UPDATE;
351
352 /* check for underruns */
353 tmp = I915_READ(DOVSTA);
354 if (tmp & (1 << 17))
355 DRM_DEBUG("overlay underrun, DOVSTA: %x\n", tmp);
356
Daniel Vetter4f8a5672010-02-11 14:14:43 +0100357 BEGIN_LP_RING(2);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200358 OUT_RING(MI_OVERLAY_FLIP | MI_OVERLAY_CONTINUE);
359 OUT_RING(flip_addr);
Daniel Vetter5a5a0c62009-09-15 22:57:36 +0200360 ADVANCE_LP_RING();
361
Zou Nan hai852835f2010-05-21 09:08:56 +0800362 overlay->last_flip_req =
Chris Wilson8dc5d142010-08-12 12:36:12 +0100363 i915_add_request(dev, NULL, request, &dev_priv->render_ring);
364 return 0;
Daniel Vetter5a5a0c62009-09-15 22:57:36 +0200365}
366
Daniel Vetter02e792f2009-09-15 22:57:34 +0200367/* overlay needs to be disabled in OCMD reg */
368static int intel_overlay_off(struct intel_overlay *overlay)
369{
Daniel Vetter02e792f2009-09-15 22:57:34 +0200370 struct drm_device *dev = overlay->dev;
Chris Wilson8dc5d142010-08-12 12:36:12 +0100371 u32 flip_addr = overlay->flip_addr;
372 struct drm_i915_gem_request *request;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200373
374 BUG_ON(!overlay->active);
375
Chris Wilson8dc5d142010-08-12 12:36:12 +0100376 request = kzalloc(sizeof(*request), GFP_KERNEL);
377 if (request == NULL)
378 return -ENOMEM;
379
Daniel Vetter02e792f2009-09-15 22:57:34 +0200380 /* According to intel docs the overlay hw may hang (when switching
381 * off) without loading the filter coeffs. It is however unclear whether
382 * this applies to the disabling of the overlay or to the switching off
383 * of the hw. Do it in both cases */
384 flip_addr |= OFC_UPDATE;
385
Chris Wilson8dfbc342010-08-12 12:07:32 +0100386 BEGIN_LP_RING(6);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200387 /* wait for overlay to go idle */
Daniel Vetter02e792f2009-09-15 22:57:34 +0200388 OUT_RING(MI_OVERLAY_FLIP | MI_OVERLAY_CONTINUE);
389 OUT_RING(flip_addr);
Chris Wilson722506f2010-08-12 09:28:50 +0100390 OUT_RING(MI_WAIT_FOR_EVENT | MI_WAIT_FOR_OVERLAY_FLIP);
Chris Wilson722506f2010-08-12 09:28:50 +0100391 /* turn overlay off */
Chris Wilson722506f2010-08-12 09:28:50 +0100392 OUT_RING(MI_OVERLAY_FLIP | MI_OVERLAY_OFF);
393 OUT_RING(flip_addr);
394 OUT_RING(MI_WAIT_FOR_EVENT | MI_WAIT_FOR_OVERLAY_FLIP);
Chris Wilson722506f2010-08-12 09:28:50 +0100395 ADVANCE_LP_RING();
396
Chris Wilson8dc5d142010-08-12 12:36:12 +0100397 return intel_overlay_do_wait_request(overlay, request, true,
398 SWITCH_OFF);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200399}
400
Chris Wilson5cd68c92010-08-12 12:21:54 +0100401static void intel_overlay_release_old_vid_tail(struct intel_overlay *overlay)
402{
403 struct drm_gem_object *obj = &overlay->old_vid_bo->base;
404
405 i915_gem_object_unpin(obj);
406 drm_gem_object_unreference(obj);
407
408 overlay->old_vid_bo = NULL;
409}
410
Daniel Vetter12ca45f2037-04-25 10:08:26 +0200411static void intel_overlay_off_tail(struct intel_overlay *overlay)
412{
413 struct drm_gem_object *obj;
414
415 /* never have the overlay hw on without showing a frame */
416 BUG_ON(!overlay->vid_bo);
Daniel Vettera8089e82010-04-09 19:05:09 +0000417 obj = &overlay->vid_bo->base;
Daniel Vetter12ca45f2037-04-25 10:08:26 +0200418
419 i915_gem_object_unpin(obj);
420 drm_gem_object_unreference(obj);
421 overlay->vid_bo = NULL;
422
423 overlay->crtc->overlay = NULL;
424 overlay->crtc = NULL;
425 overlay->active = 0;
426}
427
Daniel Vetter03f77ea2009-09-15 22:57:37 +0200428/* recover from an interruption due to a signal
429 * We have to be careful not to repeat work forever an make forward progess. */
430int intel_overlay_recover_from_interrupt(struct intel_overlay *overlay,
Chris Wilson722506f2010-08-12 09:28:50 +0100431 bool interruptible)
Daniel Vetter03f77ea2009-09-15 22:57:37 +0200432{
433 struct drm_device *dev = overlay->dev;
Zou Nan hai852835f2010-05-21 09:08:56 +0800434 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter03f77ea2009-09-15 22:57:37 +0200435 int ret;
Daniel Vetter03f77ea2009-09-15 22:57:37 +0200436
437 if (overlay->hw_wedged == HW_WEDGED)
438 return -EIO;
439
Zou Nan hai852835f2010-05-21 09:08:56 +0800440 ret = i915_do_wait_request(dev, overlay->last_flip_req,
Chris Wilson722506f2010-08-12 09:28:50 +0100441 interruptible, &dev_priv->render_ring);
Chris Wilsonb6c028e2010-08-12 11:55:08 +0100442 if (ret)
Daniel Vetter03f77ea2009-09-15 22:57:37 +0200443 return ret;
444
445 switch (overlay->hw_wedged) {
Chris Wilson722506f2010-08-12 09:28:50 +0100446 case RELEASE_OLD_VID:
Chris Wilson5cd68c92010-08-12 12:21:54 +0100447 intel_overlay_release_old_vid_tail(overlay);
Chris Wilson722506f2010-08-12 09:28:50 +0100448 break;
Chris Wilsonb6c028e2010-08-12 11:55:08 +0100449
Chris Wilson8dfbc342010-08-12 12:07:32 +0100450 case SWITCH_OFF:
Chris Wilson722506f2010-08-12 09:28:50 +0100451 intel_overlay_off_tail(overlay);
452 break;
Chris Wilson8dfbc342010-08-12 12:07:32 +0100453
Chris Wilson722506f2010-08-12 09:28:50 +0100454 default:
455 BUG_ON(overlay->hw_wedged != NEEDS_WAIT_FOR_FLIP);
Daniel Vetter03f77ea2009-09-15 22:57:37 +0200456 }
457
458 overlay->hw_wedged = 0;
459 overlay->last_flip_req = 0;
460 return 0;
461}
462
Daniel Vetter5a5a0c62009-09-15 22:57:36 +0200463/* Wait for pending overlay flip and release old frame.
464 * Needs to be called before the overlay register are changed
Chris Wilson8d74f652010-08-12 10:35:26 +0100465 * via intel_overlay_(un)map_regs
466 */
Daniel Vetter02e792f2009-09-15 22:57:34 +0200467static int intel_overlay_release_old_vid(struct intel_overlay *overlay)
468{
Chris Wilson5cd68c92010-08-12 12:21:54 +0100469 struct drm_device *dev = overlay->dev;
470 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200471 int ret;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200472
Chris Wilson5cd68c92010-08-12 12:21:54 +0100473 /* Only wait if there is actually an old frame to release to
474 * guarantee forward progress.
475 */
Daniel Vetter03f77ea2009-09-15 22:57:37 +0200476 if (!overlay->old_vid_bo)
477 return 0;
478
Chris Wilson5cd68c92010-08-12 12:21:54 +0100479 if (I915_READ(ISR) & I915_OVERLAY_PLANE_FLIP_PENDING_INTERRUPT) {
Chris Wilson8dc5d142010-08-12 12:36:12 +0100480 struct drm_i915_gem_request *request;
481
Chris Wilson5cd68c92010-08-12 12:21:54 +0100482 /* synchronous slowpath */
Chris Wilson8dc5d142010-08-12 12:36:12 +0100483 request = kzalloc(sizeof(*request), GFP_KERNEL);
484 if (request == NULL)
485 return -ENOMEM;
486
Chris Wilson5cd68c92010-08-12 12:21:54 +0100487 BEGIN_LP_RING(2);
488 OUT_RING(MI_WAIT_FOR_EVENT | MI_WAIT_FOR_OVERLAY_FLIP);
489 OUT_RING(MI_NOOP);
490 ADVANCE_LP_RING();
Daniel Vetter02e792f2009-09-15 22:57:34 +0200491
Chris Wilson8dc5d142010-08-12 12:36:12 +0100492 ret = intel_overlay_do_wait_request(overlay, request, true,
Chris Wilson5cd68c92010-08-12 12:21:54 +0100493 RELEASE_OLD_VID);
494 if (ret)
495 return ret;
496 }
Daniel Vetter02e792f2009-09-15 22:57:34 +0200497
Chris Wilson5cd68c92010-08-12 12:21:54 +0100498 intel_overlay_release_old_vid_tail(overlay);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200499 return 0;
500}
501
502struct put_image_params {
503 int format;
504 short dst_x;
505 short dst_y;
506 short dst_w;
507 short dst_h;
508 short src_w;
509 short src_scan_h;
510 short src_scan_w;
511 short src_h;
512 short stride_Y;
513 short stride_UV;
514 int offset_Y;
515 int offset_U;
516 int offset_V;
517};
518
519static int packed_depth_bytes(u32 format)
520{
521 switch (format & I915_OVERLAY_DEPTH_MASK) {
Chris Wilson722506f2010-08-12 09:28:50 +0100522 case I915_OVERLAY_YUV422:
523 return 4;
524 case I915_OVERLAY_YUV411:
525 /* return 6; not implemented */
526 default:
527 return -EINVAL;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200528 }
529}
530
531static int packed_width_bytes(u32 format, short width)
532{
533 switch (format & I915_OVERLAY_DEPTH_MASK) {
Chris Wilson722506f2010-08-12 09:28:50 +0100534 case I915_OVERLAY_YUV422:
535 return width << 1;
536 default:
537 return -EINVAL;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200538 }
539}
540
541static int uv_hsubsampling(u32 format)
542{
543 switch (format & I915_OVERLAY_DEPTH_MASK) {
Chris Wilson722506f2010-08-12 09:28:50 +0100544 case I915_OVERLAY_YUV422:
545 case I915_OVERLAY_YUV420:
546 return 2;
547 case I915_OVERLAY_YUV411:
548 case I915_OVERLAY_YUV410:
549 return 4;
550 default:
551 return -EINVAL;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200552 }
553}
554
555static int uv_vsubsampling(u32 format)
556{
557 switch (format & I915_OVERLAY_DEPTH_MASK) {
Chris Wilson722506f2010-08-12 09:28:50 +0100558 case I915_OVERLAY_YUV420:
559 case I915_OVERLAY_YUV410:
560 return 2;
561 case I915_OVERLAY_YUV422:
562 case I915_OVERLAY_YUV411:
563 return 1;
564 default:
565 return -EINVAL;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200566 }
567}
568
569static u32 calc_swidthsw(struct drm_device *dev, u32 offset, u32 width)
570{
571 u32 mask, shift, ret;
572 if (IS_I9XX(dev)) {
573 mask = 0x3f;
574 shift = 6;
575 } else {
576 mask = 0x1f;
577 shift = 5;
578 }
579 ret = ((offset + width + mask) >> shift) - (offset >> shift);
580 if (IS_I9XX(dev))
581 ret <<= 1;
582 ret -=1;
583 return ret << 2;
584}
585
586static const u16 y_static_hcoeffs[N_HORIZ_Y_TAPS * N_PHASES] = {
587 0x3000, 0xb4a0, 0x1930, 0x1920, 0xb4a0,
588 0x3000, 0xb500, 0x19d0, 0x1880, 0xb440,
589 0x3000, 0xb540, 0x1a88, 0x2f80, 0xb3e0,
590 0x3000, 0xb580, 0x1b30, 0x2e20, 0xb380,
591 0x3000, 0xb5c0, 0x1bd8, 0x2cc0, 0xb320,
592 0x3020, 0xb5e0, 0x1c60, 0x2b80, 0xb2c0,
593 0x3020, 0xb5e0, 0x1cf8, 0x2a20, 0xb260,
594 0x3020, 0xb5e0, 0x1d80, 0x28e0, 0xb200,
595 0x3020, 0xb5c0, 0x1e08, 0x3f40, 0xb1c0,
596 0x3020, 0xb580, 0x1e78, 0x3ce0, 0xb160,
597 0x3040, 0xb520, 0x1ed8, 0x3aa0, 0xb120,
598 0x3040, 0xb4a0, 0x1f30, 0x3880, 0xb0e0,
599 0x3040, 0xb400, 0x1f78, 0x3680, 0xb0a0,
600 0x3020, 0xb340, 0x1fb8, 0x34a0, 0xb060,
601 0x3020, 0xb240, 0x1fe0, 0x32e0, 0xb040,
602 0x3020, 0xb140, 0x1ff8, 0x3160, 0xb020,
Chris Wilson722506f2010-08-12 09:28:50 +0100603 0xb000, 0x3000, 0x0800, 0x3000, 0xb000
604};
605
Daniel Vetter02e792f2009-09-15 22:57:34 +0200606static const u16 uv_static_hcoeffs[N_HORIZ_UV_TAPS * N_PHASES] = {
607 0x3000, 0x1800, 0x1800, 0xb000, 0x18d0, 0x2e60,
608 0xb000, 0x1990, 0x2ce0, 0xb020, 0x1a68, 0x2b40,
609 0xb040, 0x1b20, 0x29e0, 0xb060, 0x1bd8, 0x2880,
610 0xb080, 0x1c88, 0x3e60, 0xb0a0, 0x1d28, 0x3c00,
611 0xb0c0, 0x1db8, 0x39e0, 0xb0e0, 0x1e40, 0x37e0,
612 0xb100, 0x1eb8, 0x3620, 0xb100, 0x1f18, 0x34a0,
613 0xb100, 0x1f68, 0x3360, 0xb0e0, 0x1fa8, 0x3240,
614 0xb0c0, 0x1fe0, 0x3140, 0xb060, 0x1ff0, 0x30a0,
Chris Wilson722506f2010-08-12 09:28:50 +0100615 0x3000, 0x0800, 0x3000
616};
Daniel Vetter02e792f2009-09-15 22:57:34 +0200617
618static void update_polyphase_filter(struct overlay_registers *regs)
619{
620 memcpy(regs->Y_HCOEFS, y_static_hcoeffs, sizeof(y_static_hcoeffs));
621 memcpy(regs->UV_HCOEFS, uv_static_hcoeffs, sizeof(uv_static_hcoeffs));
622}
623
624static bool update_scaling_factors(struct intel_overlay *overlay,
625 struct overlay_registers *regs,
626 struct put_image_params *params)
627{
628 /* fixed point with a 12 bit shift */
629 u32 xscale, yscale, xscale_UV, yscale_UV;
630#define FP_SHIFT 12
631#define FRACT_MASK 0xfff
632 bool scale_changed = false;
633 int uv_hscale = uv_hsubsampling(params->format);
634 int uv_vscale = uv_vsubsampling(params->format);
635
636 if (params->dst_w > 1)
637 xscale = ((params->src_scan_w - 1) << FP_SHIFT)
638 /(params->dst_w);
639 else
640 xscale = 1 << FP_SHIFT;
641
642 if (params->dst_h > 1)
643 yscale = ((params->src_scan_h - 1) << FP_SHIFT)
644 /(params->dst_h);
645 else
646 yscale = 1 << FP_SHIFT;
647
648 /*if (params->format & I915_OVERLAY_YUV_PLANAR) {*/
Chris Wilson722506f2010-08-12 09:28:50 +0100649 xscale_UV = xscale/uv_hscale;
650 yscale_UV = yscale/uv_vscale;
651 /* make the Y scale to UV scale ratio an exact multiply */
652 xscale = xscale_UV * uv_hscale;
653 yscale = yscale_UV * uv_vscale;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200654 /*} else {
Chris Wilson722506f2010-08-12 09:28:50 +0100655 xscale_UV = 0;
656 yscale_UV = 0;
657 }*/
Daniel Vetter02e792f2009-09-15 22:57:34 +0200658
659 if (xscale != overlay->old_xscale || yscale != overlay->old_yscale)
660 scale_changed = true;
661 overlay->old_xscale = xscale;
662 overlay->old_yscale = yscale;
663
Chris Wilson722506f2010-08-12 09:28:50 +0100664 regs->YRGBSCALE = (((yscale & FRACT_MASK) << 20) |
665 ((xscale >> FP_SHIFT) << 16) |
666 ((xscale & FRACT_MASK) << 3));
667
668 regs->UVSCALE = (((yscale_UV & FRACT_MASK) << 20) |
669 ((xscale_UV >> FP_SHIFT) << 16) |
670 ((xscale_UV & FRACT_MASK) << 3));
671
672 regs->UVSCALEV = ((((yscale >> FP_SHIFT) << 16) |
673 ((yscale_UV >> FP_SHIFT) << 0)));
Daniel Vetter02e792f2009-09-15 22:57:34 +0200674
675 if (scale_changed)
676 update_polyphase_filter(regs);
677
678 return scale_changed;
679}
680
681static void update_colorkey(struct intel_overlay *overlay,
682 struct overlay_registers *regs)
683{
684 u32 key = overlay->color_key;
Chris Wilson6ba3ddd2010-08-12 09:30:58 +0100685
Daniel Vetter02e792f2009-09-15 22:57:34 +0200686 switch (overlay->crtc->base.fb->bits_per_pixel) {
Chris Wilson722506f2010-08-12 09:28:50 +0100687 case 8:
688 regs->DCLRKV = 0;
689 regs->DCLRKM = CLK_RGB8I_MASK | DST_KEY_ENABLE;
Chris Wilson6ba3ddd2010-08-12 09:30:58 +0100690 break;
691
Chris Wilson722506f2010-08-12 09:28:50 +0100692 case 16:
693 if (overlay->crtc->base.fb->depth == 15) {
694 regs->DCLRKV = RGB15_TO_COLORKEY(key);
695 regs->DCLRKM = CLK_RGB15_MASK | DST_KEY_ENABLE;
696 } else {
697 regs->DCLRKV = RGB16_TO_COLORKEY(key);
698 regs->DCLRKM = CLK_RGB16_MASK | DST_KEY_ENABLE;
699 }
Chris Wilson6ba3ddd2010-08-12 09:30:58 +0100700 break;
701
Chris Wilson722506f2010-08-12 09:28:50 +0100702 case 24:
703 case 32:
704 regs->DCLRKV = key;
705 regs->DCLRKM = CLK_RGB24_MASK | DST_KEY_ENABLE;
Chris Wilson6ba3ddd2010-08-12 09:30:58 +0100706 break;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200707 }
708}
709
710static u32 overlay_cmd_reg(struct put_image_params *params)
711{
712 u32 cmd = OCMD_ENABLE | OCMD_BUF_TYPE_FRAME | OCMD_BUFFER0;
713
714 if (params->format & I915_OVERLAY_YUV_PLANAR) {
715 switch (params->format & I915_OVERLAY_DEPTH_MASK) {
Chris Wilson722506f2010-08-12 09:28:50 +0100716 case I915_OVERLAY_YUV422:
717 cmd |= OCMD_YUV_422_PLANAR;
718 break;
719 case I915_OVERLAY_YUV420:
720 cmd |= OCMD_YUV_420_PLANAR;
721 break;
722 case I915_OVERLAY_YUV411:
723 case I915_OVERLAY_YUV410:
724 cmd |= OCMD_YUV_410_PLANAR;
725 break;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200726 }
727 } else { /* YUV packed */
728 switch (params->format & I915_OVERLAY_DEPTH_MASK) {
Chris Wilson722506f2010-08-12 09:28:50 +0100729 case I915_OVERLAY_YUV422:
730 cmd |= OCMD_YUV_422_PACKED;
731 break;
732 case I915_OVERLAY_YUV411:
733 cmd |= OCMD_YUV_411_PACKED;
734 break;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200735 }
736
737 switch (params->format & I915_OVERLAY_SWAP_MASK) {
Chris Wilson722506f2010-08-12 09:28:50 +0100738 case I915_OVERLAY_NO_SWAP:
739 break;
740 case I915_OVERLAY_UV_SWAP:
741 cmd |= OCMD_UV_SWAP;
742 break;
743 case I915_OVERLAY_Y_SWAP:
744 cmd |= OCMD_Y_SWAP;
745 break;
746 case I915_OVERLAY_Y_AND_UV_SWAP:
747 cmd |= OCMD_Y_AND_UV_SWAP;
748 break;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200749 }
750 }
751
752 return cmd;
753}
754
Chris Wilson5fe82c52010-08-12 12:38:21 +0100755static int intel_overlay_do_put_image(struct intel_overlay *overlay,
756 struct drm_gem_object *new_bo,
757 struct put_image_params *params)
Daniel Vetter02e792f2009-09-15 22:57:34 +0200758{
759 int ret, tmp_width;
760 struct overlay_registers *regs;
761 bool scale_changed = false;
Daniel Vetter23010e42010-03-08 13:35:02 +0100762 struct drm_i915_gem_object *bo_priv = to_intel_bo(new_bo);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200763 struct drm_device *dev = overlay->dev;
764
765 BUG_ON(!mutex_is_locked(&dev->struct_mutex));
766 BUG_ON(!mutex_is_locked(&dev->mode_config.mutex));
767 BUG_ON(!overlay);
768
Daniel Vetter02e792f2009-09-15 22:57:34 +0200769 ret = intel_overlay_release_old_vid(overlay);
770 if (ret != 0)
771 return ret;
772
773 ret = i915_gem_object_pin(new_bo, PAGE_SIZE);
774 if (ret != 0)
775 return ret;
776
777 ret = i915_gem_object_set_to_gtt_domain(new_bo, 0);
778 if (ret != 0)
779 goto out_unpin;
780
781 if (!overlay->active) {
Chris Wilson8d74f652010-08-12 10:35:26 +0100782 regs = intel_overlay_map_regs(overlay);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200783 if (!regs) {
784 ret = -ENOMEM;
785 goto out_unpin;
786 }
787 regs->OCONFIG = OCONF_CC_OUT_8BIT;
788 if (IS_I965GM(overlay->dev))
789 regs->OCONFIG |= OCONF_CSC_MODE_BT709;
790 regs->OCONFIG |= overlay->crtc->pipe == 0 ?
791 OCONF_PIPE_A : OCONF_PIPE_B;
Chris Wilson9bb2ff72010-08-12 12:02:11 +0100792 intel_overlay_unmap_regs(overlay, regs);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200793
794 ret = intel_overlay_on(overlay);
795 if (ret != 0)
796 goto out_unpin;
797 }
798
Chris Wilson8d74f652010-08-12 10:35:26 +0100799 regs = intel_overlay_map_regs(overlay);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200800 if (!regs) {
801 ret = -ENOMEM;
802 goto out_unpin;
803 }
804
805 regs->DWINPOS = (params->dst_y << 16) | params->dst_x;
806 regs->DWINSZ = (params->dst_h << 16) | params->dst_w;
807
808 if (params->format & I915_OVERLAY_YUV_PACKED)
809 tmp_width = packed_width_bytes(params->format, params->src_w);
810 else
811 tmp_width = params->src_w;
812
813 regs->SWIDTH = params->src_w;
814 regs->SWIDTHSW = calc_swidthsw(overlay->dev,
Chris Wilson722506f2010-08-12 09:28:50 +0100815 params->offset_Y, tmp_width);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200816 regs->SHEIGHT = params->src_h;
817 regs->OBUF_0Y = bo_priv->gtt_offset + params-> offset_Y;
818 regs->OSTRIDE = params->stride_Y;
819
820 if (params->format & I915_OVERLAY_YUV_PLANAR) {
821 int uv_hscale = uv_hsubsampling(params->format);
822 int uv_vscale = uv_vsubsampling(params->format);
823 u32 tmp_U, tmp_V;
824 regs->SWIDTH |= (params->src_w/uv_hscale) << 16;
825 tmp_U = calc_swidthsw(overlay->dev, params->offset_U,
Chris Wilson722506f2010-08-12 09:28:50 +0100826 params->src_w/uv_hscale);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200827 tmp_V = calc_swidthsw(overlay->dev, params->offset_V,
Chris Wilson722506f2010-08-12 09:28:50 +0100828 params->src_w/uv_hscale);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200829 regs->SWIDTHSW |= max_t(u32, tmp_U, tmp_V) << 16;
830 regs->SHEIGHT |= (params->src_h/uv_vscale) << 16;
831 regs->OBUF_0U = bo_priv->gtt_offset + params->offset_U;
832 regs->OBUF_0V = bo_priv->gtt_offset + params->offset_V;
833 regs->OSTRIDE |= params->stride_UV << 16;
834 }
835
836 scale_changed = update_scaling_factors(overlay, regs, params);
837
838 update_colorkey(overlay, regs);
839
840 regs->OCMD = overlay_cmd_reg(params);
841
Chris Wilson9bb2ff72010-08-12 12:02:11 +0100842 intel_overlay_unmap_regs(overlay, regs);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200843
Chris Wilson8dc5d142010-08-12 12:36:12 +0100844 ret = intel_overlay_continue(overlay, scale_changed);
845 if (ret)
846 goto out_unpin;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200847
848 overlay->old_vid_bo = overlay->vid_bo;
Daniel Vetter23010e42010-03-08 13:35:02 +0100849 overlay->vid_bo = to_intel_bo(new_bo);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200850
851 return 0;
852
853out_unpin:
854 i915_gem_object_unpin(new_bo);
855 return ret;
856}
857
858int intel_overlay_switch_off(struct intel_overlay *overlay)
859{
860 int ret;
861 struct overlay_registers *regs;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200862 struct drm_device *dev = overlay->dev;
863
864 BUG_ON(!mutex_is_locked(&dev->struct_mutex));
865 BUG_ON(!mutex_is_locked(&dev->mode_config.mutex));
866
Daniel Vetter9bedb972009-11-30 15:55:49 +0100867 if (overlay->hw_wedged) {
868 ret = intel_overlay_recover_from_interrupt(overlay, 1);
869 if (ret != 0)
870 return ret;
871 }
872
Daniel Vetter02e792f2009-09-15 22:57:34 +0200873 if (!overlay->active)
874 return 0;
875
Daniel Vetter02e792f2009-09-15 22:57:34 +0200876 ret = intel_overlay_release_old_vid(overlay);
877 if (ret != 0)
878 return ret;
879
Chris Wilson8d74f652010-08-12 10:35:26 +0100880 regs = intel_overlay_map_regs(overlay);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200881 regs->OCMD = 0;
Chris Wilson9bb2ff72010-08-12 12:02:11 +0100882 intel_overlay_unmap_regs(overlay, regs);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200883
884 ret = intel_overlay_off(overlay);
Daniel Vetter03f77ea2009-09-15 22:57:37 +0200885 if (ret != 0)
886 return ret;
887
Daniel Vetter12ca45f2037-04-25 10:08:26 +0200888 intel_overlay_off_tail(overlay);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200889
890 return 0;
891}
892
893static int check_overlay_possible_on_crtc(struct intel_overlay *overlay,
894 struct intel_crtc *crtc)
895{
Chris Wilson722506f2010-08-12 09:28:50 +0100896 drm_i915_private_t *dev_priv = overlay->dev->dev_private;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200897 u32 pipeconf;
898 int pipeconf_reg = (crtc->pipe == 0) ? PIPEACONF : PIPEBCONF;
899
900 if (!crtc->base.enabled || crtc->dpms_mode != DRM_MODE_DPMS_ON)
901 return -EINVAL;
902
903 pipeconf = I915_READ(pipeconf_reg);
904
905 /* can't use the overlay with double wide pipe */
906 if (!IS_I965G(overlay->dev) && pipeconf & PIPEACONF_DOUBLE_WIDE)
907 return -EINVAL;
908
909 return 0;
910}
911
912static void update_pfit_vscale_ratio(struct intel_overlay *overlay)
913{
914 struct drm_device *dev = overlay->dev;
Chris Wilson722506f2010-08-12 09:28:50 +0100915 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200916 u32 pfit_control = I915_READ(PFIT_CONTROL);
Chris Wilson446d2182010-08-12 11:15:58 +0100917 u32 ratio;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200918
919 /* XXX: This is not the same logic as in the xorg driver, but more in
Chris Wilson446d2182010-08-12 11:15:58 +0100920 * line with the intel documentation for the i965
921 */
922 if (!IS_I965G(dev)) {
923 if (pfit_control & VERT_AUTO_SCALE)
924 ratio = I915_READ(PFIT_AUTO_RATIOS);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200925 else
Chris Wilson446d2182010-08-12 11:15:58 +0100926 ratio = I915_READ(PFIT_PGM_RATIOS);
927 ratio >>= PFIT_VERT_SCALE_SHIFT;
928 } else { /* on i965 use the PGM reg to read out the autoscaler values */
929 ratio = I915_READ(PFIT_PGM_RATIOS) >> PFIT_VERT_SCALE_SHIFT_965;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200930 }
931
932 overlay->pfit_vscale_ratio = ratio;
933}
934
935static int check_overlay_dst(struct intel_overlay *overlay,
936 struct drm_intel_overlay_put_image *rec)
937{
938 struct drm_display_mode *mode = &overlay->crtc->base.mode;
939
Chris Wilson722506f2010-08-12 09:28:50 +0100940 if (rec->dst_x < mode->crtc_hdisplay &&
941 rec->dst_x + rec->dst_width <= mode->crtc_hdisplay &&
942 rec->dst_y < mode->crtc_vdisplay &&
943 rec->dst_y + rec->dst_height <= mode->crtc_vdisplay)
Daniel Vetter02e792f2009-09-15 22:57:34 +0200944 return 0;
945 else
946 return -EINVAL;
947}
948
949static int check_overlay_scaling(struct put_image_params *rec)
950{
951 u32 tmp;
952
953 /* downscaling limit is 8.0 */
954 tmp = ((rec->src_scan_h << 16) / rec->dst_h) >> 16;
955 if (tmp > 7)
956 return -EINVAL;
957 tmp = ((rec->src_scan_w << 16) / rec->dst_w) >> 16;
958 if (tmp > 7)
959 return -EINVAL;
960
961 return 0;
962}
963
964static int check_overlay_src(struct drm_device *dev,
965 struct drm_intel_overlay_put_image *rec,
966 struct drm_gem_object *new_bo)
967{
Daniel Vetter02e792f2009-09-15 22:57:34 +0200968 int uv_hscale = uv_hsubsampling(rec->flags);
969 int uv_vscale = uv_vsubsampling(rec->flags);
Chris Wilson9f7c3f42010-08-12 11:29:34 +0100970 u32 stride_mask, depth, tmp;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200971
972 /* check src dimensions */
973 if (IS_845G(dev) || IS_I830(dev)) {
Chris Wilson722506f2010-08-12 09:28:50 +0100974 if (rec->src_height > IMAGE_MAX_HEIGHT_LEGACY ||
Chris Wilson9f7c3f42010-08-12 11:29:34 +0100975 rec->src_width > IMAGE_MAX_WIDTH_LEGACY)
Daniel Vetter02e792f2009-09-15 22:57:34 +0200976 return -EINVAL;
977 } else {
Chris Wilson722506f2010-08-12 09:28:50 +0100978 if (rec->src_height > IMAGE_MAX_HEIGHT ||
Chris Wilson9f7c3f42010-08-12 11:29:34 +0100979 rec->src_width > IMAGE_MAX_WIDTH)
Daniel Vetter02e792f2009-09-15 22:57:34 +0200980 return -EINVAL;
981 }
Chris Wilson9f7c3f42010-08-12 11:29:34 +0100982
Daniel Vetter02e792f2009-09-15 22:57:34 +0200983 /* better safe than sorry, use 4 as the maximal subsampling ratio */
Chris Wilson722506f2010-08-12 09:28:50 +0100984 if (rec->src_height < N_VERT_Y_TAPS*4 ||
Chris Wilson9f7c3f42010-08-12 11:29:34 +0100985 rec->src_width < N_HORIZ_Y_TAPS*4)
Daniel Vetter02e792f2009-09-15 22:57:34 +0200986 return -EINVAL;
987
Chris Wilsona1efd142010-07-12 19:35:38 +0100988 /* check alignment constraints */
Daniel Vetter02e792f2009-09-15 22:57:34 +0200989 switch (rec->flags & I915_OVERLAY_TYPE_MASK) {
Chris Wilson722506f2010-08-12 09:28:50 +0100990 case I915_OVERLAY_RGB:
991 /* not implemented */
992 return -EINVAL;
Chris Wilson9f7c3f42010-08-12 11:29:34 +0100993
Chris Wilson722506f2010-08-12 09:28:50 +0100994 case I915_OVERLAY_YUV_PACKED:
Chris Wilson722506f2010-08-12 09:28:50 +0100995 if (uv_vscale != 1)
Daniel Vetter02e792f2009-09-15 22:57:34 +0200996 return -EINVAL;
Chris Wilson9f7c3f42010-08-12 11:29:34 +0100997
998 depth = packed_depth_bytes(rec->flags);
Chris Wilson722506f2010-08-12 09:28:50 +0100999 if (depth < 0)
1000 return depth;
Chris Wilson9f7c3f42010-08-12 11:29:34 +01001001
Chris Wilson722506f2010-08-12 09:28:50 +01001002 /* ignore UV planes */
1003 rec->stride_UV = 0;
1004 rec->offset_U = 0;
1005 rec->offset_V = 0;
1006 /* check pixel alignment */
1007 if (rec->offset_Y % depth)
Daniel Vetter02e792f2009-09-15 22:57:34 +02001008 return -EINVAL;
Chris Wilson722506f2010-08-12 09:28:50 +01001009 break;
Chris Wilson9f7c3f42010-08-12 11:29:34 +01001010
Chris Wilson722506f2010-08-12 09:28:50 +01001011 case I915_OVERLAY_YUV_PLANAR:
1012 if (uv_vscale < 0 || uv_hscale < 0)
1013 return -EINVAL;
1014 /* no offset restrictions for planar formats */
1015 break;
Chris Wilson9f7c3f42010-08-12 11:29:34 +01001016
Chris Wilson722506f2010-08-12 09:28:50 +01001017 default:
1018 return -EINVAL;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001019 }
1020
1021 if (rec->src_width % uv_hscale)
1022 return -EINVAL;
1023
1024 /* stride checking */
Chris Wilsona1efd142010-07-12 19:35:38 +01001025 if (IS_I830(dev) || IS_845G(dev))
1026 stride_mask = 255;
1027 else
1028 stride_mask = 63;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001029
1030 if (rec->stride_Y & stride_mask || rec->stride_UV & stride_mask)
1031 return -EINVAL;
1032 if (IS_I965G(dev) && rec->stride_Y < 512)
1033 return -EINVAL;
1034
1035 tmp = (rec->flags & I915_OVERLAY_TYPE_MASK) == I915_OVERLAY_YUV_PLANAR ?
Chris Wilson9f7c3f42010-08-12 11:29:34 +01001036 4096 : 8192;
1037 if (rec->stride_Y > tmp || rec->stride_UV > 2*1024)
Daniel Vetter02e792f2009-09-15 22:57:34 +02001038 return -EINVAL;
1039
1040 /* check buffer dimensions */
1041 switch (rec->flags & I915_OVERLAY_TYPE_MASK) {
Chris Wilson722506f2010-08-12 09:28:50 +01001042 case I915_OVERLAY_RGB:
1043 case I915_OVERLAY_YUV_PACKED:
1044 /* always 4 Y values per depth pixels */
1045 if (packed_width_bytes(rec->flags, rec->src_width) > rec->stride_Y)
1046 return -EINVAL;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001047
Chris Wilson722506f2010-08-12 09:28:50 +01001048 tmp = rec->stride_Y*rec->src_height;
1049 if (rec->offset_Y + tmp > new_bo->size)
1050 return -EINVAL;
1051 break;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001052
Chris Wilson722506f2010-08-12 09:28:50 +01001053 case I915_OVERLAY_YUV_PLANAR:
1054 if (rec->src_width > rec->stride_Y)
1055 return -EINVAL;
1056 if (rec->src_width/uv_hscale > rec->stride_UV)
1057 return -EINVAL;
1058
Chris Wilson9f7c3f42010-08-12 11:29:34 +01001059 tmp = rec->stride_Y * rec->src_height;
Chris Wilson722506f2010-08-12 09:28:50 +01001060 if (rec->offset_Y + tmp > new_bo->size)
1061 return -EINVAL;
Chris Wilson9f7c3f42010-08-12 11:29:34 +01001062
1063 tmp = rec->stride_UV * (rec->src_height / uv_vscale);
Chris Wilson722506f2010-08-12 09:28:50 +01001064 if (rec->offset_U + tmp > new_bo->size ||
1065 rec->offset_V + tmp > new_bo->size)
1066 return -EINVAL;
1067 break;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001068 }
1069
1070 return 0;
1071}
1072
1073int intel_overlay_put_image(struct drm_device *dev, void *data,
1074 struct drm_file *file_priv)
1075{
1076 struct drm_intel_overlay_put_image *put_image_rec = data;
1077 drm_i915_private_t *dev_priv = dev->dev_private;
1078 struct intel_overlay *overlay;
1079 struct drm_mode_object *drmmode_obj;
1080 struct intel_crtc *crtc;
1081 struct drm_gem_object *new_bo;
1082 struct put_image_params *params;
1083 int ret;
1084
1085 if (!dev_priv) {
1086 DRM_ERROR("called with no initialization\n");
1087 return -EINVAL;
1088 }
1089
1090 overlay = dev_priv->overlay;
1091 if (!overlay) {
1092 DRM_DEBUG("userspace bug: no overlay\n");
1093 return -ENODEV;
1094 }
1095
1096 if (!(put_image_rec->flags & I915_OVERLAY_ENABLE)) {
1097 mutex_lock(&dev->mode_config.mutex);
1098 mutex_lock(&dev->struct_mutex);
1099
1100 ret = intel_overlay_switch_off(overlay);
1101
1102 mutex_unlock(&dev->struct_mutex);
1103 mutex_unlock(&dev->mode_config.mutex);
1104
1105 return ret;
1106 }
1107
1108 params = kmalloc(sizeof(struct put_image_params), GFP_KERNEL);
1109 if (!params)
1110 return -ENOMEM;
1111
1112 drmmode_obj = drm_mode_object_find(dev, put_image_rec->crtc_id,
Chris Wilson722506f2010-08-12 09:28:50 +01001113 DRM_MODE_OBJECT_CRTC);
Dan Carpenter915a4282010-03-06 14:05:39 +03001114 if (!drmmode_obj) {
1115 ret = -ENOENT;
1116 goto out_free;
1117 }
Daniel Vetter02e792f2009-09-15 22:57:34 +02001118 crtc = to_intel_crtc(obj_to_crtc(drmmode_obj));
1119
1120 new_bo = drm_gem_object_lookup(dev, file_priv,
Chris Wilson722506f2010-08-12 09:28:50 +01001121 put_image_rec->bo_handle);
Dan Carpenter915a4282010-03-06 14:05:39 +03001122 if (!new_bo) {
1123 ret = -ENOENT;
1124 goto out_free;
1125 }
Daniel Vetter02e792f2009-09-15 22:57:34 +02001126
1127 mutex_lock(&dev->mode_config.mutex);
1128 mutex_lock(&dev->struct_mutex);
1129
Daniel Vetter03f77ea2009-09-15 22:57:37 +02001130 if (overlay->hw_wedged) {
1131 ret = intel_overlay_recover_from_interrupt(overlay, 1);
1132 if (ret != 0)
1133 goto out_unlock;
1134 }
1135
Daniel Vetter02e792f2009-09-15 22:57:34 +02001136 if (overlay->crtc != crtc) {
1137 struct drm_display_mode *mode = &crtc->base.mode;
1138 ret = intel_overlay_switch_off(overlay);
1139 if (ret != 0)
1140 goto out_unlock;
1141
1142 ret = check_overlay_possible_on_crtc(overlay, crtc);
1143 if (ret != 0)
1144 goto out_unlock;
1145
1146 overlay->crtc = crtc;
1147 crtc->overlay = overlay;
1148
1149 if (intel_panel_fitter_pipe(dev) == crtc->pipe
1150 /* and line to wide, i.e. one-line-mode */
1151 && mode->hdisplay > 1024) {
1152 overlay->pfit_active = 1;
1153 update_pfit_vscale_ratio(overlay);
1154 } else
1155 overlay->pfit_active = 0;
1156 }
1157
1158 ret = check_overlay_dst(overlay, put_image_rec);
1159 if (ret != 0)
1160 goto out_unlock;
1161
1162 if (overlay->pfit_active) {
1163 params->dst_y = ((((u32)put_image_rec->dst_y) << 12) /
Chris Wilson722506f2010-08-12 09:28:50 +01001164 overlay->pfit_vscale_ratio);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001165 /* shifting right rounds downwards, so add 1 */
1166 params->dst_h = ((((u32)put_image_rec->dst_height) << 12) /
Chris Wilson722506f2010-08-12 09:28:50 +01001167 overlay->pfit_vscale_ratio) + 1;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001168 } else {
1169 params->dst_y = put_image_rec->dst_y;
1170 params->dst_h = put_image_rec->dst_height;
1171 }
1172 params->dst_x = put_image_rec->dst_x;
1173 params->dst_w = put_image_rec->dst_width;
1174
1175 params->src_w = put_image_rec->src_width;
1176 params->src_h = put_image_rec->src_height;
1177 params->src_scan_w = put_image_rec->src_scan_width;
1178 params->src_scan_h = put_image_rec->src_scan_height;
Chris Wilson722506f2010-08-12 09:28:50 +01001179 if (params->src_scan_h > params->src_h ||
1180 params->src_scan_w > params->src_w) {
Daniel Vetter02e792f2009-09-15 22:57:34 +02001181 ret = -EINVAL;
1182 goto out_unlock;
1183 }
1184
1185 ret = check_overlay_src(dev, put_image_rec, new_bo);
1186 if (ret != 0)
1187 goto out_unlock;
1188 params->format = put_image_rec->flags & ~I915_OVERLAY_FLAGS_MASK;
1189 params->stride_Y = put_image_rec->stride_Y;
1190 params->stride_UV = put_image_rec->stride_UV;
1191 params->offset_Y = put_image_rec->offset_Y;
1192 params->offset_U = put_image_rec->offset_U;
1193 params->offset_V = put_image_rec->offset_V;
1194
1195 /* Check scaling after src size to prevent a divide-by-zero. */
1196 ret = check_overlay_scaling(params);
1197 if (ret != 0)
1198 goto out_unlock;
1199
1200 ret = intel_overlay_do_put_image(overlay, new_bo, params);
1201 if (ret != 0)
1202 goto out_unlock;
1203
1204 mutex_unlock(&dev->struct_mutex);
1205 mutex_unlock(&dev->mode_config.mutex);
1206
1207 kfree(params);
1208
1209 return 0;
1210
1211out_unlock:
1212 mutex_unlock(&dev->struct_mutex);
1213 mutex_unlock(&dev->mode_config.mutex);
Luca Barbieribc9025b2010-02-09 05:49:12 +00001214 drm_gem_object_unreference_unlocked(new_bo);
Dan Carpenter915a4282010-03-06 14:05:39 +03001215out_free:
Daniel Vetter02e792f2009-09-15 22:57:34 +02001216 kfree(params);
1217
1218 return ret;
1219}
1220
1221static void update_reg_attrs(struct intel_overlay *overlay,
1222 struct overlay_registers *regs)
1223{
1224 regs->OCLRC0 = (overlay->contrast << 18) | (overlay->brightness & 0xff);
1225 regs->OCLRC1 = overlay->saturation;
1226}
1227
1228static bool check_gamma_bounds(u32 gamma1, u32 gamma2)
1229{
1230 int i;
1231
1232 if (gamma1 & 0xff000000 || gamma2 & 0xff000000)
1233 return false;
1234
1235 for (i = 0; i < 3; i++) {
Chris Wilson722506f2010-08-12 09:28:50 +01001236 if (((gamma1 >> i*8) & 0xff) >= ((gamma2 >> i*8) & 0xff))
Daniel Vetter02e792f2009-09-15 22:57:34 +02001237 return false;
1238 }
1239
1240 return true;
1241}
1242
1243static bool check_gamma5_errata(u32 gamma5)
1244{
1245 int i;
1246
1247 for (i = 0; i < 3; i++) {
1248 if (((gamma5 >> i*8) & 0xff) == 0x80)
1249 return false;
1250 }
1251
1252 return true;
1253}
1254
1255static int check_gamma(struct drm_intel_overlay_attrs *attrs)
1256{
Chris Wilson722506f2010-08-12 09:28:50 +01001257 if (!check_gamma_bounds(0, attrs->gamma0) ||
1258 !check_gamma_bounds(attrs->gamma0, attrs->gamma1) ||
1259 !check_gamma_bounds(attrs->gamma1, attrs->gamma2) ||
1260 !check_gamma_bounds(attrs->gamma2, attrs->gamma3) ||
1261 !check_gamma_bounds(attrs->gamma3, attrs->gamma4) ||
1262 !check_gamma_bounds(attrs->gamma4, attrs->gamma5) ||
1263 !check_gamma_bounds(attrs->gamma5, 0x00ffffff))
Daniel Vetter02e792f2009-09-15 22:57:34 +02001264 return -EINVAL;
Chris Wilson722506f2010-08-12 09:28:50 +01001265
Daniel Vetter02e792f2009-09-15 22:57:34 +02001266 if (!check_gamma5_errata(attrs->gamma5))
1267 return -EINVAL;
Chris Wilson722506f2010-08-12 09:28:50 +01001268
Daniel Vetter02e792f2009-09-15 22:57:34 +02001269 return 0;
1270}
1271
1272int intel_overlay_attrs(struct drm_device *dev, void *data,
1273 struct drm_file *file_priv)
1274{
1275 struct drm_intel_overlay_attrs *attrs = data;
1276 drm_i915_private_t *dev_priv = dev->dev_private;
1277 struct intel_overlay *overlay;
1278 struct overlay_registers *regs;
1279 int ret;
1280
1281 if (!dev_priv) {
1282 DRM_ERROR("called with no initialization\n");
1283 return -EINVAL;
1284 }
1285
1286 overlay = dev_priv->overlay;
1287 if (!overlay) {
1288 DRM_DEBUG("userspace bug: no overlay\n");
1289 return -ENODEV;
1290 }
1291
1292 mutex_lock(&dev->mode_config.mutex);
1293 mutex_lock(&dev->struct_mutex);
1294
Chris Wilson60fc3322010-08-12 10:44:45 +01001295 ret = -EINVAL;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001296 if (!(attrs->flags & I915_OVERLAY_UPDATE_ATTRS)) {
Chris Wilson60fc3322010-08-12 10:44:45 +01001297 attrs->color_key = overlay->color_key;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001298 attrs->brightness = overlay->brightness;
Chris Wilson60fc3322010-08-12 10:44:45 +01001299 attrs->contrast = overlay->contrast;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001300 attrs->saturation = overlay->saturation;
1301
1302 if (IS_I9XX(dev)) {
1303 attrs->gamma0 = I915_READ(OGAMC0);
1304 attrs->gamma1 = I915_READ(OGAMC1);
1305 attrs->gamma2 = I915_READ(OGAMC2);
1306 attrs->gamma3 = I915_READ(OGAMC3);
1307 attrs->gamma4 = I915_READ(OGAMC4);
1308 attrs->gamma5 = I915_READ(OGAMC5);
1309 }
Daniel Vetter02e792f2009-09-15 22:57:34 +02001310 } else {
Chris Wilson60fc3322010-08-12 10:44:45 +01001311 if (attrs->brightness < -128 || attrs->brightness > 127)
Daniel Vetter02e792f2009-09-15 22:57:34 +02001312 goto out_unlock;
Chris Wilson60fc3322010-08-12 10:44:45 +01001313 if (attrs->contrast > 255)
1314 goto out_unlock;
1315 if (attrs->saturation > 1023)
1316 goto out_unlock;
Chris Wilson722506f2010-08-12 09:28:50 +01001317
Chris Wilson60fc3322010-08-12 10:44:45 +01001318 overlay->color_key = attrs->color_key;
1319 overlay->brightness = attrs->brightness;
1320 overlay->contrast = attrs->contrast;
1321 overlay->saturation = attrs->saturation;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001322
Chris Wilson8d74f652010-08-12 10:35:26 +01001323 regs = intel_overlay_map_regs(overlay);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001324 if (!regs) {
1325 ret = -ENOMEM;
1326 goto out_unlock;
1327 }
1328
1329 update_reg_attrs(overlay, regs);
1330
Chris Wilson9bb2ff72010-08-12 12:02:11 +01001331 intel_overlay_unmap_regs(overlay, regs);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001332
1333 if (attrs->flags & I915_OVERLAY_UPDATE_GAMMA) {
Chris Wilson60fc3322010-08-12 10:44:45 +01001334 if (!IS_I9XX(dev))
Daniel Vetter02e792f2009-09-15 22:57:34 +02001335 goto out_unlock;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001336
1337 if (overlay->active) {
1338 ret = -EBUSY;
1339 goto out_unlock;
1340 }
1341
1342 ret = check_gamma(attrs);
Chris Wilson60fc3322010-08-12 10:44:45 +01001343 if (ret)
Daniel Vetter02e792f2009-09-15 22:57:34 +02001344 goto out_unlock;
1345
1346 I915_WRITE(OGAMC0, attrs->gamma0);
1347 I915_WRITE(OGAMC1, attrs->gamma1);
1348 I915_WRITE(OGAMC2, attrs->gamma2);
1349 I915_WRITE(OGAMC3, attrs->gamma3);
1350 I915_WRITE(OGAMC4, attrs->gamma4);
1351 I915_WRITE(OGAMC5, attrs->gamma5);
1352 }
Daniel Vetter02e792f2009-09-15 22:57:34 +02001353 }
1354
Chris Wilson60fc3322010-08-12 10:44:45 +01001355 ret = 0;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001356out_unlock:
1357 mutex_unlock(&dev->struct_mutex);
1358 mutex_unlock(&dev->mode_config.mutex);
1359
1360 return ret;
1361}
1362
1363void intel_setup_overlay(struct drm_device *dev)
1364{
1365 drm_i915_private_t *dev_priv = dev->dev_private;
1366 struct intel_overlay *overlay;
1367 struct drm_gem_object *reg_bo;
1368 struct overlay_registers *regs;
1369 int ret;
1370
Chris Wilson315781482010-08-12 09:42:51 +01001371 if (!HAS_OVERLAY(dev))
Daniel Vetter02e792f2009-09-15 22:57:34 +02001372 return;
1373
1374 overlay = kzalloc(sizeof(struct intel_overlay), GFP_KERNEL);
1375 if (!overlay)
1376 return;
1377 overlay->dev = dev;
1378
Daniel Vetterac52bc52010-04-09 19:05:06 +00001379 reg_bo = i915_gem_alloc_object(dev, PAGE_SIZE);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001380 if (!reg_bo)
1381 goto out_free;
Daniel Vetter23010e42010-03-08 13:35:02 +01001382 overlay->reg_bo = to_intel_bo(reg_bo);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001383
Chris Wilson315781482010-08-12 09:42:51 +01001384 if (OVERLAY_NEEDS_PHYSICAL(dev)) {
1385 ret = i915_gem_attach_phys_object(dev, reg_bo,
1386 I915_GEM_PHYS_OVERLAY_REGS,
Chris Wilsona2930122010-08-12 10:47:56 +01001387 PAGE_SIZE);
Chris Wilson315781482010-08-12 09:42:51 +01001388 if (ret) {
1389 DRM_ERROR("failed to attach phys overlay regs\n");
1390 goto out_free_bo;
1391 }
1392 overlay->flip_addr = overlay->reg_bo->phys_obj->handle->busaddr;
1393 } else {
Daniel Vetter02e792f2009-09-15 22:57:34 +02001394 ret = i915_gem_object_pin(reg_bo, PAGE_SIZE);
1395 if (ret) {
1396 DRM_ERROR("failed to pin overlay register bo\n");
1397 goto out_free_bo;
1398 }
1399 overlay->flip_addr = overlay->reg_bo->gtt_offset;
Chris Wilson0ddc1282010-08-12 09:35:00 +01001400
1401 ret = i915_gem_object_set_to_gtt_domain(reg_bo, true);
1402 if (ret) {
1403 DRM_ERROR("failed to move overlay register bo into the GTT\n");
1404 goto out_unpin_bo;
1405 }
Daniel Vetter02e792f2009-09-15 22:57:34 +02001406 }
1407
1408 /* init all values */
1409 overlay->color_key = 0x0101fe;
1410 overlay->brightness = -19;
1411 overlay->contrast = 75;
1412 overlay->saturation = 146;
1413
Chris Wilson8d74f652010-08-12 10:35:26 +01001414 regs = intel_overlay_map_regs(overlay);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001415 if (!regs)
1416 goto out_free_bo;
1417
1418 memset(regs, 0, sizeof(struct overlay_registers));
1419 update_polyphase_filter(regs);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001420 update_reg_attrs(overlay, regs);
1421
Chris Wilson9bb2ff72010-08-12 12:02:11 +01001422 intel_overlay_unmap_regs(overlay, regs);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001423
1424 dev_priv->overlay = overlay;
1425 DRM_INFO("initialized overlay support\n");
1426 return;
1427
Chris Wilson0ddc1282010-08-12 09:35:00 +01001428out_unpin_bo:
1429 i915_gem_object_unpin(reg_bo);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001430out_free_bo:
1431 drm_gem_object_unreference(reg_bo);
1432out_free:
1433 kfree(overlay);
1434 return;
1435}
1436
1437void intel_cleanup_overlay(struct drm_device *dev)
1438{
Chris Wilson722506f2010-08-12 09:28:50 +01001439 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001440
Chris Wilson62cf4e62010-08-12 10:50:36 +01001441 if (!dev_priv->overlay)
1442 return;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001443
Chris Wilson62cf4e62010-08-12 10:50:36 +01001444 /* The bo's should be free'd by the generic code already.
1445 * Furthermore modesetting teardown happens beforehand so the
1446 * hardware should be off already */
1447 BUG_ON(dev_priv->overlay->active);
1448
1449 drm_gem_object_unreference_unlocked(&dev_priv->overlay->reg_bo->base);
1450 kfree(dev_priv->overlay);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001451}
Chris Wilson6ef3d422010-08-04 20:26:07 +01001452
1453struct intel_overlay_error_state {
1454 struct overlay_registers regs;
1455 unsigned long base;
1456 u32 dovsta;
1457 u32 isr;
1458};
1459
1460struct intel_overlay_error_state *
1461intel_overlay_capture_error_state(struct drm_device *dev)
1462{
1463 drm_i915_private_t *dev_priv = dev->dev_private;
1464 struct intel_overlay *overlay = dev_priv->overlay;
1465 struct intel_overlay_error_state *error;
1466 struct overlay_registers __iomem *regs;
1467
1468 if (!overlay || !overlay->active)
1469 return NULL;
1470
1471 error = kmalloc(sizeof(*error), GFP_ATOMIC);
1472 if (error == NULL)
1473 return NULL;
1474
1475 error->dovsta = I915_READ(DOVSTA);
1476 error->isr = I915_READ(ISR);
Chris Wilson315781482010-08-12 09:42:51 +01001477 if (OVERLAY_NEEDS_PHYSICAL(overlay->dev))
Chris Wilson6ef3d422010-08-04 20:26:07 +01001478 error->base = (long) overlay->reg_bo->phys_obj->handle->vaddr;
Chris Wilson315781482010-08-12 09:42:51 +01001479 else
1480 error->base = (long) overlay->reg_bo->gtt_offset;
Chris Wilson6ef3d422010-08-04 20:26:07 +01001481
Chris Wilson8d74f652010-08-12 10:35:26 +01001482 regs = intel_overlay_map_regs_atomic(overlay, KM_IRQ0);
Chris Wilson6ef3d422010-08-04 20:26:07 +01001483 if (!regs)
1484 goto err;
1485
1486 memcpy_fromio(&error->regs, regs, sizeof(struct overlay_registers));
Chris Wilson9bb2ff72010-08-12 12:02:11 +01001487 intel_overlay_unmap_regs_atomic(overlay, KM_IRQ0, regs);
Chris Wilson6ef3d422010-08-04 20:26:07 +01001488
1489 return error;
1490
1491err:
1492 kfree(error);
1493 return NULL;
1494}
1495
1496void
1497intel_overlay_print_error_state(struct seq_file *m, struct intel_overlay_error_state *error)
1498{
1499 seq_printf(m, "Overlay, status: 0x%08x, interrupt: 0x%08x\n",
1500 error->dovsta, error->isr);
1501 seq_printf(m, " Register file at 0x%08lx:\n",
1502 error->base);
1503
1504#define P(x) seq_printf(m, " " #x ": 0x%08x\n", error->regs.x)
1505 P(OBUF_0Y);
1506 P(OBUF_1Y);
1507 P(OBUF_0U);
1508 P(OBUF_0V);
1509 P(OBUF_1U);
1510 P(OBUF_1V);
1511 P(OSTRIDE);
1512 P(YRGB_VPH);
1513 P(UV_VPH);
1514 P(HORZ_PH);
1515 P(INIT_PHS);
1516 P(DWINPOS);
1517 P(DWINSZ);
1518 P(SWIDTH);
1519 P(SWIDTHSW);
1520 P(SHEIGHT);
1521 P(YRGBSCALE);
1522 P(UVSCALE);
1523 P(OCLRC0);
1524 P(OCLRC1);
1525 P(DCLRKV);
1526 P(DCLRKM);
1527 P(SCLRKVH);
1528 P(SCLRKVL);
1529 P(SCLRKEN);
1530 P(OCONFIG);
1531 P(OCMD);
1532 P(OSTART_0Y);
1533 P(OSTART_1Y);
1534 P(OSTART_0U);
1535 P(OSTART_0V);
1536 P(OSTART_1U);
1537 P(OSTART_1V);
1538 P(OTILEOFF_0Y);
1539 P(OTILEOFF_1Y);
1540 P(OTILEOFF_0U);
1541 P(OTILEOFF_0V);
1542 P(OTILEOFF_1U);
1543 P(OTILEOFF_1V);
1544 P(FASTHSCALE);
1545 P(UVSCALEV);
1546#undef P
1547}