blob: 6694e9230cd5bea860740bf1d5934d0a8db1ede1 [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 */
David Howells760285e2012-10-02 18:01:07 +010028#include <drm/drmP.h>
29#include <drm/i915_drm.h>
Daniel Vetter02e792f2009-09-15 22:57:34 +020030#include "i915_drv.h"
31#include "i915_reg.h"
32#include "intel_drv.h"
33
34/* Limits for overlay size. According to intel doc, the real limits are:
35 * Y width: 4095, UV width (planar): 2047, Y height: 2047,
36 * UV width (planar): * 1023. But the xorg thinks 2048 for height and width. Use
37 * the mininum of both. */
38#define IMAGE_MAX_WIDTH 2048
39#define IMAGE_MAX_HEIGHT 2046 /* 2 * 1023 */
40/* on 830 and 845 these large limits result in the card hanging */
41#define IMAGE_MAX_WIDTH_LEGACY 1024
42#define IMAGE_MAX_HEIGHT_LEGACY 1088
43
44/* overlay register definitions */
45/* OCMD register */
46#define OCMD_TILED_SURFACE (0x1<<19)
47#define OCMD_MIRROR_MASK (0x3<<17)
48#define OCMD_MIRROR_MODE (0x3<<17)
49#define OCMD_MIRROR_HORIZONTAL (0x1<<17)
50#define OCMD_MIRROR_VERTICAL (0x2<<17)
51#define OCMD_MIRROR_BOTH (0x3<<17)
52#define OCMD_BYTEORDER_MASK (0x3<<14) /* zero for YUYV or FOURCC YUY2 */
53#define OCMD_UV_SWAP (0x1<<14) /* YVYU */
54#define OCMD_Y_SWAP (0x2<<14) /* UYVY or FOURCC UYVY */
55#define OCMD_Y_AND_UV_SWAP (0x3<<14) /* VYUY */
56#define OCMD_SOURCE_FORMAT_MASK (0xf<<10)
57#define OCMD_RGB_888 (0x1<<10) /* not in i965 Intel docs */
58#define OCMD_RGB_555 (0x2<<10) /* not in i965 Intel docs */
59#define OCMD_RGB_565 (0x3<<10) /* not in i965 Intel docs */
60#define OCMD_YUV_422_PACKED (0x8<<10)
61#define OCMD_YUV_411_PACKED (0x9<<10) /* not in i965 Intel docs */
62#define OCMD_YUV_420_PLANAR (0xc<<10)
63#define OCMD_YUV_422_PLANAR (0xd<<10)
64#define OCMD_YUV_410_PLANAR (0xe<<10) /* also 411 */
65#define OCMD_TVSYNCFLIP_PARITY (0x1<<9)
66#define OCMD_TVSYNCFLIP_ENABLE (0x1<<7)
Chris Wilsond7961362010-07-13 13:52:17 +010067#define OCMD_BUF_TYPE_MASK (0x1<<5)
Daniel Vetter02e792f2009-09-15 22:57:34 +020068#define OCMD_BUF_TYPE_FRAME (0x0<<5)
69#define OCMD_BUF_TYPE_FIELD (0x1<<5)
70#define OCMD_TEST_MODE (0x1<<4)
71#define OCMD_BUFFER_SELECT (0x3<<2)
72#define OCMD_BUFFER0 (0x0<<2)
73#define OCMD_BUFFER1 (0x1<<2)
74#define OCMD_FIELD_SELECT (0x1<<2)
75#define OCMD_FIELD0 (0x0<<1)
76#define OCMD_FIELD1 (0x1<<1)
77#define OCMD_ENABLE (0x1<<0)
78
79/* OCONFIG register */
80#define OCONF_PIPE_MASK (0x1<<18)
81#define OCONF_PIPE_A (0x0<<18)
82#define OCONF_PIPE_B (0x1<<18)
83#define OCONF_GAMMA2_ENABLE (0x1<<16)
84#define OCONF_CSC_MODE_BT601 (0x0<<5)
85#define OCONF_CSC_MODE_BT709 (0x1<<5)
86#define OCONF_CSC_BYPASS (0x1<<4)
87#define OCONF_CC_OUT_8BIT (0x1<<3)
88#define OCONF_TEST_MODE (0x1<<2)
89#define OCONF_THREE_LINE_BUFFER (0x1<<0)
90#define OCONF_TWO_LINE_BUFFER (0x0<<0)
91
92/* DCLRKM (dst-key) register */
93#define DST_KEY_ENABLE (0x1<<31)
94#define CLK_RGB24_MASK 0x0
95#define CLK_RGB16_MASK 0x070307
96#define CLK_RGB15_MASK 0x070707
97#define CLK_RGB8I_MASK 0xffffff
98
99#define RGB16_TO_COLORKEY(c) \
100 (((c & 0xF800) << 8) | ((c & 0x07E0) << 5) | ((c & 0x001F) << 3))
101#define RGB15_TO_COLORKEY(c) \
102 (((c & 0x7c00) << 9) | ((c & 0x03E0) << 6) | ((c & 0x001F) << 3))
103
104/* overlay flip addr flag */
105#define OFC_UPDATE 0x1
106
107/* polyphase filter coefficients */
108#define N_HORIZ_Y_TAPS 5
109#define N_VERT_Y_TAPS 3
110#define N_HORIZ_UV_TAPS 3
111#define N_VERT_UV_TAPS 3
112#define N_PHASES 17
113#define MAX_TAPS 5
114
115/* memory bufferd overlay registers */
116struct overlay_registers {
Akshay Joshi0206e352011-08-16 15:34:10 -0400117 u32 OBUF_0Y;
118 u32 OBUF_1Y;
119 u32 OBUF_0U;
120 u32 OBUF_0V;
121 u32 OBUF_1U;
122 u32 OBUF_1V;
123 u32 OSTRIDE;
124 u32 YRGB_VPH;
125 u32 UV_VPH;
126 u32 HORZ_PH;
127 u32 INIT_PHS;
128 u32 DWINPOS;
129 u32 DWINSZ;
130 u32 SWIDTH;
131 u32 SWIDTHSW;
132 u32 SHEIGHT;
133 u32 YRGBSCALE;
134 u32 UVSCALE;
135 u32 OCLRC0;
136 u32 OCLRC1;
137 u32 DCLRKV;
138 u32 DCLRKM;
139 u32 SCLRKVH;
140 u32 SCLRKVL;
141 u32 SCLRKEN;
142 u32 OCONFIG;
143 u32 OCMD;
144 u32 RESERVED1; /* 0x6C */
145 u32 OSTART_0Y;
146 u32 OSTART_1Y;
147 u32 OSTART_0U;
148 u32 OSTART_0V;
149 u32 OSTART_1U;
150 u32 OSTART_1V;
151 u32 OTILEOFF_0Y;
152 u32 OTILEOFF_1Y;
153 u32 OTILEOFF_0U;
154 u32 OTILEOFF_0V;
155 u32 OTILEOFF_1U;
156 u32 OTILEOFF_1V;
157 u32 FASTHSCALE; /* 0xA0 */
158 u32 UVSCALEV; /* 0xA4 */
159 u32 RESERVEDC[(0x200 - 0xA8) / 4]; /* 0xA8 - 0x1FC */
160 u16 Y_VCOEFS[N_VERT_Y_TAPS * N_PHASES]; /* 0x200 */
161 u16 RESERVEDD[0x100 / 2 - N_VERT_Y_TAPS * N_PHASES];
162 u16 Y_HCOEFS[N_HORIZ_Y_TAPS * N_PHASES]; /* 0x300 */
163 u16 RESERVEDE[0x200 / 2 - N_HORIZ_Y_TAPS * N_PHASES];
164 u16 UV_VCOEFS[N_VERT_UV_TAPS * N_PHASES]; /* 0x500 */
165 u16 RESERVEDF[0x100 / 2 - N_VERT_UV_TAPS * N_PHASES];
166 u16 UV_HCOEFS[N_HORIZ_UV_TAPS * N_PHASES]; /* 0x600 */
167 u16 RESERVEDG[0x100 / 2 - N_HORIZ_UV_TAPS * N_PHASES];
Daniel Vetter02e792f2009-09-15 22:57:34 +0200168};
169
Chris Wilson23f09ce2010-08-12 13:53:37 +0100170struct intel_overlay {
171 struct drm_device *dev;
172 struct intel_crtc *crtc;
173 struct drm_i915_gem_object *vid_bo;
174 struct drm_i915_gem_object *old_vid_bo;
Ville Syrjälä209c2a52015-03-31 10:37:23 +0300175 bool active;
176 bool pfit_active;
Chris Wilson23f09ce2010-08-12 13:53:37 +0100177 u32 pfit_vscale_ratio; /* shifted-point number, (1<<12) == 1.0 */
Chris Wilsonea9da4e2015-04-02 10:35:08 +0100178 u32 color_key:24;
179 u32 color_key_enabled:1;
Chris Wilson23f09ce2010-08-12 13:53:37 +0100180 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 */
John Harrison9bfc01a2014-11-24 18:49:31 +0000186 struct drm_i915_gem_request *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
Ben Widawsky75020bc2012-04-16 14:07:43 -0700190static struct overlay_registers __iomem *
Chris Wilson8d74f652010-08-12 10:35:26 +0100191intel_overlay_map_regs(struct intel_overlay *overlay)
Daniel Vetter02e792f2009-09-15 22:57:34 +0200192{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +0300193 struct drm_i915_private *dev_priv = to_i915(overlay->dev);
194 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Ben Widawsky75020bc2012-04-16 14:07:43 -0700195 struct overlay_registers __iomem *regs;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200196
Chris Wilson9bb2ff72010-08-12 12:02:11 +0100197 if (OVERLAY_NEEDS_PHYSICAL(overlay->dev))
Chris Wilson00731152014-05-21 12:42:56 +0100198 regs = (struct overlay_registers __iomem *)overlay->reg_bo->phys_handle->vaddr;
Chris Wilson9bb2ff72010-08-12 12:02:11 +0100199 else
Joonas Lahtinen72e96d62016-03-30 16:57:10 +0300200 regs = io_mapping_map_wc(ggtt->mappable,
Ben Widawskyf343c5f2013-07-05 14:41:04 -0700201 i915_gem_obj_ggtt_offset(overlay->reg_bo));
Daniel Vetter02e792f2009-09-15 22:57:34 +0200202
Chris Wilson9bb2ff72010-08-12 12:02:11 +0100203 return regs;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200204}
205
Chris Wilson9bb2ff72010-08-12 12:02:11 +0100206static void intel_overlay_unmap_regs(struct intel_overlay *overlay,
Ben Widawsky75020bc2012-04-16 14:07:43 -0700207 struct overlay_registers __iomem *regs)
Daniel Vetter02e792f2009-09-15 22:57:34 +0200208{
Chris Wilson8d74f652010-08-12 10:35:26 +0100209 if (!OVERLAY_NEEDS_PHYSICAL(overlay->dev))
Chris Wilson9bb2ff72010-08-12 12:02:11 +0100210 io_mapping_unmap(regs);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200211}
Daniel Vetter02e792f2009-09-15 22:57:34 +0200212
Chris Wilsonb6c028e2010-08-12 11:55:08 +0100213static int intel_overlay_do_wait_request(struct intel_overlay *overlay,
John Harrisondad540c2015-05-29 17:43:47 +0100214 struct drm_i915_gem_request *req,
Chris Wilsonb303cf92010-08-12 14:03:48 +0100215 void (*tail)(struct intel_overlay *))
Chris Wilsonb6c028e2010-08-12 11:55:08 +0100216{
Chris Wilsonb6c028e2010-08-12 11:55:08 +0100217 int ret;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200218
Ville Syrjälä77589f52015-03-31 10:37:22 +0300219 WARN_ON(overlay->last_flip_req);
John Harrisondad540c2015-05-29 17:43:47 +0100220 i915_gem_request_assign(&overlay->last_flip_req, req);
John Harrison75289872015-05-29 17:43:49 +0100221 i915_add_request(req);
Chris Wilsonacb868d2012-09-26 13:47:30 +0100222
Chris Wilsonb303cf92010-08-12 14:03:48 +0100223 overlay->flip_tail = tail;
Daniel Vettera4b3a572014-11-26 14:17:05 +0100224 ret = i915_wait_request(overlay->last_flip_req);
Chris Wilsonb6c028e2010-08-12 11:55:08 +0100225 if (ret)
226 return ret;
227
John Harrison9bfc01a2014-11-24 18:49:31 +0000228 i915_gem_request_assign(&overlay->last_flip_req, NULL);
Chris Wilsonb6c028e2010-08-12 11:55:08 +0100229 return 0;
230}
231
Daniel Vetter02e792f2009-09-15 22:57:34 +0200232/* overlay needs to be disable in OCMD reg */
233static int intel_overlay_on(struct intel_overlay *overlay)
234{
235 struct drm_device *dev = overlay->dev;
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100236 struct drm_i915_private *dev_priv = dev->dev_private;
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +0000237 struct intel_engine_cs *engine = &dev_priv->engine[RCS];
John Harrisondad540c2015-05-29 17:43:47 +0100238 struct drm_i915_gem_request *req;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200239 int ret;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200240
Ville Syrjälä77589f52015-03-31 10:37:22 +0300241 WARN_ON(overlay->active);
Daniel Vetter6306cb42012-08-12 19:27:10 +0200242 WARN_ON(IS_I830(dev) && !(dev_priv->quirks & QUIRK_PIPEA_FORCE));
Chris Wilson106dada2010-07-16 17:13:01 +0100243
Tvrtko Ursuline2f80392016-03-16 11:00:36 +0000244 req = i915_gem_request_alloc(engine, NULL);
Dave Gordon26827082016-01-19 19:02:53 +0000245 if (IS_ERR(req))
246 return PTR_ERR(req);
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100247
John Harrison5fb9de12015-05-29 17:44:07 +0100248 ret = intel_ring_begin(req, 4);
John Harrisondad540c2015-05-29 17:43:47 +0100249 if (ret) {
250 i915_gem_request_cancel(req);
251 return ret;
252 }
253
Ville Syrjälä1c7c4302015-03-31 10:37:24 +0300254 overlay->active = true;
255
Tvrtko Ursuline2f80392016-03-16 11:00:36 +0000256 intel_ring_emit(engine, MI_OVERLAY_FLIP | MI_OVERLAY_ON);
257 intel_ring_emit(engine, overlay->flip_addr | OFC_UPDATE);
258 intel_ring_emit(engine, MI_WAIT_FOR_EVENT | MI_WAIT_FOR_OVERLAY_FLIP);
259 intel_ring_emit(engine, MI_NOOP);
260 intel_ring_advance(engine);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200261
John Harrisondad540c2015-05-29 17:43:47 +0100262 return intel_overlay_do_wait_request(overlay, req, NULL);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200263}
264
265/* overlay needs to be enabled in OCMD reg */
Chris Wilson8dc5d142010-08-12 12:36:12 +0100266static int intel_overlay_continue(struct intel_overlay *overlay,
267 bool load_polyphase_filter)
Daniel Vetter02e792f2009-09-15 22:57:34 +0200268{
269 struct drm_device *dev = overlay->dev;
Jani Nikulad5d45cc2014-03-31 14:27:20 +0300270 struct drm_i915_private *dev_priv = dev->dev_private;
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +0000271 struct intel_engine_cs *engine = &dev_priv->engine[RCS];
John Harrisondad540c2015-05-29 17:43:47 +0100272 struct drm_i915_gem_request *req;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200273 u32 flip_addr = overlay->flip_addr;
274 u32 tmp;
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100275 int ret;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200276
Ville Syrjälä77589f52015-03-31 10:37:22 +0300277 WARN_ON(!overlay->active);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200278
279 if (load_polyphase_filter)
280 flip_addr |= OFC_UPDATE;
281
282 /* check for underruns */
283 tmp = I915_READ(DOVSTA);
284 if (tmp & (1 << 17))
285 DRM_DEBUG("overlay underrun, DOVSTA: %x\n", tmp);
286
Tvrtko Ursuline2f80392016-03-16 11:00:36 +0000287 req = i915_gem_request_alloc(engine, NULL);
Dave Gordon26827082016-01-19 19:02:53 +0000288 if (IS_ERR(req))
289 return PTR_ERR(req);
Chris Wilsonacb868d2012-09-26 13:47:30 +0100290
John Harrison5fb9de12015-05-29 17:44:07 +0100291 ret = intel_ring_begin(req, 2);
John Harrisondad540c2015-05-29 17:43:47 +0100292 if (ret) {
293 i915_gem_request_cancel(req);
294 return ret;
295 }
296
Tvrtko Ursuline2f80392016-03-16 11:00:36 +0000297 intel_ring_emit(engine, MI_OVERLAY_FLIP | MI_OVERLAY_CONTINUE);
298 intel_ring_emit(engine, flip_addr);
299 intel_ring_advance(engine);
Daniel Vetter5a5a0c62009-09-15 22:57:36 +0200300
John Harrison9bfc01a2014-11-24 18:49:31 +0000301 WARN_ON(overlay->last_flip_req);
John Harrisondad540c2015-05-29 17:43:47 +0100302 i915_gem_request_assign(&overlay->last_flip_req, req);
John Harrison75289872015-05-29 17:43:49 +0100303 i915_add_request(req);
John Harrisonbf7dc5b2015-05-29 17:43:24 +0100304
305 return 0;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200306}
307
Chris Wilsonb303cf92010-08-12 14:03:48 +0100308static void intel_overlay_release_old_vid_tail(struct intel_overlay *overlay)
Daniel Vetter02e792f2009-09-15 22:57:34 +0200309{
Chris Wilson05394f32010-11-08 19:18:58 +0000310 struct drm_i915_gem_object *obj = overlay->old_vid_bo;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200311
Ben Widawskyd7f46fc2013-12-06 14:10:55 -0800312 i915_gem_object_ggtt_unpin(obj);
Chris Wilson05394f32010-11-08 19:18:58 +0000313 drm_gem_object_unreference(&obj->base);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200314
Chris Wilsonb303cf92010-08-12 14:03:48 +0100315 overlay->old_vid_bo = NULL;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200316}
317
Daniel Vetter12ca45f2037-04-25 10:08:26 +0200318static void intel_overlay_off_tail(struct intel_overlay *overlay)
319{
Chris Wilson05394f32010-11-08 19:18:58 +0000320 struct drm_i915_gem_object *obj = overlay->vid_bo;
Daniel Vetter12ca45f2037-04-25 10:08:26 +0200321
322 /* never have the overlay hw on without showing a frame */
Ville Syrjälä77589f52015-03-31 10:37:22 +0300323 if (WARN_ON(!obj))
324 return;
Daniel Vetter12ca45f2037-04-25 10:08:26 +0200325
Ben Widawskyd7f46fc2013-12-06 14:10:55 -0800326 i915_gem_object_ggtt_unpin(obj);
Chris Wilson05394f32010-11-08 19:18:58 +0000327 drm_gem_object_unreference(&obj->base);
Daniel Vetter12ca45f2037-04-25 10:08:26 +0200328 overlay->vid_bo = NULL;
329
330 overlay->crtc->overlay = NULL;
331 overlay->crtc = NULL;
Ville Syrjälä209c2a52015-03-31 10:37:23 +0300332 overlay->active = false;
Daniel Vetter12ca45f2037-04-25 10:08:26 +0200333}
334
Daniel Vetter02e792f2009-09-15 22:57:34 +0200335/* overlay needs to be disabled in OCMD reg */
Chris Wilsonce453d82011-02-21 14:43:56 +0000336static int intel_overlay_off(struct intel_overlay *overlay)
Daniel Vetter03f77ea2009-09-15 22:57:37 +0200337{
338 struct drm_device *dev = overlay->dev;
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100339 struct drm_i915_private *dev_priv = dev->dev_private;
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +0000340 struct intel_engine_cs *engine = &dev_priv->engine[RCS];
John Harrisondad540c2015-05-29 17:43:47 +0100341 struct drm_i915_gem_request *req;
Chris Wilson8dc5d142010-08-12 12:36:12 +0100342 u32 flip_addr = overlay->flip_addr;
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100343 int ret;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200344
Ville Syrjälä77589f52015-03-31 10:37:22 +0300345 WARN_ON(!overlay->active);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200346
347 /* According to intel docs the overlay hw may hang (when switching
348 * off) without loading the filter coeffs. It is however unclear whether
349 * this applies to the disabling of the overlay or to the switching off
350 * of the hw. Do it in both cases */
351 flip_addr |= OFC_UPDATE;
352
Tvrtko Ursuline2f80392016-03-16 11:00:36 +0000353 req = i915_gem_request_alloc(engine, NULL);
Dave Gordon26827082016-01-19 19:02:53 +0000354 if (IS_ERR(req))
355 return PTR_ERR(req);
Chris Wilsonacb868d2012-09-26 13:47:30 +0100356
John Harrison5fb9de12015-05-29 17:44:07 +0100357 ret = intel_ring_begin(req, 6);
John Harrisondad540c2015-05-29 17:43:47 +0100358 if (ret) {
359 i915_gem_request_cancel(req);
360 return ret;
361 }
362
Daniel Vetter02e792f2009-09-15 22:57:34 +0200363 /* wait for overlay to go idle */
Tvrtko Ursuline2f80392016-03-16 11:00:36 +0000364 intel_ring_emit(engine, MI_OVERLAY_FLIP | MI_OVERLAY_CONTINUE);
365 intel_ring_emit(engine, flip_addr);
366 intel_ring_emit(engine, MI_WAIT_FOR_EVENT | MI_WAIT_FOR_OVERLAY_FLIP);
Chris Wilson722506f2010-08-12 09:28:50 +0100367 /* turn overlay off */
Daniel Vettera9193982012-10-22 12:55:55 +0200368 if (IS_I830(dev)) {
369 /* Workaround: Don't disable the overlay fully, since otherwise
370 * it dies on the next OVERLAY_ON cmd. */
Tvrtko Ursuline2f80392016-03-16 11:00:36 +0000371 intel_ring_emit(engine, MI_NOOP);
372 intel_ring_emit(engine, MI_NOOP);
373 intel_ring_emit(engine, MI_NOOP);
Daniel Vettera9193982012-10-22 12:55:55 +0200374 } else {
Tvrtko Ursuline2f80392016-03-16 11:00:36 +0000375 intel_ring_emit(engine, MI_OVERLAY_FLIP | MI_OVERLAY_OFF);
376 intel_ring_emit(engine, flip_addr);
377 intel_ring_emit(engine,
378 MI_WAIT_FOR_EVENT | MI_WAIT_FOR_OVERLAY_FLIP);
Daniel Vettera9193982012-10-22 12:55:55 +0200379 }
Tvrtko Ursuline2f80392016-03-16 11:00:36 +0000380 intel_ring_advance(engine);
Chris Wilson722506f2010-08-12 09:28:50 +0100381
John Harrisondad540c2015-05-29 17:43:47 +0100382 return intel_overlay_do_wait_request(overlay, req, intel_overlay_off_tail);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200383}
384
Daniel Vetter03f77ea2009-09-15 22:57:37 +0200385/* recover from an interruption due to a signal
386 * We have to be careful not to repeat work forever an make forward progess. */
Chris Wilsonce453d82011-02-21 14:43:56 +0000387static int intel_overlay_recover_from_interrupt(struct intel_overlay *overlay)
Daniel Vetter03f77ea2009-09-15 22:57:37 +0200388{
Daniel Vetter03f77ea2009-09-15 22:57:37 +0200389 int ret;
Daniel Vetter03f77ea2009-09-15 22:57:37 +0200390
John Harrison9bfc01a2014-11-24 18:49:31 +0000391 if (overlay->last_flip_req == NULL)
Chris Wilsonb303cf92010-08-12 14:03:48 +0100392 return 0;
Daniel Vetter03f77ea2009-09-15 22:57:37 +0200393
Daniel Vettera4b3a572014-11-26 14:17:05 +0100394 ret = i915_wait_request(overlay->last_flip_req);
Chris Wilsonb6c028e2010-08-12 11:55:08 +0100395 if (ret)
Daniel Vetter03f77ea2009-09-15 22:57:37 +0200396 return ret;
397
Chris Wilsonb303cf92010-08-12 14:03:48 +0100398 if (overlay->flip_tail)
399 overlay->flip_tail(overlay);
Daniel Vetter03f77ea2009-09-15 22:57:37 +0200400
John Harrison9bfc01a2014-11-24 18:49:31 +0000401 i915_gem_request_assign(&overlay->last_flip_req, NULL);
Daniel Vetter03f77ea2009-09-15 22:57:37 +0200402 return 0;
403}
404
Daniel Vetter5a5a0c62009-09-15 22:57:36 +0200405/* Wait for pending overlay flip and release old frame.
406 * Needs to be called before the overlay register are changed
Chris Wilson8d74f652010-08-12 10:35:26 +0100407 * via intel_overlay_(un)map_regs
408 */
Daniel Vetter02e792f2009-09-15 22:57:34 +0200409static int intel_overlay_release_old_vid(struct intel_overlay *overlay)
410{
Chris Wilson5cd68c92010-08-12 12:21:54 +0100411 struct drm_device *dev = overlay->dev;
Jani Nikulad5d45cc2014-03-31 14:27:20 +0300412 struct drm_i915_private *dev_priv = dev->dev_private;
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +0000413 struct intel_engine_cs *engine = &dev_priv->engine[RCS];
Daniel Vetter02e792f2009-09-15 22:57:34 +0200414 int ret;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200415
Ville Syrjälä1362b772014-11-26 17:07:29 +0200416 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
417
Chris Wilson5cd68c92010-08-12 12:21:54 +0100418 /* Only wait if there is actually an old frame to release to
419 * guarantee forward progress.
420 */
Daniel Vetter03f77ea2009-09-15 22:57:37 +0200421 if (!overlay->old_vid_bo)
422 return 0;
423
Chris Wilson5cd68c92010-08-12 12:21:54 +0100424 if (I915_READ(ISR) & I915_OVERLAY_PLANE_FLIP_PENDING_INTERRUPT) {
425 /* synchronous slowpath */
John Harrisondad540c2015-05-29 17:43:47 +0100426 struct drm_i915_gem_request *req;
427
Tvrtko Ursuline2f80392016-03-16 11:00:36 +0000428 req = i915_gem_request_alloc(engine, NULL);
Dave Gordon26827082016-01-19 19:02:53 +0000429 if (IS_ERR(req))
430 return PTR_ERR(req);
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100431
John Harrison5fb9de12015-05-29 17:44:07 +0100432 ret = intel_ring_begin(req, 2);
John Harrisondad540c2015-05-29 17:43:47 +0100433 if (ret) {
434 i915_gem_request_cancel(req);
435 return ret;
436 }
437
Tvrtko Ursuline2f80392016-03-16 11:00:36 +0000438 intel_ring_emit(engine,
439 MI_WAIT_FOR_EVENT | MI_WAIT_FOR_OVERLAY_FLIP);
440 intel_ring_emit(engine, MI_NOOP);
441 intel_ring_advance(engine);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200442
John Harrisondad540c2015-05-29 17:43:47 +0100443 ret = intel_overlay_do_wait_request(overlay, req,
Chris Wilsonb303cf92010-08-12 14:03:48 +0100444 intel_overlay_release_old_vid_tail);
Chris Wilson5cd68c92010-08-12 12:21:54 +0100445 if (ret)
446 return ret;
447 }
Daniel Vetter02e792f2009-09-15 22:57:34 +0200448
Chris Wilson5cd68c92010-08-12 12:21:54 +0100449 intel_overlay_release_old_vid_tail(overlay);
Daniel Vettera071fa02014-06-18 23:28:09 +0200450
451
452 i915_gem_track_fb(overlay->old_vid_bo, NULL,
453 INTEL_FRONTBUFFER_OVERLAY(overlay->crtc->pipe));
Daniel Vetter02e792f2009-09-15 22:57:34 +0200454 return 0;
455}
456
Ville Syrjälä1362b772014-11-26 17:07:29 +0200457void intel_overlay_reset(struct drm_i915_private *dev_priv)
458{
459 struct intel_overlay *overlay = dev_priv->overlay;
460
461 if (!overlay)
462 return;
463
464 intel_overlay_release_old_vid(overlay);
465
466 overlay->last_flip_req = NULL;
467 overlay->old_xscale = 0;
468 overlay->old_yscale = 0;
469 overlay->crtc = NULL;
470 overlay->active = false;
471}
472
Daniel Vetter02e792f2009-09-15 22:57:34 +0200473struct put_image_params {
474 int format;
475 short dst_x;
476 short dst_y;
477 short dst_w;
478 short dst_h;
479 short src_w;
480 short src_scan_h;
481 short src_scan_w;
482 short src_h;
483 short stride_Y;
484 short stride_UV;
485 int offset_Y;
486 int offset_U;
487 int offset_V;
488};
489
490static int packed_depth_bytes(u32 format)
491{
492 switch (format & I915_OVERLAY_DEPTH_MASK) {
Chris Wilson722506f2010-08-12 09:28:50 +0100493 case I915_OVERLAY_YUV422:
494 return 4;
495 case I915_OVERLAY_YUV411:
496 /* return 6; not implemented */
497 default:
498 return -EINVAL;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200499 }
500}
501
502static int packed_width_bytes(u32 format, short width)
503{
504 switch (format & I915_OVERLAY_DEPTH_MASK) {
Chris Wilson722506f2010-08-12 09:28:50 +0100505 case I915_OVERLAY_YUV422:
506 return width << 1;
507 default:
508 return -EINVAL;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200509 }
510}
511
512static int uv_hsubsampling(u32 format)
513{
514 switch (format & I915_OVERLAY_DEPTH_MASK) {
Chris Wilson722506f2010-08-12 09:28:50 +0100515 case I915_OVERLAY_YUV422:
516 case I915_OVERLAY_YUV420:
517 return 2;
518 case I915_OVERLAY_YUV411:
519 case I915_OVERLAY_YUV410:
520 return 4;
521 default:
522 return -EINVAL;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200523 }
524}
525
526static int uv_vsubsampling(u32 format)
527{
528 switch (format & I915_OVERLAY_DEPTH_MASK) {
Chris Wilson722506f2010-08-12 09:28:50 +0100529 case I915_OVERLAY_YUV420:
530 case I915_OVERLAY_YUV410:
531 return 2;
532 case I915_OVERLAY_YUV422:
533 case I915_OVERLAY_YUV411:
534 return 1;
535 default:
536 return -EINVAL;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200537 }
538}
539
540static u32 calc_swidthsw(struct drm_device *dev, u32 offset, u32 width)
541{
542 u32 mask, shift, ret;
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100543 if (IS_GEN2(dev)) {
Daniel Vetter02e792f2009-09-15 22:57:34 +0200544 mask = 0x1f;
545 shift = 5;
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100546 } else {
547 mask = 0x3f;
548 shift = 6;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200549 }
550 ret = ((offset + width + mask) >> shift) - (offset >> shift);
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100551 if (!IS_GEN2(dev))
Daniel Vetter02e792f2009-09-15 22:57:34 +0200552 ret <<= 1;
Akshay Joshi0206e352011-08-16 15:34:10 -0400553 ret -= 1;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200554 return ret << 2;
555}
556
557static const u16 y_static_hcoeffs[N_HORIZ_Y_TAPS * N_PHASES] = {
558 0x3000, 0xb4a0, 0x1930, 0x1920, 0xb4a0,
559 0x3000, 0xb500, 0x19d0, 0x1880, 0xb440,
560 0x3000, 0xb540, 0x1a88, 0x2f80, 0xb3e0,
561 0x3000, 0xb580, 0x1b30, 0x2e20, 0xb380,
562 0x3000, 0xb5c0, 0x1bd8, 0x2cc0, 0xb320,
563 0x3020, 0xb5e0, 0x1c60, 0x2b80, 0xb2c0,
564 0x3020, 0xb5e0, 0x1cf8, 0x2a20, 0xb260,
565 0x3020, 0xb5e0, 0x1d80, 0x28e0, 0xb200,
566 0x3020, 0xb5c0, 0x1e08, 0x3f40, 0xb1c0,
567 0x3020, 0xb580, 0x1e78, 0x3ce0, 0xb160,
568 0x3040, 0xb520, 0x1ed8, 0x3aa0, 0xb120,
569 0x3040, 0xb4a0, 0x1f30, 0x3880, 0xb0e0,
570 0x3040, 0xb400, 0x1f78, 0x3680, 0xb0a0,
571 0x3020, 0xb340, 0x1fb8, 0x34a0, 0xb060,
572 0x3020, 0xb240, 0x1fe0, 0x32e0, 0xb040,
573 0x3020, 0xb140, 0x1ff8, 0x3160, 0xb020,
Chris Wilson722506f2010-08-12 09:28:50 +0100574 0xb000, 0x3000, 0x0800, 0x3000, 0xb000
575};
576
Daniel Vetter02e792f2009-09-15 22:57:34 +0200577static const u16 uv_static_hcoeffs[N_HORIZ_UV_TAPS * N_PHASES] = {
578 0x3000, 0x1800, 0x1800, 0xb000, 0x18d0, 0x2e60,
579 0xb000, 0x1990, 0x2ce0, 0xb020, 0x1a68, 0x2b40,
580 0xb040, 0x1b20, 0x29e0, 0xb060, 0x1bd8, 0x2880,
581 0xb080, 0x1c88, 0x3e60, 0xb0a0, 0x1d28, 0x3c00,
582 0xb0c0, 0x1db8, 0x39e0, 0xb0e0, 0x1e40, 0x37e0,
583 0xb100, 0x1eb8, 0x3620, 0xb100, 0x1f18, 0x34a0,
584 0xb100, 0x1f68, 0x3360, 0xb0e0, 0x1fa8, 0x3240,
585 0xb0c0, 0x1fe0, 0x3140, 0xb060, 0x1ff0, 0x30a0,
Chris Wilson722506f2010-08-12 09:28:50 +0100586 0x3000, 0x0800, 0x3000
587};
Daniel Vetter02e792f2009-09-15 22:57:34 +0200588
Ben Widawsky75020bc2012-04-16 14:07:43 -0700589static void update_polyphase_filter(struct overlay_registers __iomem *regs)
Daniel Vetter02e792f2009-09-15 22:57:34 +0200590{
Ben Widawsky75020bc2012-04-16 14:07:43 -0700591 memcpy_toio(regs->Y_HCOEFS, y_static_hcoeffs, sizeof(y_static_hcoeffs));
592 memcpy_toio(regs->UV_HCOEFS, uv_static_hcoeffs,
593 sizeof(uv_static_hcoeffs));
Daniel Vetter02e792f2009-09-15 22:57:34 +0200594}
595
596static bool update_scaling_factors(struct intel_overlay *overlay,
Ben Widawsky75020bc2012-04-16 14:07:43 -0700597 struct overlay_registers __iomem *regs,
Daniel Vetter02e792f2009-09-15 22:57:34 +0200598 struct put_image_params *params)
599{
600 /* fixed point with a 12 bit shift */
601 u32 xscale, yscale, xscale_UV, yscale_UV;
602#define FP_SHIFT 12
603#define FRACT_MASK 0xfff
604 bool scale_changed = false;
605 int uv_hscale = uv_hsubsampling(params->format);
606 int uv_vscale = uv_vsubsampling(params->format);
607
608 if (params->dst_w > 1)
609 xscale = ((params->src_scan_w - 1) << FP_SHIFT)
610 /(params->dst_w);
611 else
612 xscale = 1 << FP_SHIFT;
613
614 if (params->dst_h > 1)
615 yscale = ((params->src_scan_h - 1) << FP_SHIFT)
616 /(params->dst_h);
617 else
618 yscale = 1 << FP_SHIFT;
619
620 /*if (params->format & I915_OVERLAY_YUV_PLANAR) {*/
Chris Wilson722506f2010-08-12 09:28:50 +0100621 xscale_UV = xscale/uv_hscale;
622 yscale_UV = yscale/uv_vscale;
623 /* make the Y scale to UV scale ratio an exact multiply */
624 xscale = xscale_UV * uv_hscale;
625 yscale = yscale_UV * uv_vscale;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200626 /*} else {
Chris Wilson722506f2010-08-12 09:28:50 +0100627 xscale_UV = 0;
628 yscale_UV = 0;
629 }*/
Daniel Vetter02e792f2009-09-15 22:57:34 +0200630
631 if (xscale != overlay->old_xscale || yscale != overlay->old_yscale)
632 scale_changed = true;
633 overlay->old_xscale = xscale;
634 overlay->old_yscale = yscale;
635
Ben Widawsky75020bc2012-04-16 14:07:43 -0700636 iowrite32(((yscale & FRACT_MASK) << 20) |
637 ((xscale >> FP_SHIFT) << 16) |
638 ((xscale & FRACT_MASK) << 3),
639 &regs->YRGBSCALE);
Chris Wilson722506f2010-08-12 09:28:50 +0100640
Ben Widawsky75020bc2012-04-16 14:07:43 -0700641 iowrite32(((yscale_UV & FRACT_MASK) << 20) |
642 ((xscale_UV >> FP_SHIFT) << 16) |
643 ((xscale_UV & FRACT_MASK) << 3),
644 &regs->UVSCALE);
Chris Wilson722506f2010-08-12 09:28:50 +0100645
Ben Widawsky75020bc2012-04-16 14:07:43 -0700646 iowrite32((((yscale >> FP_SHIFT) << 16) |
647 ((yscale_UV >> FP_SHIFT) << 0)),
648 &regs->UVSCALEV);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200649
650 if (scale_changed)
651 update_polyphase_filter(regs);
652
653 return scale_changed;
654}
655
656static void update_colorkey(struct intel_overlay *overlay,
Ben Widawsky75020bc2012-04-16 14:07:43 -0700657 struct overlay_registers __iomem *regs)
Daniel Vetter02e792f2009-09-15 22:57:34 +0200658{
659 u32 key = overlay->color_key;
Chris Wilsonea9da4e2015-04-02 10:35:08 +0100660 u32 flags;
661
662 flags = 0;
663 if (overlay->color_key_enabled)
664 flags |= DST_KEY_ENABLE;
Chris Wilson6ba3ddd2010-08-12 09:30:58 +0100665
Matt Roperf4510a22014-04-01 15:22:40 -0700666 switch (overlay->crtc->base.primary->fb->bits_per_pixel) {
Chris Wilson722506f2010-08-12 09:28:50 +0100667 case 8:
Chris Wilsonea9da4e2015-04-02 10:35:08 +0100668 key = 0;
669 flags |= CLK_RGB8I_MASK;
Chris Wilson6ba3ddd2010-08-12 09:30:58 +0100670 break;
671
Chris Wilson722506f2010-08-12 09:28:50 +0100672 case 16:
Matt Roperf4510a22014-04-01 15:22:40 -0700673 if (overlay->crtc->base.primary->fb->depth == 15) {
Chris Wilsonea9da4e2015-04-02 10:35:08 +0100674 key = RGB15_TO_COLORKEY(key);
675 flags |= CLK_RGB15_MASK;
Chris Wilson722506f2010-08-12 09:28:50 +0100676 } else {
Chris Wilsonea9da4e2015-04-02 10:35:08 +0100677 key = RGB16_TO_COLORKEY(key);
678 flags |= CLK_RGB16_MASK;
Chris Wilson722506f2010-08-12 09:28:50 +0100679 }
Chris Wilson6ba3ddd2010-08-12 09:30:58 +0100680 break;
681
Chris Wilson722506f2010-08-12 09:28:50 +0100682 case 24:
683 case 32:
Chris Wilsonea9da4e2015-04-02 10:35:08 +0100684 flags |= CLK_RGB24_MASK;
Chris Wilson6ba3ddd2010-08-12 09:30:58 +0100685 break;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200686 }
Chris Wilsonea9da4e2015-04-02 10:35:08 +0100687
688 iowrite32(key, &regs->DCLRKV);
689 iowrite32(flags, &regs->DCLRKM);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200690}
691
692static u32 overlay_cmd_reg(struct put_image_params *params)
693{
694 u32 cmd = OCMD_ENABLE | OCMD_BUF_TYPE_FRAME | OCMD_BUFFER0;
695
696 if (params->format & I915_OVERLAY_YUV_PLANAR) {
697 switch (params->format & I915_OVERLAY_DEPTH_MASK) {
Chris Wilson722506f2010-08-12 09:28:50 +0100698 case I915_OVERLAY_YUV422:
699 cmd |= OCMD_YUV_422_PLANAR;
700 break;
701 case I915_OVERLAY_YUV420:
702 cmd |= OCMD_YUV_420_PLANAR;
703 break;
704 case I915_OVERLAY_YUV411:
705 case I915_OVERLAY_YUV410:
706 cmd |= OCMD_YUV_410_PLANAR;
707 break;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200708 }
709 } else { /* YUV packed */
710 switch (params->format & I915_OVERLAY_DEPTH_MASK) {
Chris Wilson722506f2010-08-12 09:28:50 +0100711 case I915_OVERLAY_YUV422:
712 cmd |= OCMD_YUV_422_PACKED;
713 break;
714 case I915_OVERLAY_YUV411:
715 cmd |= OCMD_YUV_411_PACKED;
716 break;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200717 }
718
719 switch (params->format & I915_OVERLAY_SWAP_MASK) {
Chris Wilson722506f2010-08-12 09:28:50 +0100720 case I915_OVERLAY_NO_SWAP:
721 break;
722 case I915_OVERLAY_UV_SWAP:
723 cmd |= OCMD_UV_SWAP;
724 break;
725 case I915_OVERLAY_Y_SWAP:
726 cmd |= OCMD_Y_SWAP;
727 break;
728 case I915_OVERLAY_Y_AND_UV_SWAP:
729 cmd |= OCMD_Y_AND_UV_SWAP;
730 break;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200731 }
732 }
733
734 return cmd;
735}
736
Chris Wilson5fe82c52010-08-12 12:38:21 +0100737static int intel_overlay_do_put_image(struct intel_overlay *overlay,
Chris Wilson05394f32010-11-08 19:18:58 +0000738 struct drm_i915_gem_object *new_bo,
Chris Wilson5fe82c52010-08-12 12:38:21 +0100739 struct put_image_params *params)
Daniel Vetter02e792f2009-09-15 22:57:34 +0200740{
741 int ret, tmp_width;
Ben Widawsky75020bc2012-04-16 14:07:43 -0700742 struct overlay_registers __iomem *regs;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200743 bool scale_changed = false;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200744 struct drm_device *dev = overlay->dev;
Ben Widawsky75020bc2012-04-16 14:07:43 -0700745 u32 swidth, swidthsw, sheight, ostride;
Daniel Vettera071fa02014-06-18 23:28:09 +0200746 enum pipe pipe = overlay->crtc->pipe;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200747
Ville Syrjälä77589f52015-03-31 10:37:22 +0300748 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
749 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
Daniel Vetter02e792f2009-09-15 22:57:34 +0200750
Daniel Vetter02e792f2009-09-15 22:57:34 +0200751 ret = intel_overlay_release_old_vid(overlay);
752 if (ret != 0)
753 return ret;
754
Maarten Lankhorst7580d772015-08-18 13:40:06 +0200755 ret = i915_gem_object_pin_to_display_plane(new_bo, 0,
Tvrtko Ursuline6617332015-03-23 11:10:33 +0000756 &i915_ggtt_view_normal);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200757 if (ret != 0)
758 return ret;
759
Chris Wilsond9e86c02010-11-10 16:40:20 +0000760 ret = i915_gem_object_put_fence(new_bo);
761 if (ret)
762 goto out_unpin;
763
Daniel Vetter02e792f2009-09-15 22:57:34 +0200764 if (!overlay->active) {
Ben Widawsky75020bc2012-04-16 14:07:43 -0700765 u32 oconfig;
Chris Wilson8d74f652010-08-12 10:35:26 +0100766 regs = intel_overlay_map_regs(overlay);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200767 if (!regs) {
768 ret = -ENOMEM;
769 goto out_unpin;
770 }
Ben Widawsky75020bc2012-04-16 14:07:43 -0700771 oconfig = OCONF_CC_OUT_8BIT;
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100772 if (IS_GEN4(overlay->dev))
Ben Widawsky75020bc2012-04-16 14:07:43 -0700773 oconfig |= OCONF_CSC_MODE_BT709;
Daniel Vettera071fa02014-06-18 23:28:09 +0200774 oconfig |= pipe == 0 ?
Daniel Vetter02e792f2009-09-15 22:57:34 +0200775 OCONF_PIPE_A : OCONF_PIPE_B;
Ben Widawsky75020bc2012-04-16 14:07:43 -0700776 iowrite32(oconfig, &regs->OCONFIG);
Chris Wilson9bb2ff72010-08-12 12:02:11 +0100777 intel_overlay_unmap_regs(overlay, regs);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200778
779 ret = intel_overlay_on(overlay);
780 if (ret != 0)
781 goto out_unpin;
782 }
783
Chris Wilson8d74f652010-08-12 10:35:26 +0100784 regs = intel_overlay_map_regs(overlay);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200785 if (!regs) {
786 ret = -ENOMEM;
787 goto out_unpin;
788 }
789
Ben Widawsky75020bc2012-04-16 14:07:43 -0700790 iowrite32((params->dst_y << 16) | params->dst_x, &regs->DWINPOS);
791 iowrite32((params->dst_h << 16) | params->dst_w, &regs->DWINSZ);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200792
793 if (params->format & I915_OVERLAY_YUV_PACKED)
794 tmp_width = packed_width_bytes(params->format, params->src_w);
795 else
796 tmp_width = params->src_w;
797
Ben Widawsky75020bc2012-04-16 14:07:43 -0700798 swidth = params->src_w;
799 swidthsw = calc_swidthsw(overlay->dev, params->offset_Y, tmp_width);
800 sheight = params->src_h;
Ben Widawskyf343c5f2013-07-05 14:41:04 -0700801 iowrite32(i915_gem_obj_ggtt_offset(new_bo) + params->offset_Y, &regs->OBUF_0Y);
Ben Widawsky75020bc2012-04-16 14:07:43 -0700802 ostride = params->stride_Y;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200803
804 if (params->format & I915_OVERLAY_YUV_PLANAR) {
805 int uv_hscale = uv_hsubsampling(params->format);
806 int uv_vscale = uv_vsubsampling(params->format);
807 u32 tmp_U, tmp_V;
Ben Widawsky75020bc2012-04-16 14:07:43 -0700808 swidth |= (params->src_w/uv_hscale) << 16;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200809 tmp_U = calc_swidthsw(overlay->dev, params->offset_U,
Chris Wilson722506f2010-08-12 09:28:50 +0100810 params->src_w/uv_hscale);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200811 tmp_V = calc_swidthsw(overlay->dev, params->offset_V,
Chris Wilson722506f2010-08-12 09:28:50 +0100812 params->src_w/uv_hscale);
Ben Widawsky75020bc2012-04-16 14:07:43 -0700813 swidthsw |= max_t(u32, tmp_U, tmp_V) << 16;
814 sheight |= (params->src_h/uv_vscale) << 16;
Ben Widawskyf343c5f2013-07-05 14:41:04 -0700815 iowrite32(i915_gem_obj_ggtt_offset(new_bo) + params->offset_U, &regs->OBUF_0U);
816 iowrite32(i915_gem_obj_ggtt_offset(new_bo) + params->offset_V, &regs->OBUF_0V);
Ben Widawsky75020bc2012-04-16 14:07:43 -0700817 ostride |= params->stride_UV << 16;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200818 }
819
Ben Widawsky75020bc2012-04-16 14:07:43 -0700820 iowrite32(swidth, &regs->SWIDTH);
821 iowrite32(swidthsw, &regs->SWIDTHSW);
822 iowrite32(sheight, &regs->SHEIGHT);
823 iowrite32(ostride, &regs->OSTRIDE);
824
Daniel Vetter02e792f2009-09-15 22:57:34 +0200825 scale_changed = update_scaling_factors(overlay, regs, params);
826
827 update_colorkey(overlay, regs);
828
Ben Widawsky75020bc2012-04-16 14:07:43 -0700829 iowrite32(overlay_cmd_reg(params), &regs->OCMD);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200830
Chris Wilson9bb2ff72010-08-12 12:02:11 +0100831 intel_overlay_unmap_regs(overlay, regs);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200832
Chris Wilson8dc5d142010-08-12 12:36:12 +0100833 ret = intel_overlay_continue(overlay, scale_changed);
834 if (ret)
835 goto out_unpin;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200836
Daniel Vettera071fa02014-06-18 23:28:09 +0200837 i915_gem_track_fb(overlay->vid_bo, new_bo,
838 INTEL_FRONTBUFFER_OVERLAY(pipe));
839
Daniel Vetter02e792f2009-09-15 22:57:34 +0200840 overlay->old_vid_bo = overlay->vid_bo;
Chris Wilson05394f32010-11-08 19:18:58 +0000841 overlay->vid_bo = new_bo;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200842
Daniel Vetterf99d7062014-06-19 16:01:59 +0200843 intel_frontbuffer_flip(dev,
844 INTEL_FRONTBUFFER_OVERLAY(pipe));
845
Daniel Vetter02e792f2009-09-15 22:57:34 +0200846 return 0;
847
848out_unpin:
Ben Widawskyd7f46fc2013-12-06 14:10:55 -0800849 i915_gem_object_ggtt_unpin(new_bo);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200850 return ret;
851}
852
Chris Wilsonce453d82011-02-21 14:43:56 +0000853int intel_overlay_switch_off(struct intel_overlay *overlay)
Daniel Vetter02e792f2009-09-15 22:57:34 +0200854{
Ben Widawsky75020bc2012-04-16 14:07:43 -0700855 struct overlay_registers __iomem *regs;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200856 struct drm_device *dev = overlay->dev;
Chris Wilson5dcdbcb2010-08-12 13:50:28 +0100857 int ret;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200858
Ville Syrjälä77589f52015-03-31 10:37:22 +0300859 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
860 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
Daniel Vetter02e792f2009-09-15 22:57:34 +0200861
Chris Wilsonce453d82011-02-21 14:43:56 +0000862 ret = intel_overlay_recover_from_interrupt(overlay);
Chris Wilsonb303cf92010-08-12 14:03:48 +0100863 if (ret != 0)
864 return ret;
Daniel Vetter9bedb972009-11-30 15:55:49 +0100865
Daniel Vetter02e792f2009-09-15 22:57:34 +0200866 if (!overlay->active)
867 return 0;
868
Daniel Vetter02e792f2009-09-15 22:57:34 +0200869 ret = intel_overlay_release_old_vid(overlay);
870 if (ret != 0)
871 return ret;
872
Chris Wilson8d74f652010-08-12 10:35:26 +0100873 regs = intel_overlay_map_regs(overlay);
Ben Widawsky75020bc2012-04-16 14:07:43 -0700874 iowrite32(0, &regs->OCMD);
Chris Wilson9bb2ff72010-08-12 12:02:11 +0100875 intel_overlay_unmap_regs(overlay, regs);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200876
Chris Wilsonce453d82011-02-21 14:43:56 +0000877 ret = intel_overlay_off(overlay);
Daniel Vetter03f77ea2009-09-15 22:57:37 +0200878 if (ret != 0)
879 return ret;
880
Daniel Vetter12ca45f2037-04-25 10:08:26 +0200881 intel_overlay_off_tail(overlay);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200882 return 0;
883}
884
885static int check_overlay_possible_on_crtc(struct intel_overlay *overlay,
886 struct intel_crtc *crtc)
887{
Chris Wilsonf7abfe82010-09-13 14:19:16 +0100888 if (!crtc->active)
Daniel Vetter02e792f2009-09-15 22:57:34 +0200889 return -EINVAL;
890
Daniel Vetter02e792f2009-09-15 22:57:34 +0200891 /* can't use the overlay with double wide pipe */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +0200892 if (crtc->config->double_wide)
Daniel Vetter02e792f2009-09-15 22:57:34 +0200893 return -EINVAL;
894
895 return 0;
896}
897
898static void update_pfit_vscale_ratio(struct intel_overlay *overlay)
899{
900 struct drm_device *dev = overlay->dev;
Jani Nikulad5d45cc2014-03-31 14:27:20 +0300901 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200902 u32 pfit_control = I915_READ(PFIT_CONTROL);
Chris Wilson446d2182010-08-12 11:15:58 +0100903 u32 ratio;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200904
905 /* XXX: This is not the same logic as in the xorg driver, but more in
Chris Wilson446d2182010-08-12 11:15:58 +0100906 * line with the intel documentation for the i965
907 */
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100908 if (INTEL_INFO(dev)->gen >= 4) {
Akshay Joshi0206e352011-08-16 15:34:10 -0400909 /* on i965 use the PGM reg to read out the autoscaler values */
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100910 ratio = I915_READ(PFIT_PGM_RATIOS) >> PFIT_VERT_SCALE_SHIFT_965;
911 } else {
Chris Wilson446d2182010-08-12 11:15:58 +0100912 if (pfit_control & VERT_AUTO_SCALE)
913 ratio = I915_READ(PFIT_AUTO_RATIOS);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200914 else
Chris Wilson446d2182010-08-12 11:15:58 +0100915 ratio = I915_READ(PFIT_PGM_RATIOS);
916 ratio >>= PFIT_VERT_SCALE_SHIFT;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200917 }
918
919 overlay->pfit_vscale_ratio = ratio;
920}
921
922static int check_overlay_dst(struct intel_overlay *overlay,
923 struct drm_intel_overlay_put_image *rec)
924{
925 struct drm_display_mode *mode = &overlay->crtc->base.mode;
926
Daniel Vetter75c13992012-01-28 23:48:46 +0100927 if (rec->dst_x < mode->hdisplay &&
928 rec->dst_x + rec->dst_width <= mode->hdisplay &&
929 rec->dst_y < mode->vdisplay &&
930 rec->dst_y + rec->dst_height <= mode->vdisplay)
Daniel Vetter02e792f2009-09-15 22:57:34 +0200931 return 0;
932 else
933 return -EINVAL;
934}
935
936static int check_overlay_scaling(struct put_image_params *rec)
937{
938 u32 tmp;
939
940 /* downscaling limit is 8.0 */
941 tmp = ((rec->src_scan_h << 16) / rec->dst_h) >> 16;
942 if (tmp > 7)
943 return -EINVAL;
944 tmp = ((rec->src_scan_w << 16) / rec->dst_w) >> 16;
945 if (tmp > 7)
946 return -EINVAL;
947
948 return 0;
949}
950
951static int check_overlay_src(struct drm_device *dev,
952 struct drm_intel_overlay_put_image *rec,
Chris Wilson05394f32010-11-08 19:18:58 +0000953 struct drm_i915_gem_object *new_bo)
Daniel Vetter02e792f2009-09-15 22:57:34 +0200954{
Daniel Vetter02e792f2009-09-15 22:57:34 +0200955 int uv_hscale = uv_hsubsampling(rec->flags);
956 int uv_vscale = uv_vsubsampling(rec->flags);
Dan Carpenter8f28f542010-10-27 23:17:25 +0200957 u32 stride_mask;
958 int depth;
959 u32 tmp;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200960
961 /* check src dimensions */
962 if (IS_845G(dev) || IS_I830(dev)) {
Chris Wilson722506f2010-08-12 09:28:50 +0100963 if (rec->src_height > IMAGE_MAX_HEIGHT_LEGACY ||
Chris Wilson9f7c3f42010-08-12 11:29:34 +0100964 rec->src_width > IMAGE_MAX_WIDTH_LEGACY)
Daniel Vetter02e792f2009-09-15 22:57:34 +0200965 return -EINVAL;
966 } else {
Chris Wilson722506f2010-08-12 09:28:50 +0100967 if (rec->src_height > IMAGE_MAX_HEIGHT ||
Chris Wilson9f7c3f42010-08-12 11:29:34 +0100968 rec->src_width > IMAGE_MAX_WIDTH)
Daniel Vetter02e792f2009-09-15 22:57:34 +0200969 return -EINVAL;
970 }
Chris Wilson9f7c3f42010-08-12 11:29:34 +0100971
Daniel Vetter02e792f2009-09-15 22:57:34 +0200972 /* better safe than sorry, use 4 as the maximal subsampling ratio */
Chris Wilson722506f2010-08-12 09:28:50 +0100973 if (rec->src_height < N_VERT_Y_TAPS*4 ||
Chris Wilson9f7c3f42010-08-12 11:29:34 +0100974 rec->src_width < N_HORIZ_Y_TAPS*4)
Daniel Vetter02e792f2009-09-15 22:57:34 +0200975 return -EINVAL;
976
Chris Wilsona1efd142010-07-12 19:35:38 +0100977 /* check alignment constraints */
Daniel Vetter02e792f2009-09-15 22:57:34 +0200978 switch (rec->flags & I915_OVERLAY_TYPE_MASK) {
Chris Wilson722506f2010-08-12 09:28:50 +0100979 case I915_OVERLAY_RGB:
980 /* not implemented */
981 return -EINVAL;
Chris Wilson9f7c3f42010-08-12 11:29:34 +0100982
Chris Wilson722506f2010-08-12 09:28:50 +0100983 case I915_OVERLAY_YUV_PACKED:
Chris Wilson722506f2010-08-12 09:28:50 +0100984 if (uv_vscale != 1)
Daniel Vetter02e792f2009-09-15 22:57:34 +0200985 return -EINVAL;
Chris Wilson9f7c3f42010-08-12 11:29:34 +0100986
987 depth = packed_depth_bytes(rec->flags);
Chris Wilson722506f2010-08-12 09:28:50 +0100988 if (depth < 0)
989 return depth;
Chris Wilson9f7c3f42010-08-12 11:29:34 +0100990
Chris Wilson722506f2010-08-12 09:28:50 +0100991 /* ignore UV planes */
992 rec->stride_UV = 0;
993 rec->offset_U = 0;
994 rec->offset_V = 0;
995 /* check pixel alignment */
996 if (rec->offset_Y % depth)
Daniel Vetter02e792f2009-09-15 22:57:34 +0200997 return -EINVAL;
Chris Wilson722506f2010-08-12 09:28:50 +0100998 break;
Chris Wilson9f7c3f42010-08-12 11:29:34 +0100999
Chris Wilson722506f2010-08-12 09:28:50 +01001000 case I915_OVERLAY_YUV_PLANAR:
1001 if (uv_vscale < 0 || uv_hscale < 0)
1002 return -EINVAL;
1003 /* no offset restrictions for planar formats */
1004 break;
Chris Wilson9f7c3f42010-08-12 11:29:34 +01001005
Chris Wilson722506f2010-08-12 09:28:50 +01001006 default:
1007 return -EINVAL;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001008 }
1009
1010 if (rec->src_width % uv_hscale)
1011 return -EINVAL;
1012
1013 /* stride checking */
Chris Wilsona1efd142010-07-12 19:35:38 +01001014 if (IS_I830(dev) || IS_845G(dev))
1015 stride_mask = 255;
1016 else
1017 stride_mask = 63;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001018
1019 if (rec->stride_Y & stride_mask || rec->stride_UV & stride_mask)
1020 return -EINVAL;
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001021 if (IS_GEN4(dev) && rec->stride_Y < 512)
Daniel Vetter02e792f2009-09-15 22:57:34 +02001022 return -EINVAL;
1023
1024 tmp = (rec->flags & I915_OVERLAY_TYPE_MASK) == I915_OVERLAY_YUV_PLANAR ?
Chris Wilson9f7c3f42010-08-12 11:29:34 +01001025 4096 : 8192;
1026 if (rec->stride_Y > tmp || rec->stride_UV > 2*1024)
Daniel Vetter02e792f2009-09-15 22:57:34 +02001027 return -EINVAL;
1028
1029 /* check buffer dimensions */
1030 switch (rec->flags & I915_OVERLAY_TYPE_MASK) {
Chris Wilson722506f2010-08-12 09:28:50 +01001031 case I915_OVERLAY_RGB:
1032 case I915_OVERLAY_YUV_PACKED:
1033 /* always 4 Y values per depth pixels */
1034 if (packed_width_bytes(rec->flags, rec->src_width) > rec->stride_Y)
1035 return -EINVAL;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001036
Chris Wilson722506f2010-08-12 09:28:50 +01001037 tmp = rec->stride_Y*rec->src_height;
Chris Wilson05394f32010-11-08 19:18:58 +00001038 if (rec->offset_Y + tmp > new_bo->base.size)
Chris Wilson722506f2010-08-12 09:28:50 +01001039 return -EINVAL;
1040 break;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001041
Chris Wilson722506f2010-08-12 09:28:50 +01001042 case I915_OVERLAY_YUV_PLANAR:
1043 if (rec->src_width > rec->stride_Y)
1044 return -EINVAL;
1045 if (rec->src_width/uv_hscale > rec->stride_UV)
1046 return -EINVAL;
1047
Chris Wilson9f7c3f42010-08-12 11:29:34 +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;
Chris Wilson9f7c3f42010-08-12 11:29:34 +01001051
1052 tmp = rec->stride_UV * (rec->src_height / uv_vscale);
Chris Wilson05394f32010-11-08 19:18:58 +00001053 if (rec->offset_U + tmp > new_bo->base.size ||
1054 rec->offset_V + tmp > new_bo->base.size)
Chris Wilson722506f2010-08-12 09:28:50 +01001055 return -EINVAL;
1056 break;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001057 }
1058
1059 return 0;
1060}
1061
Chris Wilsone9e331a2010-09-13 01:16:10 +01001062/**
1063 * Return the pipe currently connected to the panel fitter,
1064 * or -1 if the panel fitter is not present or not in use
1065 */
1066static int intel_panel_fitter_pipe(struct drm_device *dev)
1067{
1068 struct drm_i915_private *dev_priv = dev->dev_private;
1069 u32 pfit_control;
1070
1071 /* i830 doesn't have a panel fitter */
Ville Syrjälädc9e7dec2014-01-10 14:06:45 +02001072 if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
Chris Wilsone9e331a2010-09-13 01:16:10 +01001073 return -1;
1074
1075 pfit_control = I915_READ(PFIT_CONTROL);
1076
1077 /* See if the panel fitter is in use */
1078 if ((pfit_control & PFIT_ENABLE) == 0)
1079 return -1;
1080
1081 /* 965 can place panel fitter on either pipe */
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001082 if (IS_GEN4(dev))
Chris Wilsone9e331a2010-09-13 01:16:10 +01001083 return (pfit_control >> 29) & 0x3;
1084
1085 /* older chips can only use pipe 1 */
1086 return 1;
1087}
1088
Daniel Vetter02e792f2009-09-15 22:57:34 +02001089int intel_overlay_put_image(struct drm_device *dev, void *data,
Akshay Joshi0206e352011-08-16 15:34:10 -04001090 struct drm_file *file_priv)
Daniel Vetter02e792f2009-09-15 22:57:34 +02001091{
1092 struct drm_intel_overlay_put_image *put_image_rec = data;
Jani Nikulad5d45cc2014-03-31 14:27:20 +03001093 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001094 struct intel_overlay *overlay;
Rob Clark7707e652014-07-17 23:30:04 -04001095 struct drm_crtc *drmmode_crtc;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001096 struct intel_crtc *crtc;
Chris Wilson05394f32010-11-08 19:18:58 +00001097 struct drm_i915_gem_object *new_bo;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001098 struct put_image_params *params;
1099 int ret;
1100
Daniel Vetter02e792f2009-09-15 22:57:34 +02001101 overlay = dev_priv->overlay;
1102 if (!overlay) {
1103 DRM_DEBUG("userspace bug: no overlay\n");
1104 return -ENODEV;
1105 }
1106
1107 if (!(put_image_rec->flags & I915_OVERLAY_ENABLE)) {
Daniel Vettera0e99e62012-12-02 01:05:46 +01001108 drm_modeset_lock_all(dev);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001109 mutex_lock(&dev->struct_mutex);
1110
Chris Wilsonce453d82011-02-21 14:43:56 +00001111 ret = intel_overlay_switch_off(overlay);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001112
1113 mutex_unlock(&dev->struct_mutex);
Daniel Vettera0e99e62012-12-02 01:05:46 +01001114 drm_modeset_unlock_all(dev);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001115
1116 return ret;
1117 }
1118
Daniel Vetterb14c5672013-09-19 12:18:32 +02001119 params = kmalloc(sizeof(*params), GFP_KERNEL);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001120 if (!params)
1121 return -ENOMEM;
1122
Rob Clark7707e652014-07-17 23:30:04 -04001123 drmmode_crtc = drm_crtc_find(dev, put_image_rec->crtc_id);
1124 if (!drmmode_crtc) {
Dan Carpenter915a4282010-03-06 14:05:39 +03001125 ret = -ENOENT;
1126 goto out_free;
1127 }
Rob Clark7707e652014-07-17 23:30:04 -04001128 crtc = to_intel_crtc(drmmode_crtc);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001129
Chris Wilson05394f32010-11-08 19:18:58 +00001130 new_bo = to_intel_bo(drm_gem_object_lookup(dev, file_priv,
1131 put_image_rec->bo_handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00001132 if (&new_bo->base == NULL) {
Dan Carpenter915a4282010-03-06 14:05:39 +03001133 ret = -ENOENT;
1134 goto out_free;
1135 }
Daniel Vetter02e792f2009-09-15 22:57:34 +02001136
Daniel Vettera0e99e62012-12-02 01:05:46 +01001137 drm_modeset_lock_all(dev);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001138 mutex_lock(&dev->struct_mutex);
1139
Chris Wilsond9e86c02010-11-10 16:40:20 +00001140 if (new_bo->tiling_mode) {
Daniel Vetter3b25b312014-02-14 14:06:06 +01001141 DRM_DEBUG_KMS("buffer used for overlay image can not be tiled\n");
Chris Wilsond9e86c02010-11-10 16:40:20 +00001142 ret = -EINVAL;
1143 goto out_unlock;
1144 }
1145
Chris Wilsonce453d82011-02-21 14:43:56 +00001146 ret = intel_overlay_recover_from_interrupt(overlay);
Chris Wilsonb303cf92010-08-12 14:03:48 +01001147 if (ret != 0)
1148 goto out_unlock;
Daniel Vetter03f77ea2009-09-15 22:57:37 +02001149
Daniel Vetter02e792f2009-09-15 22:57:34 +02001150 if (overlay->crtc != crtc) {
1151 struct drm_display_mode *mode = &crtc->base.mode;
Chris Wilsonce453d82011-02-21 14:43:56 +00001152 ret = intel_overlay_switch_off(overlay);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001153 if (ret != 0)
1154 goto out_unlock;
1155
1156 ret = check_overlay_possible_on_crtc(overlay, crtc);
1157 if (ret != 0)
1158 goto out_unlock;
1159
1160 overlay->crtc = crtc;
1161 crtc->overlay = overlay;
1162
Chris Wilsone9e331a2010-09-13 01:16:10 +01001163 /* line too wide, i.e. one-line-mode */
1164 if (mode->hdisplay > 1024 &&
1165 intel_panel_fitter_pipe(dev) == crtc->pipe) {
Ville Syrjälä209c2a52015-03-31 10:37:23 +03001166 overlay->pfit_active = true;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001167 update_pfit_vscale_ratio(overlay);
1168 } else
Ville Syrjälä209c2a52015-03-31 10:37:23 +03001169 overlay->pfit_active = false;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001170 }
1171
1172 ret = check_overlay_dst(overlay, put_image_rec);
1173 if (ret != 0)
1174 goto out_unlock;
1175
1176 if (overlay->pfit_active) {
1177 params->dst_y = ((((u32)put_image_rec->dst_y) << 12) /
Chris Wilson722506f2010-08-12 09:28:50 +01001178 overlay->pfit_vscale_ratio);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001179 /* shifting right rounds downwards, so add 1 */
1180 params->dst_h = ((((u32)put_image_rec->dst_height) << 12) /
Chris Wilson722506f2010-08-12 09:28:50 +01001181 overlay->pfit_vscale_ratio) + 1;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001182 } else {
1183 params->dst_y = put_image_rec->dst_y;
1184 params->dst_h = put_image_rec->dst_height;
1185 }
1186 params->dst_x = put_image_rec->dst_x;
1187 params->dst_w = put_image_rec->dst_width;
1188
1189 params->src_w = put_image_rec->src_width;
1190 params->src_h = put_image_rec->src_height;
1191 params->src_scan_w = put_image_rec->src_scan_width;
1192 params->src_scan_h = put_image_rec->src_scan_height;
Chris Wilson722506f2010-08-12 09:28:50 +01001193 if (params->src_scan_h > params->src_h ||
1194 params->src_scan_w > params->src_w) {
Daniel Vetter02e792f2009-09-15 22:57:34 +02001195 ret = -EINVAL;
1196 goto out_unlock;
1197 }
1198
1199 ret = check_overlay_src(dev, put_image_rec, new_bo);
1200 if (ret != 0)
1201 goto out_unlock;
1202 params->format = put_image_rec->flags & ~I915_OVERLAY_FLAGS_MASK;
1203 params->stride_Y = put_image_rec->stride_Y;
1204 params->stride_UV = put_image_rec->stride_UV;
1205 params->offset_Y = put_image_rec->offset_Y;
1206 params->offset_U = put_image_rec->offset_U;
1207 params->offset_V = put_image_rec->offset_V;
1208
1209 /* Check scaling after src size to prevent a divide-by-zero. */
1210 ret = check_overlay_scaling(params);
1211 if (ret != 0)
1212 goto out_unlock;
1213
1214 ret = intel_overlay_do_put_image(overlay, new_bo, params);
1215 if (ret != 0)
1216 goto out_unlock;
1217
1218 mutex_unlock(&dev->struct_mutex);
Daniel Vettera0e99e62012-12-02 01:05:46 +01001219 drm_modeset_unlock_all(dev);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001220
1221 kfree(params);
1222
1223 return 0;
1224
1225out_unlock:
1226 mutex_unlock(&dev->struct_mutex);
Daniel Vettera0e99e62012-12-02 01:05:46 +01001227 drm_modeset_unlock_all(dev);
Chris Wilson05394f32010-11-08 19:18:58 +00001228 drm_gem_object_unreference_unlocked(&new_bo->base);
Dan Carpenter915a4282010-03-06 14:05:39 +03001229out_free:
Daniel Vetter02e792f2009-09-15 22:57:34 +02001230 kfree(params);
1231
1232 return ret;
1233}
1234
1235static void update_reg_attrs(struct intel_overlay *overlay,
Ben Widawsky75020bc2012-04-16 14:07:43 -07001236 struct overlay_registers __iomem *regs)
Daniel Vetter02e792f2009-09-15 22:57:34 +02001237{
Ben Widawsky75020bc2012-04-16 14:07:43 -07001238 iowrite32((overlay->contrast << 18) | (overlay->brightness & 0xff),
1239 &regs->OCLRC0);
1240 iowrite32(overlay->saturation, &regs->OCLRC1);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001241}
1242
1243static bool check_gamma_bounds(u32 gamma1, u32 gamma2)
1244{
1245 int i;
1246
1247 if (gamma1 & 0xff000000 || gamma2 & 0xff000000)
1248 return false;
1249
1250 for (i = 0; i < 3; i++) {
Chris Wilson722506f2010-08-12 09:28:50 +01001251 if (((gamma1 >> i*8) & 0xff) >= ((gamma2 >> i*8) & 0xff))
Daniel Vetter02e792f2009-09-15 22:57:34 +02001252 return false;
1253 }
1254
1255 return true;
1256}
1257
1258static bool check_gamma5_errata(u32 gamma5)
1259{
1260 int i;
1261
1262 for (i = 0; i < 3; i++) {
1263 if (((gamma5 >> i*8) & 0xff) == 0x80)
1264 return false;
1265 }
1266
1267 return true;
1268}
1269
1270static int check_gamma(struct drm_intel_overlay_attrs *attrs)
1271{
Chris Wilson722506f2010-08-12 09:28:50 +01001272 if (!check_gamma_bounds(0, attrs->gamma0) ||
1273 !check_gamma_bounds(attrs->gamma0, attrs->gamma1) ||
1274 !check_gamma_bounds(attrs->gamma1, attrs->gamma2) ||
1275 !check_gamma_bounds(attrs->gamma2, attrs->gamma3) ||
1276 !check_gamma_bounds(attrs->gamma3, attrs->gamma4) ||
1277 !check_gamma_bounds(attrs->gamma4, attrs->gamma5) ||
1278 !check_gamma_bounds(attrs->gamma5, 0x00ffffff))
Daniel Vetter02e792f2009-09-15 22:57:34 +02001279 return -EINVAL;
Chris Wilson722506f2010-08-12 09:28:50 +01001280
Daniel Vetter02e792f2009-09-15 22:57:34 +02001281 if (!check_gamma5_errata(attrs->gamma5))
1282 return -EINVAL;
Chris Wilson722506f2010-08-12 09:28:50 +01001283
Daniel Vetter02e792f2009-09-15 22:57:34 +02001284 return 0;
1285}
1286
1287int intel_overlay_attrs(struct drm_device *dev, void *data,
Akshay Joshi0206e352011-08-16 15:34:10 -04001288 struct drm_file *file_priv)
Daniel Vetter02e792f2009-09-15 22:57:34 +02001289{
1290 struct drm_intel_overlay_attrs *attrs = data;
Jani Nikulad5d45cc2014-03-31 14:27:20 +03001291 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001292 struct intel_overlay *overlay;
Ben Widawsky75020bc2012-04-16 14:07:43 -07001293 struct overlay_registers __iomem *regs;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001294 int ret;
1295
Daniel Vetter02e792f2009-09-15 22:57:34 +02001296 overlay = dev_priv->overlay;
1297 if (!overlay) {
1298 DRM_DEBUG("userspace bug: no overlay\n");
1299 return -ENODEV;
1300 }
1301
Daniel Vettera0e99e62012-12-02 01:05:46 +01001302 drm_modeset_lock_all(dev);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001303 mutex_lock(&dev->struct_mutex);
1304
Chris Wilson60fc3322010-08-12 10:44:45 +01001305 ret = -EINVAL;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001306 if (!(attrs->flags & I915_OVERLAY_UPDATE_ATTRS)) {
Chris Wilson60fc3322010-08-12 10:44:45 +01001307 attrs->color_key = overlay->color_key;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001308 attrs->brightness = overlay->brightness;
Chris Wilson60fc3322010-08-12 10:44:45 +01001309 attrs->contrast = overlay->contrast;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001310 attrs->saturation = overlay->saturation;
1311
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001312 if (!IS_GEN2(dev)) {
Daniel Vetter02e792f2009-09-15 22:57:34 +02001313 attrs->gamma0 = I915_READ(OGAMC0);
1314 attrs->gamma1 = I915_READ(OGAMC1);
1315 attrs->gamma2 = I915_READ(OGAMC2);
1316 attrs->gamma3 = I915_READ(OGAMC3);
1317 attrs->gamma4 = I915_READ(OGAMC4);
1318 attrs->gamma5 = I915_READ(OGAMC5);
1319 }
Daniel Vetter02e792f2009-09-15 22:57:34 +02001320 } else {
Chris Wilson60fc3322010-08-12 10:44:45 +01001321 if (attrs->brightness < -128 || attrs->brightness > 127)
Daniel Vetter02e792f2009-09-15 22:57:34 +02001322 goto out_unlock;
Chris Wilson60fc3322010-08-12 10:44:45 +01001323 if (attrs->contrast > 255)
Daniel Vetter02e792f2009-09-15 22:57:34 +02001324 goto out_unlock;
Chris Wilson60fc3322010-08-12 10:44:45 +01001325 if (attrs->saturation > 1023)
Daniel Vetter02e792f2009-09-15 22:57:34 +02001326 goto out_unlock;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001327
Chris Wilson60fc3322010-08-12 10:44:45 +01001328 overlay->color_key = attrs->color_key;
1329 overlay->brightness = attrs->brightness;
1330 overlay->contrast = attrs->contrast;
1331 overlay->saturation = attrs->saturation;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001332
Chris Wilson8d74f652010-08-12 10:35:26 +01001333 regs = intel_overlay_map_regs(overlay);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001334 if (!regs) {
1335 ret = -ENOMEM;
1336 goto out_unlock;
1337 }
1338
1339 update_reg_attrs(overlay, regs);
1340
Chris Wilson9bb2ff72010-08-12 12:02:11 +01001341 intel_overlay_unmap_regs(overlay, regs);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001342
1343 if (attrs->flags & I915_OVERLAY_UPDATE_GAMMA) {
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001344 if (IS_GEN2(dev))
Daniel Vetter02e792f2009-09-15 22:57:34 +02001345 goto out_unlock;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001346
1347 if (overlay->active) {
1348 ret = -EBUSY;
1349 goto out_unlock;
1350 }
1351
1352 ret = check_gamma(attrs);
Chris Wilson60fc3322010-08-12 10:44:45 +01001353 if (ret)
Daniel Vetter02e792f2009-09-15 22:57:34 +02001354 goto out_unlock;
1355
1356 I915_WRITE(OGAMC0, attrs->gamma0);
1357 I915_WRITE(OGAMC1, attrs->gamma1);
1358 I915_WRITE(OGAMC2, attrs->gamma2);
1359 I915_WRITE(OGAMC3, attrs->gamma3);
1360 I915_WRITE(OGAMC4, attrs->gamma4);
1361 I915_WRITE(OGAMC5, attrs->gamma5);
1362 }
Daniel Vetter02e792f2009-09-15 22:57:34 +02001363 }
Chris Wilsonea9da4e2015-04-02 10:35:08 +01001364 overlay->color_key_enabled = (attrs->flags & I915_OVERLAY_DISABLE_DEST_COLORKEY) == 0;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001365
Chris Wilson60fc3322010-08-12 10:44:45 +01001366 ret = 0;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001367out_unlock:
1368 mutex_unlock(&dev->struct_mutex);
Daniel Vettera0e99e62012-12-02 01:05:46 +01001369 drm_modeset_unlock_all(dev);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001370
1371 return ret;
1372}
1373
1374void intel_setup_overlay(struct drm_device *dev)
1375{
Jani Nikulad5d45cc2014-03-31 14:27:20 +03001376 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001377 struct intel_overlay *overlay;
Chris Wilson05394f32010-11-08 19:18:58 +00001378 struct drm_i915_gem_object *reg_bo;
Ben Widawsky75020bc2012-04-16 14:07:43 -07001379 struct overlay_registers __iomem *regs;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001380 int ret;
1381
Chris Wilson315781482010-08-12 09:42:51 +01001382 if (!HAS_OVERLAY(dev))
Daniel Vetter02e792f2009-09-15 22:57:34 +02001383 return;
1384
Daniel Vetterb14c5672013-09-19 12:18:32 +02001385 overlay = kzalloc(sizeof(*overlay), GFP_KERNEL);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001386 if (!overlay)
1387 return;
Chris Wilson79d24272011-06-28 11:27:47 +01001388
1389 mutex_lock(&dev->struct_mutex);
1390 if (WARN_ON(dev_priv->overlay))
1391 goto out_free;
1392
Daniel Vetter02e792f2009-09-15 22:57:34 +02001393 overlay->dev = dev;
1394
Daniel Vetterf63a4842013-07-23 19:24:38 +02001395 reg_bo = NULL;
1396 if (!OVERLAY_NEEDS_PHYSICAL(dev))
1397 reg_bo = i915_gem_object_create_stolen(dev, PAGE_SIZE);
Chris Wilson80405132012-11-15 11:32:29 +00001398 if (reg_bo == NULL)
1399 reg_bo = i915_gem_alloc_object(dev, PAGE_SIZE);
1400 if (reg_bo == NULL)
Daniel Vetter02e792f2009-09-15 22:57:34 +02001401 goto out_free;
Chris Wilson05394f32010-11-08 19:18:58 +00001402 overlay->reg_bo = reg_bo;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001403
Chris Wilson315781482010-08-12 09:42:51 +01001404 if (OVERLAY_NEEDS_PHYSICAL(dev)) {
Chris Wilson00731152014-05-21 12:42:56 +01001405 ret = i915_gem_object_attach_phys(reg_bo, PAGE_SIZE);
Akshay Joshi0206e352011-08-16 15:34:10 -04001406 if (ret) {
1407 DRM_ERROR("failed to attach phys overlay regs\n");
1408 goto out_free_bo;
1409 }
Chris Wilson00731152014-05-21 12:42:56 +01001410 overlay->flip_addr = reg_bo->phys_handle->busaddr;
Chris Wilson315781482010-08-12 09:42:51 +01001411 } else {
Daniel Vetter1ec9e262014-02-14 14:01:11 +01001412 ret = i915_gem_obj_ggtt_pin(reg_bo, PAGE_SIZE, PIN_MAPPABLE);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001413 if (ret) {
Akshay Joshi0206e352011-08-16 15:34:10 -04001414 DRM_ERROR("failed to pin overlay register bo\n");
1415 goto out_free_bo;
1416 }
Ben Widawskyf343c5f2013-07-05 14:41:04 -07001417 overlay->flip_addr = i915_gem_obj_ggtt_offset(reg_bo);
Chris Wilson0ddc1282010-08-12 09:35:00 +01001418
1419 ret = i915_gem_object_set_to_gtt_domain(reg_bo, true);
1420 if (ret) {
Akshay Joshi0206e352011-08-16 15:34:10 -04001421 DRM_ERROR("failed to move overlay register bo into the GTT\n");
1422 goto out_unpin_bo;
1423 }
Daniel Vetter02e792f2009-09-15 22:57:34 +02001424 }
1425
1426 /* init all values */
1427 overlay->color_key = 0x0101fe;
Chris Wilsonea9da4e2015-04-02 10:35:08 +01001428 overlay->color_key_enabled = true;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001429 overlay->brightness = -19;
1430 overlay->contrast = 75;
1431 overlay->saturation = 146;
1432
Chris Wilson8d74f652010-08-12 10:35:26 +01001433 regs = intel_overlay_map_regs(overlay);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001434 if (!regs)
Chris Wilson79d24272011-06-28 11:27:47 +01001435 goto out_unpin_bo;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001436
Ben Widawsky75020bc2012-04-16 14:07:43 -07001437 memset_io(regs, 0, sizeof(struct overlay_registers));
Daniel Vetter02e792f2009-09-15 22:57:34 +02001438 update_polyphase_filter(regs);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001439 update_reg_attrs(overlay, regs);
1440
Chris Wilson9bb2ff72010-08-12 12:02:11 +01001441 intel_overlay_unmap_regs(overlay, regs);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001442
1443 dev_priv->overlay = overlay;
Chris Wilson79d24272011-06-28 11:27:47 +01001444 mutex_unlock(&dev->struct_mutex);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001445 DRM_INFO("initialized overlay support\n");
1446 return;
1447
Chris Wilson0ddc1282010-08-12 09:35:00 +01001448out_unpin_bo:
Chris Wilson79d24272011-06-28 11:27:47 +01001449 if (!OVERLAY_NEEDS_PHYSICAL(dev))
Ben Widawskyd7f46fc2013-12-06 14:10:55 -08001450 i915_gem_object_ggtt_unpin(reg_bo);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001451out_free_bo:
Chris Wilson05394f32010-11-08 19:18:58 +00001452 drm_gem_object_unreference(&reg_bo->base);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001453out_free:
Chris Wilson79d24272011-06-28 11:27:47 +01001454 mutex_unlock(&dev->struct_mutex);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001455 kfree(overlay);
1456 return;
1457}
1458
1459void intel_cleanup_overlay(struct drm_device *dev)
1460{
Jani Nikulad5d45cc2014-03-31 14:27:20 +03001461 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001462
Chris Wilson62cf4e62010-08-12 10:50:36 +01001463 if (!dev_priv->overlay)
1464 return;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001465
Chris Wilson62cf4e62010-08-12 10:50:36 +01001466 /* The bo's should be free'd by the generic code already.
1467 * Furthermore modesetting teardown happens beforehand so the
1468 * hardware should be off already */
Ville Syrjälä77589f52015-03-31 10:37:22 +03001469 WARN_ON(dev_priv->overlay->active);
Chris Wilson62cf4e62010-08-12 10:50:36 +01001470
1471 drm_gem_object_unreference_unlocked(&dev_priv->overlay->reg_bo->base);
1472 kfree(dev_priv->overlay);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001473}
Chris Wilson6ef3d422010-08-04 20:26:07 +01001474
1475struct intel_overlay_error_state {
1476 struct overlay_registers regs;
1477 unsigned long base;
1478 u32 dovsta;
1479 u32 isr;
1480};
1481
Ben Widawsky75020bc2012-04-16 14:07:43 -07001482static struct overlay_registers __iomem *
Linus Torvaldsc48c43e2010-10-26 18:57:59 -07001483intel_overlay_map_regs_atomic(struct intel_overlay *overlay)
Chris Wilson3bd3c932010-08-19 08:19:30 +01001484{
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03001485 struct drm_i915_private *dev_priv = to_i915(overlay->dev);
1486 struct i915_ggtt *ggtt = &dev_priv->ggtt;
Ben Widawsky75020bc2012-04-16 14:07:43 -07001487 struct overlay_registers __iomem *regs;
Chris Wilson3bd3c932010-08-19 08:19:30 +01001488
1489 if (OVERLAY_NEEDS_PHYSICAL(overlay->dev))
Ben Widawsky75020bc2012-04-16 14:07:43 -07001490 /* Cast to make sparse happy, but it's wc memory anyway, so
1491 * equivalent to the wc io mapping on X86. */
1492 regs = (struct overlay_registers __iomem *)
Chris Wilson00731152014-05-21 12:42:56 +01001493 overlay->reg_bo->phys_handle->vaddr;
Chris Wilson3bd3c932010-08-19 08:19:30 +01001494 else
Joonas Lahtinen72e96d62016-03-30 16:57:10 +03001495 regs = io_mapping_map_atomic_wc(ggtt->mappable,
Ben Widawskyf343c5f2013-07-05 14:41:04 -07001496 i915_gem_obj_ggtt_offset(overlay->reg_bo));
Chris Wilson3bd3c932010-08-19 08:19:30 +01001497
1498 return regs;
1499}
1500
1501static void intel_overlay_unmap_regs_atomic(struct intel_overlay *overlay,
Ben Widawsky75020bc2012-04-16 14:07:43 -07001502 struct overlay_registers __iomem *regs)
Chris Wilson3bd3c932010-08-19 08:19:30 +01001503{
1504 if (!OVERLAY_NEEDS_PHYSICAL(overlay->dev))
Linus Torvaldsc48c43e2010-10-26 18:57:59 -07001505 io_mapping_unmap_atomic(regs);
Chris Wilson3bd3c932010-08-19 08:19:30 +01001506}
1507
1508
Chris Wilson6ef3d422010-08-04 20:26:07 +01001509struct intel_overlay_error_state *
1510intel_overlay_capture_error_state(struct drm_device *dev)
1511{
Jani Nikulad5d45cc2014-03-31 14:27:20 +03001512 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson6ef3d422010-08-04 20:26:07 +01001513 struct intel_overlay *overlay = dev_priv->overlay;
1514 struct intel_overlay_error_state *error;
1515 struct overlay_registers __iomem *regs;
1516
1517 if (!overlay || !overlay->active)
1518 return NULL;
1519
1520 error = kmalloc(sizeof(*error), GFP_ATOMIC);
1521 if (error == NULL)
1522 return NULL;
1523
1524 error->dovsta = I915_READ(DOVSTA);
1525 error->isr = I915_READ(ISR);
Chris Wilson315781482010-08-12 09:42:51 +01001526 if (OVERLAY_NEEDS_PHYSICAL(overlay->dev))
Chris Wilson00731152014-05-21 12:42:56 +01001527 error->base = (__force long)overlay->reg_bo->phys_handle->vaddr;
Chris Wilson315781482010-08-12 09:42:51 +01001528 else
Ben Widawskyf343c5f2013-07-05 14:41:04 -07001529 error->base = i915_gem_obj_ggtt_offset(overlay->reg_bo);
Chris Wilson6ef3d422010-08-04 20:26:07 +01001530
1531 regs = intel_overlay_map_regs_atomic(overlay);
1532 if (!regs)
1533 goto err;
1534
1535 memcpy_fromio(&error->regs, regs, sizeof(struct overlay_registers));
Linus Torvaldsc48c43e2010-10-26 18:57:59 -07001536 intel_overlay_unmap_regs_atomic(overlay, regs);
Chris Wilson6ef3d422010-08-04 20:26:07 +01001537
1538 return error;
1539
1540err:
1541 kfree(error);
1542 return NULL;
1543}
1544
1545void
Mika Kuoppalaedc3d882013-05-23 13:55:35 +03001546intel_overlay_print_error_state(struct drm_i915_error_state_buf *m,
1547 struct intel_overlay_error_state *error)
Chris Wilson6ef3d422010-08-04 20:26:07 +01001548{
Mika Kuoppalaedc3d882013-05-23 13:55:35 +03001549 i915_error_printf(m, "Overlay, status: 0x%08x, interrupt: 0x%08x\n",
1550 error->dovsta, error->isr);
1551 i915_error_printf(m, " Register file at 0x%08lx:\n",
1552 error->base);
Chris Wilson6ef3d422010-08-04 20:26:07 +01001553
Mika Kuoppalaedc3d882013-05-23 13:55:35 +03001554#define P(x) i915_error_printf(m, " " #x ": 0x%08x\n", error->regs.x)
Chris Wilson6ef3d422010-08-04 20:26:07 +01001555 P(OBUF_0Y);
1556 P(OBUF_1Y);
1557 P(OBUF_0U);
1558 P(OBUF_0V);
1559 P(OBUF_1U);
1560 P(OBUF_1V);
1561 P(OSTRIDE);
1562 P(YRGB_VPH);
1563 P(UV_VPH);
1564 P(HORZ_PH);
1565 P(INIT_PHS);
1566 P(DWINPOS);
1567 P(DWINSZ);
1568 P(SWIDTH);
1569 P(SWIDTHSW);
1570 P(SHEIGHT);
1571 P(YRGBSCALE);
1572 P(UVSCALE);
1573 P(OCLRC0);
1574 P(OCLRC1);
1575 P(DCLRKV);
1576 P(DCLRKM);
1577 P(SCLRKVH);
1578 P(SCLRKVL);
1579 P(SCLRKEN);
1580 P(OCONFIG);
1581 P(OCMD);
1582 P(OSTART_0Y);
1583 P(OSTART_1Y);
1584 P(OSTART_0U);
1585 P(OSTART_0V);
1586 P(OSTART_1U);
1587 P(OSTART_1V);
1588 P(OTILEOFF_0Y);
1589 P(OTILEOFF_1Y);
1590 P(OTILEOFF_0U);
1591 P(OTILEOFF_0V);
1592 P(OTILEOFF_1U);
1593 P(OTILEOFF_1V);
1594 P(FASTHSCALE);
1595 P(UVSCALEV);
1596#undef P
1597}