blob: 80b331c322fb57832732d078a4492fe20075d78e [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 */
28#include "drmP.h"
29#include "drm.h"
30#include "i915_drm.h"
31#include "i915_drv.h"
32#include "i915_reg.h"
33#include "intel_drv.h"
34
35/* Limits for overlay size. According to intel doc, the real limits are:
36 * Y width: 4095, UV width (planar): 2047, Y height: 2047,
37 * UV width (planar): * 1023. But the xorg thinks 2048 for height and width. Use
38 * the mininum of both. */
39#define IMAGE_MAX_WIDTH 2048
40#define IMAGE_MAX_HEIGHT 2046 /* 2 * 1023 */
41/* on 830 and 845 these large limits result in the card hanging */
42#define IMAGE_MAX_WIDTH_LEGACY 1024
43#define IMAGE_MAX_HEIGHT_LEGACY 1088
44
45/* overlay register definitions */
46/* OCMD register */
47#define OCMD_TILED_SURFACE (0x1<<19)
48#define OCMD_MIRROR_MASK (0x3<<17)
49#define OCMD_MIRROR_MODE (0x3<<17)
50#define OCMD_MIRROR_HORIZONTAL (0x1<<17)
51#define OCMD_MIRROR_VERTICAL (0x2<<17)
52#define OCMD_MIRROR_BOTH (0x3<<17)
53#define OCMD_BYTEORDER_MASK (0x3<<14) /* zero for YUYV or FOURCC YUY2 */
54#define OCMD_UV_SWAP (0x1<<14) /* YVYU */
55#define OCMD_Y_SWAP (0x2<<14) /* UYVY or FOURCC UYVY */
56#define OCMD_Y_AND_UV_SWAP (0x3<<14) /* VYUY */
57#define OCMD_SOURCE_FORMAT_MASK (0xf<<10)
58#define OCMD_RGB_888 (0x1<<10) /* not in i965 Intel docs */
59#define OCMD_RGB_555 (0x2<<10) /* not in i965 Intel docs */
60#define OCMD_RGB_565 (0x3<<10) /* not in i965 Intel docs */
61#define OCMD_YUV_422_PACKED (0x8<<10)
62#define OCMD_YUV_411_PACKED (0x9<<10) /* not in i965 Intel docs */
63#define OCMD_YUV_420_PLANAR (0xc<<10)
64#define OCMD_YUV_422_PLANAR (0xd<<10)
65#define OCMD_YUV_410_PLANAR (0xe<<10) /* also 411 */
66#define OCMD_TVSYNCFLIP_PARITY (0x1<<9)
67#define OCMD_TVSYNCFLIP_ENABLE (0x1<<7)
Chris Wilsond7961362010-07-13 13:52:17 +010068#define OCMD_BUF_TYPE_MASK (0x1<<5)
Daniel Vetter02e792f2009-09-15 22:57:34 +020069#define OCMD_BUF_TYPE_FRAME (0x0<<5)
70#define OCMD_BUF_TYPE_FIELD (0x1<<5)
71#define OCMD_TEST_MODE (0x1<<4)
72#define OCMD_BUFFER_SELECT (0x3<<2)
73#define OCMD_BUFFER0 (0x0<<2)
74#define OCMD_BUFFER1 (0x1<<2)
75#define OCMD_FIELD_SELECT (0x1<<2)
76#define OCMD_FIELD0 (0x0<<1)
77#define OCMD_FIELD1 (0x1<<1)
78#define OCMD_ENABLE (0x1<<0)
79
80/* OCONFIG register */
81#define OCONF_PIPE_MASK (0x1<<18)
82#define OCONF_PIPE_A (0x0<<18)
83#define OCONF_PIPE_B (0x1<<18)
84#define OCONF_GAMMA2_ENABLE (0x1<<16)
85#define OCONF_CSC_MODE_BT601 (0x0<<5)
86#define OCONF_CSC_MODE_BT709 (0x1<<5)
87#define OCONF_CSC_BYPASS (0x1<<4)
88#define OCONF_CC_OUT_8BIT (0x1<<3)
89#define OCONF_TEST_MODE (0x1<<2)
90#define OCONF_THREE_LINE_BUFFER (0x1<<0)
91#define OCONF_TWO_LINE_BUFFER (0x0<<0)
92
93/* DCLRKM (dst-key) register */
94#define DST_KEY_ENABLE (0x1<<31)
95#define CLK_RGB24_MASK 0x0
96#define CLK_RGB16_MASK 0x070307
97#define CLK_RGB15_MASK 0x070707
98#define CLK_RGB8I_MASK 0xffffff
99
100#define RGB16_TO_COLORKEY(c) \
101 (((c & 0xF800) << 8) | ((c & 0x07E0) << 5) | ((c & 0x001F) << 3))
102#define RGB15_TO_COLORKEY(c) \
103 (((c & 0x7c00) << 9) | ((c & 0x03E0) << 6) | ((c & 0x001F) << 3))
104
105/* overlay flip addr flag */
106#define OFC_UPDATE 0x1
107
108/* polyphase filter coefficients */
109#define N_HORIZ_Y_TAPS 5
110#define N_VERT_Y_TAPS 3
111#define N_HORIZ_UV_TAPS 3
112#define N_VERT_UV_TAPS 3
113#define N_PHASES 17
114#define MAX_TAPS 5
115
116/* memory bufferd overlay registers */
117struct overlay_registers {
Akshay Joshi0206e352011-08-16 15:34:10 -0400118 u32 OBUF_0Y;
119 u32 OBUF_1Y;
120 u32 OBUF_0U;
121 u32 OBUF_0V;
122 u32 OBUF_1U;
123 u32 OBUF_1V;
124 u32 OSTRIDE;
125 u32 YRGB_VPH;
126 u32 UV_VPH;
127 u32 HORZ_PH;
128 u32 INIT_PHS;
129 u32 DWINPOS;
130 u32 DWINSZ;
131 u32 SWIDTH;
132 u32 SWIDTHSW;
133 u32 SHEIGHT;
134 u32 YRGBSCALE;
135 u32 UVSCALE;
136 u32 OCLRC0;
137 u32 OCLRC1;
138 u32 DCLRKV;
139 u32 DCLRKM;
140 u32 SCLRKVH;
141 u32 SCLRKVL;
142 u32 SCLRKEN;
143 u32 OCONFIG;
144 u32 OCMD;
145 u32 RESERVED1; /* 0x6C */
146 u32 OSTART_0Y;
147 u32 OSTART_1Y;
148 u32 OSTART_0U;
149 u32 OSTART_0V;
150 u32 OSTART_1U;
151 u32 OSTART_1V;
152 u32 OTILEOFF_0Y;
153 u32 OTILEOFF_1Y;
154 u32 OTILEOFF_0U;
155 u32 OTILEOFF_0V;
156 u32 OTILEOFF_1U;
157 u32 OTILEOFF_1V;
158 u32 FASTHSCALE; /* 0xA0 */
159 u32 UVSCALEV; /* 0xA4 */
160 u32 RESERVEDC[(0x200 - 0xA8) / 4]; /* 0xA8 - 0x1FC */
161 u16 Y_VCOEFS[N_VERT_Y_TAPS * N_PHASES]; /* 0x200 */
162 u16 RESERVEDD[0x100 / 2 - N_VERT_Y_TAPS * N_PHASES];
163 u16 Y_HCOEFS[N_HORIZ_Y_TAPS * N_PHASES]; /* 0x300 */
164 u16 RESERVEDE[0x200 / 2 - N_HORIZ_Y_TAPS * N_PHASES];
165 u16 UV_VCOEFS[N_VERT_UV_TAPS * N_PHASES]; /* 0x500 */
166 u16 RESERVEDF[0x100 / 2 - N_VERT_UV_TAPS * N_PHASES];
167 u16 UV_HCOEFS[N_HORIZ_UV_TAPS * N_PHASES]; /* 0x600 */
168 u16 RESERVEDG[0x100 / 2 - N_HORIZ_UV_TAPS * N_PHASES];
Daniel Vetter02e792f2009-09-15 22:57:34 +0200169};
170
Chris Wilson23f09ce2010-08-12 13:53:37 +0100171struct intel_overlay {
172 struct drm_device *dev;
173 struct intel_crtc *crtc;
174 struct drm_i915_gem_object *vid_bo;
175 struct drm_i915_gem_object *old_vid_bo;
176 int active;
177 int pfit_active;
178 u32 pfit_vscale_ratio; /* shifted-point number, (1<<12) == 1.0 */
179 u32 color_key;
180 u32 brightness, contrast, saturation;
181 u32 old_xscale, old_yscale;
182 /* register access */
183 u32 flip_addr;
184 struct drm_i915_gem_object *reg_bo;
185 /* flip handling */
186 uint32_t last_flip_req;
Chris Wilsonb303cf92010-08-12 14:03:48 +0100187 void (*flip_tail)(struct intel_overlay *);
Chris Wilson23f09ce2010-08-12 13:53:37 +0100188};
Daniel Vetter02e792f2009-09-15 22:57:34 +0200189
Chris Wilson8d74f652010-08-12 10:35:26 +0100190static struct overlay_registers *
Chris Wilson8d74f652010-08-12 10:35:26 +0100191intel_overlay_map_regs(struct intel_overlay *overlay)
Daniel Vetter02e792f2009-09-15 22:57:34 +0200192{
Akshay Joshi0206e352011-08-16 15:34:10 -0400193 drm_i915_private_t *dev_priv = overlay->dev->dev_private;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200194 struct overlay_registers *regs;
195
Chris Wilson9bb2ff72010-08-12 12:02:11 +0100196 if (OVERLAY_NEEDS_PHYSICAL(overlay->dev))
Daniel Vetter02e792f2009-09-15 22:57:34 +0200197 regs = overlay->reg_bo->phys_obj->handle->vaddr;
Chris Wilson9bb2ff72010-08-12 12:02:11 +0100198 else
Chris Wilson8d74f652010-08-12 10:35:26 +0100199 regs = io_mapping_map_wc(dev_priv->mm.gtt_mapping,
200 overlay->reg_bo->gtt_offset);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200201
Chris Wilson9bb2ff72010-08-12 12:02:11 +0100202 return regs;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200203}
204
Chris Wilson9bb2ff72010-08-12 12:02:11 +0100205static void intel_overlay_unmap_regs(struct intel_overlay *overlay,
206 struct overlay_registers *regs)
Daniel Vetter02e792f2009-09-15 22:57:34 +0200207{
Chris Wilson8d74f652010-08-12 10:35:26 +0100208 if (!OVERLAY_NEEDS_PHYSICAL(overlay->dev))
Chris Wilson9bb2ff72010-08-12 12:02:11 +0100209 io_mapping_unmap(regs);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200210}
Daniel Vetter02e792f2009-09-15 22:57:34 +0200211
Chris Wilsonb6c028e2010-08-12 11:55:08 +0100212static int intel_overlay_do_wait_request(struct intel_overlay *overlay,
Chris Wilson8dc5d142010-08-12 12:36:12 +0100213 struct drm_i915_gem_request *request,
Chris Wilsonb303cf92010-08-12 14:03:48 +0100214 void (*tail)(struct intel_overlay *))
Chris Wilsonb6c028e2010-08-12 11:55:08 +0100215{
216 struct drm_device *dev = overlay->dev;
217 drm_i915_private_t *dev_priv = dev->dev_private;
218 int ret;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200219
Chris Wilsonb303cf92010-08-12 14:03:48 +0100220 BUG_ON(overlay->last_flip_req);
Chris Wilsondb53a302011-02-03 11:57:46 +0000221 ret = i915_add_request(LP_RING(dev_priv), NULL, request);
Chris Wilson3cce4692010-10-27 16:11:02 +0100222 if (ret) {
223 kfree(request);
224 return ret;
225 }
226 overlay->last_flip_req = request->seqno;
Chris Wilsonb303cf92010-08-12 14:03:48 +0100227 overlay->flip_tail = tail;
Ben Widawskyb93f9cf2012-01-25 15:39:34 -0800228 ret = i915_wait_request(LP_RING(dev_priv), overlay->last_flip_req,
229 true);
Chris Wilsonb6c028e2010-08-12 11:55:08 +0100230 if (ret)
231 return ret;
232
Chris Wilsonb6c028e2010-08-12 11:55:08 +0100233 overlay->last_flip_req = 0;
234 return 0;
235}
236
Chris Wilson106dada2010-07-16 17:13:01 +0100237/* Workaround for i830 bug where pipe a must be enable to change control regs */
238static int
239i830_activate_pipe_a(struct drm_device *dev)
240{
241 drm_i915_private_t *dev_priv = dev->dev_private;
242 struct intel_crtc *crtc;
243 struct drm_crtc_helper_funcs *crtc_funcs;
244 struct drm_display_mode vesa_640x480 = {
245 DRM_MODE("640x480", DRM_MODE_TYPE_DRIVER, 25175, 640, 656,
246 752, 800, 0, 480, 489, 492, 525, 0,
247 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC)
248 }, *mode;
249
250 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[0]);
251 if (crtc->dpms_mode == DRM_MODE_DPMS_ON)
252 return 0;
253
254 /* most i8xx have pipe a forced on, so don't trust dpms mode */
Jesse Barnes9db4a9c2011-02-07 12:26:52 -0800255 if (I915_READ(_PIPEACONF) & PIPECONF_ENABLE)
Chris Wilson106dada2010-07-16 17:13:01 +0100256 return 0;
257
258 crtc_funcs = crtc->base.helper_private;
259 if (crtc_funcs->dpms == NULL)
260 return 0;
261
262 DRM_DEBUG_DRIVER("Enabling pipe A in order to enable overlay\n");
263
264 mode = drm_mode_duplicate(dev, &vesa_640x480);
Daniel Vetterca9bfa72012-01-28 14:49:20 +0100265 drm_mode_set_crtcinfo(mode, 0);
Akshay Joshi0206e352011-08-16 15:34:10 -0400266 if (!drm_crtc_helper_set_mode(&crtc->base, mode,
Chris Wilson106dada2010-07-16 17:13:01 +0100267 crtc->base.x, crtc->base.y,
268 crtc->base.fb))
269 return 0;
270
271 crtc_funcs->dpms(&crtc->base, DRM_MODE_DPMS_ON);
272 return 1;
273}
274
275static void
276i830_deactivate_pipe_a(struct drm_device *dev)
277{
278 drm_i915_private_t *dev_priv = dev->dev_private;
279 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[0];
280 struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
281
282 crtc_funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200283}
284
285/* overlay needs to be disable in OCMD reg */
286static int intel_overlay_on(struct intel_overlay *overlay)
287{
288 struct drm_device *dev = overlay->dev;
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100289 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson8dc5d142010-08-12 12:36:12 +0100290 struct drm_i915_gem_request *request;
Chris Wilson106dada2010-07-16 17:13:01 +0100291 int pipe_a_quirk = 0;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200292 int ret;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200293
294 BUG_ON(overlay->active);
Daniel Vetter03f77ea2009-09-15 22:57:37 +0200295 overlay->active = 1;
Daniel Vetter03f77ea2009-09-15 22:57:37 +0200296
Chris Wilson106dada2010-07-16 17:13:01 +0100297 if (IS_I830(dev)) {
298 pipe_a_quirk = i830_activate_pipe_a(dev);
299 if (pipe_a_quirk < 0)
300 return pipe_a_quirk;
301 }
302
Chris Wilson8dc5d142010-08-12 12:36:12 +0100303 request = kzalloc(sizeof(*request), GFP_KERNEL);
Chris Wilson106dada2010-07-16 17:13:01 +0100304 if (request == NULL) {
305 ret = -ENOMEM;
306 goto out;
307 }
Daniel Vetter02e792f2009-09-15 22:57:34 +0200308
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100309 ret = BEGIN_LP_RING(4);
310 if (ret) {
311 kfree(request);
312 goto out;
313 }
314
Daniel Vetter02e792f2009-09-15 22:57:34 +0200315 OUT_RING(MI_OVERLAY_FLIP | MI_OVERLAY_ON);
316 OUT_RING(overlay->flip_addr | OFC_UPDATE);
317 OUT_RING(MI_WAIT_FOR_EVENT | MI_WAIT_FOR_OVERLAY_FLIP);
318 OUT_RING(MI_NOOP);
319 ADVANCE_LP_RING();
320
Chris Wilsonce453d82011-02-21 14:43:56 +0000321 ret = intel_overlay_do_wait_request(overlay, request, NULL);
Chris Wilson106dada2010-07-16 17:13:01 +0100322out:
323 if (pipe_a_quirk)
324 i830_deactivate_pipe_a(dev);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200325
Chris Wilson106dada2010-07-16 17:13:01 +0100326 return ret;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200327}
328
329/* overlay needs to be enabled in OCMD reg */
Chris Wilson8dc5d142010-08-12 12:36:12 +0100330static int intel_overlay_continue(struct intel_overlay *overlay,
331 bool load_polyphase_filter)
Daniel Vetter02e792f2009-09-15 22:57:34 +0200332{
333 struct drm_device *dev = overlay->dev;
Akshay Joshi0206e352011-08-16 15:34:10 -0400334 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson8dc5d142010-08-12 12:36:12 +0100335 struct drm_i915_gem_request *request;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200336 u32 flip_addr = overlay->flip_addr;
337 u32 tmp;
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100338 int ret;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200339
340 BUG_ON(!overlay->active);
341
Chris Wilson8dc5d142010-08-12 12:36:12 +0100342 request = kzalloc(sizeof(*request), GFP_KERNEL);
343 if (request == NULL)
344 return -ENOMEM;
345
Daniel Vetter02e792f2009-09-15 22:57:34 +0200346 if (load_polyphase_filter)
347 flip_addr |= OFC_UPDATE;
348
349 /* check for underruns */
350 tmp = I915_READ(DOVSTA);
351 if (tmp & (1 << 17))
352 DRM_DEBUG("overlay underrun, DOVSTA: %x\n", tmp);
353
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100354 ret = BEGIN_LP_RING(2);
355 if (ret) {
356 kfree(request);
357 return ret;
358 }
Daniel Vetter02e792f2009-09-15 22:57:34 +0200359 OUT_RING(MI_OVERLAY_FLIP | MI_OVERLAY_CONTINUE);
360 OUT_RING(flip_addr);
Akshay Joshi0206e352011-08-16 15:34:10 -0400361 ADVANCE_LP_RING();
Daniel Vetter5a5a0c62009-09-15 22:57:36 +0200362
Chris Wilsondb53a302011-02-03 11:57:46 +0000363 ret = i915_add_request(LP_RING(dev_priv), NULL, request);
Chris Wilson3cce4692010-10-27 16:11:02 +0100364 if (ret) {
365 kfree(request);
366 return ret;
367 }
368
369 overlay->last_flip_req = request->seqno;
Daniel Vetter03f77ea2009-09-15 22:57:37 +0200370 return 0;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200371}
372
Chris Wilsonb303cf92010-08-12 14:03:48 +0100373static void intel_overlay_release_old_vid_tail(struct intel_overlay *overlay)
Daniel Vetter02e792f2009-09-15 22:57:34 +0200374{
Chris Wilson05394f32010-11-08 19:18:58 +0000375 struct drm_i915_gem_object *obj = overlay->old_vid_bo;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200376
Chris Wilsonb303cf92010-08-12 14:03:48 +0100377 i915_gem_object_unpin(obj);
Chris Wilson05394f32010-11-08 19:18:58 +0000378 drm_gem_object_unreference(&obj->base);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200379
Chris Wilsonb303cf92010-08-12 14:03:48 +0100380 overlay->old_vid_bo = NULL;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200381}
382
Daniel Vetter12ca45f2037-04-25 10:08:26 +0200383static void intel_overlay_off_tail(struct intel_overlay *overlay)
384{
Chris Wilson05394f32010-11-08 19:18:58 +0000385 struct drm_i915_gem_object *obj = overlay->vid_bo;
Daniel Vetter12ca45f2037-04-25 10:08:26 +0200386
387 /* never have the overlay hw on without showing a frame */
388 BUG_ON(!overlay->vid_bo);
Daniel Vetter12ca45f2037-04-25 10:08:26 +0200389
390 i915_gem_object_unpin(obj);
Chris Wilson05394f32010-11-08 19:18:58 +0000391 drm_gem_object_unreference(&obj->base);
Daniel Vetter12ca45f2037-04-25 10:08:26 +0200392 overlay->vid_bo = NULL;
393
394 overlay->crtc->overlay = NULL;
395 overlay->crtc = NULL;
396 overlay->active = 0;
397}
398
Daniel Vetter02e792f2009-09-15 22:57:34 +0200399/* overlay needs to be disabled in OCMD reg */
Chris Wilsonce453d82011-02-21 14:43:56 +0000400static int intel_overlay_off(struct intel_overlay *overlay)
Daniel Vetter03f77ea2009-09-15 22:57:37 +0200401{
402 struct drm_device *dev = overlay->dev;
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100403 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson8dc5d142010-08-12 12:36:12 +0100404 u32 flip_addr = overlay->flip_addr;
405 struct drm_i915_gem_request *request;
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100406 int ret;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200407
408 BUG_ON(!overlay->active);
409
Chris Wilson8dc5d142010-08-12 12:36:12 +0100410 request = kzalloc(sizeof(*request), GFP_KERNEL);
411 if (request == NULL)
412 return -ENOMEM;
413
Daniel Vetter02e792f2009-09-15 22:57:34 +0200414 /* According to intel docs the overlay hw may hang (when switching
415 * off) without loading the filter coeffs. It is however unclear whether
416 * this applies to the disabling of the overlay or to the switching off
417 * of the hw. Do it in both cases */
418 flip_addr |= OFC_UPDATE;
419
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100420 ret = BEGIN_LP_RING(6);
421 if (ret) {
422 kfree(request);
423 return ret;
424 }
Daniel Vetter02e792f2009-09-15 22:57:34 +0200425 /* wait for overlay to go idle */
Daniel Vetter02e792f2009-09-15 22:57:34 +0200426 OUT_RING(MI_OVERLAY_FLIP | MI_OVERLAY_CONTINUE);
427 OUT_RING(flip_addr);
Chris Wilson722506f2010-08-12 09:28:50 +0100428 OUT_RING(MI_WAIT_FOR_EVENT | MI_WAIT_FOR_OVERLAY_FLIP);
Chris Wilson722506f2010-08-12 09:28:50 +0100429 /* turn overlay off */
Chris Wilson722506f2010-08-12 09:28:50 +0100430 OUT_RING(MI_OVERLAY_FLIP | MI_OVERLAY_OFF);
431 OUT_RING(flip_addr);
432 OUT_RING(MI_WAIT_FOR_EVENT | MI_WAIT_FOR_OVERLAY_FLIP);
Chris Wilson722506f2010-08-12 09:28:50 +0100433 ADVANCE_LP_RING();
434
Chris Wilsonce453d82011-02-21 14:43:56 +0000435 return intel_overlay_do_wait_request(overlay, request,
Chris Wilsonb303cf92010-08-12 14:03:48 +0100436 intel_overlay_off_tail);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200437}
438
Daniel Vetter03f77ea2009-09-15 22:57:37 +0200439/* recover from an interruption due to a signal
440 * We have to be careful not to repeat work forever an make forward progess. */
Chris Wilsonce453d82011-02-21 14:43:56 +0000441static int intel_overlay_recover_from_interrupt(struct intel_overlay *overlay)
Daniel Vetter03f77ea2009-09-15 22:57:37 +0200442{
443 struct drm_device *dev = overlay->dev;
Zou Nan hai852835f2010-05-21 09:08:56 +0800444 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter03f77ea2009-09-15 22:57:37 +0200445 int ret;
Daniel Vetter03f77ea2009-09-15 22:57:37 +0200446
Chris Wilsonb303cf92010-08-12 14:03:48 +0100447 if (overlay->last_flip_req == 0)
448 return 0;
Daniel Vetter03f77ea2009-09-15 22:57:37 +0200449
Ben Widawskyb93f9cf2012-01-25 15:39:34 -0800450 ret = i915_wait_request(LP_RING(dev_priv), overlay->last_flip_req,
451 true);
Chris Wilsonb6c028e2010-08-12 11:55:08 +0100452 if (ret)
Daniel Vetter03f77ea2009-09-15 22:57:37 +0200453 return ret;
454
Chris Wilsonb303cf92010-08-12 14:03:48 +0100455 if (overlay->flip_tail)
456 overlay->flip_tail(overlay);
Daniel Vetter03f77ea2009-09-15 22:57:37 +0200457
Daniel Vetter03f77ea2009-09-15 22:57:37 +0200458 overlay->last_flip_req = 0;
459 return 0;
460}
461
Daniel Vetter5a5a0c62009-09-15 22:57:36 +0200462/* Wait for pending overlay flip and release old frame.
463 * Needs to be called before the overlay register are changed
Chris Wilson8d74f652010-08-12 10:35:26 +0100464 * via intel_overlay_(un)map_regs
465 */
Daniel Vetter02e792f2009-09-15 22:57:34 +0200466static int intel_overlay_release_old_vid(struct intel_overlay *overlay)
467{
Chris Wilson5cd68c92010-08-12 12:21:54 +0100468 struct drm_device *dev = overlay->dev;
469 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200470 int ret;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200471
Chris Wilson5cd68c92010-08-12 12:21:54 +0100472 /* Only wait if there is actually an old frame to release to
473 * guarantee forward progress.
474 */
Daniel Vetter03f77ea2009-09-15 22:57:37 +0200475 if (!overlay->old_vid_bo)
476 return 0;
477
Chris Wilson5cd68c92010-08-12 12:21:54 +0100478 if (I915_READ(ISR) & I915_OVERLAY_PLANE_FLIP_PENDING_INTERRUPT) {
Chris Wilson8dc5d142010-08-12 12:36:12 +0100479 struct drm_i915_gem_request *request;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200480
Chris Wilson5cd68c92010-08-12 12:21:54 +0100481 /* synchronous slowpath */
Chris Wilson8dc5d142010-08-12 12:36:12 +0100482 request = kzalloc(sizeof(*request), GFP_KERNEL);
483 if (request == NULL)
484 return -ENOMEM;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200485
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100486 ret = BEGIN_LP_RING(2);
487 if (ret) {
488 kfree(request);
489 return ret;
490 }
491
Chris Wilson5cd68c92010-08-12 12:21:54 +0100492 OUT_RING(MI_WAIT_FOR_EVENT | MI_WAIT_FOR_OVERLAY_FLIP);
493 OUT_RING(MI_NOOP);
494 ADVANCE_LP_RING();
Daniel Vetter02e792f2009-09-15 22:57:34 +0200495
Chris Wilsonce453d82011-02-21 14:43:56 +0000496 ret = intel_overlay_do_wait_request(overlay, request,
Chris Wilsonb303cf92010-08-12 14:03:48 +0100497 intel_overlay_release_old_vid_tail);
Chris Wilson5cd68c92010-08-12 12:21:54 +0100498 if (ret)
499 return ret;
500 }
Daniel Vetter02e792f2009-09-15 22:57:34 +0200501
Chris Wilson5cd68c92010-08-12 12:21:54 +0100502 intel_overlay_release_old_vid_tail(overlay);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200503 return 0;
504}
505
506struct put_image_params {
507 int format;
508 short dst_x;
509 short dst_y;
510 short dst_w;
511 short dst_h;
512 short src_w;
513 short src_scan_h;
514 short src_scan_w;
515 short src_h;
516 short stride_Y;
517 short stride_UV;
518 int offset_Y;
519 int offset_U;
520 int offset_V;
521};
522
523static int packed_depth_bytes(u32 format)
524{
525 switch (format & I915_OVERLAY_DEPTH_MASK) {
Chris Wilson722506f2010-08-12 09:28:50 +0100526 case I915_OVERLAY_YUV422:
527 return 4;
528 case I915_OVERLAY_YUV411:
529 /* return 6; not implemented */
530 default:
531 return -EINVAL;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200532 }
533}
534
535static int packed_width_bytes(u32 format, short width)
536{
537 switch (format & I915_OVERLAY_DEPTH_MASK) {
Chris Wilson722506f2010-08-12 09:28:50 +0100538 case I915_OVERLAY_YUV422:
539 return width << 1;
540 default:
541 return -EINVAL;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200542 }
543}
544
545static int uv_hsubsampling(u32 format)
546{
547 switch (format & I915_OVERLAY_DEPTH_MASK) {
Chris Wilson722506f2010-08-12 09:28:50 +0100548 case I915_OVERLAY_YUV422:
549 case I915_OVERLAY_YUV420:
550 return 2;
551 case I915_OVERLAY_YUV411:
552 case I915_OVERLAY_YUV410:
553 return 4;
554 default:
555 return -EINVAL;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200556 }
557}
558
559static int uv_vsubsampling(u32 format)
560{
561 switch (format & I915_OVERLAY_DEPTH_MASK) {
Chris Wilson722506f2010-08-12 09:28:50 +0100562 case I915_OVERLAY_YUV420:
563 case I915_OVERLAY_YUV410:
564 return 2;
565 case I915_OVERLAY_YUV422:
566 case I915_OVERLAY_YUV411:
567 return 1;
568 default:
569 return -EINVAL;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200570 }
571}
572
573static u32 calc_swidthsw(struct drm_device *dev, u32 offset, u32 width)
574{
575 u32 mask, shift, ret;
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100576 if (IS_GEN2(dev)) {
Daniel Vetter02e792f2009-09-15 22:57:34 +0200577 mask = 0x1f;
578 shift = 5;
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100579 } else {
580 mask = 0x3f;
581 shift = 6;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200582 }
583 ret = ((offset + width + mask) >> shift) - (offset >> shift);
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100584 if (!IS_GEN2(dev))
Daniel Vetter02e792f2009-09-15 22:57:34 +0200585 ret <<= 1;
Akshay Joshi0206e352011-08-16 15:34:10 -0400586 ret -= 1;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200587 return ret << 2;
588}
589
590static const u16 y_static_hcoeffs[N_HORIZ_Y_TAPS * N_PHASES] = {
591 0x3000, 0xb4a0, 0x1930, 0x1920, 0xb4a0,
592 0x3000, 0xb500, 0x19d0, 0x1880, 0xb440,
593 0x3000, 0xb540, 0x1a88, 0x2f80, 0xb3e0,
594 0x3000, 0xb580, 0x1b30, 0x2e20, 0xb380,
595 0x3000, 0xb5c0, 0x1bd8, 0x2cc0, 0xb320,
596 0x3020, 0xb5e0, 0x1c60, 0x2b80, 0xb2c0,
597 0x3020, 0xb5e0, 0x1cf8, 0x2a20, 0xb260,
598 0x3020, 0xb5e0, 0x1d80, 0x28e0, 0xb200,
599 0x3020, 0xb5c0, 0x1e08, 0x3f40, 0xb1c0,
600 0x3020, 0xb580, 0x1e78, 0x3ce0, 0xb160,
601 0x3040, 0xb520, 0x1ed8, 0x3aa0, 0xb120,
602 0x3040, 0xb4a0, 0x1f30, 0x3880, 0xb0e0,
603 0x3040, 0xb400, 0x1f78, 0x3680, 0xb0a0,
604 0x3020, 0xb340, 0x1fb8, 0x34a0, 0xb060,
605 0x3020, 0xb240, 0x1fe0, 0x32e0, 0xb040,
606 0x3020, 0xb140, 0x1ff8, 0x3160, 0xb020,
Chris Wilson722506f2010-08-12 09:28:50 +0100607 0xb000, 0x3000, 0x0800, 0x3000, 0xb000
608};
609
Daniel Vetter02e792f2009-09-15 22:57:34 +0200610static const u16 uv_static_hcoeffs[N_HORIZ_UV_TAPS * N_PHASES] = {
611 0x3000, 0x1800, 0x1800, 0xb000, 0x18d0, 0x2e60,
612 0xb000, 0x1990, 0x2ce0, 0xb020, 0x1a68, 0x2b40,
613 0xb040, 0x1b20, 0x29e0, 0xb060, 0x1bd8, 0x2880,
614 0xb080, 0x1c88, 0x3e60, 0xb0a0, 0x1d28, 0x3c00,
615 0xb0c0, 0x1db8, 0x39e0, 0xb0e0, 0x1e40, 0x37e0,
616 0xb100, 0x1eb8, 0x3620, 0xb100, 0x1f18, 0x34a0,
617 0xb100, 0x1f68, 0x3360, 0xb0e0, 0x1fa8, 0x3240,
618 0xb0c0, 0x1fe0, 0x3140, 0xb060, 0x1ff0, 0x30a0,
Chris Wilson722506f2010-08-12 09:28:50 +0100619 0x3000, 0x0800, 0x3000
620};
Daniel Vetter02e792f2009-09-15 22:57:34 +0200621
622static void update_polyphase_filter(struct overlay_registers *regs)
623{
624 memcpy(regs->Y_HCOEFS, y_static_hcoeffs, sizeof(y_static_hcoeffs));
625 memcpy(regs->UV_HCOEFS, uv_static_hcoeffs, sizeof(uv_static_hcoeffs));
626}
627
628static bool update_scaling_factors(struct intel_overlay *overlay,
629 struct overlay_registers *regs,
630 struct put_image_params *params)
631{
632 /* fixed point with a 12 bit shift */
633 u32 xscale, yscale, xscale_UV, yscale_UV;
634#define FP_SHIFT 12
635#define FRACT_MASK 0xfff
636 bool scale_changed = false;
637 int uv_hscale = uv_hsubsampling(params->format);
638 int uv_vscale = uv_vsubsampling(params->format);
639
640 if (params->dst_w > 1)
641 xscale = ((params->src_scan_w - 1) << FP_SHIFT)
642 /(params->dst_w);
643 else
644 xscale = 1 << FP_SHIFT;
645
646 if (params->dst_h > 1)
647 yscale = ((params->src_scan_h - 1) << FP_SHIFT)
648 /(params->dst_h);
649 else
650 yscale = 1 << FP_SHIFT;
651
652 /*if (params->format & I915_OVERLAY_YUV_PLANAR) {*/
Chris Wilson722506f2010-08-12 09:28:50 +0100653 xscale_UV = xscale/uv_hscale;
654 yscale_UV = yscale/uv_vscale;
655 /* make the Y scale to UV scale ratio an exact multiply */
656 xscale = xscale_UV * uv_hscale;
657 yscale = yscale_UV * uv_vscale;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200658 /*} else {
Chris Wilson722506f2010-08-12 09:28:50 +0100659 xscale_UV = 0;
660 yscale_UV = 0;
661 }*/
Daniel Vetter02e792f2009-09-15 22:57:34 +0200662
663 if (xscale != overlay->old_xscale || yscale != overlay->old_yscale)
664 scale_changed = true;
665 overlay->old_xscale = xscale;
666 overlay->old_yscale = yscale;
667
Chris Wilson722506f2010-08-12 09:28:50 +0100668 regs->YRGBSCALE = (((yscale & FRACT_MASK) << 20) |
669 ((xscale >> FP_SHIFT) << 16) |
670 ((xscale & FRACT_MASK) << 3));
671
672 regs->UVSCALE = (((yscale_UV & FRACT_MASK) << 20) |
673 ((xscale_UV >> FP_SHIFT) << 16) |
674 ((xscale_UV & FRACT_MASK) << 3));
675
676 regs->UVSCALEV = ((((yscale >> FP_SHIFT) << 16) |
677 ((yscale_UV >> FP_SHIFT) << 0)));
Daniel Vetter02e792f2009-09-15 22:57:34 +0200678
679 if (scale_changed)
680 update_polyphase_filter(regs);
681
682 return scale_changed;
683}
684
685static void update_colorkey(struct intel_overlay *overlay,
686 struct overlay_registers *regs)
687{
688 u32 key = overlay->color_key;
Chris Wilson6ba3ddd2010-08-12 09:30:58 +0100689
Daniel Vetter02e792f2009-09-15 22:57:34 +0200690 switch (overlay->crtc->base.fb->bits_per_pixel) {
Chris Wilson722506f2010-08-12 09:28:50 +0100691 case 8:
692 regs->DCLRKV = 0;
693 regs->DCLRKM = CLK_RGB8I_MASK | DST_KEY_ENABLE;
Chris Wilson6ba3ddd2010-08-12 09:30:58 +0100694 break;
695
Chris Wilson722506f2010-08-12 09:28:50 +0100696 case 16:
697 if (overlay->crtc->base.fb->depth == 15) {
698 regs->DCLRKV = RGB15_TO_COLORKEY(key);
699 regs->DCLRKM = CLK_RGB15_MASK | DST_KEY_ENABLE;
700 } else {
701 regs->DCLRKV = RGB16_TO_COLORKEY(key);
702 regs->DCLRKM = CLK_RGB16_MASK | DST_KEY_ENABLE;
703 }
Chris Wilson6ba3ddd2010-08-12 09:30:58 +0100704 break;
705
Chris Wilson722506f2010-08-12 09:28:50 +0100706 case 24:
707 case 32:
708 regs->DCLRKV = key;
709 regs->DCLRKM = CLK_RGB24_MASK | DST_KEY_ENABLE;
Chris Wilson6ba3ddd2010-08-12 09:30:58 +0100710 break;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200711 }
712}
713
714static u32 overlay_cmd_reg(struct put_image_params *params)
715{
716 u32 cmd = OCMD_ENABLE | OCMD_BUF_TYPE_FRAME | OCMD_BUFFER0;
717
718 if (params->format & I915_OVERLAY_YUV_PLANAR) {
719 switch (params->format & I915_OVERLAY_DEPTH_MASK) {
Chris Wilson722506f2010-08-12 09:28:50 +0100720 case I915_OVERLAY_YUV422:
721 cmd |= OCMD_YUV_422_PLANAR;
722 break;
723 case I915_OVERLAY_YUV420:
724 cmd |= OCMD_YUV_420_PLANAR;
725 break;
726 case I915_OVERLAY_YUV411:
727 case I915_OVERLAY_YUV410:
728 cmd |= OCMD_YUV_410_PLANAR;
729 break;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200730 }
731 } else { /* YUV packed */
732 switch (params->format & I915_OVERLAY_DEPTH_MASK) {
Chris Wilson722506f2010-08-12 09:28:50 +0100733 case I915_OVERLAY_YUV422:
734 cmd |= OCMD_YUV_422_PACKED;
735 break;
736 case I915_OVERLAY_YUV411:
737 cmd |= OCMD_YUV_411_PACKED;
738 break;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200739 }
740
741 switch (params->format & I915_OVERLAY_SWAP_MASK) {
Chris Wilson722506f2010-08-12 09:28:50 +0100742 case I915_OVERLAY_NO_SWAP:
743 break;
744 case I915_OVERLAY_UV_SWAP:
745 cmd |= OCMD_UV_SWAP;
746 break;
747 case I915_OVERLAY_Y_SWAP:
748 cmd |= OCMD_Y_SWAP;
749 break;
750 case I915_OVERLAY_Y_AND_UV_SWAP:
751 cmd |= OCMD_Y_AND_UV_SWAP;
752 break;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200753 }
754 }
755
756 return cmd;
757}
758
Chris Wilson5fe82c52010-08-12 12:38:21 +0100759static int intel_overlay_do_put_image(struct intel_overlay *overlay,
Chris Wilson05394f32010-11-08 19:18:58 +0000760 struct drm_i915_gem_object *new_bo,
Chris Wilson5fe82c52010-08-12 12:38:21 +0100761 struct put_image_params *params)
Daniel Vetter02e792f2009-09-15 22:57:34 +0200762{
763 int ret, tmp_width;
764 struct overlay_registers *regs;
765 bool scale_changed = false;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200766 struct drm_device *dev = overlay->dev;
767
768 BUG_ON(!mutex_is_locked(&dev->struct_mutex));
769 BUG_ON(!mutex_is_locked(&dev->mode_config.mutex));
770 BUG_ON(!overlay);
771
Daniel Vetter02e792f2009-09-15 22:57:34 +0200772 ret = intel_overlay_release_old_vid(overlay);
773 if (ret != 0)
774 return ret;
775
Chris Wilson2da3b9b2011-04-14 09:41:17 +0100776 ret = i915_gem_object_pin_to_display_plane(new_bo, 0, NULL);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200777 if (ret != 0)
778 return ret;
779
Chris Wilsond9e86c02010-11-10 16:40:20 +0000780 ret = i915_gem_object_put_fence(new_bo);
781 if (ret)
782 goto out_unpin;
783
Daniel Vetter02e792f2009-09-15 22:57:34 +0200784 if (!overlay->active) {
Chris Wilson8d74f652010-08-12 10:35:26 +0100785 regs = intel_overlay_map_regs(overlay);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200786 if (!regs) {
787 ret = -ENOMEM;
788 goto out_unpin;
789 }
790 regs->OCONFIG = OCONF_CC_OUT_8BIT;
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100791 if (IS_GEN4(overlay->dev))
Daniel Vetter02e792f2009-09-15 22:57:34 +0200792 regs->OCONFIG |= OCONF_CSC_MODE_BT709;
793 regs->OCONFIG |= overlay->crtc->pipe == 0 ?
794 OCONF_PIPE_A : OCONF_PIPE_B;
Chris Wilson9bb2ff72010-08-12 12:02:11 +0100795 intel_overlay_unmap_regs(overlay, regs);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200796
797 ret = intel_overlay_on(overlay);
798 if (ret != 0)
799 goto out_unpin;
800 }
801
Chris Wilson8d74f652010-08-12 10:35:26 +0100802 regs = intel_overlay_map_regs(overlay);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200803 if (!regs) {
804 ret = -ENOMEM;
805 goto out_unpin;
806 }
807
808 regs->DWINPOS = (params->dst_y << 16) | params->dst_x;
809 regs->DWINSZ = (params->dst_h << 16) | params->dst_w;
810
811 if (params->format & I915_OVERLAY_YUV_PACKED)
812 tmp_width = packed_width_bytes(params->format, params->src_w);
813 else
814 tmp_width = params->src_w;
815
816 regs->SWIDTH = params->src_w;
817 regs->SWIDTHSW = calc_swidthsw(overlay->dev,
Chris Wilson722506f2010-08-12 09:28:50 +0100818 params->offset_Y, tmp_width);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200819 regs->SHEIGHT = params->src_h;
Akshay Joshi0206e352011-08-16 15:34:10 -0400820 regs->OBUF_0Y = new_bo->gtt_offset + params->offset_Y;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200821 regs->OSTRIDE = params->stride_Y;
822
823 if (params->format & I915_OVERLAY_YUV_PLANAR) {
824 int uv_hscale = uv_hsubsampling(params->format);
825 int uv_vscale = uv_vsubsampling(params->format);
826 u32 tmp_U, tmp_V;
827 regs->SWIDTH |= (params->src_w/uv_hscale) << 16;
828 tmp_U = calc_swidthsw(overlay->dev, params->offset_U,
Chris Wilson722506f2010-08-12 09:28:50 +0100829 params->src_w/uv_hscale);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200830 tmp_V = calc_swidthsw(overlay->dev, params->offset_V,
Chris Wilson722506f2010-08-12 09:28:50 +0100831 params->src_w/uv_hscale);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200832 regs->SWIDTHSW |= max_t(u32, tmp_U, tmp_V) << 16;
833 regs->SHEIGHT |= (params->src_h/uv_vscale) << 16;
Chris Wilson05394f32010-11-08 19:18:58 +0000834 regs->OBUF_0U = new_bo->gtt_offset + params->offset_U;
835 regs->OBUF_0V = new_bo->gtt_offset + params->offset_V;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200836 regs->OSTRIDE |= params->stride_UV << 16;
837 }
838
839 scale_changed = update_scaling_factors(overlay, regs, params);
840
841 update_colorkey(overlay, regs);
842
843 regs->OCMD = overlay_cmd_reg(params);
844
Chris Wilson9bb2ff72010-08-12 12:02:11 +0100845 intel_overlay_unmap_regs(overlay, regs);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200846
Chris Wilson8dc5d142010-08-12 12:36:12 +0100847 ret = intel_overlay_continue(overlay, scale_changed);
848 if (ret)
849 goto out_unpin;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200850
851 overlay->old_vid_bo = overlay->vid_bo;
Chris Wilson05394f32010-11-08 19:18:58 +0000852 overlay->vid_bo = new_bo;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200853
854 return 0;
855
856out_unpin:
857 i915_gem_object_unpin(new_bo);
858 return ret;
859}
860
Chris Wilsonce453d82011-02-21 14:43:56 +0000861int intel_overlay_switch_off(struct intel_overlay *overlay)
Daniel Vetter02e792f2009-09-15 22:57:34 +0200862{
Daniel Vetter02e792f2009-09-15 22:57:34 +0200863 struct overlay_registers *regs;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200864 struct drm_device *dev = overlay->dev;
Chris Wilson5dcdbcb2010-08-12 13:50:28 +0100865 int ret;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200866
867 BUG_ON(!mutex_is_locked(&dev->struct_mutex));
868 BUG_ON(!mutex_is_locked(&dev->mode_config.mutex));
869
Chris Wilsonce453d82011-02-21 14:43:56 +0000870 ret = intel_overlay_recover_from_interrupt(overlay);
Chris Wilsonb303cf92010-08-12 14:03:48 +0100871 if (ret != 0)
872 return ret;
Daniel Vetter9bedb972009-11-30 15:55:49 +0100873
Daniel Vetter02e792f2009-09-15 22:57:34 +0200874 if (!overlay->active)
875 return 0;
876
Daniel Vetter02e792f2009-09-15 22:57:34 +0200877 ret = intel_overlay_release_old_vid(overlay);
878 if (ret != 0)
879 return ret;
880
Chris Wilson8d74f652010-08-12 10:35:26 +0100881 regs = intel_overlay_map_regs(overlay);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200882 regs->OCMD = 0;
Chris Wilson9bb2ff72010-08-12 12:02:11 +0100883 intel_overlay_unmap_regs(overlay, regs);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200884
Chris Wilsonce453d82011-02-21 14:43:56 +0000885 ret = intel_overlay_off(overlay);
Daniel Vetter03f77ea2009-09-15 22:57:37 +0200886 if (ret != 0)
887 return ret;
888
Daniel Vetter12ca45f2037-04-25 10:08:26 +0200889 intel_overlay_off_tail(overlay);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200890 return 0;
891}
892
893static int check_overlay_possible_on_crtc(struct intel_overlay *overlay,
894 struct intel_crtc *crtc)
895{
Chris Wilson722506f2010-08-12 09:28:50 +0100896 drm_i915_private_t *dev_priv = overlay->dev->dev_private;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200897
Chris Wilsonf7abfe82010-09-13 14:19:16 +0100898 if (!crtc->active)
Daniel Vetter02e792f2009-09-15 22:57:34 +0200899 return -EINVAL;
900
Daniel Vetter02e792f2009-09-15 22:57:34 +0200901 /* can't use the overlay with double wide pipe */
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100902 if (INTEL_INFO(overlay->dev)->gen < 4 &&
Chris Wilsonf7abfe82010-09-13 14:19:16 +0100903 (I915_READ(PIPECONF(crtc->pipe)) & (PIPECONF_DOUBLE_WIDE | PIPECONF_ENABLE)) != PIPECONF_ENABLE)
Daniel Vetter02e792f2009-09-15 22:57:34 +0200904 return -EINVAL;
905
906 return 0;
907}
908
909static void update_pfit_vscale_ratio(struct intel_overlay *overlay)
910{
911 struct drm_device *dev = overlay->dev;
Chris Wilson722506f2010-08-12 09:28:50 +0100912 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200913 u32 pfit_control = I915_READ(PFIT_CONTROL);
Chris Wilson446d2182010-08-12 11:15:58 +0100914 u32 ratio;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200915
916 /* XXX: This is not the same logic as in the xorg driver, but more in
Chris Wilson446d2182010-08-12 11:15:58 +0100917 * line with the intel documentation for the i965
918 */
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100919 if (INTEL_INFO(dev)->gen >= 4) {
Akshay Joshi0206e352011-08-16 15:34:10 -0400920 /* on i965 use the PGM reg to read out the autoscaler values */
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100921 ratio = I915_READ(PFIT_PGM_RATIOS) >> PFIT_VERT_SCALE_SHIFT_965;
922 } else {
Chris Wilson446d2182010-08-12 11:15:58 +0100923 if (pfit_control & VERT_AUTO_SCALE)
924 ratio = I915_READ(PFIT_AUTO_RATIOS);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200925 else
Chris Wilson446d2182010-08-12 11:15:58 +0100926 ratio = I915_READ(PFIT_PGM_RATIOS);
927 ratio >>= PFIT_VERT_SCALE_SHIFT;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200928 }
929
930 overlay->pfit_vscale_ratio = ratio;
931}
932
933static int check_overlay_dst(struct intel_overlay *overlay,
934 struct drm_intel_overlay_put_image *rec)
935{
936 struct drm_display_mode *mode = &overlay->crtc->base.mode;
937
Daniel Vetter75c13992012-01-28 23:48:46 +0100938 if (rec->dst_x < mode->hdisplay &&
939 rec->dst_x + rec->dst_width <= mode->hdisplay &&
940 rec->dst_y < mode->vdisplay &&
941 rec->dst_y + rec->dst_height <= mode->vdisplay)
Daniel Vetter02e792f2009-09-15 22:57:34 +0200942 return 0;
943 else
944 return -EINVAL;
945}
946
947static int check_overlay_scaling(struct put_image_params *rec)
948{
949 u32 tmp;
950
951 /* downscaling limit is 8.0 */
952 tmp = ((rec->src_scan_h << 16) / rec->dst_h) >> 16;
953 if (tmp > 7)
954 return -EINVAL;
955 tmp = ((rec->src_scan_w << 16) / rec->dst_w) >> 16;
956 if (tmp > 7)
957 return -EINVAL;
958
959 return 0;
960}
961
962static int check_overlay_src(struct drm_device *dev,
963 struct drm_intel_overlay_put_image *rec,
Chris Wilson05394f32010-11-08 19:18:58 +0000964 struct drm_i915_gem_object *new_bo)
Daniel Vetter02e792f2009-09-15 22:57:34 +0200965{
Daniel Vetter02e792f2009-09-15 22:57:34 +0200966 int uv_hscale = uv_hsubsampling(rec->flags);
967 int uv_vscale = uv_vsubsampling(rec->flags);
Dan Carpenter8f28f542010-10-27 23:17:25 +0200968 u32 stride_mask;
969 int depth;
970 u32 tmp;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200971
972 /* check src dimensions */
973 if (IS_845G(dev) || IS_I830(dev)) {
Chris Wilson722506f2010-08-12 09:28:50 +0100974 if (rec->src_height > IMAGE_MAX_HEIGHT_LEGACY ||
Chris Wilson9f7c3f42010-08-12 11:29:34 +0100975 rec->src_width > IMAGE_MAX_WIDTH_LEGACY)
Daniel Vetter02e792f2009-09-15 22:57:34 +0200976 return -EINVAL;
977 } else {
Chris Wilson722506f2010-08-12 09:28:50 +0100978 if (rec->src_height > IMAGE_MAX_HEIGHT ||
Chris Wilson9f7c3f42010-08-12 11:29:34 +0100979 rec->src_width > IMAGE_MAX_WIDTH)
Daniel Vetter02e792f2009-09-15 22:57:34 +0200980 return -EINVAL;
981 }
Chris Wilson9f7c3f42010-08-12 11:29:34 +0100982
Daniel Vetter02e792f2009-09-15 22:57:34 +0200983 /* better safe than sorry, use 4 as the maximal subsampling ratio */
Chris Wilson722506f2010-08-12 09:28:50 +0100984 if (rec->src_height < N_VERT_Y_TAPS*4 ||
Chris Wilson9f7c3f42010-08-12 11:29:34 +0100985 rec->src_width < N_HORIZ_Y_TAPS*4)
Daniel Vetter02e792f2009-09-15 22:57:34 +0200986 return -EINVAL;
987
Chris Wilsona1efd142010-07-12 19:35:38 +0100988 /* check alignment constraints */
Daniel Vetter02e792f2009-09-15 22:57:34 +0200989 switch (rec->flags & I915_OVERLAY_TYPE_MASK) {
Chris Wilson722506f2010-08-12 09:28:50 +0100990 case I915_OVERLAY_RGB:
991 /* not implemented */
992 return -EINVAL;
Chris Wilson9f7c3f42010-08-12 11:29:34 +0100993
Chris Wilson722506f2010-08-12 09:28:50 +0100994 case I915_OVERLAY_YUV_PACKED:
Chris Wilson722506f2010-08-12 09:28:50 +0100995 if (uv_vscale != 1)
Daniel Vetter02e792f2009-09-15 22:57:34 +0200996 return -EINVAL;
Chris Wilson9f7c3f42010-08-12 11:29:34 +0100997
998 depth = packed_depth_bytes(rec->flags);
Chris Wilson722506f2010-08-12 09:28:50 +0100999 if (depth < 0)
1000 return depth;
Chris Wilson9f7c3f42010-08-12 11:29:34 +01001001
Chris Wilson722506f2010-08-12 09:28:50 +01001002 /* ignore UV planes */
1003 rec->stride_UV = 0;
1004 rec->offset_U = 0;
1005 rec->offset_V = 0;
1006 /* check pixel alignment */
1007 if (rec->offset_Y % depth)
Daniel Vetter02e792f2009-09-15 22:57:34 +02001008 return -EINVAL;
Chris Wilson722506f2010-08-12 09:28:50 +01001009 break;
Chris Wilson9f7c3f42010-08-12 11:29:34 +01001010
Chris Wilson722506f2010-08-12 09:28:50 +01001011 case I915_OVERLAY_YUV_PLANAR:
1012 if (uv_vscale < 0 || uv_hscale < 0)
1013 return -EINVAL;
1014 /* no offset restrictions for planar formats */
1015 break;
Chris Wilson9f7c3f42010-08-12 11:29:34 +01001016
Chris Wilson722506f2010-08-12 09:28:50 +01001017 default:
1018 return -EINVAL;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001019 }
1020
1021 if (rec->src_width % uv_hscale)
1022 return -EINVAL;
1023
1024 /* stride checking */
Chris Wilsona1efd142010-07-12 19:35:38 +01001025 if (IS_I830(dev) || IS_845G(dev))
1026 stride_mask = 255;
1027 else
1028 stride_mask = 63;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001029
1030 if (rec->stride_Y & stride_mask || rec->stride_UV & stride_mask)
1031 return -EINVAL;
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001032 if (IS_GEN4(dev) && rec->stride_Y < 512)
Daniel Vetter02e792f2009-09-15 22:57:34 +02001033 return -EINVAL;
1034
1035 tmp = (rec->flags & I915_OVERLAY_TYPE_MASK) == I915_OVERLAY_YUV_PLANAR ?
Chris Wilson9f7c3f42010-08-12 11:29:34 +01001036 4096 : 8192;
1037 if (rec->stride_Y > tmp || rec->stride_UV > 2*1024)
Daniel Vetter02e792f2009-09-15 22:57:34 +02001038 return -EINVAL;
1039
1040 /* check buffer dimensions */
1041 switch (rec->flags & I915_OVERLAY_TYPE_MASK) {
Chris Wilson722506f2010-08-12 09:28:50 +01001042 case I915_OVERLAY_RGB:
1043 case I915_OVERLAY_YUV_PACKED:
1044 /* always 4 Y values per depth pixels */
1045 if (packed_width_bytes(rec->flags, rec->src_width) > rec->stride_Y)
1046 return -EINVAL;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001047
Chris Wilson722506f2010-08-12 09:28:50 +01001048 tmp = rec->stride_Y*rec->src_height;
Chris Wilson05394f32010-11-08 19:18:58 +00001049 if (rec->offset_Y + tmp > new_bo->base.size)
Chris Wilson722506f2010-08-12 09:28:50 +01001050 return -EINVAL;
1051 break;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001052
Chris Wilson722506f2010-08-12 09:28:50 +01001053 case I915_OVERLAY_YUV_PLANAR:
1054 if (rec->src_width > rec->stride_Y)
1055 return -EINVAL;
1056 if (rec->src_width/uv_hscale > rec->stride_UV)
1057 return -EINVAL;
1058
Chris Wilson9f7c3f42010-08-12 11:29:34 +01001059 tmp = rec->stride_Y * rec->src_height;
Chris Wilson05394f32010-11-08 19:18:58 +00001060 if (rec->offset_Y + tmp > new_bo->base.size)
Chris Wilson722506f2010-08-12 09:28:50 +01001061 return -EINVAL;
Chris Wilson9f7c3f42010-08-12 11:29:34 +01001062
1063 tmp = rec->stride_UV * (rec->src_height / uv_vscale);
Chris Wilson05394f32010-11-08 19:18:58 +00001064 if (rec->offset_U + tmp > new_bo->base.size ||
1065 rec->offset_V + tmp > new_bo->base.size)
Chris Wilson722506f2010-08-12 09:28:50 +01001066 return -EINVAL;
1067 break;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001068 }
1069
1070 return 0;
1071}
1072
Chris Wilsone9e331a2010-09-13 01:16:10 +01001073/**
1074 * Return the pipe currently connected to the panel fitter,
1075 * or -1 if the panel fitter is not present or not in use
1076 */
1077static int intel_panel_fitter_pipe(struct drm_device *dev)
1078{
1079 struct drm_i915_private *dev_priv = dev->dev_private;
1080 u32 pfit_control;
1081
1082 /* i830 doesn't have a panel fitter */
1083 if (IS_I830(dev))
1084 return -1;
1085
1086 pfit_control = I915_READ(PFIT_CONTROL);
1087
1088 /* See if the panel fitter is in use */
1089 if ((pfit_control & PFIT_ENABLE) == 0)
1090 return -1;
1091
1092 /* 965 can place panel fitter on either pipe */
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001093 if (IS_GEN4(dev))
Chris Wilsone9e331a2010-09-13 01:16:10 +01001094 return (pfit_control >> 29) & 0x3;
1095
1096 /* older chips can only use pipe 1 */
1097 return 1;
1098}
1099
Daniel Vetter02e792f2009-09-15 22:57:34 +02001100int intel_overlay_put_image(struct drm_device *dev, void *data,
Akshay Joshi0206e352011-08-16 15:34:10 -04001101 struct drm_file *file_priv)
Daniel Vetter02e792f2009-09-15 22:57:34 +02001102{
1103 struct drm_intel_overlay_put_image *put_image_rec = data;
1104 drm_i915_private_t *dev_priv = dev->dev_private;
1105 struct intel_overlay *overlay;
1106 struct drm_mode_object *drmmode_obj;
1107 struct intel_crtc *crtc;
Chris Wilson05394f32010-11-08 19:18:58 +00001108 struct drm_i915_gem_object *new_bo;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001109 struct put_image_params *params;
1110 int ret;
1111
1112 if (!dev_priv) {
1113 DRM_ERROR("called with no initialization\n");
1114 return -EINVAL;
1115 }
1116
1117 overlay = dev_priv->overlay;
1118 if (!overlay) {
1119 DRM_DEBUG("userspace bug: no overlay\n");
1120 return -ENODEV;
1121 }
1122
1123 if (!(put_image_rec->flags & I915_OVERLAY_ENABLE)) {
1124 mutex_lock(&dev->mode_config.mutex);
1125 mutex_lock(&dev->struct_mutex);
1126
Chris Wilsonce453d82011-02-21 14:43:56 +00001127 ret = intel_overlay_switch_off(overlay);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001128
1129 mutex_unlock(&dev->struct_mutex);
1130 mutex_unlock(&dev->mode_config.mutex);
1131
1132 return ret;
1133 }
1134
1135 params = kmalloc(sizeof(struct put_image_params), GFP_KERNEL);
1136 if (!params)
1137 return -ENOMEM;
1138
1139 drmmode_obj = drm_mode_object_find(dev, put_image_rec->crtc_id,
Chris Wilson722506f2010-08-12 09:28:50 +01001140 DRM_MODE_OBJECT_CRTC);
Dan Carpenter915a4282010-03-06 14:05:39 +03001141 if (!drmmode_obj) {
1142 ret = -ENOENT;
1143 goto out_free;
1144 }
Daniel Vetter02e792f2009-09-15 22:57:34 +02001145 crtc = to_intel_crtc(obj_to_crtc(drmmode_obj));
1146
Chris Wilson05394f32010-11-08 19:18:58 +00001147 new_bo = to_intel_bo(drm_gem_object_lookup(dev, file_priv,
1148 put_image_rec->bo_handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00001149 if (&new_bo->base == NULL) {
Dan Carpenter915a4282010-03-06 14:05:39 +03001150 ret = -ENOENT;
1151 goto out_free;
1152 }
Daniel Vetter02e792f2009-09-15 22:57:34 +02001153
1154 mutex_lock(&dev->mode_config.mutex);
1155 mutex_lock(&dev->struct_mutex);
1156
Chris Wilsond9e86c02010-11-10 16:40:20 +00001157 if (new_bo->tiling_mode) {
1158 DRM_ERROR("buffer used for overlay image can not be tiled\n");
1159 ret = -EINVAL;
1160 goto out_unlock;
1161 }
1162
Chris Wilsonce453d82011-02-21 14:43:56 +00001163 ret = intel_overlay_recover_from_interrupt(overlay);
Chris Wilsonb303cf92010-08-12 14:03:48 +01001164 if (ret != 0)
1165 goto out_unlock;
Daniel Vetter03f77ea2009-09-15 22:57:37 +02001166
Daniel Vetter02e792f2009-09-15 22:57:34 +02001167 if (overlay->crtc != crtc) {
1168 struct drm_display_mode *mode = &crtc->base.mode;
Chris Wilsonce453d82011-02-21 14:43:56 +00001169 ret = intel_overlay_switch_off(overlay);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001170 if (ret != 0)
1171 goto out_unlock;
1172
1173 ret = check_overlay_possible_on_crtc(overlay, crtc);
1174 if (ret != 0)
1175 goto out_unlock;
1176
1177 overlay->crtc = crtc;
1178 crtc->overlay = overlay;
1179
Chris Wilsone9e331a2010-09-13 01:16:10 +01001180 /* line too wide, i.e. one-line-mode */
1181 if (mode->hdisplay > 1024 &&
1182 intel_panel_fitter_pipe(dev) == crtc->pipe) {
Daniel Vetter02e792f2009-09-15 22:57:34 +02001183 overlay->pfit_active = 1;
1184 update_pfit_vscale_ratio(overlay);
1185 } else
1186 overlay->pfit_active = 0;
1187 }
1188
1189 ret = check_overlay_dst(overlay, put_image_rec);
1190 if (ret != 0)
1191 goto out_unlock;
1192
1193 if (overlay->pfit_active) {
1194 params->dst_y = ((((u32)put_image_rec->dst_y) << 12) /
Chris Wilson722506f2010-08-12 09:28:50 +01001195 overlay->pfit_vscale_ratio);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001196 /* shifting right rounds downwards, so add 1 */
1197 params->dst_h = ((((u32)put_image_rec->dst_height) << 12) /
Chris Wilson722506f2010-08-12 09:28:50 +01001198 overlay->pfit_vscale_ratio) + 1;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001199 } else {
1200 params->dst_y = put_image_rec->dst_y;
1201 params->dst_h = put_image_rec->dst_height;
1202 }
1203 params->dst_x = put_image_rec->dst_x;
1204 params->dst_w = put_image_rec->dst_width;
1205
1206 params->src_w = put_image_rec->src_width;
1207 params->src_h = put_image_rec->src_height;
1208 params->src_scan_w = put_image_rec->src_scan_width;
1209 params->src_scan_h = put_image_rec->src_scan_height;
Chris Wilson722506f2010-08-12 09:28:50 +01001210 if (params->src_scan_h > params->src_h ||
1211 params->src_scan_w > params->src_w) {
Daniel Vetter02e792f2009-09-15 22:57:34 +02001212 ret = -EINVAL;
1213 goto out_unlock;
1214 }
1215
1216 ret = check_overlay_src(dev, put_image_rec, new_bo);
1217 if (ret != 0)
1218 goto out_unlock;
1219 params->format = put_image_rec->flags & ~I915_OVERLAY_FLAGS_MASK;
1220 params->stride_Y = put_image_rec->stride_Y;
1221 params->stride_UV = put_image_rec->stride_UV;
1222 params->offset_Y = put_image_rec->offset_Y;
1223 params->offset_U = put_image_rec->offset_U;
1224 params->offset_V = put_image_rec->offset_V;
1225
1226 /* Check scaling after src size to prevent a divide-by-zero. */
1227 ret = check_overlay_scaling(params);
1228 if (ret != 0)
1229 goto out_unlock;
1230
1231 ret = intel_overlay_do_put_image(overlay, new_bo, params);
1232 if (ret != 0)
1233 goto out_unlock;
1234
1235 mutex_unlock(&dev->struct_mutex);
1236 mutex_unlock(&dev->mode_config.mutex);
1237
1238 kfree(params);
1239
1240 return 0;
1241
1242out_unlock:
1243 mutex_unlock(&dev->struct_mutex);
1244 mutex_unlock(&dev->mode_config.mutex);
Chris Wilson05394f32010-11-08 19:18:58 +00001245 drm_gem_object_unreference_unlocked(&new_bo->base);
Dan Carpenter915a4282010-03-06 14:05:39 +03001246out_free:
Daniel Vetter02e792f2009-09-15 22:57:34 +02001247 kfree(params);
1248
1249 return ret;
1250}
1251
1252static void update_reg_attrs(struct intel_overlay *overlay,
1253 struct overlay_registers *regs)
1254{
1255 regs->OCLRC0 = (overlay->contrast << 18) | (overlay->brightness & 0xff);
1256 regs->OCLRC1 = overlay->saturation;
1257}
1258
1259static bool check_gamma_bounds(u32 gamma1, u32 gamma2)
1260{
1261 int i;
1262
1263 if (gamma1 & 0xff000000 || gamma2 & 0xff000000)
1264 return false;
1265
1266 for (i = 0; i < 3; i++) {
Chris Wilson722506f2010-08-12 09:28:50 +01001267 if (((gamma1 >> i*8) & 0xff) >= ((gamma2 >> i*8) & 0xff))
Daniel Vetter02e792f2009-09-15 22:57:34 +02001268 return false;
1269 }
1270
1271 return true;
1272}
1273
1274static bool check_gamma5_errata(u32 gamma5)
1275{
1276 int i;
1277
1278 for (i = 0; i < 3; i++) {
1279 if (((gamma5 >> i*8) & 0xff) == 0x80)
1280 return false;
1281 }
1282
1283 return true;
1284}
1285
1286static int check_gamma(struct drm_intel_overlay_attrs *attrs)
1287{
Chris Wilson722506f2010-08-12 09:28:50 +01001288 if (!check_gamma_bounds(0, attrs->gamma0) ||
1289 !check_gamma_bounds(attrs->gamma0, attrs->gamma1) ||
1290 !check_gamma_bounds(attrs->gamma1, attrs->gamma2) ||
1291 !check_gamma_bounds(attrs->gamma2, attrs->gamma3) ||
1292 !check_gamma_bounds(attrs->gamma3, attrs->gamma4) ||
1293 !check_gamma_bounds(attrs->gamma4, attrs->gamma5) ||
1294 !check_gamma_bounds(attrs->gamma5, 0x00ffffff))
Daniel Vetter02e792f2009-09-15 22:57:34 +02001295 return -EINVAL;
Chris Wilson722506f2010-08-12 09:28:50 +01001296
Daniel Vetter02e792f2009-09-15 22:57:34 +02001297 if (!check_gamma5_errata(attrs->gamma5))
1298 return -EINVAL;
Chris Wilson722506f2010-08-12 09:28:50 +01001299
Daniel Vetter02e792f2009-09-15 22:57:34 +02001300 return 0;
1301}
1302
1303int intel_overlay_attrs(struct drm_device *dev, void *data,
Akshay Joshi0206e352011-08-16 15:34:10 -04001304 struct drm_file *file_priv)
Daniel Vetter02e792f2009-09-15 22:57:34 +02001305{
1306 struct drm_intel_overlay_attrs *attrs = data;
Akshay Joshi0206e352011-08-16 15:34:10 -04001307 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001308 struct intel_overlay *overlay;
1309 struct overlay_registers *regs;
1310 int ret;
1311
1312 if (!dev_priv) {
1313 DRM_ERROR("called with no initialization\n");
1314 return -EINVAL;
1315 }
1316
1317 overlay = dev_priv->overlay;
1318 if (!overlay) {
1319 DRM_DEBUG("userspace bug: no overlay\n");
1320 return -ENODEV;
1321 }
1322
1323 mutex_lock(&dev->mode_config.mutex);
1324 mutex_lock(&dev->struct_mutex);
1325
Chris Wilson60fc3322010-08-12 10:44:45 +01001326 ret = -EINVAL;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001327 if (!(attrs->flags & I915_OVERLAY_UPDATE_ATTRS)) {
Chris Wilson60fc3322010-08-12 10:44:45 +01001328 attrs->color_key = overlay->color_key;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001329 attrs->brightness = overlay->brightness;
Chris Wilson60fc3322010-08-12 10:44:45 +01001330 attrs->contrast = overlay->contrast;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001331 attrs->saturation = overlay->saturation;
1332
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001333 if (!IS_GEN2(dev)) {
Daniel Vetter02e792f2009-09-15 22:57:34 +02001334 attrs->gamma0 = I915_READ(OGAMC0);
1335 attrs->gamma1 = I915_READ(OGAMC1);
1336 attrs->gamma2 = I915_READ(OGAMC2);
1337 attrs->gamma3 = I915_READ(OGAMC3);
1338 attrs->gamma4 = I915_READ(OGAMC4);
1339 attrs->gamma5 = I915_READ(OGAMC5);
1340 }
Daniel Vetter02e792f2009-09-15 22:57:34 +02001341 } else {
Chris Wilson60fc3322010-08-12 10:44:45 +01001342 if (attrs->brightness < -128 || attrs->brightness > 127)
Daniel Vetter02e792f2009-09-15 22:57:34 +02001343 goto out_unlock;
Chris Wilson60fc3322010-08-12 10:44:45 +01001344 if (attrs->contrast > 255)
Daniel Vetter02e792f2009-09-15 22:57:34 +02001345 goto out_unlock;
Chris Wilson60fc3322010-08-12 10:44:45 +01001346 if (attrs->saturation > 1023)
Daniel Vetter02e792f2009-09-15 22:57:34 +02001347 goto out_unlock;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001348
Chris Wilson60fc3322010-08-12 10:44:45 +01001349 overlay->color_key = attrs->color_key;
1350 overlay->brightness = attrs->brightness;
1351 overlay->contrast = attrs->contrast;
1352 overlay->saturation = attrs->saturation;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001353
Chris Wilson8d74f652010-08-12 10:35:26 +01001354 regs = intel_overlay_map_regs(overlay);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001355 if (!regs) {
1356 ret = -ENOMEM;
1357 goto out_unlock;
1358 }
1359
1360 update_reg_attrs(overlay, regs);
1361
Chris Wilson9bb2ff72010-08-12 12:02:11 +01001362 intel_overlay_unmap_regs(overlay, regs);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001363
1364 if (attrs->flags & I915_OVERLAY_UPDATE_GAMMA) {
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001365 if (IS_GEN2(dev))
Daniel Vetter02e792f2009-09-15 22:57:34 +02001366 goto out_unlock;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001367
1368 if (overlay->active) {
1369 ret = -EBUSY;
1370 goto out_unlock;
1371 }
1372
1373 ret = check_gamma(attrs);
Chris Wilson60fc3322010-08-12 10:44:45 +01001374 if (ret)
Daniel Vetter02e792f2009-09-15 22:57:34 +02001375 goto out_unlock;
1376
1377 I915_WRITE(OGAMC0, attrs->gamma0);
1378 I915_WRITE(OGAMC1, attrs->gamma1);
1379 I915_WRITE(OGAMC2, attrs->gamma2);
1380 I915_WRITE(OGAMC3, attrs->gamma3);
1381 I915_WRITE(OGAMC4, attrs->gamma4);
1382 I915_WRITE(OGAMC5, attrs->gamma5);
1383 }
Daniel Vetter02e792f2009-09-15 22:57:34 +02001384 }
1385
Chris Wilson60fc3322010-08-12 10:44:45 +01001386 ret = 0;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001387out_unlock:
1388 mutex_unlock(&dev->struct_mutex);
1389 mutex_unlock(&dev->mode_config.mutex);
1390
1391 return ret;
1392}
1393
1394void intel_setup_overlay(struct drm_device *dev)
1395{
Akshay Joshi0206e352011-08-16 15:34:10 -04001396 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001397 struct intel_overlay *overlay;
Chris Wilson05394f32010-11-08 19:18:58 +00001398 struct drm_i915_gem_object *reg_bo;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001399 struct overlay_registers *regs;
1400 int ret;
1401
Chris Wilson315781482010-08-12 09:42:51 +01001402 if (!HAS_OVERLAY(dev))
Daniel Vetter02e792f2009-09-15 22:57:34 +02001403 return;
1404
1405 overlay = kzalloc(sizeof(struct intel_overlay), GFP_KERNEL);
1406 if (!overlay)
1407 return;
Chris Wilson79d24272011-06-28 11:27:47 +01001408
1409 mutex_lock(&dev->struct_mutex);
1410 if (WARN_ON(dev_priv->overlay))
1411 goto out_free;
1412
Daniel Vetter02e792f2009-09-15 22:57:34 +02001413 overlay->dev = dev;
1414
Daniel Vetterac52bc52010-04-09 19:05:06 +00001415 reg_bo = i915_gem_alloc_object(dev, PAGE_SIZE);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001416 if (!reg_bo)
1417 goto out_free;
Chris Wilson05394f32010-11-08 19:18:58 +00001418 overlay->reg_bo = reg_bo;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001419
Chris Wilson315781482010-08-12 09:42:51 +01001420 if (OVERLAY_NEEDS_PHYSICAL(dev)) {
1421 ret = i915_gem_attach_phys_object(dev, reg_bo,
1422 I915_GEM_PHYS_OVERLAY_REGS,
Chris Wilsona2930122010-08-12 10:47:56 +01001423 PAGE_SIZE);
Akshay Joshi0206e352011-08-16 15:34:10 -04001424 if (ret) {
1425 DRM_ERROR("failed to attach phys overlay regs\n");
1426 goto out_free_bo;
1427 }
Chris Wilson05394f32010-11-08 19:18:58 +00001428 overlay->flip_addr = reg_bo->phys_obj->handle->busaddr;
Chris Wilson315781482010-08-12 09:42:51 +01001429 } else {
Daniel Vetter75e9e912010-11-04 17:11:09 +01001430 ret = i915_gem_object_pin(reg_bo, PAGE_SIZE, true);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001431 if (ret) {
Akshay Joshi0206e352011-08-16 15:34:10 -04001432 DRM_ERROR("failed to pin overlay register bo\n");
1433 goto out_free_bo;
1434 }
Chris Wilson05394f32010-11-08 19:18:58 +00001435 overlay->flip_addr = reg_bo->gtt_offset;
Chris Wilson0ddc1282010-08-12 09:35:00 +01001436
1437 ret = i915_gem_object_set_to_gtt_domain(reg_bo, true);
1438 if (ret) {
Akshay Joshi0206e352011-08-16 15:34:10 -04001439 DRM_ERROR("failed to move overlay register bo into the GTT\n");
1440 goto out_unpin_bo;
1441 }
Daniel Vetter02e792f2009-09-15 22:57:34 +02001442 }
1443
1444 /* init all values */
1445 overlay->color_key = 0x0101fe;
1446 overlay->brightness = -19;
1447 overlay->contrast = 75;
1448 overlay->saturation = 146;
1449
Chris Wilson8d74f652010-08-12 10:35:26 +01001450 regs = intel_overlay_map_regs(overlay);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001451 if (!regs)
Chris Wilson79d24272011-06-28 11:27:47 +01001452 goto out_unpin_bo;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001453
1454 memset(regs, 0, sizeof(struct overlay_registers));
1455 update_polyphase_filter(regs);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001456 update_reg_attrs(overlay, regs);
1457
Chris Wilson9bb2ff72010-08-12 12:02:11 +01001458 intel_overlay_unmap_regs(overlay, regs);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001459
1460 dev_priv->overlay = overlay;
Chris Wilson79d24272011-06-28 11:27:47 +01001461 mutex_unlock(&dev->struct_mutex);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001462 DRM_INFO("initialized overlay support\n");
1463 return;
1464
Chris Wilson0ddc1282010-08-12 09:35:00 +01001465out_unpin_bo:
Chris Wilson79d24272011-06-28 11:27:47 +01001466 if (!OVERLAY_NEEDS_PHYSICAL(dev))
1467 i915_gem_object_unpin(reg_bo);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001468out_free_bo:
Chris Wilson05394f32010-11-08 19:18:58 +00001469 drm_gem_object_unreference(&reg_bo->base);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001470out_free:
Chris Wilson79d24272011-06-28 11:27:47 +01001471 mutex_unlock(&dev->struct_mutex);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001472 kfree(overlay);
1473 return;
1474}
1475
1476void intel_cleanup_overlay(struct drm_device *dev)
1477{
Chris Wilson722506f2010-08-12 09:28:50 +01001478 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001479
Chris Wilson62cf4e62010-08-12 10:50:36 +01001480 if (!dev_priv->overlay)
1481 return;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001482
Chris Wilson62cf4e62010-08-12 10:50:36 +01001483 /* The bo's should be free'd by the generic code already.
1484 * Furthermore modesetting teardown happens beforehand so the
1485 * hardware should be off already */
1486 BUG_ON(dev_priv->overlay->active);
1487
1488 drm_gem_object_unreference_unlocked(&dev_priv->overlay->reg_bo->base);
1489 kfree(dev_priv->overlay);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001490}
Chris Wilson6ef3d422010-08-04 20:26:07 +01001491
Chris Wilson3bd3c932010-08-19 08:19:30 +01001492#ifdef CONFIG_DEBUG_FS
1493#include <linux/seq_file.h>
1494
Chris Wilson6ef3d422010-08-04 20:26:07 +01001495struct intel_overlay_error_state {
1496 struct overlay_registers regs;
1497 unsigned long base;
1498 u32 dovsta;
1499 u32 isr;
1500};
1501
Chris Wilson3bd3c932010-08-19 08:19:30 +01001502static struct overlay_registers *
Linus Torvaldsc48c43e2010-10-26 18:57:59 -07001503intel_overlay_map_regs_atomic(struct intel_overlay *overlay)
Chris Wilson3bd3c932010-08-19 08:19:30 +01001504{
Linus Torvaldsc48c43e2010-10-26 18:57:59 -07001505 drm_i915_private_t *dev_priv = overlay->dev->dev_private;
Chris Wilson3bd3c932010-08-19 08:19:30 +01001506 struct overlay_registers *regs;
1507
1508 if (OVERLAY_NEEDS_PHYSICAL(overlay->dev))
1509 regs = overlay->reg_bo->phys_obj->handle->vaddr;
1510 else
1511 regs = io_mapping_map_atomic_wc(dev_priv->mm.gtt_mapping,
Linus Torvaldsc48c43e2010-10-26 18:57:59 -07001512 overlay->reg_bo->gtt_offset);
Chris Wilson3bd3c932010-08-19 08:19:30 +01001513
1514 return regs;
1515}
1516
1517static void intel_overlay_unmap_regs_atomic(struct intel_overlay *overlay,
Chris Wilson3bd3c932010-08-19 08:19:30 +01001518 struct overlay_registers *regs)
1519{
1520 if (!OVERLAY_NEEDS_PHYSICAL(overlay->dev))
Linus Torvaldsc48c43e2010-10-26 18:57:59 -07001521 io_mapping_unmap_atomic(regs);
Chris Wilson3bd3c932010-08-19 08:19:30 +01001522}
1523
1524
Chris Wilson6ef3d422010-08-04 20:26:07 +01001525struct intel_overlay_error_state *
1526intel_overlay_capture_error_state(struct drm_device *dev)
1527{
Akshay Joshi0206e352011-08-16 15:34:10 -04001528 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson6ef3d422010-08-04 20:26:07 +01001529 struct intel_overlay *overlay = dev_priv->overlay;
1530 struct intel_overlay_error_state *error;
1531 struct overlay_registers __iomem *regs;
1532
1533 if (!overlay || !overlay->active)
1534 return NULL;
1535
1536 error = kmalloc(sizeof(*error), GFP_ATOMIC);
1537 if (error == NULL)
1538 return NULL;
1539
1540 error->dovsta = I915_READ(DOVSTA);
1541 error->isr = I915_READ(ISR);
Chris Wilson315781482010-08-12 09:42:51 +01001542 if (OVERLAY_NEEDS_PHYSICAL(overlay->dev))
Chris Wilson6ef3d422010-08-04 20:26:07 +01001543 error->base = (long) overlay->reg_bo->phys_obj->handle->vaddr;
Chris Wilson315781482010-08-12 09:42:51 +01001544 else
1545 error->base = (long) overlay->reg_bo->gtt_offset;
Chris Wilson6ef3d422010-08-04 20:26:07 +01001546
1547 regs = intel_overlay_map_regs_atomic(overlay);
1548 if (!regs)
1549 goto err;
1550
1551 memcpy_fromio(&error->regs, regs, sizeof(struct overlay_registers));
Linus Torvaldsc48c43e2010-10-26 18:57:59 -07001552 intel_overlay_unmap_regs_atomic(overlay, regs);
Chris Wilson6ef3d422010-08-04 20:26:07 +01001553
1554 return error;
1555
1556err:
1557 kfree(error);
1558 return NULL;
1559}
1560
1561void
1562intel_overlay_print_error_state(struct seq_file *m, struct intel_overlay_error_state *error)
1563{
1564 seq_printf(m, "Overlay, status: 0x%08x, interrupt: 0x%08x\n",
1565 error->dovsta, error->isr);
1566 seq_printf(m, " Register file at 0x%08lx:\n",
1567 error->base);
1568
1569#define P(x) seq_printf(m, " " #x ": 0x%08x\n", error->regs.x)
1570 P(OBUF_0Y);
1571 P(OBUF_1Y);
1572 P(OBUF_0U);
1573 P(OBUF_0V);
1574 P(OBUF_1U);
1575 P(OBUF_1V);
1576 P(OSTRIDE);
1577 P(YRGB_VPH);
1578 P(UV_VPH);
1579 P(HORZ_PH);
1580 P(INIT_PHS);
1581 P(DWINPOS);
1582 P(DWINSZ);
1583 P(SWIDTH);
1584 P(SWIDTHSW);
1585 P(SHEIGHT);
1586 P(YRGBSCALE);
1587 P(UVSCALE);
1588 P(OCLRC0);
1589 P(OCLRC1);
1590 P(DCLRKV);
1591 P(DCLRKM);
1592 P(SCLRKVH);
1593 P(SCLRKVL);
1594 P(SCLRKEN);
1595 P(OCONFIG);
1596 P(OCMD);
1597 P(OSTART_0Y);
1598 P(OSTART_1Y);
1599 P(OSTART_0U);
1600 P(OSTART_0V);
1601 P(OSTART_1U);
1602 P(OSTART_1V);
1603 P(OTILEOFF_0Y);
1604 P(OTILEOFF_1Y);
1605 P(OTILEOFF_0U);
1606 P(OTILEOFF_0V);
1607 P(OTILEOFF_1U);
1608 P(OTILEOFF_1V);
1609 P(FASTHSCALE);
1610 P(UVSCALEV);
1611#undef P
1612}
Chris Wilson3bd3c932010-08-19 08:19:30 +01001613#endif