blob: 3533355c5ea342b8cd278652e76d3a815488ff5c [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 */
Chris Wilson5dcdbcb2010-08-12 13:50:28 +0100368static int intel_overlay_off(struct intel_overlay *overlay,
369 bool interruptible)
Daniel Vetter02e792f2009-09-15 22:57:34 +0200370{
Daniel Vetter02e792f2009-09-15 22:57:34 +0200371 struct drm_device *dev = overlay->dev;
Chris Wilson8dc5d142010-08-12 12:36:12 +0100372 u32 flip_addr = overlay->flip_addr;
373 struct drm_i915_gem_request *request;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200374
375 BUG_ON(!overlay->active);
376
Chris Wilson8dc5d142010-08-12 12:36:12 +0100377 request = kzalloc(sizeof(*request), GFP_KERNEL);
378 if (request == NULL)
379 return -ENOMEM;
380
Daniel Vetter02e792f2009-09-15 22:57:34 +0200381 /* According to intel docs the overlay hw may hang (when switching
382 * off) without loading the filter coeffs. It is however unclear whether
383 * this applies to the disabling of the overlay or to the switching off
384 * of the hw. Do it in both cases */
385 flip_addr |= OFC_UPDATE;
386
Chris Wilson8dfbc342010-08-12 12:07:32 +0100387 BEGIN_LP_RING(6);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200388 /* wait for overlay to go idle */
Daniel Vetter02e792f2009-09-15 22:57:34 +0200389 OUT_RING(MI_OVERLAY_FLIP | MI_OVERLAY_CONTINUE);
390 OUT_RING(flip_addr);
Chris Wilson722506f2010-08-12 09:28:50 +0100391 OUT_RING(MI_WAIT_FOR_EVENT | MI_WAIT_FOR_OVERLAY_FLIP);
Chris Wilson722506f2010-08-12 09:28:50 +0100392 /* turn overlay off */
Chris Wilson722506f2010-08-12 09:28:50 +0100393 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);
Chris Wilson722506f2010-08-12 09:28:50 +0100396 ADVANCE_LP_RING();
397
Chris Wilson5dcdbcb2010-08-12 13:50:28 +0100398 return intel_overlay_do_wait_request(overlay, request, interruptible,
Chris Wilson8dc5d142010-08-12 12:36:12 +0100399 SWITCH_OFF);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200400}
401
Chris Wilson5cd68c92010-08-12 12:21:54 +0100402static void intel_overlay_release_old_vid_tail(struct intel_overlay *overlay)
403{
404 struct drm_gem_object *obj = &overlay->old_vid_bo->base;
405
406 i915_gem_object_unpin(obj);
407 drm_gem_object_unreference(obj);
408
409 overlay->old_vid_bo = NULL;
410}
411
Daniel Vetter12ca45f2037-04-25 10:08:26 +0200412static void intel_overlay_off_tail(struct intel_overlay *overlay)
413{
414 struct drm_gem_object *obj;
415
416 /* never have the overlay hw on without showing a frame */
417 BUG_ON(!overlay->vid_bo);
Daniel Vettera8089e82010-04-09 19:05:09 +0000418 obj = &overlay->vid_bo->base;
Daniel Vetter12ca45f2037-04-25 10:08:26 +0200419
420 i915_gem_object_unpin(obj);
421 drm_gem_object_unreference(obj);
422 overlay->vid_bo = NULL;
423
424 overlay->crtc->overlay = NULL;
425 overlay->crtc = NULL;
426 overlay->active = 0;
427}
428
Daniel Vetter03f77ea2009-09-15 22:57:37 +0200429/* recover from an interruption due to a signal
430 * We have to be careful not to repeat work forever an make forward progess. */
Chris Wilson5dcdbcb2010-08-12 13:50:28 +0100431static int intel_overlay_recover_from_interrupt(struct intel_overlay *overlay,
432 bool interruptible)
Daniel Vetter03f77ea2009-09-15 22:57:37 +0200433{
434 struct drm_device *dev = overlay->dev;
Zou Nan hai852835f2010-05-21 09:08:56 +0800435 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter03f77ea2009-09-15 22:57:37 +0200436 int ret;
Daniel Vetter03f77ea2009-09-15 22:57:37 +0200437
438 if (overlay->hw_wedged == HW_WEDGED)
439 return -EIO;
440
Zou Nan hai852835f2010-05-21 09:08:56 +0800441 ret = i915_do_wait_request(dev, overlay->last_flip_req,
Chris Wilson722506f2010-08-12 09:28:50 +0100442 interruptible, &dev_priv->render_ring);
Chris Wilsonb6c028e2010-08-12 11:55:08 +0100443 if (ret)
Daniel Vetter03f77ea2009-09-15 22:57:37 +0200444 return ret;
445
446 switch (overlay->hw_wedged) {
Chris Wilson722506f2010-08-12 09:28:50 +0100447 case RELEASE_OLD_VID:
Chris Wilson5cd68c92010-08-12 12:21:54 +0100448 intel_overlay_release_old_vid_tail(overlay);
Chris Wilson722506f2010-08-12 09:28:50 +0100449 break;
Chris Wilsonb6c028e2010-08-12 11:55:08 +0100450
Chris Wilson8dfbc342010-08-12 12:07:32 +0100451 case SWITCH_OFF:
Chris Wilson722506f2010-08-12 09:28:50 +0100452 intel_overlay_off_tail(overlay);
453 break;
Chris Wilson8dfbc342010-08-12 12:07:32 +0100454
Chris Wilson722506f2010-08-12 09:28:50 +0100455 default:
456 BUG_ON(overlay->hw_wedged != NEEDS_WAIT_FOR_FLIP);
Daniel Vetter03f77ea2009-09-15 22:57:37 +0200457 }
458
459 overlay->hw_wedged = 0;
460 overlay->last_flip_req = 0;
461 return 0;
462}
463
Daniel Vetter5a5a0c62009-09-15 22:57:36 +0200464/* Wait for pending overlay flip and release old frame.
465 * Needs to be called before the overlay register are changed
Chris Wilson8d74f652010-08-12 10:35:26 +0100466 * via intel_overlay_(un)map_regs
467 */
Daniel Vetter02e792f2009-09-15 22:57:34 +0200468static int intel_overlay_release_old_vid(struct intel_overlay *overlay)
469{
Chris Wilson5cd68c92010-08-12 12:21:54 +0100470 struct drm_device *dev = overlay->dev;
471 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200472 int ret;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200473
Chris Wilson5cd68c92010-08-12 12:21:54 +0100474 /* Only wait if there is actually an old frame to release to
475 * guarantee forward progress.
476 */
Daniel Vetter03f77ea2009-09-15 22:57:37 +0200477 if (!overlay->old_vid_bo)
478 return 0;
479
Chris Wilson5cd68c92010-08-12 12:21:54 +0100480 if (I915_READ(ISR) & I915_OVERLAY_PLANE_FLIP_PENDING_INTERRUPT) {
Chris Wilson8dc5d142010-08-12 12:36:12 +0100481 struct drm_i915_gem_request *request;
482
Chris Wilson5cd68c92010-08-12 12:21:54 +0100483 /* synchronous slowpath */
Chris Wilson8dc5d142010-08-12 12:36:12 +0100484 request = kzalloc(sizeof(*request), GFP_KERNEL);
485 if (request == NULL)
486 return -ENOMEM;
487
Chris Wilson5cd68c92010-08-12 12:21:54 +0100488 BEGIN_LP_RING(2);
489 OUT_RING(MI_WAIT_FOR_EVENT | MI_WAIT_FOR_OVERLAY_FLIP);
490 OUT_RING(MI_NOOP);
491 ADVANCE_LP_RING();
Daniel Vetter02e792f2009-09-15 22:57:34 +0200492
Chris Wilson8dc5d142010-08-12 12:36:12 +0100493 ret = intel_overlay_do_wait_request(overlay, request, true,
Chris Wilson5cd68c92010-08-12 12:21:54 +0100494 RELEASE_OLD_VID);
495 if (ret)
496 return ret;
497 }
Daniel Vetter02e792f2009-09-15 22:57:34 +0200498
Chris Wilson5cd68c92010-08-12 12:21:54 +0100499 intel_overlay_release_old_vid_tail(overlay);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200500 return 0;
501}
502
503struct put_image_params {
504 int format;
505 short dst_x;
506 short dst_y;
507 short dst_w;
508 short dst_h;
509 short src_w;
510 short src_scan_h;
511 short src_scan_w;
512 short src_h;
513 short stride_Y;
514 short stride_UV;
515 int offset_Y;
516 int offset_U;
517 int offset_V;
518};
519
520static int packed_depth_bytes(u32 format)
521{
522 switch (format & I915_OVERLAY_DEPTH_MASK) {
Chris Wilson722506f2010-08-12 09:28:50 +0100523 case I915_OVERLAY_YUV422:
524 return 4;
525 case I915_OVERLAY_YUV411:
526 /* return 6; not implemented */
527 default:
528 return -EINVAL;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200529 }
530}
531
532static int packed_width_bytes(u32 format, short width)
533{
534 switch (format & I915_OVERLAY_DEPTH_MASK) {
Chris Wilson722506f2010-08-12 09:28:50 +0100535 case I915_OVERLAY_YUV422:
536 return width << 1;
537 default:
538 return -EINVAL;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200539 }
540}
541
542static int uv_hsubsampling(u32 format)
543{
544 switch (format & I915_OVERLAY_DEPTH_MASK) {
Chris Wilson722506f2010-08-12 09:28:50 +0100545 case I915_OVERLAY_YUV422:
546 case I915_OVERLAY_YUV420:
547 return 2;
548 case I915_OVERLAY_YUV411:
549 case I915_OVERLAY_YUV410:
550 return 4;
551 default:
552 return -EINVAL;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200553 }
554}
555
556static int uv_vsubsampling(u32 format)
557{
558 switch (format & I915_OVERLAY_DEPTH_MASK) {
Chris Wilson722506f2010-08-12 09:28:50 +0100559 case I915_OVERLAY_YUV420:
560 case I915_OVERLAY_YUV410:
561 return 2;
562 case I915_OVERLAY_YUV422:
563 case I915_OVERLAY_YUV411:
564 return 1;
565 default:
566 return -EINVAL;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200567 }
568}
569
570static u32 calc_swidthsw(struct drm_device *dev, u32 offset, u32 width)
571{
572 u32 mask, shift, ret;
573 if (IS_I9XX(dev)) {
574 mask = 0x3f;
575 shift = 6;
576 } else {
577 mask = 0x1f;
578 shift = 5;
579 }
580 ret = ((offset + width + mask) >> shift) - (offset >> shift);
581 if (IS_I9XX(dev))
582 ret <<= 1;
583 ret -=1;
584 return ret << 2;
585}
586
587static const u16 y_static_hcoeffs[N_HORIZ_Y_TAPS * N_PHASES] = {
588 0x3000, 0xb4a0, 0x1930, 0x1920, 0xb4a0,
589 0x3000, 0xb500, 0x19d0, 0x1880, 0xb440,
590 0x3000, 0xb540, 0x1a88, 0x2f80, 0xb3e0,
591 0x3000, 0xb580, 0x1b30, 0x2e20, 0xb380,
592 0x3000, 0xb5c0, 0x1bd8, 0x2cc0, 0xb320,
593 0x3020, 0xb5e0, 0x1c60, 0x2b80, 0xb2c0,
594 0x3020, 0xb5e0, 0x1cf8, 0x2a20, 0xb260,
595 0x3020, 0xb5e0, 0x1d80, 0x28e0, 0xb200,
596 0x3020, 0xb5c0, 0x1e08, 0x3f40, 0xb1c0,
597 0x3020, 0xb580, 0x1e78, 0x3ce0, 0xb160,
598 0x3040, 0xb520, 0x1ed8, 0x3aa0, 0xb120,
599 0x3040, 0xb4a0, 0x1f30, 0x3880, 0xb0e0,
600 0x3040, 0xb400, 0x1f78, 0x3680, 0xb0a0,
601 0x3020, 0xb340, 0x1fb8, 0x34a0, 0xb060,
602 0x3020, 0xb240, 0x1fe0, 0x32e0, 0xb040,
603 0x3020, 0xb140, 0x1ff8, 0x3160, 0xb020,
Chris Wilson722506f2010-08-12 09:28:50 +0100604 0xb000, 0x3000, 0x0800, 0x3000, 0xb000
605};
606
Daniel Vetter02e792f2009-09-15 22:57:34 +0200607static const u16 uv_static_hcoeffs[N_HORIZ_UV_TAPS * N_PHASES] = {
608 0x3000, 0x1800, 0x1800, 0xb000, 0x18d0, 0x2e60,
609 0xb000, 0x1990, 0x2ce0, 0xb020, 0x1a68, 0x2b40,
610 0xb040, 0x1b20, 0x29e0, 0xb060, 0x1bd8, 0x2880,
611 0xb080, 0x1c88, 0x3e60, 0xb0a0, 0x1d28, 0x3c00,
612 0xb0c0, 0x1db8, 0x39e0, 0xb0e0, 0x1e40, 0x37e0,
613 0xb100, 0x1eb8, 0x3620, 0xb100, 0x1f18, 0x34a0,
614 0xb100, 0x1f68, 0x3360, 0xb0e0, 0x1fa8, 0x3240,
615 0xb0c0, 0x1fe0, 0x3140, 0xb060, 0x1ff0, 0x30a0,
Chris Wilson722506f2010-08-12 09:28:50 +0100616 0x3000, 0x0800, 0x3000
617};
Daniel Vetter02e792f2009-09-15 22:57:34 +0200618
619static void update_polyphase_filter(struct overlay_registers *regs)
620{
621 memcpy(regs->Y_HCOEFS, y_static_hcoeffs, sizeof(y_static_hcoeffs));
622 memcpy(regs->UV_HCOEFS, uv_static_hcoeffs, sizeof(uv_static_hcoeffs));
623}
624
625static bool update_scaling_factors(struct intel_overlay *overlay,
626 struct overlay_registers *regs,
627 struct put_image_params *params)
628{
629 /* fixed point with a 12 bit shift */
630 u32 xscale, yscale, xscale_UV, yscale_UV;
631#define FP_SHIFT 12
632#define FRACT_MASK 0xfff
633 bool scale_changed = false;
634 int uv_hscale = uv_hsubsampling(params->format);
635 int uv_vscale = uv_vsubsampling(params->format);
636
637 if (params->dst_w > 1)
638 xscale = ((params->src_scan_w - 1) << FP_SHIFT)
639 /(params->dst_w);
640 else
641 xscale = 1 << FP_SHIFT;
642
643 if (params->dst_h > 1)
644 yscale = ((params->src_scan_h - 1) << FP_SHIFT)
645 /(params->dst_h);
646 else
647 yscale = 1 << FP_SHIFT;
648
649 /*if (params->format & I915_OVERLAY_YUV_PLANAR) {*/
Chris Wilson722506f2010-08-12 09:28:50 +0100650 xscale_UV = xscale/uv_hscale;
651 yscale_UV = yscale/uv_vscale;
652 /* make the Y scale to UV scale ratio an exact multiply */
653 xscale = xscale_UV * uv_hscale;
654 yscale = yscale_UV * uv_vscale;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200655 /*} else {
Chris Wilson722506f2010-08-12 09:28:50 +0100656 xscale_UV = 0;
657 yscale_UV = 0;
658 }*/
Daniel Vetter02e792f2009-09-15 22:57:34 +0200659
660 if (xscale != overlay->old_xscale || yscale != overlay->old_yscale)
661 scale_changed = true;
662 overlay->old_xscale = xscale;
663 overlay->old_yscale = yscale;
664
Chris Wilson722506f2010-08-12 09:28:50 +0100665 regs->YRGBSCALE = (((yscale & FRACT_MASK) << 20) |
666 ((xscale >> FP_SHIFT) << 16) |
667 ((xscale & FRACT_MASK) << 3));
668
669 regs->UVSCALE = (((yscale_UV & FRACT_MASK) << 20) |
670 ((xscale_UV >> FP_SHIFT) << 16) |
671 ((xscale_UV & FRACT_MASK) << 3));
672
673 regs->UVSCALEV = ((((yscale >> FP_SHIFT) << 16) |
674 ((yscale_UV >> FP_SHIFT) << 0)));
Daniel Vetter02e792f2009-09-15 22:57:34 +0200675
676 if (scale_changed)
677 update_polyphase_filter(regs);
678
679 return scale_changed;
680}
681
682static void update_colorkey(struct intel_overlay *overlay,
683 struct overlay_registers *regs)
684{
685 u32 key = overlay->color_key;
Chris Wilson6ba3ddd2010-08-12 09:30:58 +0100686
Daniel Vetter02e792f2009-09-15 22:57:34 +0200687 switch (overlay->crtc->base.fb->bits_per_pixel) {
Chris Wilson722506f2010-08-12 09:28:50 +0100688 case 8:
689 regs->DCLRKV = 0;
690 regs->DCLRKM = CLK_RGB8I_MASK | DST_KEY_ENABLE;
Chris Wilson6ba3ddd2010-08-12 09:30:58 +0100691 break;
692
Chris Wilson722506f2010-08-12 09:28:50 +0100693 case 16:
694 if (overlay->crtc->base.fb->depth == 15) {
695 regs->DCLRKV = RGB15_TO_COLORKEY(key);
696 regs->DCLRKM = CLK_RGB15_MASK | DST_KEY_ENABLE;
697 } else {
698 regs->DCLRKV = RGB16_TO_COLORKEY(key);
699 regs->DCLRKM = CLK_RGB16_MASK | DST_KEY_ENABLE;
700 }
Chris Wilson6ba3ddd2010-08-12 09:30:58 +0100701 break;
702
Chris Wilson722506f2010-08-12 09:28:50 +0100703 case 24:
704 case 32:
705 regs->DCLRKV = key;
706 regs->DCLRKM = CLK_RGB24_MASK | DST_KEY_ENABLE;
Chris Wilson6ba3ddd2010-08-12 09:30:58 +0100707 break;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200708 }
709}
710
711static u32 overlay_cmd_reg(struct put_image_params *params)
712{
713 u32 cmd = OCMD_ENABLE | OCMD_BUF_TYPE_FRAME | OCMD_BUFFER0;
714
715 if (params->format & I915_OVERLAY_YUV_PLANAR) {
716 switch (params->format & I915_OVERLAY_DEPTH_MASK) {
Chris Wilson722506f2010-08-12 09:28:50 +0100717 case I915_OVERLAY_YUV422:
718 cmd |= OCMD_YUV_422_PLANAR;
719 break;
720 case I915_OVERLAY_YUV420:
721 cmd |= OCMD_YUV_420_PLANAR;
722 break;
723 case I915_OVERLAY_YUV411:
724 case I915_OVERLAY_YUV410:
725 cmd |= OCMD_YUV_410_PLANAR;
726 break;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200727 }
728 } else { /* YUV packed */
729 switch (params->format & I915_OVERLAY_DEPTH_MASK) {
Chris Wilson722506f2010-08-12 09:28:50 +0100730 case I915_OVERLAY_YUV422:
731 cmd |= OCMD_YUV_422_PACKED;
732 break;
733 case I915_OVERLAY_YUV411:
734 cmd |= OCMD_YUV_411_PACKED;
735 break;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200736 }
737
738 switch (params->format & I915_OVERLAY_SWAP_MASK) {
Chris Wilson722506f2010-08-12 09:28:50 +0100739 case I915_OVERLAY_NO_SWAP:
740 break;
741 case I915_OVERLAY_UV_SWAP:
742 cmd |= OCMD_UV_SWAP;
743 break;
744 case I915_OVERLAY_Y_SWAP:
745 cmd |= OCMD_Y_SWAP;
746 break;
747 case I915_OVERLAY_Y_AND_UV_SWAP:
748 cmd |= OCMD_Y_AND_UV_SWAP;
749 break;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200750 }
751 }
752
753 return cmd;
754}
755
Chris Wilson5fe82c52010-08-12 12:38:21 +0100756static int intel_overlay_do_put_image(struct intel_overlay *overlay,
757 struct drm_gem_object *new_bo,
758 struct put_image_params *params)
Daniel Vetter02e792f2009-09-15 22:57:34 +0200759{
760 int ret, tmp_width;
761 struct overlay_registers *regs;
762 bool scale_changed = false;
Daniel Vetter23010e42010-03-08 13:35:02 +0100763 struct drm_i915_gem_object *bo_priv = to_intel_bo(new_bo);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200764 struct drm_device *dev = overlay->dev;
765
766 BUG_ON(!mutex_is_locked(&dev->struct_mutex));
767 BUG_ON(!mutex_is_locked(&dev->mode_config.mutex));
768 BUG_ON(!overlay);
769
Daniel Vetter02e792f2009-09-15 22:57:34 +0200770 ret = intel_overlay_release_old_vid(overlay);
771 if (ret != 0)
772 return ret;
773
774 ret = i915_gem_object_pin(new_bo, PAGE_SIZE);
775 if (ret != 0)
776 return ret;
777
778 ret = i915_gem_object_set_to_gtt_domain(new_bo, 0);
779 if (ret != 0)
780 goto out_unpin;
781
782 if (!overlay->active) {
Chris Wilson8d74f652010-08-12 10:35:26 +0100783 regs = intel_overlay_map_regs(overlay);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200784 if (!regs) {
785 ret = -ENOMEM;
786 goto out_unpin;
787 }
788 regs->OCONFIG = OCONF_CC_OUT_8BIT;
789 if (IS_I965GM(overlay->dev))
790 regs->OCONFIG |= OCONF_CSC_MODE_BT709;
791 regs->OCONFIG |= overlay->crtc->pipe == 0 ?
792 OCONF_PIPE_A : OCONF_PIPE_B;
Chris Wilson9bb2ff72010-08-12 12:02:11 +0100793 intel_overlay_unmap_regs(overlay, regs);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200794
795 ret = intel_overlay_on(overlay);
796 if (ret != 0)
797 goto out_unpin;
798 }
799
Chris Wilson8d74f652010-08-12 10:35:26 +0100800 regs = intel_overlay_map_regs(overlay);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200801 if (!regs) {
802 ret = -ENOMEM;
803 goto out_unpin;
804 }
805
806 regs->DWINPOS = (params->dst_y << 16) | params->dst_x;
807 regs->DWINSZ = (params->dst_h << 16) | params->dst_w;
808
809 if (params->format & I915_OVERLAY_YUV_PACKED)
810 tmp_width = packed_width_bytes(params->format, params->src_w);
811 else
812 tmp_width = params->src_w;
813
814 regs->SWIDTH = params->src_w;
815 regs->SWIDTHSW = calc_swidthsw(overlay->dev,
Chris Wilson722506f2010-08-12 09:28:50 +0100816 params->offset_Y, tmp_width);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200817 regs->SHEIGHT = params->src_h;
818 regs->OBUF_0Y = bo_priv->gtt_offset + params-> offset_Y;
819 regs->OSTRIDE = params->stride_Y;
820
821 if (params->format & I915_OVERLAY_YUV_PLANAR) {
822 int uv_hscale = uv_hsubsampling(params->format);
823 int uv_vscale = uv_vsubsampling(params->format);
824 u32 tmp_U, tmp_V;
825 regs->SWIDTH |= (params->src_w/uv_hscale) << 16;
826 tmp_U = calc_swidthsw(overlay->dev, params->offset_U,
Chris Wilson722506f2010-08-12 09:28:50 +0100827 params->src_w/uv_hscale);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200828 tmp_V = calc_swidthsw(overlay->dev, params->offset_V,
Chris Wilson722506f2010-08-12 09:28:50 +0100829 params->src_w/uv_hscale);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200830 regs->SWIDTHSW |= max_t(u32, tmp_U, tmp_V) << 16;
831 regs->SHEIGHT |= (params->src_h/uv_vscale) << 16;
832 regs->OBUF_0U = bo_priv->gtt_offset + params->offset_U;
833 regs->OBUF_0V = bo_priv->gtt_offset + params->offset_V;
834 regs->OSTRIDE |= params->stride_UV << 16;
835 }
836
837 scale_changed = update_scaling_factors(overlay, regs, params);
838
839 update_colorkey(overlay, regs);
840
841 regs->OCMD = overlay_cmd_reg(params);
842
Chris Wilson9bb2ff72010-08-12 12:02:11 +0100843 intel_overlay_unmap_regs(overlay, regs);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200844
Chris Wilson8dc5d142010-08-12 12:36:12 +0100845 ret = intel_overlay_continue(overlay, scale_changed);
846 if (ret)
847 goto out_unpin;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200848
849 overlay->old_vid_bo = overlay->vid_bo;
Daniel Vetter23010e42010-03-08 13:35:02 +0100850 overlay->vid_bo = to_intel_bo(new_bo);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200851
852 return 0;
853
854out_unpin:
855 i915_gem_object_unpin(new_bo);
856 return ret;
857}
858
Chris Wilson5dcdbcb2010-08-12 13:50:28 +0100859int intel_overlay_switch_off(struct intel_overlay *overlay,
860 bool interruptible)
Daniel Vetter02e792f2009-09-15 22:57:34 +0200861{
Daniel Vetter02e792f2009-09-15 22:57:34 +0200862 struct overlay_registers *regs;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200863 struct drm_device *dev = overlay->dev;
Chris Wilson5dcdbcb2010-08-12 13:50:28 +0100864 int ret;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200865
866 BUG_ON(!mutex_is_locked(&dev->struct_mutex));
867 BUG_ON(!mutex_is_locked(&dev->mode_config.mutex));
868
Daniel Vetter9bedb972009-11-30 15:55:49 +0100869 if (overlay->hw_wedged) {
Chris Wilson5dcdbcb2010-08-12 13:50:28 +0100870 ret = intel_overlay_recover_from_interrupt(overlay,
871 interruptible);
Daniel Vetter9bedb972009-11-30 15:55:49 +0100872 if (ret != 0)
873 return ret;
874 }
875
Daniel Vetter02e792f2009-09-15 22:57:34 +0200876 if (!overlay->active)
877 return 0;
878
Daniel Vetter02e792f2009-09-15 22:57:34 +0200879 ret = intel_overlay_release_old_vid(overlay);
880 if (ret != 0)
881 return ret;
882
Chris Wilson8d74f652010-08-12 10:35:26 +0100883 regs = intel_overlay_map_regs(overlay);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200884 regs->OCMD = 0;
Chris Wilson9bb2ff72010-08-12 12:02:11 +0100885 intel_overlay_unmap_regs(overlay, regs);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200886
Chris Wilson5dcdbcb2010-08-12 13:50:28 +0100887 ret = intel_overlay_off(overlay, interruptible);
Daniel Vetter03f77ea2009-09-15 22:57:37 +0200888 if (ret != 0)
889 return ret;
890
Daniel Vetter12ca45f2037-04-25 10:08:26 +0200891 intel_overlay_off_tail(overlay);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200892
893 return 0;
894}
895
896static int check_overlay_possible_on_crtc(struct intel_overlay *overlay,
897 struct intel_crtc *crtc)
898{
Chris Wilson722506f2010-08-12 09:28:50 +0100899 drm_i915_private_t *dev_priv = overlay->dev->dev_private;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200900 u32 pipeconf;
901 int pipeconf_reg = (crtc->pipe == 0) ? PIPEACONF : PIPEBCONF;
902
903 if (!crtc->base.enabled || crtc->dpms_mode != DRM_MODE_DPMS_ON)
904 return -EINVAL;
905
906 pipeconf = I915_READ(pipeconf_reg);
907
908 /* can't use the overlay with double wide pipe */
909 if (!IS_I965G(overlay->dev) && pipeconf & PIPEACONF_DOUBLE_WIDE)
910 return -EINVAL;
911
912 return 0;
913}
914
915static void update_pfit_vscale_ratio(struct intel_overlay *overlay)
916{
917 struct drm_device *dev = overlay->dev;
Chris Wilson722506f2010-08-12 09:28:50 +0100918 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200919 u32 pfit_control = I915_READ(PFIT_CONTROL);
Chris Wilson446d2182010-08-12 11:15:58 +0100920 u32 ratio;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200921
922 /* XXX: This is not the same logic as in the xorg driver, but more in
Chris Wilson446d2182010-08-12 11:15:58 +0100923 * line with the intel documentation for the i965
924 */
925 if (!IS_I965G(dev)) {
926 if (pfit_control & VERT_AUTO_SCALE)
927 ratio = I915_READ(PFIT_AUTO_RATIOS);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200928 else
Chris Wilson446d2182010-08-12 11:15:58 +0100929 ratio = I915_READ(PFIT_PGM_RATIOS);
930 ratio >>= PFIT_VERT_SCALE_SHIFT;
931 } else { /* on i965 use the PGM reg to read out the autoscaler values */
932 ratio = I915_READ(PFIT_PGM_RATIOS) >> PFIT_VERT_SCALE_SHIFT_965;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200933 }
934
935 overlay->pfit_vscale_ratio = ratio;
936}
937
938static int check_overlay_dst(struct intel_overlay *overlay,
939 struct drm_intel_overlay_put_image *rec)
940{
941 struct drm_display_mode *mode = &overlay->crtc->base.mode;
942
Chris Wilson722506f2010-08-12 09:28:50 +0100943 if (rec->dst_x < mode->crtc_hdisplay &&
944 rec->dst_x + rec->dst_width <= mode->crtc_hdisplay &&
945 rec->dst_y < mode->crtc_vdisplay &&
946 rec->dst_y + rec->dst_height <= mode->crtc_vdisplay)
Daniel Vetter02e792f2009-09-15 22:57:34 +0200947 return 0;
948 else
949 return -EINVAL;
950}
951
952static int check_overlay_scaling(struct put_image_params *rec)
953{
954 u32 tmp;
955
956 /* downscaling limit is 8.0 */
957 tmp = ((rec->src_scan_h << 16) / rec->dst_h) >> 16;
958 if (tmp > 7)
959 return -EINVAL;
960 tmp = ((rec->src_scan_w << 16) / rec->dst_w) >> 16;
961 if (tmp > 7)
962 return -EINVAL;
963
964 return 0;
965}
966
967static int check_overlay_src(struct drm_device *dev,
968 struct drm_intel_overlay_put_image *rec,
969 struct drm_gem_object *new_bo)
970{
Daniel Vetter02e792f2009-09-15 22:57:34 +0200971 int uv_hscale = uv_hsubsampling(rec->flags);
972 int uv_vscale = uv_vsubsampling(rec->flags);
Chris Wilson9f7c3f42010-08-12 11:29:34 +0100973 u32 stride_mask, depth, tmp;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200974
975 /* check src dimensions */
976 if (IS_845G(dev) || IS_I830(dev)) {
Chris Wilson722506f2010-08-12 09:28:50 +0100977 if (rec->src_height > IMAGE_MAX_HEIGHT_LEGACY ||
Chris Wilson9f7c3f42010-08-12 11:29:34 +0100978 rec->src_width > IMAGE_MAX_WIDTH_LEGACY)
Daniel Vetter02e792f2009-09-15 22:57:34 +0200979 return -EINVAL;
980 } else {
Chris Wilson722506f2010-08-12 09:28:50 +0100981 if (rec->src_height > IMAGE_MAX_HEIGHT ||
Chris Wilson9f7c3f42010-08-12 11:29:34 +0100982 rec->src_width > IMAGE_MAX_WIDTH)
Daniel Vetter02e792f2009-09-15 22:57:34 +0200983 return -EINVAL;
984 }
Chris Wilson9f7c3f42010-08-12 11:29:34 +0100985
Daniel Vetter02e792f2009-09-15 22:57:34 +0200986 /* better safe than sorry, use 4 as the maximal subsampling ratio */
Chris Wilson722506f2010-08-12 09:28:50 +0100987 if (rec->src_height < N_VERT_Y_TAPS*4 ||
Chris Wilson9f7c3f42010-08-12 11:29:34 +0100988 rec->src_width < N_HORIZ_Y_TAPS*4)
Daniel Vetter02e792f2009-09-15 22:57:34 +0200989 return -EINVAL;
990
Chris Wilsona1efd142010-07-12 19:35:38 +0100991 /* check alignment constraints */
Daniel Vetter02e792f2009-09-15 22:57:34 +0200992 switch (rec->flags & I915_OVERLAY_TYPE_MASK) {
Chris Wilson722506f2010-08-12 09:28:50 +0100993 case I915_OVERLAY_RGB:
994 /* not implemented */
995 return -EINVAL;
Chris Wilson9f7c3f42010-08-12 11:29:34 +0100996
Chris Wilson722506f2010-08-12 09:28:50 +0100997 case I915_OVERLAY_YUV_PACKED:
Chris Wilson722506f2010-08-12 09:28:50 +0100998 if (uv_vscale != 1)
Daniel Vetter02e792f2009-09-15 22:57:34 +0200999 return -EINVAL;
Chris Wilson9f7c3f42010-08-12 11:29:34 +01001000
1001 depth = packed_depth_bytes(rec->flags);
Chris Wilson722506f2010-08-12 09:28:50 +01001002 if (depth < 0)
1003 return depth;
Chris Wilson9f7c3f42010-08-12 11:29:34 +01001004
Chris Wilson722506f2010-08-12 09:28:50 +01001005 /* ignore UV planes */
1006 rec->stride_UV = 0;
1007 rec->offset_U = 0;
1008 rec->offset_V = 0;
1009 /* check pixel alignment */
1010 if (rec->offset_Y % depth)
Daniel Vetter02e792f2009-09-15 22:57:34 +02001011 return -EINVAL;
Chris Wilson722506f2010-08-12 09:28:50 +01001012 break;
Chris Wilson9f7c3f42010-08-12 11:29:34 +01001013
Chris Wilson722506f2010-08-12 09:28:50 +01001014 case I915_OVERLAY_YUV_PLANAR:
1015 if (uv_vscale < 0 || uv_hscale < 0)
1016 return -EINVAL;
1017 /* no offset restrictions for planar formats */
1018 break;
Chris Wilson9f7c3f42010-08-12 11:29:34 +01001019
Chris Wilson722506f2010-08-12 09:28:50 +01001020 default:
1021 return -EINVAL;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001022 }
1023
1024 if (rec->src_width % uv_hscale)
1025 return -EINVAL;
1026
1027 /* stride checking */
Chris Wilsona1efd142010-07-12 19:35:38 +01001028 if (IS_I830(dev) || IS_845G(dev))
1029 stride_mask = 255;
1030 else
1031 stride_mask = 63;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001032
1033 if (rec->stride_Y & stride_mask || rec->stride_UV & stride_mask)
1034 return -EINVAL;
1035 if (IS_I965G(dev) && rec->stride_Y < 512)
1036 return -EINVAL;
1037
1038 tmp = (rec->flags & I915_OVERLAY_TYPE_MASK) == I915_OVERLAY_YUV_PLANAR ?
Chris Wilson9f7c3f42010-08-12 11:29:34 +01001039 4096 : 8192;
1040 if (rec->stride_Y > tmp || rec->stride_UV > 2*1024)
Daniel Vetter02e792f2009-09-15 22:57:34 +02001041 return -EINVAL;
1042
1043 /* check buffer dimensions */
1044 switch (rec->flags & I915_OVERLAY_TYPE_MASK) {
Chris Wilson722506f2010-08-12 09:28:50 +01001045 case I915_OVERLAY_RGB:
1046 case I915_OVERLAY_YUV_PACKED:
1047 /* always 4 Y values per depth pixels */
1048 if (packed_width_bytes(rec->flags, rec->src_width) > rec->stride_Y)
1049 return -EINVAL;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001050
Chris Wilson722506f2010-08-12 09:28:50 +01001051 tmp = rec->stride_Y*rec->src_height;
1052 if (rec->offset_Y + tmp > new_bo->size)
1053 return -EINVAL;
1054 break;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001055
Chris Wilson722506f2010-08-12 09:28:50 +01001056 case I915_OVERLAY_YUV_PLANAR:
1057 if (rec->src_width > rec->stride_Y)
1058 return -EINVAL;
1059 if (rec->src_width/uv_hscale > rec->stride_UV)
1060 return -EINVAL;
1061
Chris Wilson9f7c3f42010-08-12 11:29:34 +01001062 tmp = rec->stride_Y * rec->src_height;
Chris Wilson722506f2010-08-12 09:28:50 +01001063 if (rec->offset_Y + tmp > new_bo->size)
1064 return -EINVAL;
Chris Wilson9f7c3f42010-08-12 11:29:34 +01001065
1066 tmp = rec->stride_UV * (rec->src_height / uv_vscale);
Chris Wilson722506f2010-08-12 09:28:50 +01001067 if (rec->offset_U + tmp > new_bo->size ||
1068 rec->offset_V + tmp > new_bo->size)
1069 return -EINVAL;
1070 break;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001071 }
1072
1073 return 0;
1074}
1075
1076int intel_overlay_put_image(struct drm_device *dev, void *data,
1077 struct drm_file *file_priv)
1078{
1079 struct drm_intel_overlay_put_image *put_image_rec = data;
1080 drm_i915_private_t *dev_priv = dev->dev_private;
1081 struct intel_overlay *overlay;
1082 struct drm_mode_object *drmmode_obj;
1083 struct intel_crtc *crtc;
1084 struct drm_gem_object *new_bo;
1085 struct put_image_params *params;
1086 int ret;
1087
1088 if (!dev_priv) {
1089 DRM_ERROR("called with no initialization\n");
1090 return -EINVAL;
1091 }
1092
1093 overlay = dev_priv->overlay;
1094 if (!overlay) {
1095 DRM_DEBUG("userspace bug: no overlay\n");
1096 return -ENODEV;
1097 }
1098
1099 if (!(put_image_rec->flags & I915_OVERLAY_ENABLE)) {
1100 mutex_lock(&dev->mode_config.mutex);
1101 mutex_lock(&dev->struct_mutex);
1102
Chris Wilson5dcdbcb2010-08-12 13:50:28 +01001103 ret = intel_overlay_switch_off(overlay, true);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001104
1105 mutex_unlock(&dev->struct_mutex);
1106 mutex_unlock(&dev->mode_config.mutex);
1107
1108 return ret;
1109 }
1110
1111 params = kmalloc(sizeof(struct put_image_params), GFP_KERNEL);
1112 if (!params)
1113 return -ENOMEM;
1114
1115 drmmode_obj = drm_mode_object_find(dev, put_image_rec->crtc_id,
Chris Wilson722506f2010-08-12 09:28:50 +01001116 DRM_MODE_OBJECT_CRTC);
Dan Carpenter915a4282010-03-06 14:05:39 +03001117 if (!drmmode_obj) {
1118 ret = -ENOENT;
1119 goto out_free;
1120 }
Daniel Vetter02e792f2009-09-15 22:57:34 +02001121 crtc = to_intel_crtc(obj_to_crtc(drmmode_obj));
1122
1123 new_bo = drm_gem_object_lookup(dev, file_priv,
Chris Wilson722506f2010-08-12 09:28:50 +01001124 put_image_rec->bo_handle);
Dan Carpenter915a4282010-03-06 14:05:39 +03001125 if (!new_bo) {
1126 ret = -ENOENT;
1127 goto out_free;
1128 }
Daniel Vetter02e792f2009-09-15 22:57:34 +02001129
1130 mutex_lock(&dev->mode_config.mutex);
1131 mutex_lock(&dev->struct_mutex);
1132
Daniel Vetter03f77ea2009-09-15 22:57:37 +02001133 if (overlay->hw_wedged) {
1134 ret = intel_overlay_recover_from_interrupt(overlay, 1);
1135 if (ret != 0)
1136 goto out_unlock;
1137 }
1138
Daniel Vetter02e792f2009-09-15 22:57:34 +02001139 if (overlay->crtc != crtc) {
1140 struct drm_display_mode *mode = &crtc->base.mode;
Chris Wilson5dcdbcb2010-08-12 13:50:28 +01001141 ret = intel_overlay_switch_off(overlay, true);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001142 if (ret != 0)
1143 goto out_unlock;
1144
1145 ret = check_overlay_possible_on_crtc(overlay, crtc);
1146 if (ret != 0)
1147 goto out_unlock;
1148
1149 overlay->crtc = crtc;
1150 crtc->overlay = overlay;
1151
1152 if (intel_panel_fitter_pipe(dev) == crtc->pipe
1153 /* and line to wide, i.e. one-line-mode */
1154 && mode->hdisplay > 1024) {
1155 overlay->pfit_active = 1;
1156 update_pfit_vscale_ratio(overlay);
1157 } else
1158 overlay->pfit_active = 0;
1159 }
1160
1161 ret = check_overlay_dst(overlay, put_image_rec);
1162 if (ret != 0)
1163 goto out_unlock;
1164
1165 if (overlay->pfit_active) {
1166 params->dst_y = ((((u32)put_image_rec->dst_y) << 12) /
Chris Wilson722506f2010-08-12 09:28:50 +01001167 overlay->pfit_vscale_ratio);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001168 /* shifting right rounds downwards, so add 1 */
1169 params->dst_h = ((((u32)put_image_rec->dst_height) << 12) /
Chris Wilson722506f2010-08-12 09:28:50 +01001170 overlay->pfit_vscale_ratio) + 1;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001171 } else {
1172 params->dst_y = put_image_rec->dst_y;
1173 params->dst_h = put_image_rec->dst_height;
1174 }
1175 params->dst_x = put_image_rec->dst_x;
1176 params->dst_w = put_image_rec->dst_width;
1177
1178 params->src_w = put_image_rec->src_width;
1179 params->src_h = put_image_rec->src_height;
1180 params->src_scan_w = put_image_rec->src_scan_width;
1181 params->src_scan_h = put_image_rec->src_scan_height;
Chris Wilson722506f2010-08-12 09:28:50 +01001182 if (params->src_scan_h > params->src_h ||
1183 params->src_scan_w > params->src_w) {
Daniel Vetter02e792f2009-09-15 22:57:34 +02001184 ret = -EINVAL;
1185 goto out_unlock;
1186 }
1187
1188 ret = check_overlay_src(dev, put_image_rec, new_bo);
1189 if (ret != 0)
1190 goto out_unlock;
1191 params->format = put_image_rec->flags & ~I915_OVERLAY_FLAGS_MASK;
1192 params->stride_Y = put_image_rec->stride_Y;
1193 params->stride_UV = put_image_rec->stride_UV;
1194 params->offset_Y = put_image_rec->offset_Y;
1195 params->offset_U = put_image_rec->offset_U;
1196 params->offset_V = put_image_rec->offset_V;
1197
1198 /* Check scaling after src size to prevent a divide-by-zero. */
1199 ret = check_overlay_scaling(params);
1200 if (ret != 0)
1201 goto out_unlock;
1202
1203 ret = intel_overlay_do_put_image(overlay, new_bo, params);
1204 if (ret != 0)
1205 goto out_unlock;
1206
1207 mutex_unlock(&dev->struct_mutex);
1208 mutex_unlock(&dev->mode_config.mutex);
1209
1210 kfree(params);
1211
1212 return 0;
1213
1214out_unlock:
1215 mutex_unlock(&dev->struct_mutex);
1216 mutex_unlock(&dev->mode_config.mutex);
Luca Barbieribc9025b2010-02-09 05:49:12 +00001217 drm_gem_object_unreference_unlocked(new_bo);
Dan Carpenter915a4282010-03-06 14:05:39 +03001218out_free:
Daniel Vetter02e792f2009-09-15 22:57:34 +02001219 kfree(params);
1220
1221 return ret;
1222}
1223
1224static void update_reg_attrs(struct intel_overlay *overlay,
1225 struct overlay_registers *regs)
1226{
1227 regs->OCLRC0 = (overlay->contrast << 18) | (overlay->brightness & 0xff);
1228 regs->OCLRC1 = overlay->saturation;
1229}
1230
1231static bool check_gamma_bounds(u32 gamma1, u32 gamma2)
1232{
1233 int i;
1234
1235 if (gamma1 & 0xff000000 || gamma2 & 0xff000000)
1236 return false;
1237
1238 for (i = 0; i < 3; i++) {
Chris Wilson722506f2010-08-12 09:28:50 +01001239 if (((gamma1 >> i*8) & 0xff) >= ((gamma2 >> i*8) & 0xff))
Daniel Vetter02e792f2009-09-15 22:57:34 +02001240 return false;
1241 }
1242
1243 return true;
1244}
1245
1246static bool check_gamma5_errata(u32 gamma5)
1247{
1248 int i;
1249
1250 for (i = 0; i < 3; i++) {
1251 if (((gamma5 >> i*8) & 0xff) == 0x80)
1252 return false;
1253 }
1254
1255 return true;
1256}
1257
1258static int check_gamma(struct drm_intel_overlay_attrs *attrs)
1259{
Chris Wilson722506f2010-08-12 09:28:50 +01001260 if (!check_gamma_bounds(0, attrs->gamma0) ||
1261 !check_gamma_bounds(attrs->gamma0, attrs->gamma1) ||
1262 !check_gamma_bounds(attrs->gamma1, attrs->gamma2) ||
1263 !check_gamma_bounds(attrs->gamma2, attrs->gamma3) ||
1264 !check_gamma_bounds(attrs->gamma3, attrs->gamma4) ||
1265 !check_gamma_bounds(attrs->gamma4, attrs->gamma5) ||
1266 !check_gamma_bounds(attrs->gamma5, 0x00ffffff))
Daniel Vetter02e792f2009-09-15 22:57:34 +02001267 return -EINVAL;
Chris Wilson722506f2010-08-12 09:28:50 +01001268
Daniel Vetter02e792f2009-09-15 22:57:34 +02001269 if (!check_gamma5_errata(attrs->gamma5))
1270 return -EINVAL;
Chris Wilson722506f2010-08-12 09:28:50 +01001271
Daniel Vetter02e792f2009-09-15 22:57:34 +02001272 return 0;
1273}
1274
1275int intel_overlay_attrs(struct drm_device *dev, void *data,
1276 struct drm_file *file_priv)
1277{
1278 struct drm_intel_overlay_attrs *attrs = data;
1279 drm_i915_private_t *dev_priv = dev->dev_private;
1280 struct intel_overlay *overlay;
1281 struct overlay_registers *regs;
1282 int ret;
1283
1284 if (!dev_priv) {
1285 DRM_ERROR("called with no initialization\n");
1286 return -EINVAL;
1287 }
1288
1289 overlay = dev_priv->overlay;
1290 if (!overlay) {
1291 DRM_DEBUG("userspace bug: no overlay\n");
1292 return -ENODEV;
1293 }
1294
1295 mutex_lock(&dev->mode_config.mutex);
1296 mutex_lock(&dev->struct_mutex);
1297
Chris Wilson60fc3322010-08-12 10:44:45 +01001298 ret = -EINVAL;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001299 if (!(attrs->flags & I915_OVERLAY_UPDATE_ATTRS)) {
Chris Wilson60fc3322010-08-12 10:44:45 +01001300 attrs->color_key = overlay->color_key;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001301 attrs->brightness = overlay->brightness;
Chris Wilson60fc3322010-08-12 10:44:45 +01001302 attrs->contrast = overlay->contrast;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001303 attrs->saturation = overlay->saturation;
1304
1305 if (IS_I9XX(dev)) {
1306 attrs->gamma0 = I915_READ(OGAMC0);
1307 attrs->gamma1 = I915_READ(OGAMC1);
1308 attrs->gamma2 = I915_READ(OGAMC2);
1309 attrs->gamma3 = I915_READ(OGAMC3);
1310 attrs->gamma4 = I915_READ(OGAMC4);
1311 attrs->gamma5 = I915_READ(OGAMC5);
1312 }
Daniel Vetter02e792f2009-09-15 22:57:34 +02001313 } else {
Chris Wilson60fc3322010-08-12 10:44:45 +01001314 if (attrs->brightness < -128 || attrs->brightness > 127)
Daniel Vetter02e792f2009-09-15 22:57:34 +02001315 goto out_unlock;
Chris Wilson60fc3322010-08-12 10:44:45 +01001316 if (attrs->contrast > 255)
1317 goto out_unlock;
1318 if (attrs->saturation > 1023)
1319 goto out_unlock;
Chris Wilson722506f2010-08-12 09:28:50 +01001320
Chris Wilson60fc3322010-08-12 10:44:45 +01001321 overlay->color_key = attrs->color_key;
1322 overlay->brightness = attrs->brightness;
1323 overlay->contrast = attrs->contrast;
1324 overlay->saturation = attrs->saturation;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001325
Chris Wilson8d74f652010-08-12 10:35:26 +01001326 regs = intel_overlay_map_regs(overlay);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001327 if (!regs) {
1328 ret = -ENOMEM;
1329 goto out_unlock;
1330 }
1331
1332 update_reg_attrs(overlay, regs);
1333
Chris Wilson9bb2ff72010-08-12 12:02:11 +01001334 intel_overlay_unmap_regs(overlay, regs);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001335
1336 if (attrs->flags & I915_OVERLAY_UPDATE_GAMMA) {
Chris Wilson60fc3322010-08-12 10:44:45 +01001337 if (!IS_I9XX(dev))
Daniel Vetter02e792f2009-09-15 22:57:34 +02001338 goto out_unlock;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001339
1340 if (overlay->active) {
1341 ret = -EBUSY;
1342 goto out_unlock;
1343 }
1344
1345 ret = check_gamma(attrs);
Chris Wilson60fc3322010-08-12 10:44:45 +01001346 if (ret)
Daniel Vetter02e792f2009-09-15 22:57:34 +02001347 goto out_unlock;
1348
1349 I915_WRITE(OGAMC0, attrs->gamma0);
1350 I915_WRITE(OGAMC1, attrs->gamma1);
1351 I915_WRITE(OGAMC2, attrs->gamma2);
1352 I915_WRITE(OGAMC3, attrs->gamma3);
1353 I915_WRITE(OGAMC4, attrs->gamma4);
1354 I915_WRITE(OGAMC5, attrs->gamma5);
1355 }
Daniel Vetter02e792f2009-09-15 22:57:34 +02001356 }
1357
Chris Wilson60fc3322010-08-12 10:44:45 +01001358 ret = 0;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001359out_unlock:
1360 mutex_unlock(&dev->struct_mutex);
1361 mutex_unlock(&dev->mode_config.mutex);
1362
1363 return ret;
1364}
1365
1366void intel_setup_overlay(struct drm_device *dev)
1367{
1368 drm_i915_private_t *dev_priv = dev->dev_private;
1369 struct intel_overlay *overlay;
1370 struct drm_gem_object *reg_bo;
1371 struct overlay_registers *regs;
1372 int ret;
1373
Chris Wilson315781482010-08-12 09:42:51 +01001374 if (!HAS_OVERLAY(dev))
Daniel Vetter02e792f2009-09-15 22:57:34 +02001375 return;
1376
1377 overlay = kzalloc(sizeof(struct intel_overlay), GFP_KERNEL);
1378 if (!overlay)
1379 return;
1380 overlay->dev = dev;
1381
Daniel Vetterac52bc52010-04-09 19:05:06 +00001382 reg_bo = i915_gem_alloc_object(dev, PAGE_SIZE);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001383 if (!reg_bo)
1384 goto out_free;
Daniel Vetter23010e42010-03-08 13:35:02 +01001385 overlay->reg_bo = to_intel_bo(reg_bo);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001386
Chris Wilson315781482010-08-12 09:42:51 +01001387 if (OVERLAY_NEEDS_PHYSICAL(dev)) {
1388 ret = i915_gem_attach_phys_object(dev, reg_bo,
1389 I915_GEM_PHYS_OVERLAY_REGS,
Chris Wilsona2930122010-08-12 10:47:56 +01001390 PAGE_SIZE);
Chris Wilson315781482010-08-12 09:42:51 +01001391 if (ret) {
1392 DRM_ERROR("failed to attach phys overlay regs\n");
1393 goto out_free_bo;
1394 }
1395 overlay->flip_addr = overlay->reg_bo->phys_obj->handle->busaddr;
1396 } else {
Daniel Vetter02e792f2009-09-15 22:57:34 +02001397 ret = i915_gem_object_pin(reg_bo, PAGE_SIZE);
1398 if (ret) {
1399 DRM_ERROR("failed to pin overlay register bo\n");
1400 goto out_free_bo;
1401 }
1402 overlay->flip_addr = overlay->reg_bo->gtt_offset;
Chris Wilson0ddc1282010-08-12 09:35:00 +01001403
1404 ret = i915_gem_object_set_to_gtt_domain(reg_bo, true);
1405 if (ret) {
1406 DRM_ERROR("failed to move overlay register bo into the GTT\n");
1407 goto out_unpin_bo;
1408 }
Daniel Vetter02e792f2009-09-15 22:57:34 +02001409 }
1410
1411 /* init all values */
1412 overlay->color_key = 0x0101fe;
1413 overlay->brightness = -19;
1414 overlay->contrast = 75;
1415 overlay->saturation = 146;
1416
Chris Wilson8d74f652010-08-12 10:35:26 +01001417 regs = intel_overlay_map_regs(overlay);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001418 if (!regs)
1419 goto out_free_bo;
1420
1421 memset(regs, 0, sizeof(struct overlay_registers));
1422 update_polyphase_filter(regs);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001423 update_reg_attrs(overlay, regs);
1424
Chris Wilson9bb2ff72010-08-12 12:02:11 +01001425 intel_overlay_unmap_regs(overlay, regs);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001426
1427 dev_priv->overlay = overlay;
1428 DRM_INFO("initialized overlay support\n");
1429 return;
1430
Chris Wilson0ddc1282010-08-12 09:35:00 +01001431out_unpin_bo:
1432 i915_gem_object_unpin(reg_bo);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001433out_free_bo:
1434 drm_gem_object_unreference(reg_bo);
1435out_free:
1436 kfree(overlay);
1437 return;
1438}
1439
1440void intel_cleanup_overlay(struct drm_device *dev)
1441{
Chris Wilson722506f2010-08-12 09:28:50 +01001442 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001443
Chris Wilson62cf4e62010-08-12 10:50:36 +01001444 if (!dev_priv->overlay)
1445 return;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001446
Chris Wilson62cf4e62010-08-12 10:50:36 +01001447 /* The bo's should be free'd by the generic code already.
1448 * Furthermore modesetting teardown happens beforehand so the
1449 * hardware should be off already */
1450 BUG_ON(dev_priv->overlay->active);
1451
1452 drm_gem_object_unreference_unlocked(&dev_priv->overlay->reg_bo->base);
1453 kfree(dev_priv->overlay);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001454}
Chris Wilson6ef3d422010-08-04 20:26:07 +01001455
1456struct intel_overlay_error_state {
1457 struct overlay_registers regs;
1458 unsigned long base;
1459 u32 dovsta;
1460 u32 isr;
1461};
1462
1463struct intel_overlay_error_state *
1464intel_overlay_capture_error_state(struct drm_device *dev)
1465{
1466 drm_i915_private_t *dev_priv = dev->dev_private;
1467 struct intel_overlay *overlay = dev_priv->overlay;
1468 struct intel_overlay_error_state *error;
1469 struct overlay_registers __iomem *regs;
1470
1471 if (!overlay || !overlay->active)
1472 return NULL;
1473
1474 error = kmalloc(sizeof(*error), GFP_ATOMIC);
1475 if (error == NULL)
1476 return NULL;
1477
1478 error->dovsta = I915_READ(DOVSTA);
1479 error->isr = I915_READ(ISR);
Chris Wilson315781482010-08-12 09:42:51 +01001480 if (OVERLAY_NEEDS_PHYSICAL(overlay->dev))
Chris Wilson6ef3d422010-08-04 20:26:07 +01001481 error->base = (long) overlay->reg_bo->phys_obj->handle->vaddr;
Chris Wilson315781482010-08-12 09:42:51 +01001482 else
1483 error->base = (long) overlay->reg_bo->gtt_offset;
Chris Wilson6ef3d422010-08-04 20:26:07 +01001484
Chris Wilson8d74f652010-08-12 10:35:26 +01001485 regs = intel_overlay_map_regs_atomic(overlay, KM_IRQ0);
Chris Wilson6ef3d422010-08-04 20:26:07 +01001486 if (!regs)
1487 goto err;
1488
1489 memcpy_fromio(&error->regs, regs, sizeof(struct overlay_registers));
Chris Wilson9bb2ff72010-08-12 12:02:11 +01001490 intel_overlay_unmap_regs_atomic(overlay, KM_IRQ0, regs);
Chris Wilson6ef3d422010-08-04 20:26:07 +01001491
1492 return error;
1493
1494err:
1495 kfree(error);
1496 return NULL;
1497}
1498
1499void
1500intel_overlay_print_error_state(struct seq_file *m, struct intel_overlay_error_state *error)
1501{
1502 seq_printf(m, "Overlay, status: 0x%08x, interrupt: 0x%08x\n",
1503 error->dovsta, error->isr);
1504 seq_printf(m, " Register file at 0x%08lx:\n",
1505 error->base);
1506
1507#define P(x) seq_printf(m, " " #x ": 0x%08x\n", error->regs.x)
1508 P(OBUF_0Y);
1509 P(OBUF_1Y);
1510 P(OBUF_0U);
1511 P(OBUF_0V);
1512 P(OBUF_1U);
1513 P(OBUF_1V);
1514 P(OSTRIDE);
1515 P(YRGB_VPH);
1516 P(UV_VPH);
1517 P(HORZ_PH);
1518 P(INIT_PHS);
1519 P(DWINPOS);
1520 P(DWINSZ);
1521 P(SWIDTH);
1522 P(SWIDTHSW);
1523 P(SHEIGHT);
1524 P(YRGBSCALE);
1525 P(UVSCALE);
1526 P(OCLRC0);
1527 P(OCLRC1);
1528 P(DCLRKV);
1529 P(DCLRKM);
1530 P(SCLRKVH);
1531 P(SCLRKVL);
1532 P(SCLRKEN);
1533 P(OCONFIG);
1534 P(OCMD);
1535 P(OSTART_0Y);
1536 P(OSTART_1Y);
1537 P(OSTART_0U);
1538 P(OSTART_0V);
1539 P(OSTART_1U);
1540 P(OSTART_1V);
1541 P(OTILEOFF_0Y);
1542 P(OTILEOFF_1Y);
1543 P(OTILEOFF_0U);
1544 P(OTILEOFF_0V);
1545 P(OTILEOFF_1U);
1546 P(OTILEOFF_1V);
1547 P(FASTHSCALE);
1548 P(UVSCALEV);
1549#undef P
1550}