blob: 743ced7c4ae7a1a3c7faab7a0eb515e30465a77b [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};
191
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)
194{
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))
Chris Wilson8d74f652010-08-12 10:35:26 +0100199 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);
203
Chris Wilson9bb2ff72010-08-12 12:02:11 +0100204 return regs;
Chris Wilson8d74f652010-08-12 10:35:26 +0100205}
206
Chris Wilson9bb2ff72010-08-12 12:02:11 +0100207static void intel_overlay_unmap_regs(struct intel_overlay *overlay,
208 struct overlay_registers *regs)
Chris Wilson8d74f652010-08-12 10:35:26 +0100209{
210 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}
213
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;
222
Chris Wilsonb303cf92010-08-12 14:03:48 +0100223 BUG_ON(overlay->last_flip_req);
Chris Wilsonb6c028e2010-08-12 11:55:08 +0100224 overlay->last_flip_req =
Chris Wilson8dc5d142010-08-12 12:36:12 +0100225 i915_add_request(dev, NULL, request, &dev_priv->render_ring);
Chris Wilsonb6c028e2010-08-12 11:55:08 +0100226 if (overlay->last_flip_req == 0)
227 return -ENOMEM;
228
Chris Wilsonb303cf92010-08-12 14:03:48 +0100229 overlay->flip_tail = tail;
Chris Wilsonb6c028e2010-08-12 11:55:08 +0100230 ret = i915_do_wait_request(dev,
231 overlay->last_flip_req, true,
232 &dev_priv->render_ring);
233 if (ret)
234 return ret;
235
Chris Wilsonb6c028e2010-08-12 11:55:08 +0100236 overlay->last_flip_req = 0;
237 return 0;
238}
239
Chris Wilson106dada2010-07-16 17:13:01 +0100240/* Workaround for i830 bug where pipe a must be enable to change control regs */
241static int
242i830_activate_pipe_a(struct drm_device *dev)
243{
244 drm_i915_private_t *dev_priv = dev->dev_private;
245 struct intel_crtc *crtc;
246 struct drm_crtc_helper_funcs *crtc_funcs;
247 struct drm_display_mode vesa_640x480 = {
248 DRM_MODE("640x480", DRM_MODE_TYPE_DRIVER, 25175, 640, 656,
249 752, 800, 0, 480, 489, 492, 525, 0,
250 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC)
251 }, *mode;
252
253 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[0]);
254 if (crtc->dpms_mode == DRM_MODE_DPMS_ON)
255 return 0;
256
257 /* most i8xx have pipe a forced on, so don't trust dpms mode */
258 if (I915_READ(PIPEACONF) & PIPEACONF_ENABLE)
259 return 0;
260
261 crtc_funcs = crtc->base.helper_private;
262 if (crtc_funcs->dpms == NULL)
263 return 0;
264
265 DRM_DEBUG_DRIVER("Enabling pipe A in order to enable overlay\n");
266
267 mode = drm_mode_duplicate(dev, &vesa_640x480);
268 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
269 if(!drm_crtc_helper_set_mode(&crtc->base, mode,
270 crtc->base.x, crtc->base.y,
271 crtc->base.fb))
272 return 0;
273
274 crtc_funcs->dpms(&crtc->base, DRM_MODE_DPMS_ON);
275 return 1;
276}
277
278static void
279i830_deactivate_pipe_a(struct drm_device *dev)
280{
281 drm_i915_private_t *dev_priv = dev->dev_private;
282 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[0];
283 struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
284
285 crtc_funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
286}
287
Daniel Vetter02e792f2009-09-15 22:57:34 +0200288/* overlay needs to be disable in OCMD reg */
289static int intel_overlay_on(struct intel_overlay *overlay)
290{
291 struct drm_device *dev = overlay->dev;
Chris Wilson8dc5d142010-08-12 12:36:12 +0100292 struct drm_i915_gem_request *request;
Chris Wilson106dada2010-07-16 17:13:01 +0100293 int pipe_a_quirk = 0;
294 int ret;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200295
296 BUG_ON(overlay->active);
Daniel Vetter03f77ea2009-09-15 22:57:37 +0200297 overlay->active = 1;
Daniel Vetter03f77ea2009-09-15 22:57:37 +0200298
Chris Wilson106dada2010-07-16 17:13:01 +0100299 if (IS_I830(dev)) {
300 pipe_a_quirk = i830_activate_pipe_a(dev);
301 if (pipe_a_quirk < 0)
302 return pipe_a_quirk;
303 }
304
Chris Wilson8dc5d142010-08-12 12:36:12 +0100305 request = kzalloc(sizeof(*request), GFP_KERNEL);
Chris Wilson106dada2010-07-16 17:13:01 +0100306 if (request == NULL) {
307 ret = -ENOMEM;
308 goto out;
309 }
Chris Wilson8dc5d142010-08-12 12:36:12 +0100310
Daniel Vetter4f8a5672010-02-11 14:14:43 +0100311 BEGIN_LP_RING(4);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200312 OUT_RING(MI_OVERLAY_FLIP | MI_OVERLAY_ON);
313 OUT_RING(overlay->flip_addr | OFC_UPDATE);
314 OUT_RING(MI_WAIT_FOR_EVENT | MI_WAIT_FOR_OVERLAY_FLIP);
315 OUT_RING(MI_NOOP);
316 ADVANCE_LP_RING();
317
Chris Wilsonb303cf92010-08-12 14:03:48 +0100318 ret = intel_overlay_do_wait_request(overlay, request, true, NULL);
Chris Wilson106dada2010-07-16 17:13:01 +0100319out:
320 if (pipe_a_quirk)
321 i830_deactivate_pipe_a(dev);
322
323 return ret;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200324}
325
326/* overlay needs to be enabled in OCMD reg */
Chris Wilson8dc5d142010-08-12 12:36:12 +0100327static int intel_overlay_continue(struct intel_overlay *overlay,
328 bool load_polyphase_filter)
Daniel Vetter02e792f2009-09-15 22:57:34 +0200329{
330 struct drm_device *dev = overlay->dev;
331 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilson8dc5d142010-08-12 12:36:12 +0100332 struct drm_i915_gem_request *request;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200333 u32 flip_addr = overlay->flip_addr;
334 u32 tmp;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200335
336 BUG_ON(!overlay->active);
337
Chris Wilson8dc5d142010-08-12 12:36:12 +0100338 request = kzalloc(sizeof(*request), GFP_KERNEL);
339 if (request == NULL)
340 return -ENOMEM;
341
Daniel Vetter02e792f2009-09-15 22:57:34 +0200342 if (load_polyphase_filter)
343 flip_addr |= OFC_UPDATE;
344
345 /* check for underruns */
346 tmp = I915_READ(DOVSTA);
347 if (tmp & (1 << 17))
348 DRM_DEBUG("overlay underrun, DOVSTA: %x\n", tmp);
349
Daniel Vetter4f8a5672010-02-11 14:14:43 +0100350 BEGIN_LP_RING(2);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200351 OUT_RING(MI_OVERLAY_FLIP | MI_OVERLAY_CONTINUE);
352 OUT_RING(flip_addr);
Daniel Vetter5a5a0c62009-09-15 22:57:36 +0200353 ADVANCE_LP_RING();
354
Zou Nan hai852835f2010-05-21 09:08:56 +0800355 overlay->last_flip_req =
Chris Wilson8dc5d142010-08-12 12:36:12 +0100356 i915_add_request(dev, NULL, request, &dev_priv->render_ring);
357 return 0;
Daniel Vetter5a5a0c62009-09-15 22:57:36 +0200358}
359
Chris Wilsonb303cf92010-08-12 14:03:48 +0100360static void intel_overlay_release_old_vid_tail(struct intel_overlay *overlay)
361{
362 struct drm_gem_object *obj = &overlay->old_vid_bo->base;
363
364 i915_gem_object_unpin(obj);
365 drm_gem_object_unreference(obj);
366
367 overlay->old_vid_bo = NULL;
368}
369
370static void intel_overlay_off_tail(struct intel_overlay *overlay)
371{
372 struct drm_gem_object *obj;
373
374 /* never have the overlay hw on without showing a frame */
375 BUG_ON(!overlay->vid_bo);
376 obj = &overlay->vid_bo->base;
377
378 i915_gem_object_unpin(obj);
379 drm_gem_object_unreference(obj);
380 overlay->vid_bo = NULL;
381
382 overlay->crtc->overlay = NULL;
383 overlay->crtc = NULL;
384 overlay->active = 0;
385}
386
Daniel Vetter02e792f2009-09-15 22:57:34 +0200387/* overlay needs to be disabled in OCMD reg */
Chris Wilson5dcdbcb2010-08-12 13:50:28 +0100388static int intel_overlay_off(struct intel_overlay *overlay,
389 bool interruptible)
Daniel Vetter02e792f2009-09-15 22:57:34 +0200390{
Daniel Vetter02e792f2009-09-15 22:57:34 +0200391 struct drm_device *dev = overlay->dev;
Chris Wilson8dc5d142010-08-12 12:36:12 +0100392 u32 flip_addr = overlay->flip_addr;
393 struct drm_i915_gem_request *request;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200394
395 BUG_ON(!overlay->active);
396
Chris Wilson8dc5d142010-08-12 12:36:12 +0100397 request = kzalloc(sizeof(*request), GFP_KERNEL);
398 if (request == NULL)
399 return -ENOMEM;
400
Daniel Vetter02e792f2009-09-15 22:57:34 +0200401 /* According to intel docs the overlay hw may hang (when switching
402 * off) without loading the filter coeffs. It is however unclear whether
403 * this applies to the disabling of the overlay or to the switching off
404 * of the hw. Do it in both cases */
405 flip_addr |= OFC_UPDATE;
406
Chris Wilson8dfbc342010-08-12 12:07:32 +0100407 BEGIN_LP_RING(6);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200408 /* wait for overlay to go idle */
Daniel Vetter02e792f2009-09-15 22:57:34 +0200409 OUT_RING(MI_OVERLAY_FLIP | MI_OVERLAY_CONTINUE);
410 OUT_RING(flip_addr);
Chris Wilson722506f2010-08-12 09:28:50 +0100411 OUT_RING(MI_WAIT_FOR_EVENT | MI_WAIT_FOR_OVERLAY_FLIP);
Chris Wilson722506f2010-08-12 09:28:50 +0100412 /* turn overlay off */
Chris Wilson722506f2010-08-12 09:28:50 +0100413 OUT_RING(MI_OVERLAY_FLIP | MI_OVERLAY_OFF);
414 OUT_RING(flip_addr);
415 OUT_RING(MI_WAIT_FOR_EVENT | MI_WAIT_FOR_OVERLAY_FLIP);
Chris Wilson722506f2010-08-12 09:28:50 +0100416 ADVANCE_LP_RING();
417
Chris Wilson5dcdbcb2010-08-12 13:50:28 +0100418 return intel_overlay_do_wait_request(overlay, request, interruptible,
Chris Wilsonb303cf92010-08-12 14:03:48 +0100419 intel_overlay_off_tail);
Daniel Vetter12ca45f2037-04-25 10:08:26 +0200420}
421
Daniel Vetter03f77ea2009-09-15 22:57:37 +0200422/* recover from an interruption due to a signal
423 * We have to be careful not to repeat work forever an make forward progess. */
Chris Wilson5dcdbcb2010-08-12 13:50:28 +0100424static int intel_overlay_recover_from_interrupt(struct intel_overlay *overlay,
425 bool interruptible)
Daniel Vetter03f77ea2009-09-15 22:57:37 +0200426{
427 struct drm_device *dev = overlay->dev;
Zou Nan hai852835f2010-05-21 09:08:56 +0800428 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter03f77ea2009-09-15 22:57:37 +0200429 int ret;
Daniel Vetter03f77ea2009-09-15 22:57:37 +0200430
Chris Wilsonb303cf92010-08-12 14:03:48 +0100431 if (overlay->last_flip_req == 0)
432 return 0;
Daniel Vetter03f77ea2009-09-15 22:57:37 +0200433
Zou Nan hai852835f2010-05-21 09:08:56 +0800434 ret = i915_do_wait_request(dev, overlay->last_flip_req,
Chris Wilson722506f2010-08-12 09:28:50 +0100435 interruptible, &dev_priv->render_ring);
Chris Wilsonb6c028e2010-08-12 11:55:08 +0100436 if (ret)
Daniel Vetter03f77ea2009-09-15 22:57:37 +0200437 return ret;
438
Chris Wilsonb303cf92010-08-12 14:03:48 +0100439 if (overlay->flip_tail)
440 overlay->flip_tail(overlay);
Chris Wilsonb6c028e2010-08-12 11:55:08 +0100441
Daniel Vetter03f77ea2009-09-15 22:57:37 +0200442 overlay->last_flip_req = 0;
443 return 0;
444}
445
Daniel Vetter5a5a0c62009-09-15 22:57:36 +0200446/* Wait for pending overlay flip and release old frame.
447 * Needs to be called before the overlay register are changed
Chris Wilson8d74f652010-08-12 10:35:26 +0100448 * via intel_overlay_(un)map_regs
449 */
Daniel Vetter02e792f2009-09-15 22:57:34 +0200450static int intel_overlay_release_old_vid(struct intel_overlay *overlay)
451{
Chris Wilson5cd68c92010-08-12 12:21:54 +0100452 struct drm_device *dev = overlay->dev;
453 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200454 int ret;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200455
Chris Wilson5cd68c92010-08-12 12:21:54 +0100456 /* Only wait if there is actually an old frame to release to
457 * guarantee forward progress.
458 */
Daniel Vetter03f77ea2009-09-15 22:57:37 +0200459 if (!overlay->old_vid_bo)
460 return 0;
461
Chris Wilson5cd68c92010-08-12 12:21:54 +0100462 if (I915_READ(ISR) & I915_OVERLAY_PLANE_FLIP_PENDING_INTERRUPT) {
Chris Wilson8dc5d142010-08-12 12:36:12 +0100463 struct drm_i915_gem_request *request;
464
Chris Wilson5cd68c92010-08-12 12:21:54 +0100465 /* synchronous slowpath */
Chris Wilson8dc5d142010-08-12 12:36:12 +0100466 request = kzalloc(sizeof(*request), GFP_KERNEL);
467 if (request == NULL)
468 return -ENOMEM;
469
Chris Wilson5cd68c92010-08-12 12:21:54 +0100470 BEGIN_LP_RING(2);
471 OUT_RING(MI_WAIT_FOR_EVENT | MI_WAIT_FOR_OVERLAY_FLIP);
472 OUT_RING(MI_NOOP);
473 ADVANCE_LP_RING();
Daniel Vetter02e792f2009-09-15 22:57:34 +0200474
Chris Wilson8dc5d142010-08-12 12:36:12 +0100475 ret = intel_overlay_do_wait_request(overlay, request, true,
Chris Wilsonb303cf92010-08-12 14:03:48 +0100476 intel_overlay_release_old_vid_tail);
Chris Wilson5cd68c92010-08-12 12:21:54 +0100477 if (ret)
478 return ret;
479 }
Daniel Vetter02e792f2009-09-15 22:57:34 +0200480
Chris Wilson5cd68c92010-08-12 12:21:54 +0100481 intel_overlay_release_old_vid_tail(overlay);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200482 return 0;
483}
484
485struct put_image_params {
486 int format;
487 short dst_x;
488 short dst_y;
489 short dst_w;
490 short dst_h;
491 short src_w;
492 short src_scan_h;
493 short src_scan_w;
494 short src_h;
495 short stride_Y;
496 short stride_UV;
497 int offset_Y;
498 int offset_U;
499 int offset_V;
500};
501
502static int packed_depth_bytes(u32 format)
503{
504 switch (format & I915_OVERLAY_DEPTH_MASK) {
Chris Wilson722506f2010-08-12 09:28:50 +0100505 case I915_OVERLAY_YUV422:
506 return 4;
507 case I915_OVERLAY_YUV411:
508 /* return 6; not implemented */
509 default:
510 return -EINVAL;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200511 }
512}
513
514static int packed_width_bytes(u32 format, short width)
515{
516 switch (format & I915_OVERLAY_DEPTH_MASK) {
Chris Wilson722506f2010-08-12 09:28:50 +0100517 case I915_OVERLAY_YUV422:
518 return width << 1;
519 default:
520 return -EINVAL;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200521 }
522}
523
524static int uv_hsubsampling(u32 format)
525{
526 switch (format & I915_OVERLAY_DEPTH_MASK) {
Chris Wilson722506f2010-08-12 09:28:50 +0100527 case I915_OVERLAY_YUV422:
528 case I915_OVERLAY_YUV420:
529 return 2;
530 case I915_OVERLAY_YUV411:
531 case I915_OVERLAY_YUV410:
532 return 4;
533 default:
534 return -EINVAL;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200535 }
536}
537
538static int uv_vsubsampling(u32 format)
539{
540 switch (format & I915_OVERLAY_DEPTH_MASK) {
Chris Wilson722506f2010-08-12 09:28:50 +0100541 case I915_OVERLAY_YUV420:
542 case I915_OVERLAY_YUV410:
543 return 2;
544 case I915_OVERLAY_YUV422:
545 case I915_OVERLAY_YUV411:
546 return 1;
547 default:
548 return -EINVAL;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200549 }
550}
551
552static u32 calc_swidthsw(struct drm_device *dev, u32 offset, u32 width)
553{
554 u32 mask, shift, ret;
555 if (IS_I9XX(dev)) {
556 mask = 0x3f;
557 shift = 6;
558 } else {
559 mask = 0x1f;
560 shift = 5;
561 }
562 ret = ((offset + width + mask) >> shift) - (offset >> shift);
563 if (IS_I9XX(dev))
564 ret <<= 1;
565 ret -=1;
566 return ret << 2;
567}
568
569static const u16 y_static_hcoeffs[N_HORIZ_Y_TAPS * N_PHASES] = {
570 0x3000, 0xb4a0, 0x1930, 0x1920, 0xb4a0,
571 0x3000, 0xb500, 0x19d0, 0x1880, 0xb440,
572 0x3000, 0xb540, 0x1a88, 0x2f80, 0xb3e0,
573 0x3000, 0xb580, 0x1b30, 0x2e20, 0xb380,
574 0x3000, 0xb5c0, 0x1bd8, 0x2cc0, 0xb320,
575 0x3020, 0xb5e0, 0x1c60, 0x2b80, 0xb2c0,
576 0x3020, 0xb5e0, 0x1cf8, 0x2a20, 0xb260,
577 0x3020, 0xb5e0, 0x1d80, 0x28e0, 0xb200,
578 0x3020, 0xb5c0, 0x1e08, 0x3f40, 0xb1c0,
579 0x3020, 0xb580, 0x1e78, 0x3ce0, 0xb160,
580 0x3040, 0xb520, 0x1ed8, 0x3aa0, 0xb120,
581 0x3040, 0xb4a0, 0x1f30, 0x3880, 0xb0e0,
582 0x3040, 0xb400, 0x1f78, 0x3680, 0xb0a0,
583 0x3020, 0xb340, 0x1fb8, 0x34a0, 0xb060,
584 0x3020, 0xb240, 0x1fe0, 0x32e0, 0xb040,
585 0x3020, 0xb140, 0x1ff8, 0x3160, 0xb020,
Chris Wilson722506f2010-08-12 09:28:50 +0100586 0xb000, 0x3000, 0x0800, 0x3000, 0xb000
587};
588
Daniel Vetter02e792f2009-09-15 22:57:34 +0200589static const u16 uv_static_hcoeffs[N_HORIZ_UV_TAPS * N_PHASES] = {
590 0x3000, 0x1800, 0x1800, 0xb000, 0x18d0, 0x2e60,
591 0xb000, 0x1990, 0x2ce0, 0xb020, 0x1a68, 0x2b40,
592 0xb040, 0x1b20, 0x29e0, 0xb060, 0x1bd8, 0x2880,
593 0xb080, 0x1c88, 0x3e60, 0xb0a0, 0x1d28, 0x3c00,
594 0xb0c0, 0x1db8, 0x39e0, 0xb0e0, 0x1e40, 0x37e0,
595 0xb100, 0x1eb8, 0x3620, 0xb100, 0x1f18, 0x34a0,
596 0xb100, 0x1f68, 0x3360, 0xb0e0, 0x1fa8, 0x3240,
597 0xb0c0, 0x1fe0, 0x3140, 0xb060, 0x1ff0, 0x30a0,
Chris Wilson722506f2010-08-12 09:28:50 +0100598 0x3000, 0x0800, 0x3000
599};
Daniel Vetter02e792f2009-09-15 22:57:34 +0200600
601static void update_polyphase_filter(struct overlay_registers *regs)
602{
603 memcpy(regs->Y_HCOEFS, y_static_hcoeffs, sizeof(y_static_hcoeffs));
604 memcpy(regs->UV_HCOEFS, uv_static_hcoeffs, sizeof(uv_static_hcoeffs));
605}
606
607static bool update_scaling_factors(struct intel_overlay *overlay,
608 struct overlay_registers *regs,
609 struct put_image_params *params)
610{
611 /* fixed point with a 12 bit shift */
612 u32 xscale, yscale, xscale_UV, yscale_UV;
613#define FP_SHIFT 12
614#define FRACT_MASK 0xfff
615 bool scale_changed = false;
616 int uv_hscale = uv_hsubsampling(params->format);
617 int uv_vscale = uv_vsubsampling(params->format);
618
619 if (params->dst_w > 1)
620 xscale = ((params->src_scan_w - 1) << FP_SHIFT)
621 /(params->dst_w);
622 else
623 xscale = 1 << FP_SHIFT;
624
625 if (params->dst_h > 1)
626 yscale = ((params->src_scan_h - 1) << FP_SHIFT)
627 /(params->dst_h);
628 else
629 yscale = 1 << FP_SHIFT;
630
631 /*if (params->format & I915_OVERLAY_YUV_PLANAR) {*/
Chris Wilson722506f2010-08-12 09:28:50 +0100632 xscale_UV = xscale/uv_hscale;
633 yscale_UV = yscale/uv_vscale;
634 /* make the Y scale to UV scale ratio an exact multiply */
635 xscale = xscale_UV * uv_hscale;
636 yscale = yscale_UV * uv_vscale;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200637 /*} else {
Chris Wilson722506f2010-08-12 09:28:50 +0100638 xscale_UV = 0;
639 yscale_UV = 0;
640 }*/
Daniel Vetter02e792f2009-09-15 22:57:34 +0200641
642 if (xscale != overlay->old_xscale || yscale != overlay->old_yscale)
643 scale_changed = true;
644 overlay->old_xscale = xscale;
645 overlay->old_yscale = yscale;
646
Chris Wilson722506f2010-08-12 09:28:50 +0100647 regs->YRGBSCALE = (((yscale & FRACT_MASK) << 20) |
648 ((xscale >> FP_SHIFT) << 16) |
649 ((xscale & FRACT_MASK) << 3));
650
651 regs->UVSCALE = (((yscale_UV & FRACT_MASK) << 20) |
652 ((xscale_UV >> FP_SHIFT) << 16) |
653 ((xscale_UV & FRACT_MASK) << 3));
654
655 regs->UVSCALEV = ((((yscale >> FP_SHIFT) << 16) |
656 ((yscale_UV >> FP_SHIFT) << 0)));
Daniel Vetter02e792f2009-09-15 22:57:34 +0200657
658 if (scale_changed)
659 update_polyphase_filter(regs);
660
661 return scale_changed;
662}
663
664static void update_colorkey(struct intel_overlay *overlay,
665 struct overlay_registers *regs)
666{
667 u32 key = overlay->color_key;
Chris Wilson6ba3ddd2010-08-12 09:30:58 +0100668
Daniel Vetter02e792f2009-09-15 22:57:34 +0200669 switch (overlay->crtc->base.fb->bits_per_pixel) {
Chris Wilson722506f2010-08-12 09:28:50 +0100670 case 8:
671 regs->DCLRKV = 0;
672 regs->DCLRKM = CLK_RGB8I_MASK | DST_KEY_ENABLE;
Chris Wilson6ba3ddd2010-08-12 09:30:58 +0100673 break;
674
Chris Wilson722506f2010-08-12 09:28:50 +0100675 case 16:
676 if (overlay->crtc->base.fb->depth == 15) {
677 regs->DCLRKV = RGB15_TO_COLORKEY(key);
678 regs->DCLRKM = CLK_RGB15_MASK | DST_KEY_ENABLE;
679 } else {
680 regs->DCLRKV = RGB16_TO_COLORKEY(key);
681 regs->DCLRKM = CLK_RGB16_MASK | DST_KEY_ENABLE;
682 }
Chris Wilson6ba3ddd2010-08-12 09:30:58 +0100683 break;
684
Chris Wilson722506f2010-08-12 09:28:50 +0100685 case 24:
686 case 32:
687 regs->DCLRKV = key;
688 regs->DCLRKM = CLK_RGB24_MASK | DST_KEY_ENABLE;
Chris Wilson6ba3ddd2010-08-12 09:30:58 +0100689 break;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200690 }
691}
692
693static u32 overlay_cmd_reg(struct put_image_params *params)
694{
695 u32 cmd = OCMD_ENABLE | OCMD_BUF_TYPE_FRAME | OCMD_BUFFER0;
696
697 if (params->format & I915_OVERLAY_YUV_PLANAR) {
698 switch (params->format & I915_OVERLAY_DEPTH_MASK) {
Chris Wilson722506f2010-08-12 09:28:50 +0100699 case I915_OVERLAY_YUV422:
700 cmd |= OCMD_YUV_422_PLANAR;
701 break;
702 case I915_OVERLAY_YUV420:
703 cmd |= OCMD_YUV_420_PLANAR;
704 break;
705 case I915_OVERLAY_YUV411:
706 case I915_OVERLAY_YUV410:
707 cmd |= OCMD_YUV_410_PLANAR;
708 break;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200709 }
710 } else { /* YUV packed */
711 switch (params->format & I915_OVERLAY_DEPTH_MASK) {
Chris Wilson722506f2010-08-12 09:28:50 +0100712 case I915_OVERLAY_YUV422:
713 cmd |= OCMD_YUV_422_PACKED;
714 break;
715 case I915_OVERLAY_YUV411:
716 cmd |= OCMD_YUV_411_PACKED;
717 break;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200718 }
719
720 switch (params->format & I915_OVERLAY_SWAP_MASK) {
Chris Wilson722506f2010-08-12 09:28:50 +0100721 case I915_OVERLAY_NO_SWAP:
722 break;
723 case I915_OVERLAY_UV_SWAP:
724 cmd |= OCMD_UV_SWAP;
725 break;
726 case I915_OVERLAY_Y_SWAP:
727 cmd |= OCMD_Y_SWAP;
728 break;
729 case I915_OVERLAY_Y_AND_UV_SWAP:
730 cmd |= OCMD_Y_AND_UV_SWAP;
731 break;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200732 }
733 }
734
735 return cmd;
736}
737
Chris Wilson5fe82c52010-08-12 12:38:21 +0100738static int intel_overlay_do_put_image(struct intel_overlay *overlay,
739 struct drm_gem_object *new_bo,
740 struct put_image_params *params)
Daniel Vetter02e792f2009-09-15 22:57:34 +0200741{
742 int ret, tmp_width;
743 struct overlay_registers *regs;
744 bool scale_changed = false;
Daniel Vetter23010e42010-03-08 13:35:02 +0100745 struct drm_i915_gem_object *bo_priv = to_intel_bo(new_bo);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200746 struct drm_device *dev = overlay->dev;
747
748 BUG_ON(!mutex_is_locked(&dev->struct_mutex));
749 BUG_ON(!mutex_is_locked(&dev->mode_config.mutex));
750 BUG_ON(!overlay);
751
Daniel Vetter02e792f2009-09-15 22:57:34 +0200752 ret = intel_overlay_release_old_vid(overlay);
753 if (ret != 0)
754 return ret;
755
756 ret = i915_gem_object_pin(new_bo, PAGE_SIZE);
757 if (ret != 0)
758 return ret;
759
760 ret = i915_gem_object_set_to_gtt_domain(new_bo, 0);
761 if (ret != 0)
762 goto out_unpin;
763
764 if (!overlay->active) {
Chris Wilson8d74f652010-08-12 10:35:26 +0100765 regs = intel_overlay_map_regs(overlay);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200766 if (!regs) {
767 ret = -ENOMEM;
768 goto out_unpin;
769 }
770 regs->OCONFIG = OCONF_CC_OUT_8BIT;
771 if (IS_I965GM(overlay->dev))
772 regs->OCONFIG |= OCONF_CSC_MODE_BT709;
773 regs->OCONFIG |= overlay->crtc->pipe == 0 ?
774 OCONF_PIPE_A : OCONF_PIPE_B;
Chris Wilson9bb2ff72010-08-12 12:02:11 +0100775 intel_overlay_unmap_regs(overlay, regs);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200776
777 ret = intel_overlay_on(overlay);
778 if (ret != 0)
779 goto out_unpin;
780 }
781
Chris Wilson8d74f652010-08-12 10:35:26 +0100782 regs = intel_overlay_map_regs(overlay);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200783 if (!regs) {
784 ret = -ENOMEM;
785 goto out_unpin;
786 }
787
788 regs->DWINPOS = (params->dst_y << 16) | params->dst_x;
789 regs->DWINSZ = (params->dst_h << 16) | params->dst_w;
790
791 if (params->format & I915_OVERLAY_YUV_PACKED)
792 tmp_width = packed_width_bytes(params->format, params->src_w);
793 else
794 tmp_width = params->src_w;
795
796 regs->SWIDTH = params->src_w;
797 regs->SWIDTHSW = calc_swidthsw(overlay->dev,
Chris Wilson722506f2010-08-12 09:28:50 +0100798 params->offset_Y, tmp_width);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200799 regs->SHEIGHT = params->src_h;
800 regs->OBUF_0Y = bo_priv->gtt_offset + params-> offset_Y;
801 regs->OSTRIDE = params->stride_Y;
802
803 if (params->format & I915_OVERLAY_YUV_PLANAR) {
804 int uv_hscale = uv_hsubsampling(params->format);
805 int uv_vscale = uv_vsubsampling(params->format);
806 u32 tmp_U, tmp_V;
807 regs->SWIDTH |= (params->src_w/uv_hscale) << 16;
808 tmp_U = calc_swidthsw(overlay->dev, params->offset_U,
Chris Wilson722506f2010-08-12 09:28:50 +0100809 params->src_w/uv_hscale);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200810 tmp_V = calc_swidthsw(overlay->dev, params->offset_V,
Chris Wilson722506f2010-08-12 09:28:50 +0100811 params->src_w/uv_hscale);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200812 regs->SWIDTHSW |= max_t(u32, tmp_U, tmp_V) << 16;
813 regs->SHEIGHT |= (params->src_h/uv_vscale) << 16;
814 regs->OBUF_0U = bo_priv->gtt_offset + params->offset_U;
815 regs->OBUF_0V = bo_priv->gtt_offset + params->offset_V;
816 regs->OSTRIDE |= params->stride_UV << 16;
817 }
818
819 scale_changed = update_scaling_factors(overlay, regs, params);
820
821 update_colorkey(overlay, regs);
822
823 regs->OCMD = overlay_cmd_reg(params);
824
Chris Wilson9bb2ff72010-08-12 12:02:11 +0100825 intel_overlay_unmap_regs(overlay, regs);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200826
Chris Wilson8dc5d142010-08-12 12:36:12 +0100827 ret = intel_overlay_continue(overlay, scale_changed);
828 if (ret)
829 goto out_unpin;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200830
831 overlay->old_vid_bo = overlay->vid_bo;
Daniel Vetter23010e42010-03-08 13:35:02 +0100832 overlay->vid_bo = to_intel_bo(new_bo);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200833
834 return 0;
835
836out_unpin:
837 i915_gem_object_unpin(new_bo);
838 return ret;
839}
840
Chris Wilson5dcdbcb2010-08-12 13:50:28 +0100841int intel_overlay_switch_off(struct intel_overlay *overlay,
842 bool interruptible)
Daniel Vetter02e792f2009-09-15 22:57:34 +0200843{
Daniel Vetter02e792f2009-09-15 22:57:34 +0200844 struct overlay_registers *regs;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200845 struct drm_device *dev = overlay->dev;
Chris Wilson5dcdbcb2010-08-12 13:50:28 +0100846 int ret;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200847
848 BUG_ON(!mutex_is_locked(&dev->struct_mutex));
849 BUG_ON(!mutex_is_locked(&dev->mode_config.mutex));
850
Chris Wilsonb303cf92010-08-12 14:03:48 +0100851 ret = intel_overlay_recover_from_interrupt(overlay, interruptible);
852 if (ret != 0)
853 return ret;
Daniel Vetter9bedb972009-11-30 15:55:49 +0100854
Daniel Vetter02e792f2009-09-15 22:57:34 +0200855 if (!overlay->active)
856 return 0;
857
Daniel Vetter02e792f2009-09-15 22:57:34 +0200858 ret = intel_overlay_release_old_vid(overlay);
859 if (ret != 0)
860 return ret;
861
Chris Wilson8d74f652010-08-12 10:35:26 +0100862 regs = intel_overlay_map_regs(overlay);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200863 regs->OCMD = 0;
Chris Wilson9bb2ff72010-08-12 12:02:11 +0100864 intel_overlay_unmap_regs(overlay, regs);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200865
Chris Wilson5dcdbcb2010-08-12 13:50:28 +0100866 ret = intel_overlay_off(overlay, interruptible);
Daniel Vetter03f77ea2009-09-15 22:57:37 +0200867 if (ret != 0)
868 return ret;
869
Daniel Vetter12ca45f2037-04-25 10:08:26 +0200870 intel_overlay_off_tail(overlay);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200871 return 0;
872}
873
874static int check_overlay_possible_on_crtc(struct intel_overlay *overlay,
875 struct intel_crtc *crtc)
876{
Chris Wilson722506f2010-08-12 09:28:50 +0100877 drm_i915_private_t *dev_priv = overlay->dev->dev_private;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200878 u32 pipeconf;
879 int pipeconf_reg = (crtc->pipe == 0) ? PIPEACONF : PIPEBCONF;
880
881 if (!crtc->base.enabled || crtc->dpms_mode != DRM_MODE_DPMS_ON)
882 return -EINVAL;
883
884 pipeconf = I915_READ(pipeconf_reg);
885
886 /* can't use the overlay with double wide pipe */
887 if (!IS_I965G(overlay->dev) && pipeconf & PIPEACONF_DOUBLE_WIDE)
888 return -EINVAL;
889
890 return 0;
891}
892
893static void update_pfit_vscale_ratio(struct intel_overlay *overlay)
894{
895 struct drm_device *dev = overlay->dev;
Chris Wilson722506f2010-08-12 09:28:50 +0100896 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200897 u32 pfit_control = I915_READ(PFIT_CONTROL);
Chris Wilson446d2182010-08-12 11:15:58 +0100898 u32 ratio;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200899
900 /* XXX: This is not the same logic as in the xorg driver, but more in
Chris Wilson446d2182010-08-12 11:15:58 +0100901 * line with the intel documentation for the i965
902 */
903 if (!IS_I965G(dev)) {
904 if (pfit_control & VERT_AUTO_SCALE)
905 ratio = I915_READ(PFIT_AUTO_RATIOS);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200906 else
Chris Wilson446d2182010-08-12 11:15:58 +0100907 ratio = I915_READ(PFIT_PGM_RATIOS);
908 ratio >>= PFIT_VERT_SCALE_SHIFT;
909 } else { /* on i965 use the PGM reg to read out the autoscaler values */
910 ratio = I915_READ(PFIT_PGM_RATIOS) >> PFIT_VERT_SCALE_SHIFT_965;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200911 }
912
913 overlay->pfit_vscale_ratio = ratio;
914}
915
916static int check_overlay_dst(struct intel_overlay *overlay,
917 struct drm_intel_overlay_put_image *rec)
918{
919 struct drm_display_mode *mode = &overlay->crtc->base.mode;
920
Chris Wilson722506f2010-08-12 09:28:50 +0100921 if (rec->dst_x < mode->crtc_hdisplay &&
922 rec->dst_x + rec->dst_width <= mode->crtc_hdisplay &&
923 rec->dst_y < mode->crtc_vdisplay &&
924 rec->dst_y + rec->dst_height <= mode->crtc_vdisplay)
Daniel Vetter02e792f2009-09-15 22:57:34 +0200925 return 0;
926 else
927 return -EINVAL;
928}
929
930static int check_overlay_scaling(struct put_image_params *rec)
931{
932 u32 tmp;
933
934 /* downscaling limit is 8.0 */
935 tmp = ((rec->src_scan_h << 16) / rec->dst_h) >> 16;
936 if (tmp > 7)
937 return -EINVAL;
938 tmp = ((rec->src_scan_w << 16) / rec->dst_w) >> 16;
939 if (tmp > 7)
940 return -EINVAL;
941
942 return 0;
943}
944
945static int check_overlay_src(struct drm_device *dev,
946 struct drm_intel_overlay_put_image *rec,
947 struct drm_gem_object *new_bo)
948{
Daniel Vetter02e792f2009-09-15 22:57:34 +0200949 int uv_hscale = uv_hsubsampling(rec->flags);
950 int uv_vscale = uv_vsubsampling(rec->flags);
Chris Wilson9f7c3f42010-08-12 11:29:34 +0100951 u32 stride_mask, depth, tmp;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200952
953 /* check src dimensions */
954 if (IS_845G(dev) || IS_I830(dev)) {
Chris Wilson722506f2010-08-12 09:28:50 +0100955 if (rec->src_height > IMAGE_MAX_HEIGHT_LEGACY ||
Chris Wilson9f7c3f42010-08-12 11:29:34 +0100956 rec->src_width > IMAGE_MAX_WIDTH_LEGACY)
Daniel Vetter02e792f2009-09-15 22:57:34 +0200957 return -EINVAL;
958 } else {
Chris Wilson722506f2010-08-12 09:28:50 +0100959 if (rec->src_height > IMAGE_MAX_HEIGHT ||
Chris Wilson9f7c3f42010-08-12 11:29:34 +0100960 rec->src_width > IMAGE_MAX_WIDTH)
Daniel Vetter02e792f2009-09-15 22:57:34 +0200961 return -EINVAL;
962 }
Chris Wilson9f7c3f42010-08-12 11:29:34 +0100963
Daniel Vetter02e792f2009-09-15 22:57:34 +0200964 /* better safe than sorry, use 4 as the maximal subsampling ratio */
Chris Wilson722506f2010-08-12 09:28:50 +0100965 if (rec->src_height < N_VERT_Y_TAPS*4 ||
Chris Wilson9f7c3f42010-08-12 11:29:34 +0100966 rec->src_width < N_HORIZ_Y_TAPS*4)
Daniel Vetter02e792f2009-09-15 22:57:34 +0200967 return -EINVAL;
968
Chris Wilsona1efd142010-07-12 19:35:38 +0100969 /* check alignment constraints */
Daniel Vetter02e792f2009-09-15 22:57:34 +0200970 switch (rec->flags & I915_OVERLAY_TYPE_MASK) {
Chris Wilson722506f2010-08-12 09:28:50 +0100971 case I915_OVERLAY_RGB:
972 /* not implemented */
973 return -EINVAL;
Chris Wilson9f7c3f42010-08-12 11:29:34 +0100974
Chris Wilson722506f2010-08-12 09:28:50 +0100975 case I915_OVERLAY_YUV_PACKED:
Chris Wilson722506f2010-08-12 09:28:50 +0100976 if (uv_vscale != 1)
Daniel Vetter02e792f2009-09-15 22:57:34 +0200977 return -EINVAL;
Chris Wilson9f7c3f42010-08-12 11:29:34 +0100978
979 depth = packed_depth_bytes(rec->flags);
Chris Wilson722506f2010-08-12 09:28:50 +0100980 if (depth < 0)
981 return depth;
Chris Wilson9f7c3f42010-08-12 11:29:34 +0100982
Chris Wilson722506f2010-08-12 09:28:50 +0100983 /* ignore UV planes */
984 rec->stride_UV = 0;
985 rec->offset_U = 0;
986 rec->offset_V = 0;
987 /* check pixel alignment */
988 if (rec->offset_Y % depth)
Daniel Vetter02e792f2009-09-15 22:57:34 +0200989 return -EINVAL;
Chris Wilson722506f2010-08-12 09:28:50 +0100990 break;
Chris Wilson9f7c3f42010-08-12 11:29:34 +0100991
Chris Wilson722506f2010-08-12 09:28:50 +0100992 case I915_OVERLAY_YUV_PLANAR:
993 if (uv_vscale < 0 || uv_hscale < 0)
994 return -EINVAL;
995 /* no offset restrictions for planar formats */
996 break;
Chris Wilson9f7c3f42010-08-12 11:29:34 +0100997
Chris Wilson722506f2010-08-12 09:28:50 +0100998 default:
999 return -EINVAL;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001000 }
1001
1002 if (rec->src_width % uv_hscale)
1003 return -EINVAL;
1004
1005 /* stride checking */
Chris Wilsona1efd142010-07-12 19:35:38 +01001006 if (IS_I830(dev) || IS_845G(dev))
1007 stride_mask = 255;
1008 else
1009 stride_mask = 63;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001010
1011 if (rec->stride_Y & stride_mask || rec->stride_UV & stride_mask)
1012 return -EINVAL;
1013 if (IS_I965G(dev) && rec->stride_Y < 512)
1014 return -EINVAL;
1015
1016 tmp = (rec->flags & I915_OVERLAY_TYPE_MASK) == I915_OVERLAY_YUV_PLANAR ?
Chris Wilson9f7c3f42010-08-12 11:29:34 +01001017 4096 : 8192;
1018 if (rec->stride_Y > tmp || rec->stride_UV > 2*1024)
Daniel Vetter02e792f2009-09-15 22:57:34 +02001019 return -EINVAL;
1020
1021 /* check buffer dimensions */
1022 switch (rec->flags & I915_OVERLAY_TYPE_MASK) {
Chris Wilson722506f2010-08-12 09:28:50 +01001023 case I915_OVERLAY_RGB:
1024 case I915_OVERLAY_YUV_PACKED:
1025 /* always 4 Y values per depth pixels */
1026 if (packed_width_bytes(rec->flags, rec->src_width) > rec->stride_Y)
1027 return -EINVAL;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001028
Chris Wilson722506f2010-08-12 09:28:50 +01001029 tmp = rec->stride_Y*rec->src_height;
1030 if (rec->offset_Y + tmp > new_bo->size)
1031 return -EINVAL;
1032 break;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001033
Chris Wilson722506f2010-08-12 09:28:50 +01001034 case I915_OVERLAY_YUV_PLANAR:
1035 if (rec->src_width > rec->stride_Y)
1036 return -EINVAL;
1037 if (rec->src_width/uv_hscale > rec->stride_UV)
1038 return -EINVAL;
1039
Chris Wilson9f7c3f42010-08-12 11:29:34 +01001040 tmp = rec->stride_Y * rec->src_height;
Chris Wilson722506f2010-08-12 09:28:50 +01001041 if (rec->offset_Y + tmp > new_bo->size)
1042 return -EINVAL;
Chris Wilson9f7c3f42010-08-12 11:29:34 +01001043
1044 tmp = rec->stride_UV * (rec->src_height / uv_vscale);
Chris Wilson722506f2010-08-12 09:28:50 +01001045 if (rec->offset_U + tmp > new_bo->size ||
1046 rec->offset_V + tmp > new_bo->size)
1047 return -EINVAL;
1048 break;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001049 }
1050
1051 return 0;
1052}
1053
1054int intel_overlay_put_image(struct drm_device *dev, void *data,
1055 struct drm_file *file_priv)
1056{
1057 struct drm_intel_overlay_put_image *put_image_rec = data;
1058 drm_i915_private_t *dev_priv = dev->dev_private;
1059 struct intel_overlay *overlay;
1060 struct drm_mode_object *drmmode_obj;
1061 struct intel_crtc *crtc;
1062 struct drm_gem_object *new_bo;
1063 struct put_image_params *params;
1064 int ret;
1065
1066 if (!dev_priv) {
1067 DRM_ERROR("called with no initialization\n");
1068 return -EINVAL;
1069 }
1070
1071 overlay = dev_priv->overlay;
1072 if (!overlay) {
1073 DRM_DEBUG("userspace bug: no overlay\n");
1074 return -ENODEV;
1075 }
1076
1077 if (!(put_image_rec->flags & I915_OVERLAY_ENABLE)) {
1078 mutex_lock(&dev->mode_config.mutex);
1079 mutex_lock(&dev->struct_mutex);
1080
Chris Wilson5dcdbcb2010-08-12 13:50:28 +01001081 ret = intel_overlay_switch_off(overlay, true);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001082
1083 mutex_unlock(&dev->struct_mutex);
1084 mutex_unlock(&dev->mode_config.mutex);
1085
1086 return ret;
1087 }
1088
1089 params = kmalloc(sizeof(struct put_image_params), GFP_KERNEL);
1090 if (!params)
1091 return -ENOMEM;
1092
1093 drmmode_obj = drm_mode_object_find(dev, put_image_rec->crtc_id,
Chris Wilson722506f2010-08-12 09:28:50 +01001094 DRM_MODE_OBJECT_CRTC);
Dan Carpenter915a4282010-03-06 14:05:39 +03001095 if (!drmmode_obj) {
1096 ret = -ENOENT;
1097 goto out_free;
1098 }
Daniel Vetter02e792f2009-09-15 22:57:34 +02001099 crtc = to_intel_crtc(obj_to_crtc(drmmode_obj));
1100
1101 new_bo = drm_gem_object_lookup(dev, file_priv,
Chris Wilson722506f2010-08-12 09:28:50 +01001102 put_image_rec->bo_handle);
Dan Carpenter915a4282010-03-06 14:05:39 +03001103 if (!new_bo) {
1104 ret = -ENOENT;
1105 goto out_free;
1106 }
Daniel Vetter02e792f2009-09-15 22:57:34 +02001107
1108 mutex_lock(&dev->mode_config.mutex);
1109 mutex_lock(&dev->struct_mutex);
1110
Chris Wilsonb303cf92010-08-12 14:03:48 +01001111 ret = intel_overlay_recover_from_interrupt(overlay, true);
1112 if (ret != 0)
1113 goto out_unlock;
Daniel Vetter03f77ea2009-09-15 22:57:37 +02001114
Daniel Vetter02e792f2009-09-15 22:57:34 +02001115 if (overlay->crtc != crtc) {
1116 struct drm_display_mode *mode = &crtc->base.mode;
Chris Wilson5dcdbcb2010-08-12 13:50:28 +01001117 ret = intel_overlay_switch_off(overlay, true);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001118 if (ret != 0)
1119 goto out_unlock;
1120
1121 ret = check_overlay_possible_on_crtc(overlay, crtc);
1122 if (ret != 0)
1123 goto out_unlock;
1124
1125 overlay->crtc = crtc;
1126 crtc->overlay = overlay;
1127
1128 if (intel_panel_fitter_pipe(dev) == crtc->pipe
1129 /* and line to wide, i.e. one-line-mode */
1130 && mode->hdisplay > 1024) {
1131 overlay->pfit_active = 1;
1132 update_pfit_vscale_ratio(overlay);
1133 } else
1134 overlay->pfit_active = 0;
1135 }
1136
1137 ret = check_overlay_dst(overlay, put_image_rec);
1138 if (ret != 0)
1139 goto out_unlock;
1140
1141 if (overlay->pfit_active) {
1142 params->dst_y = ((((u32)put_image_rec->dst_y) << 12) /
Chris Wilson722506f2010-08-12 09:28:50 +01001143 overlay->pfit_vscale_ratio);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001144 /* shifting right rounds downwards, so add 1 */
1145 params->dst_h = ((((u32)put_image_rec->dst_height) << 12) /
Chris Wilson722506f2010-08-12 09:28:50 +01001146 overlay->pfit_vscale_ratio) + 1;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001147 } else {
1148 params->dst_y = put_image_rec->dst_y;
1149 params->dst_h = put_image_rec->dst_height;
1150 }
1151 params->dst_x = put_image_rec->dst_x;
1152 params->dst_w = put_image_rec->dst_width;
1153
1154 params->src_w = put_image_rec->src_width;
1155 params->src_h = put_image_rec->src_height;
1156 params->src_scan_w = put_image_rec->src_scan_width;
1157 params->src_scan_h = put_image_rec->src_scan_height;
Chris Wilson722506f2010-08-12 09:28:50 +01001158 if (params->src_scan_h > params->src_h ||
1159 params->src_scan_w > params->src_w) {
Daniel Vetter02e792f2009-09-15 22:57:34 +02001160 ret = -EINVAL;
1161 goto out_unlock;
1162 }
1163
1164 ret = check_overlay_src(dev, put_image_rec, new_bo);
1165 if (ret != 0)
1166 goto out_unlock;
1167 params->format = put_image_rec->flags & ~I915_OVERLAY_FLAGS_MASK;
1168 params->stride_Y = put_image_rec->stride_Y;
1169 params->stride_UV = put_image_rec->stride_UV;
1170 params->offset_Y = put_image_rec->offset_Y;
1171 params->offset_U = put_image_rec->offset_U;
1172 params->offset_V = put_image_rec->offset_V;
1173
1174 /* Check scaling after src size to prevent a divide-by-zero. */
1175 ret = check_overlay_scaling(params);
1176 if (ret != 0)
1177 goto out_unlock;
1178
1179 ret = intel_overlay_do_put_image(overlay, new_bo, params);
1180 if (ret != 0)
1181 goto out_unlock;
1182
1183 mutex_unlock(&dev->struct_mutex);
1184 mutex_unlock(&dev->mode_config.mutex);
1185
1186 kfree(params);
1187
1188 return 0;
1189
1190out_unlock:
1191 mutex_unlock(&dev->struct_mutex);
1192 mutex_unlock(&dev->mode_config.mutex);
Luca Barbieribc9025b2010-02-09 05:49:12 +00001193 drm_gem_object_unreference_unlocked(new_bo);
Dan Carpenter915a4282010-03-06 14:05:39 +03001194out_free:
Daniel Vetter02e792f2009-09-15 22:57:34 +02001195 kfree(params);
1196
1197 return ret;
1198}
1199
1200static void update_reg_attrs(struct intel_overlay *overlay,
1201 struct overlay_registers *regs)
1202{
1203 regs->OCLRC0 = (overlay->contrast << 18) | (overlay->brightness & 0xff);
1204 regs->OCLRC1 = overlay->saturation;
1205}
1206
1207static bool check_gamma_bounds(u32 gamma1, u32 gamma2)
1208{
1209 int i;
1210
1211 if (gamma1 & 0xff000000 || gamma2 & 0xff000000)
1212 return false;
1213
1214 for (i = 0; i < 3; i++) {
Chris Wilson722506f2010-08-12 09:28:50 +01001215 if (((gamma1 >> i*8) & 0xff) >= ((gamma2 >> i*8) & 0xff))
Daniel Vetter02e792f2009-09-15 22:57:34 +02001216 return false;
1217 }
1218
1219 return true;
1220}
1221
1222static bool check_gamma5_errata(u32 gamma5)
1223{
1224 int i;
1225
1226 for (i = 0; i < 3; i++) {
1227 if (((gamma5 >> i*8) & 0xff) == 0x80)
1228 return false;
1229 }
1230
1231 return true;
1232}
1233
1234static int check_gamma(struct drm_intel_overlay_attrs *attrs)
1235{
Chris Wilson722506f2010-08-12 09:28:50 +01001236 if (!check_gamma_bounds(0, attrs->gamma0) ||
1237 !check_gamma_bounds(attrs->gamma0, attrs->gamma1) ||
1238 !check_gamma_bounds(attrs->gamma1, attrs->gamma2) ||
1239 !check_gamma_bounds(attrs->gamma2, attrs->gamma3) ||
1240 !check_gamma_bounds(attrs->gamma3, attrs->gamma4) ||
1241 !check_gamma_bounds(attrs->gamma4, attrs->gamma5) ||
1242 !check_gamma_bounds(attrs->gamma5, 0x00ffffff))
Daniel Vetter02e792f2009-09-15 22:57:34 +02001243 return -EINVAL;
Chris Wilson722506f2010-08-12 09:28:50 +01001244
Daniel Vetter02e792f2009-09-15 22:57:34 +02001245 if (!check_gamma5_errata(attrs->gamma5))
1246 return -EINVAL;
Chris Wilson722506f2010-08-12 09:28:50 +01001247
Daniel Vetter02e792f2009-09-15 22:57:34 +02001248 return 0;
1249}
1250
1251int intel_overlay_attrs(struct drm_device *dev, void *data,
1252 struct drm_file *file_priv)
1253{
1254 struct drm_intel_overlay_attrs *attrs = data;
1255 drm_i915_private_t *dev_priv = dev->dev_private;
1256 struct intel_overlay *overlay;
1257 struct overlay_registers *regs;
1258 int ret;
1259
1260 if (!dev_priv) {
1261 DRM_ERROR("called with no initialization\n");
1262 return -EINVAL;
1263 }
1264
1265 overlay = dev_priv->overlay;
1266 if (!overlay) {
1267 DRM_DEBUG("userspace bug: no overlay\n");
1268 return -ENODEV;
1269 }
1270
1271 mutex_lock(&dev->mode_config.mutex);
1272 mutex_lock(&dev->struct_mutex);
1273
Chris Wilson60fc3322010-08-12 10:44:45 +01001274 ret = -EINVAL;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001275 if (!(attrs->flags & I915_OVERLAY_UPDATE_ATTRS)) {
Chris Wilson60fc3322010-08-12 10:44:45 +01001276 attrs->color_key = overlay->color_key;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001277 attrs->brightness = overlay->brightness;
Chris Wilson60fc3322010-08-12 10:44:45 +01001278 attrs->contrast = overlay->contrast;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001279 attrs->saturation = overlay->saturation;
1280
1281 if (IS_I9XX(dev)) {
1282 attrs->gamma0 = I915_READ(OGAMC0);
1283 attrs->gamma1 = I915_READ(OGAMC1);
1284 attrs->gamma2 = I915_READ(OGAMC2);
1285 attrs->gamma3 = I915_READ(OGAMC3);
1286 attrs->gamma4 = I915_READ(OGAMC4);
1287 attrs->gamma5 = I915_READ(OGAMC5);
1288 }
Daniel Vetter02e792f2009-09-15 22:57:34 +02001289 } else {
Chris Wilson60fc3322010-08-12 10:44:45 +01001290 if (attrs->brightness < -128 || attrs->brightness > 127)
Daniel Vetter02e792f2009-09-15 22:57:34 +02001291 goto out_unlock;
Chris Wilson60fc3322010-08-12 10:44:45 +01001292 if (attrs->contrast > 255)
1293 goto out_unlock;
1294 if (attrs->saturation > 1023)
1295 goto out_unlock;
Chris Wilson722506f2010-08-12 09:28:50 +01001296
Chris Wilson60fc3322010-08-12 10:44:45 +01001297 overlay->color_key = attrs->color_key;
1298 overlay->brightness = attrs->brightness;
1299 overlay->contrast = attrs->contrast;
1300 overlay->saturation = attrs->saturation;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001301
Chris Wilson8d74f652010-08-12 10:35:26 +01001302 regs = intel_overlay_map_regs(overlay);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001303 if (!regs) {
1304 ret = -ENOMEM;
1305 goto out_unlock;
1306 }
1307
1308 update_reg_attrs(overlay, regs);
1309
Chris Wilson9bb2ff72010-08-12 12:02:11 +01001310 intel_overlay_unmap_regs(overlay, regs);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001311
1312 if (attrs->flags & I915_OVERLAY_UPDATE_GAMMA) {
Chris Wilson60fc3322010-08-12 10:44:45 +01001313 if (!IS_I9XX(dev))
Daniel Vetter02e792f2009-09-15 22:57:34 +02001314 goto out_unlock;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001315
1316 if (overlay->active) {
1317 ret = -EBUSY;
1318 goto out_unlock;
1319 }
1320
1321 ret = check_gamma(attrs);
Chris Wilson60fc3322010-08-12 10:44:45 +01001322 if (ret)
Daniel Vetter02e792f2009-09-15 22:57:34 +02001323 goto out_unlock;
1324
1325 I915_WRITE(OGAMC0, attrs->gamma0);
1326 I915_WRITE(OGAMC1, attrs->gamma1);
1327 I915_WRITE(OGAMC2, attrs->gamma2);
1328 I915_WRITE(OGAMC3, attrs->gamma3);
1329 I915_WRITE(OGAMC4, attrs->gamma4);
1330 I915_WRITE(OGAMC5, attrs->gamma5);
1331 }
Daniel Vetter02e792f2009-09-15 22:57:34 +02001332 }
1333
Chris Wilson60fc3322010-08-12 10:44:45 +01001334 ret = 0;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001335out_unlock:
1336 mutex_unlock(&dev->struct_mutex);
1337 mutex_unlock(&dev->mode_config.mutex);
1338
1339 return ret;
1340}
1341
1342void intel_setup_overlay(struct drm_device *dev)
1343{
1344 drm_i915_private_t *dev_priv = dev->dev_private;
1345 struct intel_overlay *overlay;
1346 struct drm_gem_object *reg_bo;
1347 struct overlay_registers *regs;
1348 int ret;
1349
Chris Wilson315781482010-08-12 09:42:51 +01001350 if (!HAS_OVERLAY(dev))
Daniel Vetter02e792f2009-09-15 22:57:34 +02001351 return;
1352
1353 overlay = kzalloc(sizeof(struct intel_overlay), GFP_KERNEL);
1354 if (!overlay)
1355 return;
1356 overlay->dev = dev;
1357
Daniel Vetterac52bc52010-04-09 19:05:06 +00001358 reg_bo = i915_gem_alloc_object(dev, PAGE_SIZE);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001359 if (!reg_bo)
1360 goto out_free;
Daniel Vetter23010e42010-03-08 13:35:02 +01001361 overlay->reg_bo = to_intel_bo(reg_bo);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001362
Chris Wilson315781482010-08-12 09:42:51 +01001363 if (OVERLAY_NEEDS_PHYSICAL(dev)) {
1364 ret = i915_gem_attach_phys_object(dev, reg_bo,
1365 I915_GEM_PHYS_OVERLAY_REGS,
Chris Wilsona2930122010-08-12 10:47:56 +01001366 PAGE_SIZE);
Chris Wilson315781482010-08-12 09:42:51 +01001367 if (ret) {
1368 DRM_ERROR("failed to attach phys overlay regs\n");
1369 goto out_free_bo;
1370 }
1371 overlay->flip_addr = overlay->reg_bo->phys_obj->handle->busaddr;
1372 } else {
Daniel Vetter02e792f2009-09-15 22:57:34 +02001373 ret = i915_gem_object_pin(reg_bo, PAGE_SIZE);
1374 if (ret) {
1375 DRM_ERROR("failed to pin overlay register bo\n");
1376 goto out_free_bo;
1377 }
1378 overlay->flip_addr = overlay->reg_bo->gtt_offset;
Chris Wilson0ddc1282010-08-12 09:35:00 +01001379
1380 ret = i915_gem_object_set_to_gtt_domain(reg_bo, true);
1381 if (ret) {
1382 DRM_ERROR("failed to move overlay register bo into the GTT\n");
1383 goto out_unpin_bo;
1384 }
Daniel Vetter02e792f2009-09-15 22:57:34 +02001385 }
1386
1387 /* init all values */
1388 overlay->color_key = 0x0101fe;
1389 overlay->brightness = -19;
1390 overlay->contrast = 75;
1391 overlay->saturation = 146;
1392
Chris Wilson8d74f652010-08-12 10:35:26 +01001393 regs = intel_overlay_map_regs(overlay);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001394 if (!regs)
1395 goto out_free_bo;
1396
1397 memset(regs, 0, sizeof(struct overlay_registers));
1398 update_polyphase_filter(regs);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001399 update_reg_attrs(overlay, regs);
1400
Chris Wilson9bb2ff72010-08-12 12:02:11 +01001401 intel_overlay_unmap_regs(overlay, regs);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001402
1403 dev_priv->overlay = overlay;
1404 DRM_INFO("initialized overlay support\n");
1405 return;
1406
Chris Wilson0ddc1282010-08-12 09:35:00 +01001407out_unpin_bo:
1408 i915_gem_object_unpin(reg_bo);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001409out_free_bo:
1410 drm_gem_object_unreference(reg_bo);
1411out_free:
1412 kfree(overlay);
1413 return;
1414}
1415
1416void intel_cleanup_overlay(struct drm_device *dev)
1417{
Chris Wilson722506f2010-08-12 09:28:50 +01001418 drm_i915_private_t *dev_priv = dev->dev_private;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001419
Chris Wilson62cf4e62010-08-12 10:50:36 +01001420 if (!dev_priv->overlay)
1421 return;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001422
Chris Wilson62cf4e62010-08-12 10:50:36 +01001423 /* The bo's should be free'd by the generic code already.
1424 * Furthermore modesetting teardown happens beforehand so the
1425 * hardware should be off already */
1426 BUG_ON(dev_priv->overlay->active);
1427
1428 drm_gem_object_unreference_unlocked(&dev_priv->overlay->reg_bo->base);
1429 kfree(dev_priv->overlay);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001430}
Chris Wilson6ef3d422010-08-04 20:26:07 +01001431
Chris Wilson3bd3c932010-08-19 08:19:30 +01001432#ifdef CONFIG_DEBUG_FS
1433#include <linux/seq_file.h>
1434
Chris Wilson6ef3d422010-08-04 20:26:07 +01001435struct intel_overlay_error_state {
1436 struct overlay_registers regs;
1437 unsigned long base;
1438 u32 dovsta;
1439 u32 isr;
1440};
1441
Chris Wilson3bd3c932010-08-19 08:19:30 +01001442static struct overlay_registers *
1443intel_overlay_map_regs_atomic(struct intel_overlay *overlay,
1444 int slot)
1445{
1446 drm_i915_private_t *dev_priv = overlay->dev->dev_private;
1447 struct overlay_registers *regs;
1448
1449 if (OVERLAY_NEEDS_PHYSICAL(overlay->dev))
1450 regs = overlay->reg_bo->phys_obj->handle->vaddr;
1451 else
1452 regs = io_mapping_map_atomic_wc(dev_priv->mm.gtt_mapping,
1453 overlay->reg_bo->gtt_offset,
1454 slot);
1455
1456 return regs;
1457}
1458
1459static void intel_overlay_unmap_regs_atomic(struct intel_overlay *overlay,
1460 int slot,
1461 struct overlay_registers *regs)
1462{
1463 if (!OVERLAY_NEEDS_PHYSICAL(overlay->dev))
1464 io_mapping_unmap_atomic(regs, slot);
1465}
1466
1467
Chris Wilson6ef3d422010-08-04 20:26:07 +01001468struct intel_overlay_error_state *
1469intel_overlay_capture_error_state(struct drm_device *dev)
1470{
1471 drm_i915_private_t *dev_priv = dev->dev_private;
1472 struct intel_overlay *overlay = dev_priv->overlay;
1473 struct intel_overlay_error_state *error;
1474 struct overlay_registers __iomem *regs;
1475
1476 if (!overlay || !overlay->active)
1477 return NULL;
1478
1479 error = kmalloc(sizeof(*error), GFP_ATOMIC);
1480 if (error == NULL)
1481 return NULL;
1482
1483 error->dovsta = I915_READ(DOVSTA);
1484 error->isr = I915_READ(ISR);
Chris Wilson315781482010-08-12 09:42:51 +01001485 if (OVERLAY_NEEDS_PHYSICAL(overlay->dev))
Chris Wilson6ef3d422010-08-04 20:26:07 +01001486 error->base = (long) overlay->reg_bo->phys_obj->handle->vaddr;
Chris Wilson315781482010-08-12 09:42:51 +01001487 else
1488 error->base = (long) overlay->reg_bo->gtt_offset;
Chris Wilson6ef3d422010-08-04 20:26:07 +01001489
Chris Wilson8d74f652010-08-12 10:35:26 +01001490 regs = intel_overlay_map_regs_atomic(overlay, KM_IRQ0);
Chris Wilson6ef3d422010-08-04 20:26:07 +01001491 if (!regs)
1492 goto err;
1493
1494 memcpy_fromio(&error->regs, regs, sizeof(struct overlay_registers));
Chris Wilson9bb2ff72010-08-12 12:02:11 +01001495 intel_overlay_unmap_regs_atomic(overlay, KM_IRQ0, regs);
Chris Wilson6ef3d422010-08-04 20:26:07 +01001496
1497 return error;
1498
1499err:
1500 kfree(error);
1501 return NULL;
1502}
1503
1504void
1505intel_overlay_print_error_state(struct seq_file *m, struct intel_overlay_error_state *error)
1506{
1507 seq_printf(m, "Overlay, status: 0x%08x, interrupt: 0x%08x\n",
1508 error->dovsta, error->isr);
1509 seq_printf(m, " Register file at 0x%08lx:\n",
1510 error->base);
1511
1512#define P(x) seq_printf(m, " " #x ": 0x%08x\n", error->regs.x)
1513 P(OBUF_0Y);
1514 P(OBUF_1Y);
1515 P(OBUF_0U);
1516 P(OBUF_0V);
1517 P(OBUF_1U);
1518 P(OBUF_1V);
1519 P(OSTRIDE);
1520 P(YRGB_VPH);
1521 P(UV_VPH);
1522 P(HORZ_PH);
1523 P(INIT_PHS);
1524 P(DWINPOS);
1525 P(DWINSZ);
1526 P(SWIDTH);
1527 P(SWIDTHSW);
1528 P(SHEIGHT);
1529 P(YRGBSCALE);
1530 P(UVSCALE);
1531 P(OCLRC0);
1532 P(OCLRC1);
1533 P(DCLRKV);
1534 P(DCLRKM);
1535 P(SCLRKVH);
1536 P(SCLRKVL);
1537 P(SCLRKEN);
1538 P(OCONFIG);
1539 P(OCMD);
1540 P(OSTART_0Y);
1541 P(OSTART_1Y);
1542 P(OSTART_0U);
1543 P(OSTART_0V);
1544 P(OSTART_1U);
1545 P(OSTART_1V);
1546 P(OTILEOFF_0Y);
1547 P(OTILEOFF_1Y);
1548 P(OTILEOFF_0U);
1549 P(OTILEOFF_0V);
1550 P(OTILEOFF_1U);
1551 P(OTILEOFF_1V);
1552 P(FASTHSCALE);
1553 P(UVSCALEV);
1554#undef P
1555}
Chris Wilson3bd3c932010-08-19 08:19:30 +01001556#endif