blob: ec8ffaccbbdb2fa1c8c064a8c82b4aa65a8c9293 [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 Wilson23f09ce2010-08-12 13:53:37 +0100173struct intel_overlay {
174 struct drm_device *dev;
175 struct intel_crtc *crtc;
176 struct drm_i915_gem_object *vid_bo;
177 struct drm_i915_gem_object *old_vid_bo;
178 int active;
179 int pfit_active;
180 u32 pfit_vscale_ratio; /* shifted-point number, (1<<12) == 1.0 */
181 u32 color_key;
182 u32 brightness, contrast, saturation;
183 u32 old_xscale, old_yscale;
184 /* register access */
185 u32 flip_addr;
186 struct drm_i915_gem_object *reg_bo;
187 /* flip handling */
188 uint32_t last_flip_req;
Chris Wilsonb303cf92010-08-12 14:03:48 +0100189 void (*flip_tail)(struct intel_overlay *);
Chris Wilson23f09ce2010-08-12 13:53:37 +0100190};
Daniel Vetter02e792f2009-09-15 22:57:34 +0200191
Chris Wilson8d74f652010-08-12 10:35:26 +0100192static struct overlay_registers *
Chris Wilson8d74f652010-08-12 10:35:26 +0100193intel_overlay_map_regs(struct intel_overlay *overlay)
Daniel Vetter02e792f2009-09-15 22:57:34 +0200194{
195 drm_i915_private_t *dev_priv = overlay->dev->dev_private;
196 struct overlay_registers *regs;
197
Chris Wilson9bb2ff72010-08-12 12:02:11 +0100198 if (OVERLAY_NEEDS_PHYSICAL(overlay->dev))
Daniel Vetter02e792f2009-09-15 22:57:34 +0200199 regs = overlay->reg_bo->phys_obj->handle->vaddr;
Chris Wilson9bb2ff72010-08-12 12:02:11 +0100200 else
Chris Wilson8d74f652010-08-12 10:35:26 +0100201 regs = io_mapping_map_wc(dev_priv->mm.gtt_mapping,
202 overlay->reg_bo->gtt_offset);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200203
Chris Wilson9bb2ff72010-08-12 12:02:11 +0100204 return regs;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200205}
206
Chris Wilson9bb2ff72010-08-12 12:02:11 +0100207static void intel_overlay_unmap_regs(struct intel_overlay *overlay,
208 struct overlay_registers *regs)
Daniel Vetter02e792f2009-09-15 22:57:34 +0200209{
Chris Wilson8d74f652010-08-12 10:35:26 +0100210 if (!OVERLAY_NEEDS_PHYSICAL(overlay->dev))
Chris Wilson9bb2ff72010-08-12 12:02:11 +0100211 io_mapping_unmap(regs);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200212}
Daniel Vetter02e792f2009-09-15 22:57:34 +0200213
Chris Wilsonb6c028e2010-08-12 11:55:08 +0100214static int intel_overlay_do_wait_request(struct intel_overlay *overlay,
Chris Wilson8dc5d142010-08-12 12:36:12 +0100215 struct drm_i915_gem_request *request,
Chris Wilsonb6c028e2010-08-12 11:55:08 +0100216 bool interruptible,
Chris Wilsonb303cf92010-08-12 14:03:48 +0100217 void (*tail)(struct intel_overlay *))
Chris Wilsonb6c028e2010-08-12 11:55:08 +0100218{
219 struct drm_device *dev = overlay->dev;
220 drm_i915_private_t *dev_priv = dev->dev_private;
221 int ret;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200222
Chris Wilsonb303cf92010-08-12 14:03:48 +0100223 BUG_ON(overlay->last_flip_req);
Chris Wilson3cce4692010-10-27 16:11:02 +0100224 ret = i915_add_request(dev, NULL, request, &dev_priv->render_ring);
225 if (ret) {
226 kfree(request);
227 return ret;
228 }
229 overlay->last_flip_req = request->seqno;
Chris Wilsonb303cf92010-08-12 14:03:48 +0100230 overlay->flip_tail = tail;
Chris Wilsonb6c028e2010-08-12 11:55:08 +0100231 ret = i915_do_wait_request(dev,
232 overlay->last_flip_req, true,
233 &dev_priv->render_ring);
234 if (ret)
235 return ret;
236
Chris Wilsonb6c028e2010-08-12 11:55:08 +0100237 overlay->last_flip_req = 0;
238 return 0;
239}
240
Chris Wilson106dada2010-07-16 17:13:01 +0100241/* Workaround for i830 bug where pipe a must be enable to change control regs */
242static int
243i830_activate_pipe_a(struct drm_device *dev)
244{
245 drm_i915_private_t *dev_priv = dev->dev_private;
246 struct intel_crtc *crtc;
247 struct drm_crtc_helper_funcs *crtc_funcs;
248 struct drm_display_mode vesa_640x480 = {
249 DRM_MODE("640x480", DRM_MODE_TYPE_DRIVER, 25175, 640, 656,
250 752, 800, 0, 480, 489, 492, 525, 0,
251 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC)
252 }, *mode;
253
254 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[0]);
255 if (crtc->dpms_mode == DRM_MODE_DPMS_ON)
256 return 0;
257
258 /* most i8xx have pipe a forced on, so don't trust dpms mode */
Chris Wilson5eddb702010-09-11 13:48:45 +0100259 if (I915_READ(PIPEACONF) & PIPECONF_ENABLE)
Chris Wilson106dada2010-07-16 17:13:01 +0100260 return 0;
261
262 crtc_funcs = crtc->base.helper_private;
263 if (crtc_funcs->dpms == NULL)
264 return 0;
265
266 DRM_DEBUG_DRIVER("Enabling pipe A in order to enable overlay\n");
267
268 mode = drm_mode_duplicate(dev, &vesa_640x480);
269 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
270 if(!drm_crtc_helper_set_mode(&crtc->base, mode,
271 crtc->base.x, crtc->base.y,
272 crtc->base.fb))
273 return 0;
274
275 crtc_funcs->dpms(&crtc->base, DRM_MODE_DPMS_ON);
276 return 1;
277}
278
279static void
280i830_deactivate_pipe_a(struct drm_device *dev)
281{
282 drm_i915_private_t *dev_priv = dev->dev_private;
283 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[0];
284 struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
285
286 crtc_funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200287}
288
289/* overlay needs to be disable in OCMD reg */
290static int intel_overlay_on(struct intel_overlay *overlay)
291{
292 struct drm_device *dev = overlay->dev;
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100293 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson8dc5d142010-08-12 12:36:12 +0100294 struct drm_i915_gem_request *request;
Chris Wilson106dada2010-07-16 17:13:01 +0100295 int pipe_a_quirk = 0;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200296 int ret;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200297
298 BUG_ON(overlay->active);
Daniel Vetter03f77ea2009-09-15 22:57:37 +0200299 overlay->active = 1;
Daniel Vetter03f77ea2009-09-15 22:57:37 +0200300
Chris Wilson106dada2010-07-16 17:13:01 +0100301 if (IS_I830(dev)) {
302 pipe_a_quirk = i830_activate_pipe_a(dev);
303 if (pipe_a_quirk < 0)
304 return pipe_a_quirk;
305 }
306
Chris Wilson8dc5d142010-08-12 12:36:12 +0100307 request = kzalloc(sizeof(*request), GFP_KERNEL);
Chris Wilson106dada2010-07-16 17:13:01 +0100308 if (request == NULL) {
309 ret = -ENOMEM;
310 goto out;
311 }
Daniel Vetter02e792f2009-09-15 22:57:34 +0200312
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100313 ret = BEGIN_LP_RING(4);
314 if (ret) {
315 kfree(request);
316 goto out;
317 }
318
Daniel Vetter02e792f2009-09-15 22:57:34 +0200319 OUT_RING(MI_OVERLAY_FLIP | MI_OVERLAY_ON);
320 OUT_RING(overlay->flip_addr | OFC_UPDATE);
321 OUT_RING(MI_WAIT_FOR_EVENT | MI_WAIT_FOR_OVERLAY_FLIP);
322 OUT_RING(MI_NOOP);
323 ADVANCE_LP_RING();
324
Chris Wilsonb303cf92010-08-12 14:03:48 +0100325 ret = intel_overlay_do_wait_request(overlay, request, true, NULL);
Chris Wilson106dada2010-07-16 17:13:01 +0100326out:
327 if (pipe_a_quirk)
328 i830_deactivate_pipe_a(dev);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200329
Chris Wilson106dada2010-07-16 17:13:01 +0100330 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;
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100342 int ret;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200343
344 BUG_ON(!overlay->active);
345
Chris Wilson8dc5d142010-08-12 12:36:12 +0100346 request = kzalloc(sizeof(*request), GFP_KERNEL);
347 if (request == NULL)
348 return -ENOMEM;
349
Daniel Vetter02e792f2009-09-15 22:57:34 +0200350 if (load_polyphase_filter)
351 flip_addr |= OFC_UPDATE;
352
353 /* check for underruns */
354 tmp = I915_READ(DOVSTA);
355 if (tmp & (1 << 17))
356 DRM_DEBUG("overlay underrun, DOVSTA: %x\n", tmp);
357
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100358 ret = BEGIN_LP_RING(2);
359 if (ret) {
360 kfree(request);
361 return ret;
362 }
Daniel Vetter02e792f2009-09-15 22:57:34 +0200363 OUT_RING(MI_OVERLAY_FLIP | MI_OVERLAY_CONTINUE);
364 OUT_RING(flip_addr);
Daniel Vetter5a5a0c62009-09-15 22:57:36 +0200365 ADVANCE_LP_RING();
366
Chris Wilson3cce4692010-10-27 16:11:02 +0100367 ret = i915_add_request(dev, NULL, request, &dev_priv->render_ring);
368 if (ret) {
369 kfree(request);
370 return ret;
371 }
372
373 overlay->last_flip_req = request->seqno;
Daniel Vetter03f77ea2009-09-15 22:57:37 +0200374 return 0;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200375}
376
Chris Wilsonb303cf92010-08-12 14:03:48 +0100377static void intel_overlay_release_old_vid_tail(struct intel_overlay *overlay)
Daniel Vetter02e792f2009-09-15 22:57:34 +0200378{
Chris Wilsonb303cf92010-08-12 14:03:48 +0100379 struct drm_gem_object *obj = &overlay->old_vid_bo->base;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200380
Chris Wilsonb303cf92010-08-12 14:03:48 +0100381 i915_gem_object_unpin(obj);
382 drm_gem_object_unreference(obj);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200383
Chris Wilsonb303cf92010-08-12 14:03:48 +0100384 overlay->old_vid_bo = NULL;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200385}
386
Daniel Vetter12ca45f2037-04-25 10:08:26 +0200387static void intel_overlay_off_tail(struct intel_overlay *overlay)
388{
389 struct drm_gem_object *obj;
390
391 /* never have the overlay hw on without showing a frame */
392 BUG_ON(!overlay->vid_bo);
Daniel Vettera8089e82010-04-09 19:05:09 +0000393 obj = &overlay->vid_bo->base;
Daniel Vetter12ca45f2037-04-25 10:08:26 +0200394
395 i915_gem_object_unpin(obj);
396 drm_gem_object_unreference(obj);
397 overlay->vid_bo = NULL;
398
399 overlay->crtc->overlay = NULL;
400 overlay->crtc = NULL;
401 overlay->active = 0;
402}
403
Daniel Vetter02e792f2009-09-15 22:57:34 +0200404/* overlay needs to be disabled in OCMD reg */
Chris Wilson5dcdbcb2010-08-12 13:50:28 +0100405static int intel_overlay_off(struct intel_overlay *overlay,
406 bool interruptible)
Daniel Vetter03f77ea2009-09-15 22:57:37 +0200407{
408 struct drm_device *dev = overlay->dev;
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100409 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson8dc5d142010-08-12 12:36:12 +0100410 u32 flip_addr = overlay->flip_addr;
411 struct drm_i915_gem_request *request;
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100412 int ret;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200413
414 BUG_ON(!overlay->active);
415
Chris Wilson8dc5d142010-08-12 12:36:12 +0100416 request = kzalloc(sizeof(*request), GFP_KERNEL);
417 if (request == NULL)
418 return -ENOMEM;
419
Daniel Vetter02e792f2009-09-15 22:57:34 +0200420 /* According to intel docs the overlay hw may hang (when switching
421 * off) without loading the filter coeffs. It is however unclear whether
422 * this applies to the disabling of the overlay or to the switching off
423 * of the hw. Do it in both cases */
424 flip_addr |= OFC_UPDATE;
425
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100426 ret = BEGIN_LP_RING(6);
427 if (ret) {
428 kfree(request);
429 return ret;
430 }
Daniel Vetter02e792f2009-09-15 22:57:34 +0200431 /* wait for overlay to go idle */
Daniel Vetter02e792f2009-09-15 22:57:34 +0200432 OUT_RING(MI_OVERLAY_FLIP | MI_OVERLAY_CONTINUE);
433 OUT_RING(flip_addr);
Chris Wilson722506f2010-08-12 09:28:50 +0100434 OUT_RING(MI_WAIT_FOR_EVENT | MI_WAIT_FOR_OVERLAY_FLIP);
Chris Wilson722506f2010-08-12 09:28:50 +0100435 /* turn overlay off */
Chris Wilson722506f2010-08-12 09:28:50 +0100436 OUT_RING(MI_OVERLAY_FLIP | MI_OVERLAY_OFF);
437 OUT_RING(flip_addr);
438 OUT_RING(MI_WAIT_FOR_EVENT | MI_WAIT_FOR_OVERLAY_FLIP);
Chris Wilson722506f2010-08-12 09:28:50 +0100439 ADVANCE_LP_RING();
440
Chris Wilson5dcdbcb2010-08-12 13:50:28 +0100441 return intel_overlay_do_wait_request(overlay, request, interruptible,
Chris Wilsonb303cf92010-08-12 14:03:48 +0100442 intel_overlay_off_tail);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200443}
444
Daniel Vetter03f77ea2009-09-15 22:57:37 +0200445/* recover from an interruption due to a signal
446 * We have to be careful not to repeat work forever an make forward progess. */
Chris Wilson5dcdbcb2010-08-12 13:50:28 +0100447static int intel_overlay_recover_from_interrupt(struct intel_overlay *overlay,
448 bool interruptible)
Daniel Vetter03f77ea2009-09-15 22:57:37 +0200449{
450 struct drm_device *dev = overlay->dev;
Zou Nan hai852835f2010-05-21 09:08:56 +0800451 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter03f77ea2009-09-15 22:57:37 +0200452 int ret;
Daniel Vetter03f77ea2009-09-15 22:57:37 +0200453
Chris Wilsonb303cf92010-08-12 14:03:48 +0100454 if (overlay->last_flip_req == 0)
455 return 0;
Daniel Vetter03f77ea2009-09-15 22:57:37 +0200456
Zou Nan hai852835f2010-05-21 09:08:56 +0800457 ret = i915_do_wait_request(dev, overlay->last_flip_req,
Chris Wilson722506f2010-08-12 09:28:50 +0100458 interruptible, &dev_priv->render_ring);
Chris Wilsonb6c028e2010-08-12 11:55:08 +0100459 if (ret)
Daniel Vetter03f77ea2009-09-15 22:57:37 +0200460 return ret;
461
Chris Wilsonb303cf92010-08-12 14:03:48 +0100462 if (overlay->flip_tail)
463 overlay->flip_tail(overlay);
Daniel Vetter03f77ea2009-09-15 22:57:37 +0200464
Daniel Vetter03f77ea2009-09-15 22:57:37 +0200465 overlay->last_flip_req = 0;
466 return 0;
467}
468
Daniel Vetter5a5a0c62009-09-15 22:57:36 +0200469/* Wait for pending overlay flip and release old frame.
470 * Needs to be called before the overlay register are changed
Chris Wilson8d74f652010-08-12 10:35:26 +0100471 * via intel_overlay_(un)map_regs
472 */
Daniel Vetter02e792f2009-09-15 22:57:34 +0200473static int intel_overlay_release_old_vid(struct intel_overlay *overlay)
474{
Chris Wilson5cd68c92010-08-12 12:21:54 +0100475 struct drm_device *dev = overlay->dev;
476 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200477 int ret;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200478
Chris Wilson5cd68c92010-08-12 12:21:54 +0100479 /* Only wait if there is actually an old frame to release to
480 * guarantee forward progress.
481 */
Daniel Vetter03f77ea2009-09-15 22:57:37 +0200482 if (!overlay->old_vid_bo)
483 return 0;
484
Chris Wilson5cd68c92010-08-12 12:21:54 +0100485 if (I915_READ(ISR) & I915_OVERLAY_PLANE_FLIP_PENDING_INTERRUPT) {
Chris Wilson8dc5d142010-08-12 12:36:12 +0100486 struct drm_i915_gem_request *request;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200487
Chris Wilson5cd68c92010-08-12 12:21:54 +0100488 /* synchronous slowpath */
Chris Wilson8dc5d142010-08-12 12:36:12 +0100489 request = kzalloc(sizeof(*request), GFP_KERNEL);
490 if (request == NULL)
491 return -ENOMEM;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200492
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100493 ret = BEGIN_LP_RING(2);
494 if (ret) {
495 kfree(request);
496 return ret;
497 }
498
Chris Wilson5cd68c92010-08-12 12:21:54 +0100499 OUT_RING(MI_WAIT_FOR_EVENT | MI_WAIT_FOR_OVERLAY_FLIP);
500 OUT_RING(MI_NOOP);
501 ADVANCE_LP_RING();
Daniel Vetter02e792f2009-09-15 22:57:34 +0200502
Chris Wilson8dc5d142010-08-12 12:36:12 +0100503 ret = intel_overlay_do_wait_request(overlay, request, true,
Chris Wilsonb303cf92010-08-12 14:03:48 +0100504 intel_overlay_release_old_vid_tail);
Chris Wilson5cd68c92010-08-12 12:21:54 +0100505 if (ret)
506 return ret;
507 }
Daniel Vetter02e792f2009-09-15 22:57:34 +0200508
Chris Wilson5cd68c92010-08-12 12:21:54 +0100509 intel_overlay_release_old_vid_tail(overlay);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200510 return 0;
511}
512
513struct put_image_params {
514 int format;
515 short dst_x;
516 short dst_y;
517 short dst_w;
518 short dst_h;
519 short src_w;
520 short src_scan_h;
521 short src_scan_w;
522 short src_h;
523 short stride_Y;
524 short stride_UV;
525 int offset_Y;
526 int offset_U;
527 int offset_V;
528};
529
530static int packed_depth_bytes(u32 format)
531{
532 switch (format & I915_OVERLAY_DEPTH_MASK) {
Chris Wilson722506f2010-08-12 09:28:50 +0100533 case I915_OVERLAY_YUV422:
534 return 4;
535 case I915_OVERLAY_YUV411:
536 /* return 6; not implemented */
537 default:
538 return -EINVAL;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200539 }
540}
541
542static int packed_width_bytes(u32 format, short width)
543{
544 switch (format & I915_OVERLAY_DEPTH_MASK) {
Chris Wilson722506f2010-08-12 09:28:50 +0100545 case I915_OVERLAY_YUV422:
546 return width << 1;
547 default:
548 return -EINVAL;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200549 }
550}
551
552static int uv_hsubsampling(u32 format)
553{
554 switch (format & I915_OVERLAY_DEPTH_MASK) {
Chris Wilson722506f2010-08-12 09:28:50 +0100555 case I915_OVERLAY_YUV422:
556 case I915_OVERLAY_YUV420:
557 return 2;
558 case I915_OVERLAY_YUV411:
559 case I915_OVERLAY_YUV410:
560 return 4;
561 default:
562 return -EINVAL;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200563 }
564}
565
566static int uv_vsubsampling(u32 format)
567{
568 switch (format & I915_OVERLAY_DEPTH_MASK) {
Chris Wilson722506f2010-08-12 09:28:50 +0100569 case I915_OVERLAY_YUV420:
570 case I915_OVERLAY_YUV410:
571 return 2;
572 case I915_OVERLAY_YUV422:
573 case I915_OVERLAY_YUV411:
574 return 1;
575 default:
576 return -EINVAL;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200577 }
578}
579
580static u32 calc_swidthsw(struct drm_device *dev, u32 offset, u32 width)
581{
582 u32 mask, shift, ret;
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100583 if (IS_GEN2(dev)) {
Daniel Vetter02e792f2009-09-15 22:57:34 +0200584 mask = 0x1f;
585 shift = 5;
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100586 } else {
587 mask = 0x3f;
588 shift = 6;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200589 }
590 ret = ((offset + width + mask) >> shift) - (offset >> shift);
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100591 if (!IS_GEN2(dev))
Daniel Vetter02e792f2009-09-15 22:57:34 +0200592 ret <<= 1;
593 ret -=1;
594 return ret << 2;
595}
596
597static const u16 y_static_hcoeffs[N_HORIZ_Y_TAPS * N_PHASES] = {
598 0x3000, 0xb4a0, 0x1930, 0x1920, 0xb4a0,
599 0x3000, 0xb500, 0x19d0, 0x1880, 0xb440,
600 0x3000, 0xb540, 0x1a88, 0x2f80, 0xb3e0,
601 0x3000, 0xb580, 0x1b30, 0x2e20, 0xb380,
602 0x3000, 0xb5c0, 0x1bd8, 0x2cc0, 0xb320,
603 0x3020, 0xb5e0, 0x1c60, 0x2b80, 0xb2c0,
604 0x3020, 0xb5e0, 0x1cf8, 0x2a20, 0xb260,
605 0x3020, 0xb5e0, 0x1d80, 0x28e0, 0xb200,
606 0x3020, 0xb5c0, 0x1e08, 0x3f40, 0xb1c0,
607 0x3020, 0xb580, 0x1e78, 0x3ce0, 0xb160,
608 0x3040, 0xb520, 0x1ed8, 0x3aa0, 0xb120,
609 0x3040, 0xb4a0, 0x1f30, 0x3880, 0xb0e0,
610 0x3040, 0xb400, 0x1f78, 0x3680, 0xb0a0,
611 0x3020, 0xb340, 0x1fb8, 0x34a0, 0xb060,
612 0x3020, 0xb240, 0x1fe0, 0x32e0, 0xb040,
613 0x3020, 0xb140, 0x1ff8, 0x3160, 0xb020,
Chris Wilson722506f2010-08-12 09:28:50 +0100614 0xb000, 0x3000, 0x0800, 0x3000, 0xb000
615};
616
Daniel Vetter02e792f2009-09-15 22:57:34 +0200617static const u16 uv_static_hcoeffs[N_HORIZ_UV_TAPS * N_PHASES] = {
618 0x3000, 0x1800, 0x1800, 0xb000, 0x18d0, 0x2e60,
619 0xb000, 0x1990, 0x2ce0, 0xb020, 0x1a68, 0x2b40,
620 0xb040, 0x1b20, 0x29e0, 0xb060, 0x1bd8, 0x2880,
621 0xb080, 0x1c88, 0x3e60, 0xb0a0, 0x1d28, 0x3c00,
622 0xb0c0, 0x1db8, 0x39e0, 0xb0e0, 0x1e40, 0x37e0,
623 0xb100, 0x1eb8, 0x3620, 0xb100, 0x1f18, 0x34a0,
624 0xb100, 0x1f68, 0x3360, 0xb0e0, 0x1fa8, 0x3240,
625 0xb0c0, 0x1fe0, 0x3140, 0xb060, 0x1ff0, 0x30a0,
Chris Wilson722506f2010-08-12 09:28:50 +0100626 0x3000, 0x0800, 0x3000
627};
Daniel Vetter02e792f2009-09-15 22:57:34 +0200628
629static void update_polyphase_filter(struct overlay_registers *regs)
630{
631 memcpy(regs->Y_HCOEFS, y_static_hcoeffs, sizeof(y_static_hcoeffs));
632 memcpy(regs->UV_HCOEFS, uv_static_hcoeffs, sizeof(uv_static_hcoeffs));
633}
634
635static bool update_scaling_factors(struct intel_overlay *overlay,
636 struct overlay_registers *regs,
637 struct put_image_params *params)
638{
639 /* fixed point with a 12 bit shift */
640 u32 xscale, yscale, xscale_UV, yscale_UV;
641#define FP_SHIFT 12
642#define FRACT_MASK 0xfff
643 bool scale_changed = false;
644 int uv_hscale = uv_hsubsampling(params->format);
645 int uv_vscale = uv_vsubsampling(params->format);
646
647 if (params->dst_w > 1)
648 xscale = ((params->src_scan_w - 1) << FP_SHIFT)
649 /(params->dst_w);
650 else
651 xscale = 1 << FP_SHIFT;
652
653 if (params->dst_h > 1)
654 yscale = ((params->src_scan_h - 1) << FP_SHIFT)
655 /(params->dst_h);
656 else
657 yscale = 1 << FP_SHIFT;
658
659 /*if (params->format & I915_OVERLAY_YUV_PLANAR) {*/
Chris Wilson722506f2010-08-12 09:28:50 +0100660 xscale_UV = xscale/uv_hscale;
661 yscale_UV = yscale/uv_vscale;
662 /* make the Y scale to UV scale ratio an exact multiply */
663 xscale = xscale_UV * uv_hscale;
664 yscale = yscale_UV * uv_vscale;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200665 /*} else {
Chris Wilson722506f2010-08-12 09:28:50 +0100666 xscale_UV = 0;
667 yscale_UV = 0;
668 }*/
Daniel Vetter02e792f2009-09-15 22:57:34 +0200669
670 if (xscale != overlay->old_xscale || yscale != overlay->old_yscale)
671 scale_changed = true;
672 overlay->old_xscale = xscale;
673 overlay->old_yscale = yscale;
674
Chris Wilson722506f2010-08-12 09:28:50 +0100675 regs->YRGBSCALE = (((yscale & FRACT_MASK) << 20) |
676 ((xscale >> FP_SHIFT) << 16) |
677 ((xscale & FRACT_MASK) << 3));
678
679 regs->UVSCALE = (((yscale_UV & FRACT_MASK) << 20) |
680 ((xscale_UV >> FP_SHIFT) << 16) |
681 ((xscale_UV & FRACT_MASK) << 3));
682
683 regs->UVSCALEV = ((((yscale >> FP_SHIFT) << 16) |
684 ((yscale_UV >> FP_SHIFT) << 0)));
Daniel Vetter02e792f2009-09-15 22:57:34 +0200685
686 if (scale_changed)
687 update_polyphase_filter(regs);
688
689 return scale_changed;
690}
691
692static void update_colorkey(struct intel_overlay *overlay,
693 struct overlay_registers *regs)
694{
695 u32 key = overlay->color_key;
Chris Wilson6ba3ddd2010-08-12 09:30:58 +0100696
Daniel Vetter02e792f2009-09-15 22:57:34 +0200697 switch (overlay->crtc->base.fb->bits_per_pixel) {
Chris Wilson722506f2010-08-12 09:28:50 +0100698 case 8:
699 regs->DCLRKV = 0;
700 regs->DCLRKM = CLK_RGB8I_MASK | DST_KEY_ENABLE;
Chris Wilson6ba3ddd2010-08-12 09:30:58 +0100701 break;
702
Chris Wilson722506f2010-08-12 09:28:50 +0100703 case 16:
704 if (overlay->crtc->base.fb->depth == 15) {
705 regs->DCLRKV = RGB15_TO_COLORKEY(key);
706 regs->DCLRKM = CLK_RGB15_MASK | DST_KEY_ENABLE;
707 } else {
708 regs->DCLRKV = RGB16_TO_COLORKEY(key);
709 regs->DCLRKM = CLK_RGB16_MASK | DST_KEY_ENABLE;
710 }
Chris Wilson6ba3ddd2010-08-12 09:30:58 +0100711 break;
712
Chris Wilson722506f2010-08-12 09:28:50 +0100713 case 24:
714 case 32:
715 regs->DCLRKV = key;
716 regs->DCLRKM = CLK_RGB24_MASK | DST_KEY_ENABLE;
Chris Wilson6ba3ddd2010-08-12 09:30:58 +0100717 break;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200718 }
719}
720
721static u32 overlay_cmd_reg(struct put_image_params *params)
722{
723 u32 cmd = OCMD_ENABLE | OCMD_BUF_TYPE_FRAME | OCMD_BUFFER0;
724
725 if (params->format & I915_OVERLAY_YUV_PLANAR) {
726 switch (params->format & I915_OVERLAY_DEPTH_MASK) {
Chris Wilson722506f2010-08-12 09:28:50 +0100727 case I915_OVERLAY_YUV422:
728 cmd |= OCMD_YUV_422_PLANAR;
729 break;
730 case I915_OVERLAY_YUV420:
731 cmd |= OCMD_YUV_420_PLANAR;
732 break;
733 case I915_OVERLAY_YUV411:
734 case I915_OVERLAY_YUV410:
735 cmd |= OCMD_YUV_410_PLANAR;
736 break;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200737 }
738 } else { /* YUV packed */
739 switch (params->format & I915_OVERLAY_DEPTH_MASK) {
Chris Wilson722506f2010-08-12 09:28:50 +0100740 case I915_OVERLAY_YUV422:
741 cmd |= OCMD_YUV_422_PACKED;
742 break;
743 case I915_OVERLAY_YUV411:
744 cmd |= OCMD_YUV_411_PACKED;
745 break;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200746 }
747
748 switch (params->format & I915_OVERLAY_SWAP_MASK) {
Chris Wilson722506f2010-08-12 09:28:50 +0100749 case I915_OVERLAY_NO_SWAP:
750 break;
751 case I915_OVERLAY_UV_SWAP:
752 cmd |= OCMD_UV_SWAP;
753 break;
754 case I915_OVERLAY_Y_SWAP:
755 cmd |= OCMD_Y_SWAP;
756 break;
757 case I915_OVERLAY_Y_AND_UV_SWAP:
758 cmd |= OCMD_Y_AND_UV_SWAP;
759 break;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200760 }
761 }
762
763 return cmd;
764}
765
Chris Wilson5fe82c52010-08-12 12:38:21 +0100766static int intel_overlay_do_put_image(struct intel_overlay *overlay,
767 struct drm_gem_object *new_bo,
768 struct put_image_params *params)
Daniel Vetter02e792f2009-09-15 22:57:34 +0200769{
770 int ret, tmp_width;
771 struct overlay_registers *regs;
772 bool scale_changed = false;
Daniel Vetter23010e42010-03-08 13:35:02 +0100773 struct drm_i915_gem_object *bo_priv = to_intel_bo(new_bo);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200774 struct drm_device *dev = overlay->dev;
775
776 BUG_ON(!mutex_is_locked(&dev->struct_mutex));
777 BUG_ON(!mutex_is_locked(&dev->mode_config.mutex));
778 BUG_ON(!overlay);
779
Daniel Vetter02e792f2009-09-15 22:57:34 +0200780 ret = intel_overlay_release_old_vid(overlay);
781 if (ret != 0)
782 return ret;
783
Daniel Vetter75e9e912010-11-04 17:11:09 +0100784 ret = i915_gem_object_pin(new_bo, PAGE_SIZE, true);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200785 if (ret != 0)
786 return ret;
787
788 ret = i915_gem_object_set_to_gtt_domain(new_bo, 0);
789 if (ret != 0)
790 goto out_unpin;
791
792 if (!overlay->active) {
Chris Wilson8d74f652010-08-12 10:35:26 +0100793 regs = intel_overlay_map_regs(overlay);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200794 if (!regs) {
795 ret = -ENOMEM;
796 goto out_unpin;
797 }
798 regs->OCONFIG = OCONF_CC_OUT_8BIT;
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100799 if (IS_GEN4(overlay->dev))
Daniel Vetter02e792f2009-09-15 22:57:34 +0200800 regs->OCONFIG |= OCONF_CSC_MODE_BT709;
801 regs->OCONFIG |= overlay->crtc->pipe == 0 ?
802 OCONF_PIPE_A : OCONF_PIPE_B;
Chris Wilson9bb2ff72010-08-12 12:02:11 +0100803 intel_overlay_unmap_regs(overlay, regs);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200804
805 ret = intel_overlay_on(overlay);
806 if (ret != 0)
807 goto out_unpin;
808 }
809
Chris Wilson8d74f652010-08-12 10:35:26 +0100810 regs = intel_overlay_map_regs(overlay);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200811 if (!regs) {
812 ret = -ENOMEM;
813 goto out_unpin;
814 }
815
816 regs->DWINPOS = (params->dst_y << 16) | params->dst_x;
817 regs->DWINSZ = (params->dst_h << 16) | params->dst_w;
818
819 if (params->format & I915_OVERLAY_YUV_PACKED)
820 tmp_width = packed_width_bytes(params->format, params->src_w);
821 else
822 tmp_width = params->src_w;
823
824 regs->SWIDTH = params->src_w;
825 regs->SWIDTHSW = calc_swidthsw(overlay->dev,
Chris Wilson722506f2010-08-12 09:28:50 +0100826 params->offset_Y, tmp_width);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200827 regs->SHEIGHT = params->src_h;
828 regs->OBUF_0Y = bo_priv->gtt_offset + params-> offset_Y;
829 regs->OSTRIDE = params->stride_Y;
830
831 if (params->format & I915_OVERLAY_YUV_PLANAR) {
832 int uv_hscale = uv_hsubsampling(params->format);
833 int uv_vscale = uv_vsubsampling(params->format);
834 u32 tmp_U, tmp_V;
835 regs->SWIDTH |= (params->src_w/uv_hscale) << 16;
836 tmp_U = calc_swidthsw(overlay->dev, params->offset_U,
Chris Wilson722506f2010-08-12 09:28:50 +0100837 params->src_w/uv_hscale);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200838 tmp_V = calc_swidthsw(overlay->dev, params->offset_V,
Chris Wilson722506f2010-08-12 09:28:50 +0100839 params->src_w/uv_hscale);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200840 regs->SWIDTHSW |= max_t(u32, tmp_U, tmp_V) << 16;
841 regs->SHEIGHT |= (params->src_h/uv_vscale) << 16;
842 regs->OBUF_0U = bo_priv->gtt_offset + params->offset_U;
843 regs->OBUF_0V = bo_priv->gtt_offset + params->offset_V;
844 regs->OSTRIDE |= params->stride_UV << 16;
845 }
846
847 scale_changed = update_scaling_factors(overlay, regs, params);
848
849 update_colorkey(overlay, regs);
850
851 regs->OCMD = overlay_cmd_reg(params);
852
Chris Wilson9bb2ff72010-08-12 12:02:11 +0100853 intel_overlay_unmap_regs(overlay, regs);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200854
Chris Wilson8dc5d142010-08-12 12:36:12 +0100855 ret = intel_overlay_continue(overlay, scale_changed);
856 if (ret)
857 goto out_unpin;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200858
859 overlay->old_vid_bo = overlay->vid_bo;
Daniel Vetter23010e42010-03-08 13:35:02 +0100860 overlay->vid_bo = to_intel_bo(new_bo);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200861
862 return 0;
863
864out_unpin:
865 i915_gem_object_unpin(new_bo);
866 return ret;
867}
868
Chris Wilson5dcdbcb2010-08-12 13:50:28 +0100869int intel_overlay_switch_off(struct intel_overlay *overlay,
870 bool interruptible)
Daniel Vetter02e792f2009-09-15 22:57:34 +0200871{
Daniel Vetter02e792f2009-09-15 22:57:34 +0200872 struct overlay_registers *regs;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200873 struct drm_device *dev = overlay->dev;
Chris Wilson5dcdbcb2010-08-12 13:50:28 +0100874 int ret;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200875
876 BUG_ON(!mutex_is_locked(&dev->struct_mutex));
877 BUG_ON(!mutex_is_locked(&dev->mode_config.mutex));
878
Chris Wilsonb303cf92010-08-12 14:03:48 +0100879 ret = intel_overlay_recover_from_interrupt(overlay, interruptible);
880 if (ret != 0)
881 return ret;
Daniel Vetter9bedb972009-11-30 15:55:49 +0100882
Daniel Vetter02e792f2009-09-15 22:57:34 +0200883 if (!overlay->active)
884 return 0;
885
Daniel Vetter02e792f2009-09-15 22:57:34 +0200886 ret = intel_overlay_release_old_vid(overlay);
887 if (ret != 0)
888 return ret;
889
Chris Wilson8d74f652010-08-12 10:35:26 +0100890 regs = intel_overlay_map_regs(overlay);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200891 regs->OCMD = 0;
Chris Wilson9bb2ff72010-08-12 12:02:11 +0100892 intel_overlay_unmap_regs(overlay, regs);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200893
Chris Wilson5dcdbcb2010-08-12 13:50:28 +0100894 ret = intel_overlay_off(overlay, interruptible);
Daniel Vetter03f77ea2009-09-15 22:57:37 +0200895 if (ret != 0)
896 return ret;
897
Daniel Vetter12ca45f2037-04-25 10:08:26 +0200898 intel_overlay_off_tail(overlay);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200899 return 0;
900}
901
902static int check_overlay_possible_on_crtc(struct intel_overlay *overlay,
903 struct intel_crtc *crtc)
904{
Chris Wilson722506f2010-08-12 09:28:50 +0100905 drm_i915_private_t *dev_priv = overlay->dev->dev_private;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200906
Chris Wilsonf7abfe82010-09-13 14:19:16 +0100907 if (!crtc->active)
Daniel Vetter02e792f2009-09-15 22:57:34 +0200908 return -EINVAL;
909
Daniel Vetter02e792f2009-09-15 22:57:34 +0200910 /* can't use the overlay with double wide pipe */
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100911 if (INTEL_INFO(overlay->dev)->gen < 4 &&
Chris Wilsonf7abfe82010-09-13 14:19:16 +0100912 (I915_READ(PIPECONF(crtc->pipe)) & (PIPECONF_DOUBLE_WIDE | PIPECONF_ENABLE)) != PIPECONF_ENABLE)
Daniel Vetter02e792f2009-09-15 22:57:34 +0200913 return -EINVAL;
914
915 return 0;
916}
917
918static void update_pfit_vscale_ratio(struct intel_overlay *overlay)
919{
920 struct drm_device *dev = overlay->dev;
Chris Wilson722506f2010-08-12 09:28:50 +0100921 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200922 u32 pfit_control = I915_READ(PFIT_CONTROL);
Chris Wilson446d2182010-08-12 11:15:58 +0100923 u32 ratio;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200924
925 /* XXX: This is not the same logic as in the xorg driver, but more in
Chris Wilson446d2182010-08-12 11:15:58 +0100926 * line with the intel documentation for the i965
927 */
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100928 if (INTEL_INFO(dev)->gen >= 4) {
929 /* on i965 use the PGM reg to read out the autoscaler values */
930 ratio = I915_READ(PFIT_PGM_RATIOS) >> PFIT_VERT_SCALE_SHIFT_965;
931 } else {
Chris Wilson446d2182010-08-12 11:15:58 +0100932 if (pfit_control & VERT_AUTO_SCALE)
933 ratio = I915_READ(PFIT_AUTO_RATIOS);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200934 else
Chris Wilson446d2182010-08-12 11:15:58 +0100935 ratio = I915_READ(PFIT_PGM_RATIOS);
936 ratio >>= PFIT_VERT_SCALE_SHIFT;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200937 }
938
939 overlay->pfit_vscale_ratio = ratio;
940}
941
942static int check_overlay_dst(struct intel_overlay *overlay,
943 struct drm_intel_overlay_put_image *rec)
944{
945 struct drm_display_mode *mode = &overlay->crtc->base.mode;
946
Chris Wilson722506f2010-08-12 09:28:50 +0100947 if (rec->dst_x < mode->crtc_hdisplay &&
948 rec->dst_x + rec->dst_width <= mode->crtc_hdisplay &&
949 rec->dst_y < mode->crtc_vdisplay &&
950 rec->dst_y + rec->dst_height <= mode->crtc_vdisplay)
Daniel Vetter02e792f2009-09-15 22:57:34 +0200951 return 0;
952 else
953 return -EINVAL;
954}
955
956static int check_overlay_scaling(struct put_image_params *rec)
957{
958 u32 tmp;
959
960 /* downscaling limit is 8.0 */
961 tmp = ((rec->src_scan_h << 16) / rec->dst_h) >> 16;
962 if (tmp > 7)
963 return -EINVAL;
964 tmp = ((rec->src_scan_w << 16) / rec->dst_w) >> 16;
965 if (tmp > 7)
966 return -EINVAL;
967
968 return 0;
969}
970
971static int check_overlay_src(struct drm_device *dev,
972 struct drm_intel_overlay_put_image *rec,
973 struct drm_gem_object *new_bo)
974{
Daniel Vetter02e792f2009-09-15 22:57:34 +0200975 int uv_hscale = uv_hsubsampling(rec->flags);
976 int uv_vscale = uv_vsubsampling(rec->flags);
Dan Carpenter8f28f542010-10-27 23:17:25 +0200977 u32 stride_mask;
978 int depth;
979 u32 tmp;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200980
981 /* check src dimensions */
982 if (IS_845G(dev) || IS_I830(dev)) {
Chris Wilson722506f2010-08-12 09:28:50 +0100983 if (rec->src_height > IMAGE_MAX_HEIGHT_LEGACY ||
Chris Wilson9f7c3f42010-08-12 11:29:34 +0100984 rec->src_width > IMAGE_MAX_WIDTH_LEGACY)
Daniel Vetter02e792f2009-09-15 22:57:34 +0200985 return -EINVAL;
986 } else {
Chris Wilson722506f2010-08-12 09:28:50 +0100987 if (rec->src_height > IMAGE_MAX_HEIGHT ||
Chris Wilson9f7c3f42010-08-12 11:29:34 +0100988 rec->src_width > IMAGE_MAX_WIDTH)
Daniel Vetter02e792f2009-09-15 22:57:34 +0200989 return -EINVAL;
990 }
Chris Wilson9f7c3f42010-08-12 11:29:34 +0100991
Daniel Vetter02e792f2009-09-15 22:57:34 +0200992 /* better safe than sorry, use 4 as the maximal subsampling ratio */
Chris Wilson722506f2010-08-12 09:28:50 +0100993 if (rec->src_height < N_VERT_Y_TAPS*4 ||
Chris Wilson9f7c3f42010-08-12 11:29:34 +0100994 rec->src_width < N_HORIZ_Y_TAPS*4)
Daniel Vetter02e792f2009-09-15 22:57:34 +0200995 return -EINVAL;
996
Chris Wilsona1efd142010-07-12 19:35:38 +0100997 /* check alignment constraints */
Daniel Vetter02e792f2009-09-15 22:57:34 +0200998 switch (rec->flags & I915_OVERLAY_TYPE_MASK) {
Chris Wilson722506f2010-08-12 09:28:50 +0100999 case I915_OVERLAY_RGB:
1000 /* not implemented */
1001 return -EINVAL;
Chris Wilson9f7c3f42010-08-12 11:29:34 +01001002
Chris Wilson722506f2010-08-12 09:28:50 +01001003 case I915_OVERLAY_YUV_PACKED:
Chris Wilson722506f2010-08-12 09:28:50 +01001004 if (uv_vscale != 1)
Daniel Vetter02e792f2009-09-15 22:57:34 +02001005 return -EINVAL;
Chris Wilson9f7c3f42010-08-12 11:29:34 +01001006
1007 depth = packed_depth_bytes(rec->flags);
Chris Wilson722506f2010-08-12 09:28:50 +01001008 if (depth < 0)
1009 return depth;
Chris Wilson9f7c3f42010-08-12 11:29:34 +01001010
Chris Wilson722506f2010-08-12 09:28:50 +01001011 /* ignore UV planes */
1012 rec->stride_UV = 0;
1013 rec->offset_U = 0;
1014 rec->offset_V = 0;
1015 /* check pixel alignment */
1016 if (rec->offset_Y % depth)
Daniel Vetter02e792f2009-09-15 22:57:34 +02001017 return -EINVAL;
Chris Wilson722506f2010-08-12 09:28:50 +01001018 break;
Chris Wilson9f7c3f42010-08-12 11:29:34 +01001019
Chris Wilson722506f2010-08-12 09:28:50 +01001020 case I915_OVERLAY_YUV_PLANAR:
1021 if (uv_vscale < 0 || uv_hscale < 0)
1022 return -EINVAL;
1023 /* no offset restrictions for planar formats */
1024 break;
Chris Wilson9f7c3f42010-08-12 11:29:34 +01001025
Chris Wilson722506f2010-08-12 09:28:50 +01001026 default:
1027 return -EINVAL;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001028 }
1029
1030 if (rec->src_width % uv_hscale)
1031 return -EINVAL;
1032
1033 /* stride checking */
Chris Wilsona1efd142010-07-12 19:35:38 +01001034 if (IS_I830(dev) || IS_845G(dev))
1035 stride_mask = 255;
1036 else
1037 stride_mask = 63;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001038
1039 if (rec->stride_Y & stride_mask || rec->stride_UV & stride_mask)
1040 return -EINVAL;
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001041 if (IS_GEN4(dev) && rec->stride_Y < 512)
Daniel Vetter02e792f2009-09-15 22:57:34 +02001042 return -EINVAL;
1043
1044 tmp = (rec->flags & I915_OVERLAY_TYPE_MASK) == I915_OVERLAY_YUV_PLANAR ?
Chris Wilson9f7c3f42010-08-12 11:29:34 +01001045 4096 : 8192;
1046 if (rec->stride_Y > tmp || rec->stride_UV > 2*1024)
Daniel Vetter02e792f2009-09-15 22:57:34 +02001047 return -EINVAL;
1048
1049 /* check buffer dimensions */
1050 switch (rec->flags & I915_OVERLAY_TYPE_MASK) {
Chris Wilson722506f2010-08-12 09:28:50 +01001051 case I915_OVERLAY_RGB:
1052 case I915_OVERLAY_YUV_PACKED:
1053 /* always 4 Y values per depth pixels */
1054 if (packed_width_bytes(rec->flags, rec->src_width) > rec->stride_Y)
1055 return -EINVAL;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001056
Chris Wilson722506f2010-08-12 09:28:50 +01001057 tmp = rec->stride_Y*rec->src_height;
1058 if (rec->offset_Y + tmp > new_bo->size)
1059 return -EINVAL;
1060 break;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001061
Chris Wilson722506f2010-08-12 09:28:50 +01001062 case I915_OVERLAY_YUV_PLANAR:
1063 if (rec->src_width > rec->stride_Y)
1064 return -EINVAL;
1065 if (rec->src_width/uv_hscale > rec->stride_UV)
1066 return -EINVAL;
1067
Chris Wilson9f7c3f42010-08-12 11:29:34 +01001068 tmp = rec->stride_Y * rec->src_height;
Chris Wilson722506f2010-08-12 09:28:50 +01001069 if (rec->offset_Y + tmp > new_bo->size)
1070 return -EINVAL;
Chris Wilson9f7c3f42010-08-12 11:29:34 +01001071
1072 tmp = rec->stride_UV * (rec->src_height / uv_vscale);
Chris Wilson722506f2010-08-12 09:28:50 +01001073 if (rec->offset_U + tmp > new_bo->size ||
1074 rec->offset_V + tmp > new_bo->size)
1075 return -EINVAL;
1076 break;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001077 }
1078
1079 return 0;
1080}
1081
Chris Wilsone9e331a2010-09-13 01:16:10 +01001082/**
1083 * Return the pipe currently connected to the panel fitter,
1084 * or -1 if the panel fitter is not present or not in use
1085 */
1086static int intel_panel_fitter_pipe(struct drm_device *dev)
1087{
1088 struct drm_i915_private *dev_priv = dev->dev_private;
1089 u32 pfit_control;
1090
1091 /* i830 doesn't have a panel fitter */
1092 if (IS_I830(dev))
1093 return -1;
1094
1095 pfit_control = I915_READ(PFIT_CONTROL);
1096
1097 /* See if the panel fitter is in use */
1098 if ((pfit_control & PFIT_ENABLE) == 0)
1099 return -1;
1100
1101 /* 965 can place panel fitter on either pipe */
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001102 if (IS_GEN4(dev))
Chris Wilsone9e331a2010-09-13 01:16:10 +01001103 return (pfit_control >> 29) & 0x3;
1104
1105 /* older chips can only use pipe 1 */
1106 return 1;
1107}
1108
Daniel Vetter02e792f2009-09-15 22:57:34 +02001109int intel_overlay_put_image(struct drm_device *dev, void *data,
1110 struct drm_file *file_priv)
1111{
1112 struct drm_intel_overlay_put_image *put_image_rec = data;
1113 drm_i915_private_t *dev_priv = dev->dev_private;
1114 struct intel_overlay *overlay;
1115 struct drm_mode_object *drmmode_obj;
1116 struct intel_crtc *crtc;
1117 struct drm_gem_object *new_bo;
1118 struct put_image_params *params;
1119 int ret;
1120
1121 if (!dev_priv) {
1122 DRM_ERROR("called with no initialization\n");
1123 return -EINVAL;
1124 }
1125
1126 overlay = dev_priv->overlay;
1127 if (!overlay) {
1128 DRM_DEBUG("userspace bug: no overlay\n");
1129 return -ENODEV;
1130 }
1131
1132 if (!(put_image_rec->flags & I915_OVERLAY_ENABLE)) {
1133 mutex_lock(&dev->mode_config.mutex);
1134 mutex_lock(&dev->struct_mutex);
1135
Chris Wilson5dcdbcb2010-08-12 13:50:28 +01001136 ret = intel_overlay_switch_off(overlay, true);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001137
1138 mutex_unlock(&dev->struct_mutex);
1139 mutex_unlock(&dev->mode_config.mutex);
1140
1141 return ret;
1142 }
1143
1144 params = kmalloc(sizeof(struct put_image_params), GFP_KERNEL);
1145 if (!params)
1146 return -ENOMEM;
1147
1148 drmmode_obj = drm_mode_object_find(dev, put_image_rec->crtc_id,
Chris Wilson722506f2010-08-12 09:28:50 +01001149 DRM_MODE_OBJECT_CRTC);
Dan Carpenter915a4282010-03-06 14:05:39 +03001150 if (!drmmode_obj) {
1151 ret = -ENOENT;
1152 goto out_free;
1153 }
Daniel Vetter02e792f2009-09-15 22:57:34 +02001154 crtc = to_intel_crtc(obj_to_crtc(drmmode_obj));
1155
1156 new_bo = drm_gem_object_lookup(dev, file_priv,
Chris Wilson722506f2010-08-12 09:28:50 +01001157 put_image_rec->bo_handle);
Dan Carpenter915a4282010-03-06 14:05:39 +03001158 if (!new_bo) {
1159 ret = -ENOENT;
1160 goto out_free;
1161 }
Daniel Vetter02e792f2009-09-15 22:57:34 +02001162
1163 mutex_lock(&dev->mode_config.mutex);
1164 mutex_lock(&dev->struct_mutex);
1165
Chris Wilsonb303cf92010-08-12 14:03:48 +01001166 ret = intel_overlay_recover_from_interrupt(overlay, true);
1167 if (ret != 0)
1168 goto out_unlock;
Daniel Vetter03f77ea2009-09-15 22:57:37 +02001169
Daniel Vetter02e792f2009-09-15 22:57:34 +02001170 if (overlay->crtc != crtc) {
1171 struct drm_display_mode *mode = &crtc->base.mode;
Chris Wilson5dcdbcb2010-08-12 13:50:28 +01001172 ret = intel_overlay_switch_off(overlay, true);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001173 if (ret != 0)
1174 goto out_unlock;
1175
1176 ret = check_overlay_possible_on_crtc(overlay, crtc);
1177 if (ret != 0)
1178 goto out_unlock;
1179
1180 overlay->crtc = crtc;
1181 crtc->overlay = overlay;
1182
Chris Wilsone9e331a2010-09-13 01:16:10 +01001183 /* line too wide, i.e. one-line-mode */
1184 if (mode->hdisplay > 1024 &&
1185 intel_panel_fitter_pipe(dev) == crtc->pipe) {
Daniel Vetter02e792f2009-09-15 22:57:34 +02001186 overlay->pfit_active = 1;
1187 update_pfit_vscale_ratio(overlay);
1188 } else
1189 overlay->pfit_active = 0;
1190 }
1191
1192 ret = check_overlay_dst(overlay, put_image_rec);
1193 if (ret != 0)
1194 goto out_unlock;
1195
1196 if (overlay->pfit_active) {
1197 params->dst_y = ((((u32)put_image_rec->dst_y) << 12) /
Chris Wilson722506f2010-08-12 09:28:50 +01001198 overlay->pfit_vscale_ratio);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001199 /* shifting right rounds downwards, so add 1 */
1200 params->dst_h = ((((u32)put_image_rec->dst_height) << 12) /
Chris Wilson722506f2010-08-12 09:28:50 +01001201 overlay->pfit_vscale_ratio) + 1;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001202 } else {
1203 params->dst_y = put_image_rec->dst_y;
1204 params->dst_h = put_image_rec->dst_height;
1205 }
1206 params->dst_x = put_image_rec->dst_x;
1207 params->dst_w = put_image_rec->dst_width;
1208
1209 params->src_w = put_image_rec->src_width;
1210 params->src_h = put_image_rec->src_height;
1211 params->src_scan_w = put_image_rec->src_scan_width;
1212 params->src_scan_h = put_image_rec->src_scan_height;
Chris Wilson722506f2010-08-12 09:28:50 +01001213 if (params->src_scan_h > params->src_h ||
1214 params->src_scan_w > params->src_w) {
Daniel Vetter02e792f2009-09-15 22:57:34 +02001215 ret = -EINVAL;
1216 goto out_unlock;
1217 }
1218
1219 ret = check_overlay_src(dev, put_image_rec, new_bo);
1220 if (ret != 0)
1221 goto out_unlock;
1222 params->format = put_image_rec->flags & ~I915_OVERLAY_FLAGS_MASK;
1223 params->stride_Y = put_image_rec->stride_Y;
1224 params->stride_UV = put_image_rec->stride_UV;
1225 params->offset_Y = put_image_rec->offset_Y;
1226 params->offset_U = put_image_rec->offset_U;
1227 params->offset_V = put_image_rec->offset_V;
1228
1229 /* Check scaling after src size to prevent a divide-by-zero. */
1230 ret = check_overlay_scaling(params);
1231 if (ret != 0)
1232 goto out_unlock;
1233
1234 ret = intel_overlay_do_put_image(overlay, new_bo, params);
1235 if (ret != 0)
1236 goto out_unlock;
1237
1238 mutex_unlock(&dev->struct_mutex);
1239 mutex_unlock(&dev->mode_config.mutex);
1240
1241 kfree(params);
1242
1243 return 0;
1244
1245out_unlock:
1246 mutex_unlock(&dev->struct_mutex);
1247 mutex_unlock(&dev->mode_config.mutex);
Luca Barbieribc9025b2010-02-09 05:49:12 +00001248 drm_gem_object_unreference_unlocked(new_bo);
Dan Carpenter915a4282010-03-06 14:05:39 +03001249out_free:
Daniel Vetter02e792f2009-09-15 22:57:34 +02001250 kfree(params);
1251
1252 return ret;
1253}
1254
1255static void update_reg_attrs(struct intel_overlay *overlay,
1256 struct overlay_registers *regs)
1257{
1258 regs->OCLRC0 = (overlay->contrast << 18) | (overlay->brightness & 0xff);
1259 regs->OCLRC1 = overlay->saturation;
1260}
1261
1262static bool check_gamma_bounds(u32 gamma1, u32 gamma2)
1263{
1264 int i;
1265
1266 if (gamma1 & 0xff000000 || gamma2 & 0xff000000)
1267 return false;
1268
1269 for (i = 0; i < 3; i++) {
Chris Wilson722506f2010-08-12 09:28:50 +01001270 if (((gamma1 >> i*8) & 0xff) >= ((gamma2 >> i*8) & 0xff))
Daniel Vetter02e792f2009-09-15 22:57:34 +02001271 return false;
1272 }
1273
1274 return true;
1275}
1276
1277static bool check_gamma5_errata(u32 gamma5)
1278{
1279 int i;
1280
1281 for (i = 0; i < 3; i++) {
1282 if (((gamma5 >> i*8) & 0xff) == 0x80)
1283 return false;
1284 }
1285
1286 return true;
1287}
1288
1289static int check_gamma(struct drm_intel_overlay_attrs *attrs)
1290{
Chris Wilson722506f2010-08-12 09:28:50 +01001291 if (!check_gamma_bounds(0, attrs->gamma0) ||
1292 !check_gamma_bounds(attrs->gamma0, attrs->gamma1) ||
1293 !check_gamma_bounds(attrs->gamma1, attrs->gamma2) ||
1294 !check_gamma_bounds(attrs->gamma2, attrs->gamma3) ||
1295 !check_gamma_bounds(attrs->gamma3, attrs->gamma4) ||
1296 !check_gamma_bounds(attrs->gamma4, attrs->gamma5) ||
1297 !check_gamma_bounds(attrs->gamma5, 0x00ffffff))
Daniel Vetter02e792f2009-09-15 22:57:34 +02001298 return -EINVAL;
Chris Wilson722506f2010-08-12 09:28:50 +01001299
Daniel Vetter02e792f2009-09-15 22:57:34 +02001300 if (!check_gamma5_errata(attrs->gamma5))
1301 return -EINVAL;
Chris Wilson722506f2010-08-12 09:28:50 +01001302
Daniel Vetter02e792f2009-09-15 22:57:34 +02001303 return 0;
1304}
1305
1306int intel_overlay_attrs(struct drm_device *dev, void *data,
1307 struct drm_file *file_priv)
1308{
1309 struct drm_intel_overlay_attrs *attrs = data;
1310 drm_i915_private_t *dev_priv = dev->dev_private;
1311 struct intel_overlay *overlay;
1312 struct overlay_registers *regs;
1313 int ret;
1314
1315 if (!dev_priv) {
1316 DRM_ERROR("called with no initialization\n");
1317 return -EINVAL;
1318 }
1319
1320 overlay = dev_priv->overlay;
1321 if (!overlay) {
1322 DRM_DEBUG("userspace bug: no overlay\n");
1323 return -ENODEV;
1324 }
1325
1326 mutex_lock(&dev->mode_config.mutex);
1327 mutex_lock(&dev->struct_mutex);
1328
Chris Wilson60fc3322010-08-12 10:44:45 +01001329 ret = -EINVAL;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001330 if (!(attrs->flags & I915_OVERLAY_UPDATE_ATTRS)) {
Chris Wilson60fc3322010-08-12 10:44:45 +01001331 attrs->color_key = overlay->color_key;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001332 attrs->brightness = overlay->brightness;
Chris Wilson60fc3322010-08-12 10:44:45 +01001333 attrs->contrast = overlay->contrast;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001334 attrs->saturation = overlay->saturation;
1335
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001336 if (!IS_GEN2(dev)) {
Daniel Vetter02e792f2009-09-15 22:57:34 +02001337 attrs->gamma0 = I915_READ(OGAMC0);
1338 attrs->gamma1 = I915_READ(OGAMC1);
1339 attrs->gamma2 = I915_READ(OGAMC2);
1340 attrs->gamma3 = I915_READ(OGAMC3);
1341 attrs->gamma4 = I915_READ(OGAMC4);
1342 attrs->gamma5 = I915_READ(OGAMC5);
1343 }
Daniel Vetter02e792f2009-09-15 22:57:34 +02001344 } else {
Chris Wilson60fc3322010-08-12 10:44:45 +01001345 if (attrs->brightness < -128 || attrs->brightness > 127)
Daniel Vetter02e792f2009-09-15 22:57:34 +02001346 goto out_unlock;
Chris Wilson60fc3322010-08-12 10:44:45 +01001347 if (attrs->contrast > 255)
Daniel Vetter02e792f2009-09-15 22:57:34 +02001348 goto out_unlock;
Chris Wilson60fc3322010-08-12 10:44:45 +01001349 if (attrs->saturation > 1023)
Daniel Vetter02e792f2009-09-15 22:57:34 +02001350 goto out_unlock;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001351
Chris Wilson60fc3322010-08-12 10:44:45 +01001352 overlay->color_key = attrs->color_key;
1353 overlay->brightness = attrs->brightness;
1354 overlay->contrast = attrs->contrast;
1355 overlay->saturation = attrs->saturation;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001356
Chris Wilson8d74f652010-08-12 10:35:26 +01001357 regs = intel_overlay_map_regs(overlay);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001358 if (!regs) {
1359 ret = -ENOMEM;
1360 goto out_unlock;
1361 }
1362
1363 update_reg_attrs(overlay, regs);
1364
Chris Wilson9bb2ff72010-08-12 12:02:11 +01001365 intel_overlay_unmap_regs(overlay, regs);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001366
1367 if (attrs->flags & I915_OVERLAY_UPDATE_GAMMA) {
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001368 if (IS_GEN2(dev))
Daniel Vetter02e792f2009-09-15 22:57:34 +02001369 goto out_unlock;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001370
1371 if (overlay->active) {
1372 ret = -EBUSY;
1373 goto out_unlock;
1374 }
1375
1376 ret = check_gamma(attrs);
Chris Wilson60fc3322010-08-12 10:44:45 +01001377 if (ret)
Daniel Vetter02e792f2009-09-15 22:57:34 +02001378 goto out_unlock;
1379
1380 I915_WRITE(OGAMC0, attrs->gamma0);
1381 I915_WRITE(OGAMC1, attrs->gamma1);
1382 I915_WRITE(OGAMC2, attrs->gamma2);
1383 I915_WRITE(OGAMC3, attrs->gamma3);
1384 I915_WRITE(OGAMC4, attrs->gamma4);
1385 I915_WRITE(OGAMC5, attrs->gamma5);
1386 }
Daniel Vetter02e792f2009-09-15 22:57:34 +02001387 }
1388
Chris Wilson60fc3322010-08-12 10:44:45 +01001389 ret = 0;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001390out_unlock:
1391 mutex_unlock(&dev->struct_mutex);
1392 mutex_unlock(&dev->mode_config.mutex);
1393
1394 return ret;
1395}
1396
1397void intel_setup_overlay(struct drm_device *dev)
1398{
1399 drm_i915_private_t *dev_priv = dev->dev_private;
1400 struct intel_overlay *overlay;
1401 struct drm_gem_object *reg_bo;
1402 struct overlay_registers *regs;
1403 int ret;
1404
Chris Wilson315781482010-08-12 09:42:51 +01001405 if (!HAS_OVERLAY(dev))
Daniel Vetter02e792f2009-09-15 22:57:34 +02001406 return;
1407
1408 overlay = kzalloc(sizeof(struct intel_overlay), GFP_KERNEL);
1409 if (!overlay)
1410 return;
1411 overlay->dev = dev;
1412
Daniel Vetterac52bc52010-04-09 19:05:06 +00001413 reg_bo = i915_gem_alloc_object(dev, PAGE_SIZE);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001414 if (!reg_bo)
1415 goto out_free;
Daniel Vetter23010e42010-03-08 13:35:02 +01001416 overlay->reg_bo = to_intel_bo(reg_bo);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001417
Chris Wilson315781482010-08-12 09:42:51 +01001418 if (OVERLAY_NEEDS_PHYSICAL(dev)) {
1419 ret = i915_gem_attach_phys_object(dev, reg_bo,
1420 I915_GEM_PHYS_OVERLAY_REGS,
Chris Wilsona2930122010-08-12 10:47:56 +01001421 PAGE_SIZE);
Chris Wilson315781482010-08-12 09:42:51 +01001422 if (ret) {
1423 DRM_ERROR("failed to attach phys overlay regs\n");
1424 goto out_free_bo;
1425 }
1426 overlay->flip_addr = overlay->reg_bo->phys_obj->handle->busaddr;
1427 } else {
Daniel Vetter75e9e912010-11-04 17:11:09 +01001428 ret = i915_gem_object_pin(reg_bo, PAGE_SIZE, true);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001429 if (ret) {
1430 DRM_ERROR("failed to pin overlay register bo\n");
1431 goto out_free_bo;
1432 }
1433 overlay->flip_addr = overlay->reg_bo->gtt_offset;
Chris Wilson0ddc1282010-08-12 09:35:00 +01001434
1435 ret = i915_gem_object_set_to_gtt_domain(reg_bo, true);
1436 if (ret) {
1437 DRM_ERROR("failed to move overlay register bo into the GTT\n");
1438 goto out_unpin_bo;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001439 }
Daniel Vetter02e792f2009-09-15 22:57:34 +02001440 }
1441
1442 /* init all values */
1443 overlay->color_key = 0x0101fe;
1444 overlay->brightness = -19;
1445 overlay->contrast = 75;
1446 overlay->saturation = 146;
1447
Chris Wilson8d74f652010-08-12 10:35:26 +01001448 regs = intel_overlay_map_regs(overlay);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001449 if (!regs)
1450 goto out_free_bo;
1451
1452 memset(regs, 0, sizeof(struct overlay_registers));
1453 update_polyphase_filter(regs);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001454 update_reg_attrs(overlay, regs);
1455
Chris Wilson9bb2ff72010-08-12 12:02:11 +01001456 intel_overlay_unmap_regs(overlay, regs);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001457
1458 dev_priv->overlay = overlay;
1459 DRM_INFO("initialized overlay support\n");
1460 return;
1461
Chris Wilson0ddc1282010-08-12 09:35:00 +01001462out_unpin_bo:
1463 i915_gem_object_unpin(reg_bo);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001464out_free_bo:
1465 drm_gem_object_unreference(reg_bo);
1466out_free:
1467 kfree(overlay);
1468 return;
1469}
1470
1471void intel_cleanup_overlay(struct drm_device *dev)
1472{
Chris Wilson722506f2010-08-12 09:28:50 +01001473 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001474
Chris Wilson62cf4e62010-08-12 10:50:36 +01001475 if (!dev_priv->overlay)
1476 return;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001477
Chris Wilson62cf4e62010-08-12 10:50:36 +01001478 /* The bo's should be free'd by the generic code already.
1479 * Furthermore modesetting teardown happens beforehand so the
1480 * hardware should be off already */
1481 BUG_ON(dev_priv->overlay->active);
1482
1483 drm_gem_object_unreference_unlocked(&dev_priv->overlay->reg_bo->base);
1484 kfree(dev_priv->overlay);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001485}
Chris Wilson6ef3d422010-08-04 20:26:07 +01001486
Chris Wilson3bd3c932010-08-19 08:19:30 +01001487#ifdef CONFIG_DEBUG_FS
1488#include <linux/seq_file.h>
1489
Chris Wilson6ef3d422010-08-04 20:26:07 +01001490struct intel_overlay_error_state {
1491 struct overlay_registers regs;
1492 unsigned long base;
1493 u32 dovsta;
1494 u32 isr;
1495};
1496
Chris Wilson3bd3c932010-08-19 08:19:30 +01001497static struct overlay_registers *
Linus Torvaldsc48c43e2010-10-26 18:57:59 -07001498intel_overlay_map_regs_atomic(struct intel_overlay *overlay)
Chris Wilson3bd3c932010-08-19 08:19:30 +01001499{
Linus Torvaldsc48c43e2010-10-26 18:57:59 -07001500 drm_i915_private_t *dev_priv = overlay->dev->dev_private;
Chris Wilson3bd3c932010-08-19 08:19:30 +01001501 struct overlay_registers *regs;
1502
1503 if (OVERLAY_NEEDS_PHYSICAL(overlay->dev))
1504 regs = overlay->reg_bo->phys_obj->handle->vaddr;
1505 else
1506 regs = io_mapping_map_atomic_wc(dev_priv->mm.gtt_mapping,
Linus Torvaldsc48c43e2010-10-26 18:57:59 -07001507 overlay->reg_bo->gtt_offset);
Chris Wilson3bd3c932010-08-19 08:19:30 +01001508
1509 return regs;
1510}
1511
1512static void intel_overlay_unmap_regs_atomic(struct intel_overlay *overlay,
Chris Wilson3bd3c932010-08-19 08:19:30 +01001513 struct overlay_registers *regs)
1514{
1515 if (!OVERLAY_NEEDS_PHYSICAL(overlay->dev))
Linus Torvaldsc48c43e2010-10-26 18:57:59 -07001516 io_mapping_unmap_atomic(regs);
Chris Wilson3bd3c932010-08-19 08:19:30 +01001517}
1518
1519
Chris Wilson6ef3d422010-08-04 20:26:07 +01001520struct intel_overlay_error_state *
1521intel_overlay_capture_error_state(struct drm_device *dev)
1522{
1523 drm_i915_private_t *dev_priv = dev->dev_private;
1524 struct intel_overlay *overlay = dev_priv->overlay;
1525 struct intel_overlay_error_state *error;
1526 struct overlay_registers __iomem *regs;
1527
1528 if (!overlay || !overlay->active)
1529 return NULL;
1530
1531 error = kmalloc(sizeof(*error), GFP_ATOMIC);
1532 if (error == NULL)
1533 return NULL;
1534
1535 error->dovsta = I915_READ(DOVSTA);
1536 error->isr = I915_READ(ISR);
Chris Wilson315781482010-08-12 09:42:51 +01001537 if (OVERLAY_NEEDS_PHYSICAL(overlay->dev))
Chris Wilson6ef3d422010-08-04 20:26:07 +01001538 error->base = (long) overlay->reg_bo->phys_obj->handle->vaddr;
Chris Wilson315781482010-08-12 09:42:51 +01001539 else
1540 error->base = (long) overlay->reg_bo->gtt_offset;
Chris Wilson6ef3d422010-08-04 20:26:07 +01001541
1542 regs = intel_overlay_map_regs_atomic(overlay);
1543 if (!regs)
1544 goto err;
1545
1546 memcpy_fromio(&error->regs, regs, sizeof(struct overlay_registers));
Linus Torvaldsc48c43e2010-10-26 18:57:59 -07001547 intel_overlay_unmap_regs_atomic(overlay, regs);
Chris Wilson6ef3d422010-08-04 20:26:07 +01001548
1549 return error;
1550
1551err:
1552 kfree(error);
1553 return NULL;
1554}
1555
1556void
1557intel_overlay_print_error_state(struct seq_file *m, struct intel_overlay_error_state *error)
1558{
1559 seq_printf(m, "Overlay, status: 0x%08x, interrupt: 0x%08x\n",
1560 error->dovsta, error->isr);
1561 seq_printf(m, " Register file at 0x%08lx:\n",
1562 error->base);
1563
1564#define P(x) seq_printf(m, " " #x ": 0x%08x\n", error->regs.x)
1565 P(OBUF_0Y);
1566 P(OBUF_1Y);
1567 P(OBUF_0U);
1568 P(OBUF_0V);
1569 P(OBUF_1U);
1570 P(OBUF_1V);
1571 P(OSTRIDE);
1572 P(YRGB_VPH);
1573 P(UV_VPH);
1574 P(HORZ_PH);
1575 P(INIT_PHS);
1576 P(DWINPOS);
1577 P(DWINSZ);
1578 P(SWIDTH);
1579 P(SWIDTHSW);
1580 P(SHEIGHT);
1581 P(YRGBSCALE);
1582 P(UVSCALE);
1583 P(OCLRC0);
1584 P(OCLRC1);
1585 P(DCLRKV);
1586 P(DCLRKM);
1587 P(SCLRKVH);
1588 P(SCLRKVL);
1589 P(SCLRKEN);
1590 P(OCONFIG);
1591 P(OCMD);
1592 P(OSTART_0Y);
1593 P(OSTART_1Y);
1594 P(OSTART_0U);
1595 P(OSTART_0V);
1596 P(OSTART_1U);
1597 P(OSTART_1V);
1598 P(OTILEOFF_0Y);
1599 P(OTILEOFF_1Y);
1600 P(OTILEOFF_0U);
1601 P(OTILEOFF_0V);
1602 P(OTILEOFF_1U);
1603 P(OTILEOFF_1V);
1604 P(FASTHSCALE);
1605 P(UVSCALEV);
1606#undef P
1607}
Chris Wilson3bd3c932010-08-19 08:19:30 +01001608#endif