blob: b9b042cc619fcffb2d3a01217612f12946e7d21b [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 {
Chris Wilson1ee8da62016-05-12 12:43:23 +0100171 struct drm_i915_private *i915;
Chris Wilson23f09ce2010-08-12 13:53:37 +0100172 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{
Chris Wilson1ee8da62016-05-12 12:43:23 +0100193 struct drm_i915_private *dev_priv = overlay->i915;
Ben Widawsky75020bc2012-04-16 14:07:43 -0700194 struct overlay_registers __iomem *regs;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200195
Chris Wilson1ee8da62016-05-12 12:43:23 +0100196 if (OVERLAY_NEEDS_PHYSICAL(dev_priv))
Chris Wilson00731152014-05-21 12:42:56 +0100197 regs = (struct overlay_registers __iomem *)overlay->reg_bo->phys_handle->vaddr;
Chris Wilson9bb2ff72010-08-12 12:02:11 +0100198 else
Chris Wilson1ee8da62016-05-12 12:43:23 +0100199 regs = io_mapping_map_wc(dev_priv->ggtt.mappable,
Chris Wilsond8dab002016-04-28 09:56:37 +0100200 overlay->flip_addr,
201 PAGE_SIZE);
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 Wilson1ee8da62016-05-12 12:43:23 +0100209 if (!OVERLAY_NEEDS_PHYSICAL(overlay->i915))
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{
Chris Wilson1ee8da62016-05-12 12:43:23 +0100235 struct drm_i915_private *dev_priv = overlay->i915;
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +0000236 struct intel_engine_cs *engine = &dev_priv->engine[RCS];
John Harrisondad540c2015-05-29 17:43:47 +0100237 struct drm_i915_gem_request *req;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200238 int ret;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200239
Ville Syrjälä77589f52015-03-31 10:37:22 +0300240 WARN_ON(overlay->active);
Chris Wilson1ee8da62016-05-12 12:43:23 +0100241 WARN_ON(IS_I830(dev_priv) && !(dev_priv->quirks & QUIRK_PIPEA_FORCE));
Chris Wilson106dada2010-07-16 17:13:01 +0100242
Tvrtko Ursuline2f80392016-03-16 11:00:36 +0000243 req = i915_gem_request_alloc(engine, NULL);
Dave Gordon26827082016-01-19 19:02:53 +0000244 if (IS_ERR(req))
245 return PTR_ERR(req);
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100246
John Harrison5fb9de12015-05-29 17:44:07 +0100247 ret = intel_ring_begin(req, 4);
John Harrisondad540c2015-05-29 17:43:47 +0100248 if (ret) {
Chris Wilsonaa9b7812016-04-13 17:35:15 +0100249 i915_add_request_no_flush(req);
John Harrisondad540c2015-05-29 17:43:47 +0100250 return ret;
251 }
252
Ville Syrjälä1c7c4302015-03-31 10:37:24 +0300253 overlay->active = true;
254
Tvrtko Ursuline2f80392016-03-16 11:00:36 +0000255 intel_ring_emit(engine, MI_OVERLAY_FLIP | MI_OVERLAY_ON);
256 intel_ring_emit(engine, overlay->flip_addr | OFC_UPDATE);
257 intel_ring_emit(engine, MI_WAIT_FOR_EVENT | MI_WAIT_FOR_OVERLAY_FLIP);
258 intel_ring_emit(engine, MI_NOOP);
259 intel_ring_advance(engine);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200260
John Harrisondad540c2015-05-29 17:43:47 +0100261 return intel_overlay_do_wait_request(overlay, req, NULL);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200262}
263
264/* overlay needs to be enabled in OCMD reg */
Chris Wilson8dc5d142010-08-12 12:36:12 +0100265static int intel_overlay_continue(struct intel_overlay *overlay,
266 bool load_polyphase_filter)
Daniel Vetter02e792f2009-09-15 22:57:34 +0200267{
Chris Wilson1ee8da62016-05-12 12:43:23 +0100268 struct drm_i915_private *dev_priv = overlay->i915;
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +0000269 struct intel_engine_cs *engine = &dev_priv->engine[RCS];
John Harrisondad540c2015-05-29 17:43:47 +0100270 struct drm_i915_gem_request *req;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200271 u32 flip_addr = overlay->flip_addr;
272 u32 tmp;
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100273 int ret;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200274
Ville Syrjälä77589f52015-03-31 10:37:22 +0300275 WARN_ON(!overlay->active);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200276
277 if (load_polyphase_filter)
278 flip_addr |= OFC_UPDATE;
279
280 /* check for underruns */
281 tmp = I915_READ(DOVSTA);
282 if (tmp & (1 << 17))
283 DRM_DEBUG("overlay underrun, DOVSTA: %x\n", tmp);
284
Tvrtko Ursuline2f80392016-03-16 11:00:36 +0000285 req = i915_gem_request_alloc(engine, NULL);
Dave Gordon26827082016-01-19 19:02:53 +0000286 if (IS_ERR(req))
287 return PTR_ERR(req);
Chris Wilsonacb868d2012-09-26 13:47:30 +0100288
John Harrison5fb9de12015-05-29 17:44:07 +0100289 ret = intel_ring_begin(req, 2);
John Harrisondad540c2015-05-29 17:43:47 +0100290 if (ret) {
Chris Wilsonaa9b7812016-04-13 17:35:15 +0100291 i915_add_request_no_flush(req);
John Harrisondad540c2015-05-29 17:43:47 +0100292 return ret;
293 }
294
Tvrtko Ursuline2f80392016-03-16 11:00:36 +0000295 intel_ring_emit(engine, MI_OVERLAY_FLIP | MI_OVERLAY_CONTINUE);
296 intel_ring_emit(engine, flip_addr);
297 intel_ring_advance(engine);
Daniel Vetter5a5a0c62009-09-15 22:57:36 +0200298
John Harrison9bfc01a2014-11-24 18:49:31 +0000299 WARN_ON(overlay->last_flip_req);
John Harrisondad540c2015-05-29 17:43:47 +0100300 i915_gem_request_assign(&overlay->last_flip_req, req);
John Harrison75289872015-05-29 17:43:49 +0100301 i915_add_request(req);
John Harrisonbf7dc5b2015-05-29 17:43:24 +0100302
303 return 0;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200304}
305
Chris Wilsonb303cf92010-08-12 14:03:48 +0100306static void intel_overlay_release_old_vid_tail(struct intel_overlay *overlay)
Daniel Vetter02e792f2009-09-15 22:57:34 +0200307{
Chris Wilson05394f32010-11-08 19:18:58 +0000308 struct drm_i915_gem_object *obj = overlay->old_vid_bo;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200309
Ben Widawskyd7f46fc2013-12-06 14:10:55 -0800310 i915_gem_object_ggtt_unpin(obj);
Chris Wilson05394f32010-11-08 19:18:58 +0000311 drm_gem_object_unreference(&obj->base);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200312
Chris Wilsonb303cf92010-08-12 14:03:48 +0100313 overlay->old_vid_bo = NULL;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200314}
315
Daniel Vetter12ca45f2037-04-25 10:08:26 +0200316static void intel_overlay_off_tail(struct intel_overlay *overlay)
317{
Chris Wilson05394f32010-11-08 19:18:58 +0000318 struct drm_i915_gem_object *obj = overlay->vid_bo;
Daniel Vetter12ca45f2037-04-25 10:08:26 +0200319
320 /* never have the overlay hw on without showing a frame */
Ville Syrjälä77589f52015-03-31 10:37:22 +0300321 if (WARN_ON(!obj))
322 return;
Daniel Vetter12ca45f2037-04-25 10:08:26 +0200323
Ben Widawskyd7f46fc2013-12-06 14:10:55 -0800324 i915_gem_object_ggtt_unpin(obj);
Chris Wilson05394f32010-11-08 19:18:58 +0000325 drm_gem_object_unreference(&obj->base);
Daniel Vetter12ca45f2037-04-25 10:08:26 +0200326 overlay->vid_bo = NULL;
327
328 overlay->crtc->overlay = NULL;
329 overlay->crtc = NULL;
Ville Syrjälä209c2a52015-03-31 10:37:23 +0300330 overlay->active = false;
Daniel Vetter12ca45f2037-04-25 10:08:26 +0200331}
332
Daniel Vetter02e792f2009-09-15 22:57:34 +0200333/* overlay needs to be disabled in OCMD reg */
Chris Wilsonce453d82011-02-21 14:43:56 +0000334static int intel_overlay_off(struct intel_overlay *overlay)
Daniel Vetter03f77ea2009-09-15 22:57:37 +0200335{
Chris Wilson1ee8da62016-05-12 12:43:23 +0100336 struct drm_i915_private *dev_priv = overlay->i915;
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +0000337 struct intel_engine_cs *engine = &dev_priv->engine[RCS];
John Harrisondad540c2015-05-29 17:43:47 +0100338 struct drm_i915_gem_request *req;
Chris Wilson8dc5d142010-08-12 12:36:12 +0100339 u32 flip_addr = overlay->flip_addr;
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100340 int ret;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200341
Ville Syrjälä77589f52015-03-31 10:37:22 +0300342 WARN_ON(!overlay->active);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200343
344 /* According to intel docs the overlay hw may hang (when switching
345 * off) without loading the filter coeffs. It is however unclear whether
346 * this applies to the disabling of the overlay or to the switching off
347 * of the hw. Do it in both cases */
348 flip_addr |= OFC_UPDATE;
349
Tvrtko Ursuline2f80392016-03-16 11:00:36 +0000350 req = i915_gem_request_alloc(engine, NULL);
Dave Gordon26827082016-01-19 19:02:53 +0000351 if (IS_ERR(req))
352 return PTR_ERR(req);
Chris Wilsonacb868d2012-09-26 13:47:30 +0100353
John Harrison5fb9de12015-05-29 17:44:07 +0100354 ret = intel_ring_begin(req, 6);
John Harrisondad540c2015-05-29 17:43:47 +0100355 if (ret) {
Chris Wilsonaa9b7812016-04-13 17:35:15 +0100356 i915_add_request_no_flush(req);
John Harrisondad540c2015-05-29 17:43:47 +0100357 return ret;
358 }
359
Daniel Vetter02e792f2009-09-15 22:57:34 +0200360 /* wait for overlay to go idle */
Tvrtko Ursuline2f80392016-03-16 11:00:36 +0000361 intel_ring_emit(engine, MI_OVERLAY_FLIP | MI_OVERLAY_CONTINUE);
362 intel_ring_emit(engine, flip_addr);
363 intel_ring_emit(engine, MI_WAIT_FOR_EVENT | MI_WAIT_FOR_OVERLAY_FLIP);
Chris Wilson722506f2010-08-12 09:28:50 +0100364 /* turn overlay off */
Chris Wilson1ee8da62016-05-12 12:43:23 +0100365 if (IS_I830(dev_priv)) {
Daniel Vettera9193982012-10-22 12:55:55 +0200366 /* Workaround: Don't disable the overlay fully, since otherwise
367 * it dies on the next OVERLAY_ON cmd. */
Tvrtko Ursuline2f80392016-03-16 11:00:36 +0000368 intel_ring_emit(engine, MI_NOOP);
369 intel_ring_emit(engine, MI_NOOP);
370 intel_ring_emit(engine, MI_NOOP);
Daniel Vettera9193982012-10-22 12:55:55 +0200371 } else {
Tvrtko Ursuline2f80392016-03-16 11:00:36 +0000372 intel_ring_emit(engine, MI_OVERLAY_FLIP | MI_OVERLAY_OFF);
373 intel_ring_emit(engine, flip_addr);
374 intel_ring_emit(engine,
375 MI_WAIT_FOR_EVENT | MI_WAIT_FOR_OVERLAY_FLIP);
Daniel Vettera9193982012-10-22 12:55:55 +0200376 }
Tvrtko Ursuline2f80392016-03-16 11:00:36 +0000377 intel_ring_advance(engine);
Chris Wilson722506f2010-08-12 09:28:50 +0100378
John Harrisondad540c2015-05-29 17:43:47 +0100379 return intel_overlay_do_wait_request(overlay, req, intel_overlay_off_tail);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200380}
381
Daniel Vetter03f77ea2009-09-15 22:57:37 +0200382/* recover from an interruption due to a signal
383 * We have to be careful not to repeat work forever an make forward progess. */
Chris Wilsonce453d82011-02-21 14:43:56 +0000384static int intel_overlay_recover_from_interrupt(struct intel_overlay *overlay)
Daniel Vetter03f77ea2009-09-15 22:57:37 +0200385{
Daniel Vetter03f77ea2009-09-15 22:57:37 +0200386 int ret;
Daniel Vetter03f77ea2009-09-15 22:57:37 +0200387
John Harrison9bfc01a2014-11-24 18:49:31 +0000388 if (overlay->last_flip_req == NULL)
Chris Wilsonb303cf92010-08-12 14:03:48 +0100389 return 0;
Daniel Vetter03f77ea2009-09-15 22:57:37 +0200390
Daniel Vettera4b3a572014-11-26 14:17:05 +0100391 ret = i915_wait_request(overlay->last_flip_req);
Chris Wilsonb6c028e2010-08-12 11:55:08 +0100392 if (ret)
Daniel Vetter03f77ea2009-09-15 22:57:37 +0200393 return ret;
394
Chris Wilsonb303cf92010-08-12 14:03:48 +0100395 if (overlay->flip_tail)
396 overlay->flip_tail(overlay);
Daniel Vetter03f77ea2009-09-15 22:57:37 +0200397
John Harrison9bfc01a2014-11-24 18:49:31 +0000398 i915_gem_request_assign(&overlay->last_flip_req, NULL);
Daniel Vetter03f77ea2009-09-15 22:57:37 +0200399 return 0;
400}
401
Daniel Vetter5a5a0c62009-09-15 22:57:36 +0200402/* Wait for pending overlay flip and release old frame.
403 * Needs to be called before the overlay register are changed
Chris Wilson8d74f652010-08-12 10:35:26 +0100404 * via intel_overlay_(un)map_regs
405 */
Daniel Vetter02e792f2009-09-15 22:57:34 +0200406static int intel_overlay_release_old_vid(struct intel_overlay *overlay)
407{
Chris Wilson1ee8da62016-05-12 12:43:23 +0100408 struct drm_i915_private *dev_priv = overlay->i915;
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +0000409 struct intel_engine_cs *engine = &dev_priv->engine[RCS];
Daniel Vetter02e792f2009-09-15 22:57:34 +0200410 int ret;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200411
Chris Wilson1ee8da62016-05-12 12:43:23 +0100412 lockdep_assert_held(&dev_priv->dev->struct_mutex);
Ville Syrjälä1362b772014-11-26 17:07:29 +0200413
Chris Wilson5cd68c92010-08-12 12:21:54 +0100414 /* Only wait if there is actually an old frame to release to
415 * guarantee forward progress.
416 */
Daniel Vetter03f77ea2009-09-15 22:57:37 +0200417 if (!overlay->old_vid_bo)
418 return 0;
419
Chris Wilson5cd68c92010-08-12 12:21:54 +0100420 if (I915_READ(ISR) & I915_OVERLAY_PLANE_FLIP_PENDING_INTERRUPT) {
421 /* synchronous slowpath */
John Harrisondad540c2015-05-29 17:43:47 +0100422 struct drm_i915_gem_request *req;
423
Tvrtko Ursuline2f80392016-03-16 11:00:36 +0000424 req = i915_gem_request_alloc(engine, NULL);
Dave Gordon26827082016-01-19 19:02:53 +0000425 if (IS_ERR(req))
426 return PTR_ERR(req);
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100427
John Harrison5fb9de12015-05-29 17:44:07 +0100428 ret = intel_ring_begin(req, 2);
John Harrisondad540c2015-05-29 17:43:47 +0100429 if (ret) {
Chris Wilsonaa9b7812016-04-13 17:35:15 +0100430 i915_add_request_no_flush(req);
John Harrisondad540c2015-05-29 17:43:47 +0100431 return ret;
432 }
433
Tvrtko Ursuline2f80392016-03-16 11:00:36 +0000434 intel_ring_emit(engine,
435 MI_WAIT_FOR_EVENT | MI_WAIT_FOR_OVERLAY_FLIP);
436 intel_ring_emit(engine, MI_NOOP);
437 intel_ring_advance(engine);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200438
John Harrisondad540c2015-05-29 17:43:47 +0100439 ret = intel_overlay_do_wait_request(overlay, req,
Chris Wilsonb303cf92010-08-12 14:03:48 +0100440 intel_overlay_release_old_vid_tail);
Chris Wilson5cd68c92010-08-12 12:21:54 +0100441 if (ret)
442 return ret;
443 }
Daniel Vetter02e792f2009-09-15 22:57:34 +0200444
Chris Wilson5cd68c92010-08-12 12:21:54 +0100445 intel_overlay_release_old_vid_tail(overlay);
Daniel Vettera071fa02014-06-18 23:28:09 +0200446
447
448 i915_gem_track_fb(overlay->old_vid_bo, NULL,
449 INTEL_FRONTBUFFER_OVERLAY(overlay->crtc->pipe));
Daniel Vetter02e792f2009-09-15 22:57:34 +0200450 return 0;
451}
452
Ville Syrjälä1362b772014-11-26 17:07:29 +0200453void intel_overlay_reset(struct drm_i915_private *dev_priv)
454{
455 struct intel_overlay *overlay = dev_priv->overlay;
456
457 if (!overlay)
458 return;
459
460 intel_overlay_release_old_vid(overlay);
461
462 overlay->last_flip_req = NULL;
463 overlay->old_xscale = 0;
464 overlay->old_yscale = 0;
465 overlay->crtc = NULL;
466 overlay->active = false;
467}
468
Daniel Vetter02e792f2009-09-15 22:57:34 +0200469struct put_image_params {
470 int format;
471 short dst_x;
472 short dst_y;
473 short dst_w;
474 short dst_h;
475 short src_w;
476 short src_scan_h;
477 short src_scan_w;
478 short src_h;
479 short stride_Y;
480 short stride_UV;
481 int offset_Y;
482 int offset_U;
483 int offset_V;
484};
485
486static int packed_depth_bytes(u32 format)
487{
488 switch (format & I915_OVERLAY_DEPTH_MASK) {
Chris Wilson722506f2010-08-12 09:28:50 +0100489 case I915_OVERLAY_YUV422:
490 return 4;
491 case I915_OVERLAY_YUV411:
492 /* return 6; not implemented */
493 default:
494 return -EINVAL;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200495 }
496}
497
498static int packed_width_bytes(u32 format, short width)
499{
500 switch (format & I915_OVERLAY_DEPTH_MASK) {
Chris Wilson722506f2010-08-12 09:28:50 +0100501 case I915_OVERLAY_YUV422:
502 return width << 1;
503 default:
504 return -EINVAL;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200505 }
506}
507
508static int uv_hsubsampling(u32 format)
509{
510 switch (format & I915_OVERLAY_DEPTH_MASK) {
Chris Wilson722506f2010-08-12 09:28:50 +0100511 case I915_OVERLAY_YUV422:
512 case I915_OVERLAY_YUV420:
513 return 2;
514 case I915_OVERLAY_YUV411:
515 case I915_OVERLAY_YUV410:
516 return 4;
517 default:
518 return -EINVAL;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200519 }
520}
521
522static int uv_vsubsampling(u32 format)
523{
524 switch (format & I915_OVERLAY_DEPTH_MASK) {
Chris Wilson722506f2010-08-12 09:28:50 +0100525 case I915_OVERLAY_YUV420:
526 case I915_OVERLAY_YUV410:
527 return 2;
528 case I915_OVERLAY_YUV422:
529 case I915_OVERLAY_YUV411:
530 return 1;
531 default:
532 return -EINVAL;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200533 }
534}
535
Chris Wilson1ee8da62016-05-12 12:43:23 +0100536static u32 calc_swidthsw(struct drm_i915_private *dev_priv, u32 offset, u32 width)
Daniel Vetter02e792f2009-09-15 22:57:34 +0200537{
538 u32 mask, shift, ret;
Chris Wilson1ee8da62016-05-12 12:43:23 +0100539 if (IS_GEN2(dev_priv)) {
Daniel Vetter02e792f2009-09-15 22:57:34 +0200540 mask = 0x1f;
541 shift = 5;
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100542 } else {
543 mask = 0x3f;
544 shift = 6;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200545 }
546 ret = ((offset + width + mask) >> shift) - (offset >> shift);
Chris Wilson1ee8da62016-05-12 12:43:23 +0100547 if (!IS_GEN2(dev_priv))
Daniel Vetter02e792f2009-09-15 22:57:34 +0200548 ret <<= 1;
Akshay Joshi0206e352011-08-16 15:34:10 -0400549 ret -= 1;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200550 return ret << 2;
551}
552
553static const u16 y_static_hcoeffs[N_HORIZ_Y_TAPS * N_PHASES] = {
554 0x3000, 0xb4a0, 0x1930, 0x1920, 0xb4a0,
555 0x3000, 0xb500, 0x19d0, 0x1880, 0xb440,
556 0x3000, 0xb540, 0x1a88, 0x2f80, 0xb3e0,
557 0x3000, 0xb580, 0x1b30, 0x2e20, 0xb380,
558 0x3000, 0xb5c0, 0x1bd8, 0x2cc0, 0xb320,
559 0x3020, 0xb5e0, 0x1c60, 0x2b80, 0xb2c0,
560 0x3020, 0xb5e0, 0x1cf8, 0x2a20, 0xb260,
561 0x3020, 0xb5e0, 0x1d80, 0x28e0, 0xb200,
562 0x3020, 0xb5c0, 0x1e08, 0x3f40, 0xb1c0,
563 0x3020, 0xb580, 0x1e78, 0x3ce0, 0xb160,
564 0x3040, 0xb520, 0x1ed8, 0x3aa0, 0xb120,
565 0x3040, 0xb4a0, 0x1f30, 0x3880, 0xb0e0,
566 0x3040, 0xb400, 0x1f78, 0x3680, 0xb0a0,
567 0x3020, 0xb340, 0x1fb8, 0x34a0, 0xb060,
568 0x3020, 0xb240, 0x1fe0, 0x32e0, 0xb040,
569 0x3020, 0xb140, 0x1ff8, 0x3160, 0xb020,
Chris Wilson722506f2010-08-12 09:28:50 +0100570 0xb000, 0x3000, 0x0800, 0x3000, 0xb000
571};
572
Daniel Vetter02e792f2009-09-15 22:57:34 +0200573static const u16 uv_static_hcoeffs[N_HORIZ_UV_TAPS * N_PHASES] = {
574 0x3000, 0x1800, 0x1800, 0xb000, 0x18d0, 0x2e60,
575 0xb000, 0x1990, 0x2ce0, 0xb020, 0x1a68, 0x2b40,
576 0xb040, 0x1b20, 0x29e0, 0xb060, 0x1bd8, 0x2880,
577 0xb080, 0x1c88, 0x3e60, 0xb0a0, 0x1d28, 0x3c00,
578 0xb0c0, 0x1db8, 0x39e0, 0xb0e0, 0x1e40, 0x37e0,
579 0xb100, 0x1eb8, 0x3620, 0xb100, 0x1f18, 0x34a0,
580 0xb100, 0x1f68, 0x3360, 0xb0e0, 0x1fa8, 0x3240,
581 0xb0c0, 0x1fe0, 0x3140, 0xb060, 0x1ff0, 0x30a0,
Chris Wilson722506f2010-08-12 09:28:50 +0100582 0x3000, 0x0800, 0x3000
583};
Daniel Vetter02e792f2009-09-15 22:57:34 +0200584
Ben Widawsky75020bc2012-04-16 14:07:43 -0700585static void update_polyphase_filter(struct overlay_registers __iomem *regs)
Daniel Vetter02e792f2009-09-15 22:57:34 +0200586{
Ben Widawsky75020bc2012-04-16 14:07:43 -0700587 memcpy_toio(regs->Y_HCOEFS, y_static_hcoeffs, sizeof(y_static_hcoeffs));
588 memcpy_toio(regs->UV_HCOEFS, uv_static_hcoeffs,
589 sizeof(uv_static_hcoeffs));
Daniel Vetter02e792f2009-09-15 22:57:34 +0200590}
591
592static bool update_scaling_factors(struct intel_overlay *overlay,
Ben Widawsky75020bc2012-04-16 14:07:43 -0700593 struct overlay_registers __iomem *regs,
Daniel Vetter02e792f2009-09-15 22:57:34 +0200594 struct put_image_params *params)
595{
596 /* fixed point with a 12 bit shift */
597 u32 xscale, yscale, xscale_UV, yscale_UV;
598#define FP_SHIFT 12
599#define FRACT_MASK 0xfff
600 bool scale_changed = false;
601 int uv_hscale = uv_hsubsampling(params->format);
602 int uv_vscale = uv_vsubsampling(params->format);
603
604 if (params->dst_w > 1)
605 xscale = ((params->src_scan_w - 1) << FP_SHIFT)
606 /(params->dst_w);
607 else
608 xscale = 1 << FP_SHIFT;
609
610 if (params->dst_h > 1)
611 yscale = ((params->src_scan_h - 1) << FP_SHIFT)
612 /(params->dst_h);
613 else
614 yscale = 1 << FP_SHIFT;
615
616 /*if (params->format & I915_OVERLAY_YUV_PLANAR) {*/
Chris Wilson722506f2010-08-12 09:28:50 +0100617 xscale_UV = xscale/uv_hscale;
618 yscale_UV = yscale/uv_vscale;
619 /* make the Y scale to UV scale ratio an exact multiply */
620 xscale = xscale_UV * uv_hscale;
621 yscale = yscale_UV * uv_vscale;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200622 /*} else {
Chris Wilson722506f2010-08-12 09:28:50 +0100623 xscale_UV = 0;
624 yscale_UV = 0;
625 }*/
Daniel Vetter02e792f2009-09-15 22:57:34 +0200626
627 if (xscale != overlay->old_xscale || yscale != overlay->old_yscale)
628 scale_changed = true;
629 overlay->old_xscale = xscale;
630 overlay->old_yscale = yscale;
631
Ben Widawsky75020bc2012-04-16 14:07:43 -0700632 iowrite32(((yscale & FRACT_MASK) << 20) |
633 ((xscale >> FP_SHIFT) << 16) |
634 ((xscale & FRACT_MASK) << 3),
635 &regs->YRGBSCALE);
Chris Wilson722506f2010-08-12 09:28:50 +0100636
Ben Widawsky75020bc2012-04-16 14:07:43 -0700637 iowrite32(((yscale_UV & FRACT_MASK) << 20) |
638 ((xscale_UV >> FP_SHIFT) << 16) |
639 ((xscale_UV & FRACT_MASK) << 3),
640 &regs->UVSCALE);
Chris Wilson722506f2010-08-12 09:28:50 +0100641
Ben Widawsky75020bc2012-04-16 14:07:43 -0700642 iowrite32((((yscale >> FP_SHIFT) << 16) |
643 ((yscale_UV >> FP_SHIFT) << 0)),
644 &regs->UVSCALEV);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200645
646 if (scale_changed)
647 update_polyphase_filter(regs);
648
649 return scale_changed;
650}
651
652static void update_colorkey(struct intel_overlay *overlay,
Ben Widawsky75020bc2012-04-16 14:07:43 -0700653 struct overlay_registers __iomem *regs)
Daniel Vetter02e792f2009-09-15 22:57:34 +0200654{
655 u32 key = overlay->color_key;
Chris Wilsonea9da4e2015-04-02 10:35:08 +0100656 u32 flags;
657
658 flags = 0;
659 if (overlay->color_key_enabled)
660 flags |= DST_KEY_ENABLE;
Chris Wilson6ba3ddd2010-08-12 09:30:58 +0100661
Matt Roperf4510a22014-04-01 15:22:40 -0700662 switch (overlay->crtc->base.primary->fb->bits_per_pixel) {
Chris Wilson722506f2010-08-12 09:28:50 +0100663 case 8:
Chris Wilsonea9da4e2015-04-02 10:35:08 +0100664 key = 0;
665 flags |= CLK_RGB8I_MASK;
Chris Wilson6ba3ddd2010-08-12 09:30:58 +0100666 break;
667
Chris Wilson722506f2010-08-12 09:28:50 +0100668 case 16:
Matt Roperf4510a22014-04-01 15:22:40 -0700669 if (overlay->crtc->base.primary->fb->depth == 15) {
Chris Wilsonea9da4e2015-04-02 10:35:08 +0100670 key = RGB15_TO_COLORKEY(key);
671 flags |= CLK_RGB15_MASK;
Chris Wilson722506f2010-08-12 09:28:50 +0100672 } else {
Chris Wilsonea9da4e2015-04-02 10:35:08 +0100673 key = RGB16_TO_COLORKEY(key);
674 flags |= CLK_RGB16_MASK;
Chris Wilson722506f2010-08-12 09:28:50 +0100675 }
Chris Wilson6ba3ddd2010-08-12 09:30:58 +0100676 break;
677
Chris Wilson722506f2010-08-12 09:28:50 +0100678 case 24:
679 case 32:
Chris Wilsonea9da4e2015-04-02 10:35:08 +0100680 flags |= CLK_RGB24_MASK;
Chris Wilson6ba3ddd2010-08-12 09:30:58 +0100681 break;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200682 }
Chris Wilsonea9da4e2015-04-02 10:35:08 +0100683
684 iowrite32(key, &regs->DCLRKV);
685 iowrite32(flags, &regs->DCLRKM);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200686}
687
688static u32 overlay_cmd_reg(struct put_image_params *params)
689{
690 u32 cmd = OCMD_ENABLE | OCMD_BUF_TYPE_FRAME | OCMD_BUFFER0;
691
692 if (params->format & I915_OVERLAY_YUV_PLANAR) {
693 switch (params->format & I915_OVERLAY_DEPTH_MASK) {
Chris Wilson722506f2010-08-12 09:28:50 +0100694 case I915_OVERLAY_YUV422:
695 cmd |= OCMD_YUV_422_PLANAR;
696 break;
697 case I915_OVERLAY_YUV420:
698 cmd |= OCMD_YUV_420_PLANAR;
699 break;
700 case I915_OVERLAY_YUV411:
701 case I915_OVERLAY_YUV410:
702 cmd |= OCMD_YUV_410_PLANAR;
703 break;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200704 }
705 } else { /* YUV packed */
706 switch (params->format & I915_OVERLAY_DEPTH_MASK) {
Chris Wilson722506f2010-08-12 09:28:50 +0100707 case I915_OVERLAY_YUV422:
708 cmd |= OCMD_YUV_422_PACKED;
709 break;
710 case I915_OVERLAY_YUV411:
711 cmd |= OCMD_YUV_411_PACKED;
712 break;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200713 }
714
715 switch (params->format & I915_OVERLAY_SWAP_MASK) {
Chris Wilson722506f2010-08-12 09:28:50 +0100716 case I915_OVERLAY_NO_SWAP:
717 break;
718 case I915_OVERLAY_UV_SWAP:
719 cmd |= OCMD_UV_SWAP;
720 break;
721 case I915_OVERLAY_Y_SWAP:
722 cmd |= OCMD_Y_SWAP;
723 break;
724 case I915_OVERLAY_Y_AND_UV_SWAP:
725 cmd |= OCMD_Y_AND_UV_SWAP;
726 break;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200727 }
728 }
729
730 return cmd;
731}
732
Chris Wilson5fe82c52010-08-12 12:38:21 +0100733static int intel_overlay_do_put_image(struct intel_overlay *overlay,
Chris Wilson05394f32010-11-08 19:18:58 +0000734 struct drm_i915_gem_object *new_bo,
Chris Wilson5fe82c52010-08-12 12:38:21 +0100735 struct put_image_params *params)
Daniel Vetter02e792f2009-09-15 22:57:34 +0200736{
737 int ret, tmp_width;
Ben Widawsky75020bc2012-04-16 14:07:43 -0700738 struct overlay_registers __iomem *regs;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200739 bool scale_changed = false;
Chris Wilson1ee8da62016-05-12 12:43:23 +0100740 struct drm_i915_private *dev_priv = overlay->i915;
Ben Widawsky75020bc2012-04-16 14:07:43 -0700741 u32 swidth, swidthsw, sheight, ostride;
Daniel Vettera071fa02014-06-18 23:28:09 +0200742 enum pipe pipe = overlay->crtc->pipe;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200743
Chris Wilson1ee8da62016-05-12 12:43:23 +0100744 lockdep_assert_held(&dev_priv->dev->struct_mutex);
745 WARN_ON(!drm_modeset_is_locked(&dev_priv->dev->mode_config.connection_mutex));
Daniel Vetter02e792f2009-09-15 22:57:34 +0200746
Daniel Vetter02e792f2009-09-15 22:57:34 +0200747 ret = intel_overlay_release_old_vid(overlay);
748 if (ret != 0)
749 return ret;
750
Maarten Lankhorst7580d772015-08-18 13:40:06 +0200751 ret = i915_gem_object_pin_to_display_plane(new_bo, 0,
Tvrtko Ursuline6617332015-03-23 11:10:33 +0000752 &i915_ggtt_view_normal);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200753 if (ret != 0)
754 return ret;
755
Chris Wilsond9e86c02010-11-10 16:40:20 +0000756 ret = i915_gem_object_put_fence(new_bo);
757 if (ret)
758 goto out_unpin;
759
Daniel Vetter02e792f2009-09-15 22:57:34 +0200760 if (!overlay->active) {
Ben Widawsky75020bc2012-04-16 14:07:43 -0700761 u32 oconfig;
Chris Wilson8d74f652010-08-12 10:35:26 +0100762 regs = intel_overlay_map_regs(overlay);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200763 if (!regs) {
764 ret = -ENOMEM;
765 goto out_unpin;
766 }
Ben Widawsky75020bc2012-04-16 14:07:43 -0700767 oconfig = OCONF_CC_OUT_8BIT;
Chris Wilson1ee8da62016-05-12 12:43:23 +0100768 if (IS_GEN4(dev_priv))
Ben Widawsky75020bc2012-04-16 14:07:43 -0700769 oconfig |= OCONF_CSC_MODE_BT709;
Daniel Vettera071fa02014-06-18 23:28:09 +0200770 oconfig |= pipe == 0 ?
Daniel Vetter02e792f2009-09-15 22:57:34 +0200771 OCONF_PIPE_A : OCONF_PIPE_B;
Ben Widawsky75020bc2012-04-16 14:07:43 -0700772 iowrite32(oconfig, &regs->OCONFIG);
Chris Wilson9bb2ff72010-08-12 12:02:11 +0100773 intel_overlay_unmap_regs(overlay, regs);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200774
775 ret = intel_overlay_on(overlay);
776 if (ret != 0)
777 goto out_unpin;
778 }
779
Chris Wilson8d74f652010-08-12 10:35:26 +0100780 regs = intel_overlay_map_regs(overlay);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200781 if (!regs) {
782 ret = -ENOMEM;
783 goto out_unpin;
784 }
785
Ben Widawsky75020bc2012-04-16 14:07:43 -0700786 iowrite32((params->dst_y << 16) | params->dst_x, &regs->DWINPOS);
787 iowrite32((params->dst_h << 16) | params->dst_w, &regs->DWINSZ);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200788
789 if (params->format & I915_OVERLAY_YUV_PACKED)
790 tmp_width = packed_width_bytes(params->format, params->src_w);
791 else
792 tmp_width = params->src_w;
793
Ben Widawsky75020bc2012-04-16 14:07:43 -0700794 swidth = params->src_w;
Chris Wilson1ee8da62016-05-12 12:43:23 +0100795 swidthsw = calc_swidthsw(dev_priv, params->offset_Y, tmp_width);
Ben Widawsky75020bc2012-04-16 14:07:43 -0700796 sheight = params->src_h;
Ben Widawskyf343c5f2013-07-05 14:41:04 -0700797 iowrite32(i915_gem_obj_ggtt_offset(new_bo) + params->offset_Y, &regs->OBUF_0Y);
Ben Widawsky75020bc2012-04-16 14:07:43 -0700798 ostride = params->stride_Y;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200799
800 if (params->format & I915_OVERLAY_YUV_PLANAR) {
801 int uv_hscale = uv_hsubsampling(params->format);
802 int uv_vscale = uv_vsubsampling(params->format);
803 u32 tmp_U, tmp_V;
Ben Widawsky75020bc2012-04-16 14:07:43 -0700804 swidth |= (params->src_w/uv_hscale) << 16;
Chris Wilson1ee8da62016-05-12 12:43:23 +0100805 tmp_U = calc_swidthsw(dev_priv, params->offset_U,
Chris Wilson722506f2010-08-12 09:28:50 +0100806 params->src_w/uv_hscale);
Chris Wilson1ee8da62016-05-12 12:43:23 +0100807 tmp_V = calc_swidthsw(dev_priv, params->offset_V,
Chris Wilson722506f2010-08-12 09:28:50 +0100808 params->src_w/uv_hscale);
Ben Widawsky75020bc2012-04-16 14:07:43 -0700809 swidthsw |= max_t(u32, tmp_U, tmp_V) << 16;
810 sheight |= (params->src_h/uv_vscale) << 16;
Ben Widawskyf343c5f2013-07-05 14:41:04 -0700811 iowrite32(i915_gem_obj_ggtt_offset(new_bo) + params->offset_U, &regs->OBUF_0U);
812 iowrite32(i915_gem_obj_ggtt_offset(new_bo) + params->offset_V, &regs->OBUF_0V);
Ben Widawsky75020bc2012-04-16 14:07:43 -0700813 ostride |= params->stride_UV << 16;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200814 }
815
Ben Widawsky75020bc2012-04-16 14:07:43 -0700816 iowrite32(swidth, &regs->SWIDTH);
817 iowrite32(swidthsw, &regs->SWIDTHSW);
818 iowrite32(sheight, &regs->SHEIGHT);
819 iowrite32(ostride, &regs->OSTRIDE);
820
Daniel Vetter02e792f2009-09-15 22:57:34 +0200821 scale_changed = update_scaling_factors(overlay, regs, params);
822
823 update_colorkey(overlay, regs);
824
Ben Widawsky75020bc2012-04-16 14:07:43 -0700825 iowrite32(overlay_cmd_reg(params), &regs->OCMD);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200826
Chris Wilson9bb2ff72010-08-12 12:02:11 +0100827 intel_overlay_unmap_regs(overlay, regs);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200828
Chris Wilson8dc5d142010-08-12 12:36:12 +0100829 ret = intel_overlay_continue(overlay, scale_changed);
830 if (ret)
831 goto out_unpin;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200832
Daniel Vettera071fa02014-06-18 23:28:09 +0200833 i915_gem_track_fb(overlay->vid_bo, new_bo,
834 INTEL_FRONTBUFFER_OVERLAY(pipe));
835
Daniel Vetter02e792f2009-09-15 22:57:34 +0200836 overlay->old_vid_bo = overlay->vid_bo;
Chris Wilson05394f32010-11-08 19:18:58 +0000837 overlay->vid_bo = new_bo;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200838
Chris Wilson1ee8da62016-05-12 12:43:23 +0100839 intel_frontbuffer_flip(dev_priv->dev, INTEL_FRONTBUFFER_OVERLAY(pipe));
Daniel Vetterf99d7062014-06-19 16:01:59 +0200840
Daniel Vetter02e792f2009-09-15 22:57:34 +0200841 return 0;
842
843out_unpin:
Ben Widawskyd7f46fc2013-12-06 14:10:55 -0800844 i915_gem_object_ggtt_unpin(new_bo);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200845 return ret;
846}
847
Chris Wilsonce453d82011-02-21 14:43:56 +0000848int intel_overlay_switch_off(struct intel_overlay *overlay)
Daniel Vetter02e792f2009-09-15 22:57:34 +0200849{
Chris Wilson1ee8da62016-05-12 12:43:23 +0100850 struct drm_i915_private *dev_priv = overlay->i915;
Ben Widawsky75020bc2012-04-16 14:07:43 -0700851 struct overlay_registers __iomem *regs;
Chris Wilson5dcdbcb2010-08-12 13:50:28 +0100852 int ret;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200853
Chris Wilson1ee8da62016-05-12 12:43:23 +0100854 lockdep_assert_held(&dev_priv->dev->struct_mutex);
855 WARN_ON(!drm_modeset_is_locked(&dev_priv->dev->mode_config.connection_mutex));
Daniel Vetter02e792f2009-09-15 22:57:34 +0200856
Chris Wilsonce453d82011-02-21 14:43:56 +0000857 ret = intel_overlay_recover_from_interrupt(overlay);
Chris Wilsonb303cf92010-08-12 14:03:48 +0100858 if (ret != 0)
859 return ret;
Daniel Vetter9bedb972009-11-30 15:55:49 +0100860
Daniel Vetter02e792f2009-09-15 22:57:34 +0200861 if (!overlay->active)
862 return 0;
863
Daniel Vetter02e792f2009-09-15 22:57:34 +0200864 ret = intel_overlay_release_old_vid(overlay);
865 if (ret != 0)
866 return ret;
867
Chris Wilson8d74f652010-08-12 10:35:26 +0100868 regs = intel_overlay_map_regs(overlay);
Ben Widawsky75020bc2012-04-16 14:07:43 -0700869 iowrite32(0, &regs->OCMD);
Chris Wilson9bb2ff72010-08-12 12:02:11 +0100870 intel_overlay_unmap_regs(overlay, regs);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200871
Chris Wilsonce453d82011-02-21 14:43:56 +0000872 ret = intel_overlay_off(overlay);
Daniel Vetter03f77ea2009-09-15 22:57:37 +0200873 if (ret != 0)
874 return ret;
875
Daniel Vetter12ca45f2037-04-25 10:08:26 +0200876 intel_overlay_off_tail(overlay);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200877 return 0;
878}
879
880static int check_overlay_possible_on_crtc(struct intel_overlay *overlay,
881 struct intel_crtc *crtc)
882{
Chris Wilsonf7abfe82010-09-13 14:19:16 +0100883 if (!crtc->active)
Daniel Vetter02e792f2009-09-15 22:57:34 +0200884 return -EINVAL;
885
Daniel Vetter02e792f2009-09-15 22:57:34 +0200886 /* can't use the overlay with double wide pipe */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +0200887 if (crtc->config->double_wide)
Daniel Vetter02e792f2009-09-15 22:57:34 +0200888 return -EINVAL;
889
890 return 0;
891}
892
893static void update_pfit_vscale_ratio(struct intel_overlay *overlay)
894{
Chris Wilson1ee8da62016-05-12 12:43:23 +0100895 struct drm_i915_private *dev_priv = overlay->i915;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200896 u32 pfit_control = I915_READ(PFIT_CONTROL);
Chris Wilson446d2182010-08-12 11:15:58 +0100897 u32 ratio;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200898
899 /* XXX: This is not the same logic as in the xorg driver, but more in
Chris Wilson446d2182010-08-12 11:15:58 +0100900 * line with the intel documentation for the i965
901 */
Chris Wilson1ee8da62016-05-12 12:43:23 +0100902 if (INTEL_GEN(dev_priv) >= 4) {
Akshay Joshi0206e352011-08-16 15:34:10 -0400903 /* on i965 use the PGM reg to read out the autoscaler values */
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100904 ratio = I915_READ(PFIT_PGM_RATIOS) >> PFIT_VERT_SCALE_SHIFT_965;
905 } else {
Chris Wilson446d2182010-08-12 11:15:58 +0100906 if (pfit_control & VERT_AUTO_SCALE)
907 ratio = I915_READ(PFIT_AUTO_RATIOS);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200908 else
Chris Wilson446d2182010-08-12 11:15:58 +0100909 ratio = I915_READ(PFIT_PGM_RATIOS);
910 ratio >>= PFIT_VERT_SCALE_SHIFT;
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
Daniel Vetter75c13992012-01-28 23:48:46 +0100921 if (rec->dst_x < mode->hdisplay &&
922 rec->dst_x + rec->dst_width <= mode->hdisplay &&
923 rec->dst_y < mode->vdisplay &&
924 rec->dst_y + rec->dst_height <= mode->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
Chris Wilson1ee8da62016-05-12 12:43:23 +0100945static int check_overlay_src(struct drm_i915_private *dev_priv,
Daniel Vetter02e792f2009-09-15 22:57:34 +0200946 struct drm_intel_overlay_put_image *rec,
Chris Wilson05394f32010-11-08 19:18:58 +0000947 struct drm_i915_gem_object *new_bo)
Daniel Vetter02e792f2009-09-15 22:57:34 +0200948{
Daniel Vetter02e792f2009-09-15 22:57:34 +0200949 int uv_hscale = uv_hsubsampling(rec->flags);
950 int uv_vscale = uv_vsubsampling(rec->flags);
Dan Carpenter8f28f542010-10-27 23:17:25 +0200951 u32 stride_mask;
952 int depth;
953 u32 tmp;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200954
955 /* check src dimensions */
Chris Wilson1ee8da62016-05-12 12:43:23 +0100956 if (IS_845G(dev_priv) || IS_I830(dev_priv)) {
Chris Wilson722506f2010-08-12 09:28:50 +0100957 if (rec->src_height > IMAGE_MAX_HEIGHT_LEGACY ||
Chris Wilson9f7c3f42010-08-12 11:29:34 +0100958 rec->src_width > IMAGE_MAX_WIDTH_LEGACY)
Daniel Vetter02e792f2009-09-15 22:57:34 +0200959 return -EINVAL;
960 } else {
Chris Wilson722506f2010-08-12 09:28:50 +0100961 if (rec->src_height > IMAGE_MAX_HEIGHT ||
Chris Wilson9f7c3f42010-08-12 11:29:34 +0100962 rec->src_width > IMAGE_MAX_WIDTH)
Daniel Vetter02e792f2009-09-15 22:57:34 +0200963 return -EINVAL;
964 }
Chris Wilson9f7c3f42010-08-12 11:29:34 +0100965
Daniel Vetter02e792f2009-09-15 22:57:34 +0200966 /* better safe than sorry, use 4 as the maximal subsampling ratio */
Chris Wilson722506f2010-08-12 09:28:50 +0100967 if (rec->src_height < N_VERT_Y_TAPS*4 ||
Chris Wilson9f7c3f42010-08-12 11:29:34 +0100968 rec->src_width < N_HORIZ_Y_TAPS*4)
Daniel Vetter02e792f2009-09-15 22:57:34 +0200969 return -EINVAL;
970
Chris Wilsona1efd142010-07-12 19:35:38 +0100971 /* check alignment constraints */
Daniel Vetter02e792f2009-09-15 22:57:34 +0200972 switch (rec->flags & I915_OVERLAY_TYPE_MASK) {
Chris Wilson722506f2010-08-12 09:28:50 +0100973 case I915_OVERLAY_RGB:
974 /* not implemented */
975 return -EINVAL;
Chris Wilson9f7c3f42010-08-12 11:29:34 +0100976
Chris Wilson722506f2010-08-12 09:28:50 +0100977 case I915_OVERLAY_YUV_PACKED:
Chris Wilson722506f2010-08-12 09:28:50 +0100978 if (uv_vscale != 1)
Daniel Vetter02e792f2009-09-15 22:57:34 +0200979 return -EINVAL;
Chris Wilson9f7c3f42010-08-12 11:29:34 +0100980
981 depth = packed_depth_bytes(rec->flags);
Chris Wilson722506f2010-08-12 09:28:50 +0100982 if (depth < 0)
983 return depth;
Chris Wilson9f7c3f42010-08-12 11:29:34 +0100984
Chris Wilson722506f2010-08-12 09:28:50 +0100985 /* ignore UV planes */
986 rec->stride_UV = 0;
987 rec->offset_U = 0;
988 rec->offset_V = 0;
989 /* check pixel alignment */
990 if (rec->offset_Y % depth)
Daniel Vetter02e792f2009-09-15 22:57:34 +0200991 return -EINVAL;
Chris Wilson722506f2010-08-12 09:28:50 +0100992 break;
Chris Wilson9f7c3f42010-08-12 11:29:34 +0100993
Chris Wilson722506f2010-08-12 09:28:50 +0100994 case I915_OVERLAY_YUV_PLANAR:
995 if (uv_vscale < 0 || uv_hscale < 0)
996 return -EINVAL;
997 /* no offset restrictions for planar formats */
998 break;
Chris Wilson9f7c3f42010-08-12 11:29:34 +0100999
Chris Wilson722506f2010-08-12 09:28:50 +01001000 default:
1001 return -EINVAL;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001002 }
1003
1004 if (rec->src_width % uv_hscale)
1005 return -EINVAL;
1006
1007 /* stride checking */
Chris Wilson1ee8da62016-05-12 12:43:23 +01001008 if (IS_I830(dev_priv) || IS_845G(dev_priv))
Chris Wilsona1efd142010-07-12 19:35:38 +01001009 stride_mask = 255;
1010 else
1011 stride_mask = 63;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001012
1013 if (rec->stride_Y & stride_mask || rec->stride_UV & stride_mask)
1014 return -EINVAL;
Chris Wilson1ee8da62016-05-12 12:43:23 +01001015 if (IS_GEN4(dev_priv) && rec->stride_Y < 512)
Daniel Vetter02e792f2009-09-15 22:57:34 +02001016 return -EINVAL;
1017
1018 tmp = (rec->flags & I915_OVERLAY_TYPE_MASK) == I915_OVERLAY_YUV_PLANAR ?
Chris Wilson9f7c3f42010-08-12 11:29:34 +01001019 4096 : 8192;
1020 if (rec->stride_Y > tmp || rec->stride_UV > 2*1024)
Daniel Vetter02e792f2009-09-15 22:57:34 +02001021 return -EINVAL;
1022
1023 /* check buffer dimensions */
1024 switch (rec->flags & I915_OVERLAY_TYPE_MASK) {
Chris Wilson722506f2010-08-12 09:28:50 +01001025 case I915_OVERLAY_RGB:
1026 case I915_OVERLAY_YUV_PACKED:
1027 /* always 4 Y values per depth pixels */
1028 if (packed_width_bytes(rec->flags, rec->src_width) > rec->stride_Y)
1029 return -EINVAL;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001030
Chris Wilson722506f2010-08-12 09:28:50 +01001031 tmp = rec->stride_Y*rec->src_height;
Chris Wilson05394f32010-11-08 19:18:58 +00001032 if (rec->offset_Y + tmp > new_bo->base.size)
Chris Wilson722506f2010-08-12 09:28:50 +01001033 return -EINVAL;
1034 break;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001035
Chris Wilson722506f2010-08-12 09:28:50 +01001036 case I915_OVERLAY_YUV_PLANAR:
1037 if (rec->src_width > rec->stride_Y)
1038 return -EINVAL;
1039 if (rec->src_width/uv_hscale > rec->stride_UV)
1040 return -EINVAL;
1041
Chris Wilson9f7c3f42010-08-12 11:29:34 +01001042 tmp = rec->stride_Y * rec->src_height;
Chris Wilson05394f32010-11-08 19:18:58 +00001043 if (rec->offset_Y + tmp > new_bo->base.size)
Chris Wilson722506f2010-08-12 09:28:50 +01001044 return -EINVAL;
Chris Wilson9f7c3f42010-08-12 11:29:34 +01001045
1046 tmp = rec->stride_UV * (rec->src_height / uv_vscale);
Chris Wilson05394f32010-11-08 19:18:58 +00001047 if (rec->offset_U + tmp > new_bo->base.size ||
1048 rec->offset_V + tmp > new_bo->base.size)
Chris Wilson722506f2010-08-12 09:28:50 +01001049 return -EINVAL;
1050 break;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001051 }
1052
1053 return 0;
1054}
1055
Chris Wilsone9e331a2010-09-13 01:16:10 +01001056/**
1057 * Return the pipe currently connected to the panel fitter,
1058 * or -1 if the panel fitter is not present or not in use
1059 */
Chris Wilson1ee8da62016-05-12 12:43:23 +01001060static int intel_panel_fitter_pipe(struct drm_i915_private *dev_priv)
Chris Wilsone9e331a2010-09-13 01:16:10 +01001061{
Chris Wilsone9e331a2010-09-13 01:16:10 +01001062 u32 pfit_control;
1063
1064 /* i830 doesn't have a panel fitter */
Chris Wilson1ee8da62016-05-12 12:43:23 +01001065 if (INTEL_GEN(dev_priv) <= 3 &&
1066 (IS_I830(dev_priv) || !IS_MOBILE(dev_priv)))
Chris Wilsone9e331a2010-09-13 01:16:10 +01001067 return -1;
1068
1069 pfit_control = I915_READ(PFIT_CONTROL);
1070
1071 /* See if the panel fitter is in use */
1072 if ((pfit_control & PFIT_ENABLE) == 0)
1073 return -1;
1074
1075 /* 965 can place panel fitter on either pipe */
Chris Wilson1ee8da62016-05-12 12:43:23 +01001076 if (IS_GEN4(dev_priv))
Chris Wilsone9e331a2010-09-13 01:16:10 +01001077 return (pfit_control >> 29) & 0x3;
1078
1079 /* older chips can only use pipe 1 */
1080 return 1;
1081}
1082
Chris Wilson1ee8da62016-05-12 12:43:23 +01001083int intel_overlay_put_image_ioctl(struct drm_device *dev, void *data,
1084 struct drm_file *file_priv)
Daniel Vetter02e792f2009-09-15 22:57:34 +02001085{
1086 struct drm_intel_overlay_put_image *put_image_rec = data;
Jani Nikulad5d45cc2014-03-31 14:27:20 +03001087 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001088 struct intel_overlay *overlay;
Rob Clark7707e652014-07-17 23:30:04 -04001089 struct drm_crtc *drmmode_crtc;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001090 struct intel_crtc *crtc;
Chris Wilson05394f32010-11-08 19:18:58 +00001091 struct drm_i915_gem_object *new_bo;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001092 struct put_image_params *params;
1093 int ret;
1094
Daniel Vetter02e792f2009-09-15 22:57:34 +02001095 overlay = dev_priv->overlay;
1096 if (!overlay) {
1097 DRM_DEBUG("userspace bug: no overlay\n");
1098 return -ENODEV;
1099 }
1100
1101 if (!(put_image_rec->flags & I915_OVERLAY_ENABLE)) {
Daniel Vettera0e99e62012-12-02 01:05:46 +01001102 drm_modeset_lock_all(dev);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001103 mutex_lock(&dev->struct_mutex);
1104
Chris Wilsonce453d82011-02-21 14:43:56 +00001105 ret = intel_overlay_switch_off(overlay);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001106
1107 mutex_unlock(&dev->struct_mutex);
Daniel Vettera0e99e62012-12-02 01:05:46 +01001108 drm_modeset_unlock_all(dev);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001109
1110 return ret;
1111 }
1112
Daniel Vetterb14c5672013-09-19 12:18:32 +02001113 params = kmalloc(sizeof(*params), GFP_KERNEL);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001114 if (!params)
1115 return -ENOMEM;
1116
Rob Clark7707e652014-07-17 23:30:04 -04001117 drmmode_crtc = drm_crtc_find(dev, put_image_rec->crtc_id);
1118 if (!drmmode_crtc) {
Dan Carpenter915a4282010-03-06 14:05:39 +03001119 ret = -ENOENT;
1120 goto out_free;
1121 }
Rob Clark7707e652014-07-17 23:30:04 -04001122 crtc = to_intel_crtc(drmmode_crtc);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001123
Chris Wilson05394f32010-11-08 19:18:58 +00001124 new_bo = to_intel_bo(drm_gem_object_lookup(dev, file_priv,
1125 put_image_rec->bo_handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00001126 if (&new_bo->base == NULL) {
Dan Carpenter915a4282010-03-06 14:05:39 +03001127 ret = -ENOENT;
1128 goto out_free;
1129 }
Daniel Vetter02e792f2009-09-15 22:57:34 +02001130
Daniel Vettera0e99e62012-12-02 01:05:46 +01001131 drm_modeset_lock_all(dev);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001132 mutex_lock(&dev->struct_mutex);
1133
Chris Wilsond9e86c02010-11-10 16:40:20 +00001134 if (new_bo->tiling_mode) {
Daniel Vetter3b25b312014-02-14 14:06:06 +01001135 DRM_DEBUG_KMS("buffer used for overlay image can not be tiled\n");
Chris Wilsond9e86c02010-11-10 16:40:20 +00001136 ret = -EINVAL;
1137 goto out_unlock;
1138 }
1139
Chris Wilsonce453d82011-02-21 14:43:56 +00001140 ret = intel_overlay_recover_from_interrupt(overlay);
Chris Wilsonb303cf92010-08-12 14:03:48 +01001141 if (ret != 0)
1142 goto out_unlock;
Daniel Vetter03f77ea2009-09-15 22:57:37 +02001143
Daniel Vetter02e792f2009-09-15 22:57:34 +02001144 if (overlay->crtc != crtc) {
1145 struct drm_display_mode *mode = &crtc->base.mode;
Chris Wilsonce453d82011-02-21 14:43:56 +00001146 ret = intel_overlay_switch_off(overlay);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001147 if (ret != 0)
1148 goto out_unlock;
1149
1150 ret = check_overlay_possible_on_crtc(overlay, crtc);
1151 if (ret != 0)
1152 goto out_unlock;
1153
1154 overlay->crtc = crtc;
1155 crtc->overlay = overlay;
1156
Chris Wilsone9e331a2010-09-13 01:16:10 +01001157 /* line too wide, i.e. one-line-mode */
1158 if (mode->hdisplay > 1024 &&
Chris Wilson1ee8da62016-05-12 12:43:23 +01001159 intel_panel_fitter_pipe(dev_priv) == crtc->pipe) {
Ville Syrjälä209c2a52015-03-31 10:37:23 +03001160 overlay->pfit_active = true;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001161 update_pfit_vscale_ratio(overlay);
1162 } else
Ville Syrjälä209c2a52015-03-31 10:37:23 +03001163 overlay->pfit_active = false;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001164 }
1165
1166 ret = check_overlay_dst(overlay, put_image_rec);
1167 if (ret != 0)
1168 goto out_unlock;
1169
1170 if (overlay->pfit_active) {
1171 params->dst_y = ((((u32)put_image_rec->dst_y) << 12) /
Chris Wilson722506f2010-08-12 09:28:50 +01001172 overlay->pfit_vscale_ratio);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001173 /* shifting right rounds downwards, so add 1 */
1174 params->dst_h = ((((u32)put_image_rec->dst_height) << 12) /
Chris Wilson722506f2010-08-12 09:28:50 +01001175 overlay->pfit_vscale_ratio) + 1;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001176 } else {
1177 params->dst_y = put_image_rec->dst_y;
1178 params->dst_h = put_image_rec->dst_height;
1179 }
1180 params->dst_x = put_image_rec->dst_x;
1181 params->dst_w = put_image_rec->dst_width;
1182
1183 params->src_w = put_image_rec->src_width;
1184 params->src_h = put_image_rec->src_height;
1185 params->src_scan_w = put_image_rec->src_scan_width;
1186 params->src_scan_h = put_image_rec->src_scan_height;
Chris Wilson722506f2010-08-12 09:28:50 +01001187 if (params->src_scan_h > params->src_h ||
1188 params->src_scan_w > params->src_w) {
Daniel Vetter02e792f2009-09-15 22:57:34 +02001189 ret = -EINVAL;
1190 goto out_unlock;
1191 }
1192
Chris Wilson1ee8da62016-05-12 12:43:23 +01001193 ret = check_overlay_src(dev_priv, put_image_rec, new_bo);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001194 if (ret != 0)
1195 goto out_unlock;
1196 params->format = put_image_rec->flags & ~I915_OVERLAY_FLAGS_MASK;
1197 params->stride_Y = put_image_rec->stride_Y;
1198 params->stride_UV = put_image_rec->stride_UV;
1199 params->offset_Y = put_image_rec->offset_Y;
1200 params->offset_U = put_image_rec->offset_U;
1201 params->offset_V = put_image_rec->offset_V;
1202
1203 /* Check scaling after src size to prevent a divide-by-zero. */
1204 ret = check_overlay_scaling(params);
1205 if (ret != 0)
1206 goto out_unlock;
1207
1208 ret = intel_overlay_do_put_image(overlay, new_bo, params);
1209 if (ret != 0)
1210 goto out_unlock;
1211
1212 mutex_unlock(&dev->struct_mutex);
Daniel Vettera0e99e62012-12-02 01:05:46 +01001213 drm_modeset_unlock_all(dev);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001214
1215 kfree(params);
1216
1217 return 0;
1218
1219out_unlock:
1220 mutex_unlock(&dev->struct_mutex);
Daniel Vettera0e99e62012-12-02 01:05:46 +01001221 drm_modeset_unlock_all(dev);
Chris Wilson05394f32010-11-08 19:18:58 +00001222 drm_gem_object_unreference_unlocked(&new_bo->base);
Dan Carpenter915a4282010-03-06 14:05:39 +03001223out_free:
Daniel Vetter02e792f2009-09-15 22:57:34 +02001224 kfree(params);
1225
1226 return ret;
1227}
1228
1229static void update_reg_attrs(struct intel_overlay *overlay,
Ben Widawsky75020bc2012-04-16 14:07:43 -07001230 struct overlay_registers __iomem *regs)
Daniel Vetter02e792f2009-09-15 22:57:34 +02001231{
Ben Widawsky75020bc2012-04-16 14:07:43 -07001232 iowrite32((overlay->contrast << 18) | (overlay->brightness & 0xff),
1233 &regs->OCLRC0);
1234 iowrite32(overlay->saturation, &regs->OCLRC1);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001235}
1236
1237static bool check_gamma_bounds(u32 gamma1, u32 gamma2)
1238{
1239 int i;
1240
1241 if (gamma1 & 0xff000000 || gamma2 & 0xff000000)
1242 return false;
1243
1244 for (i = 0; i < 3; i++) {
Chris Wilson722506f2010-08-12 09:28:50 +01001245 if (((gamma1 >> i*8) & 0xff) >= ((gamma2 >> i*8) & 0xff))
Daniel Vetter02e792f2009-09-15 22:57:34 +02001246 return false;
1247 }
1248
1249 return true;
1250}
1251
1252static bool check_gamma5_errata(u32 gamma5)
1253{
1254 int i;
1255
1256 for (i = 0; i < 3; i++) {
1257 if (((gamma5 >> i*8) & 0xff) == 0x80)
1258 return false;
1259 }
1260
1261 return true;
1262}
1263
1264static int check_gamma(struct drm_intel_overlay_attrs *attrs)
1265{
Chris Wilson722506f2010-08-12 09:28:50 +01001266 if (!check_gamma_bounds(0, attrs->gamma0) ||
1267 !check_gamma_bounds(attrs->gamma0, attrs->gamma1) ||
1268 !check_gamma_bounds(attrs->gamma1, attrs->gamma2) ||
1269 !check_gamma_bounds(attrs->gamma2, attrs->gamma3) ||
1270 !check_gamma_bounds(attrs->gamma3, attrs->gamma4) ||
1271 !check_gamma_bounds(attrs->gamma4, attrs->gamma5) ||
1272 !check_gamma_bounds(attrs->gamma5, 0x00ffffff))
Daniel Vetter02e792f2009-09-15 22:57:34 +02001273 return -EINVAL;
Chris Wilson722506f2010-08-12 09:28:50 +01001274
Daniel Vetter02e792f2009-09-15 22:57:34 +02001275 if (!check_gamma5_errata(attrs->gamma5))
1276 return -EINVAL;
Chris Wilson722506f2010-08-12 09:28:50 +01001277
Daniel Vetter02e792f2009-09-15 22:57:34 +02001278 return 0;
1279}
1280
Chris Wilson1ee8da62016-05-12 12:43:23 +01001281int intel_overlay_attrs_ioctl(struct drm_device *dev, void *data,
1282 struct drm_file *file_priv)
Daniel Vetter02e792f2009-09-15 22:57:34 +02001283{
1284 struct drm_intel_overlay_attrs *attrs = data;
Jani Nikulad5d45cc2014-03-31 14:27:20 +03001285 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001286 struct intel_overlay *overlay;
Ben Widawsky75020bc2012-04-16 14:07:43 -07001287 struct overlay_registers __iomem *regs;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001288 int ret;
1289
Daniel Vetter02e792f2009-09-15 22:57:34 +02001290 overlay = dev_priv->overlay;
1291 if (!overlay) {
1292 DRM_DEBUG("userspace bug: no overlay\n");
1293 return -ENODEV;
1294 }
1295
Daniel Vettera0e99e62012-12-02 01:05:46 +01001296 drm_modeset_lock_all(dev);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001297 mutex_lock(&dev->struct_mutex);
1298
Chris Wilson60fc3322010-08-12 10:44:45 +01001299 ret = -EINVAL;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001300 if (!(attrs->flags & I915_OVERLAY_UPDATE_ATTRS)) {
Chris Wilson60fc3322010-08-12 10:44:45 +01001301 attrs->color_key = overlay->color_key;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001302 attrs->brightness = overlay->brightness;
Chris Wilson60fc3322010-08-12 10:44:45 +01001303 attrs->contrast = overlay->contrast;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001304 attrs->saturation = overlay->saturation;
1305
Chris Wilson1ee8da62016-05-12 12:43:23 +01001306 if (!IS_GEN2(dev_priv)) {
Daniel Vetter02e792f2009-09-15 22:57:34 +02001307 attrs->gamma0 = I915_READ(OGAMC0);
1308 attrs->gamma1 = I915_READ(OGAMC1);
1309 attrs->gamma2 = I915_READ(OGAMC2);
1310 attrs->gamma3 = I915_READ(OGAMC3);
1311 attrs->gamma4 = I915_READ(OGAMC4);
1312 attrs->gamma5 = I915_READ(OGAMC5);
1313 }
Daniel Vetter02e792f2009-09-15 22:57:34 +02001314 } else {
Chris Wilson60fc3322010-08-12 10:44:45 +01001315 if (attrs->brightness < -128 || attrs->brightness > 127)
Daniel Vetter02e792f2009-09-15 22:57:34 +02001316 goto out_unlock;
Chris Wilson60fc3322010-08-12 10:44:45 +01001317 if (attrs->contrast > 255)
Daniel Vetter02e792f2009-09-15 22:57:34 +02001318 goto out_unlock;
Chris Wilson60fc3322010-08-12 10:44:45 +01001319 if (attrs->saturation > 1023)
Daniel Vetter02e792f2009-09-15 22:57:34 +02001320 goto out_unlock;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001321
Chris Wilson60fc3322010-08-12 10:44:45 +01001322 overlay->color_key = attrs->color_key;
1323 overlay->brightness = attrs->brightness;
1324 overlay->contrast = attrs->contrast;
1325 overlay->saturation = attrs->saturation;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001326
Chris Wilson8d74f652010-08-12 10:35:26 +01001327 regs = intel_overlay_map_regs(overlay);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001328 if (!regs) {
1329 ret = -ENOMEM;
1330 goto out_unlock;
1331 }
1332
1333 update_reg_attrs(overlay, regs);
1334
Chris Wilson9bb2ff72010-08-12 12:02:11 +01001335 intel_overlay_unmap_regs(overlay, regs);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001336
1337 if (attrs->flags & I915_OVERLAY_UPDATE_GAMMA) {
Chris Wilson1ee8da62016-05-12 12:43:23 +01001338 if (IS_GEN2(dev_priv))
Daniel Vetter02e792f2009-09-15 22:57:34 +02001339 goto out_unlock;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001340
1341 if (overlay->active) {
1342 ret = -EBUSY;
1343 goto out_unlock;
1344 }
1345
1346 ret = check_gamma(attrs);
Chris Wilson60fc3322010-08-12 10:44:45 +01001347 if (ret)
Daniel Vetter02e792f2009-09-15 22:57:34 +02001348 goto out_unlock;
1349
1350 I915_WRITE(OGAMC0, attrs->gamma0);
1351 I915_WRITE(OGAMC1, attrs->gamma1);
1352 I915_WRITE(OGAMC2, attrs->gamma2);
1353 I915_WRITE(OGAMC3, attrs->gamma3);
1354 I915_WRITE(OGAMC4, attrs->gamma4);
1355 I915_WRITE(OGAMC5, attrs->gamma5);
1356 }
Daniel Vetter02e792f2009-09-15 22:57:34 +02001357 }
Chris Wilsonea9da4e2015-04-02 10:35:08 +01001358 overlay->color_key_enabled = (attrs->flags & I915_OVERLAY_DISABLE_DEST_COLORKEY) == 0;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001359
Chris Wilson60fc3322010-08-12 10:44:45 +01001360 ret = 0;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001361out_unlock:
1362 mutex_unlock(&dev->struct_mutex);
Daniel Vettera0e99e62012-12-02 01:05:46 +01001363 drm_modeset_unlock_all(dev);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001364
1365 return ret;
1366}
1367
Chris Wilson1ee8da62016-05-12 12:43:23 +01001368void intel_setup_overlay(struct drm_i915_private *dev_priv)
Daniel Vetter02e792f2009-09-15 22:57:34 +02001369{
Daniel Vetter02e792f2009-09-15 22:57:34 +02001370 struct intel_overlay *overlay;
Chris Wilson05394f32010-11-08 19:18:58 +00001371 struct drm_i915_gem_object *reg_bo;
Ben Widawsky75020bc2012-04-16 14:07:43 -07001372 struct overlay_registers __iomem *regs;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001373 int ret;
1374
Chris Wilson1ee8da62016-05-12 12:43:23 +01001375 if (!HAS_OVERLAY(dev_priv))
Daniel Vetter02e792f2009-09-15 22:57:34 +02001376 return;
1377
Daniel Vetterb14c5672013-09-19 12:18:32 +02001378 overlay = kzalloc(sizeof(*overlay), GFP_KERNEL);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001379 if (!overlay)
1380 return;
Chris Wilson79d24272011-06-28 11:27:47 +01001381
Chris Wilson1ee8da62016-05-12 12:43:23 +01001382 mutex_lock(&dev_priv->dev->struct_mutex);
Chris Wilson79d24272011-06-28 11:27:47 +01001383 if (WARN_ON(dev_priv->overlay))
1384 goto out_free;
1385
Chris Wilson1ee8da62016-05-12 12:43:23 +01001386 overlay->i915 = dev_priv;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001387
Daniel Vetterf63a4842013-07-23 19:24:38 +02001388 reg_bo = NULL;
Chris Wilson1ee8da62016-05-12 12:43:23 +01001389 if (!OVERLAY_NEEDS_PHYSICAL(dev_priv))
1390 reg_bo = i915_gem_object_create_stolen(dev_priv->dev, PAGE_SIZE);
Chris Wilson80405132012-11-15 11:32:29 +00001391 if (reg_bo == NULL)
Chris Wilson1ee8da62016-05-12 12:43:23 +01001392 reg_bo = i915_gem_object_create(dev_priv->dev, PAGE_SIZE);
Chris Wilsonfe3db792016-04-25 13:32:13 +01001393 if (IS_ERR(reg_bo))
Daniel Vetter02e792f2009-09-15 22:57:34 +02001394 goto out_free;
Chris Wilson05394f32010-11-08 19:18:58 +00001395 overlay->reg_bo = reg_bo;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001396
Chris Wilson1ee8da62016-05-12 12:43:23 +01001397 if (OVERLAY_NEEDS_PHYSICAL(dev_priv)) {
Chris Wilson00731152014-05-21 12:42:56 +01001398 ret = i915_gem_object_attach_phys(reg_bo, PAGE_SIZE);
Akshay Joshi0206e352011-08-16 15:34:10 -04001399 if (ret) {
1400 DRM_ERROR("failed to attach phys overlay regs\n");
1401 goto out_free_bo;
1402 }
Chris Wilson00731152014-05-21 12:42:56 +01001403 overlay->flip_addr = reg_bo->phys_handle->busaddr;
Chris Wilson315781482010-08-12 09:42:51 +01001404 } else {
Daniel Vetter1ec9e262014-02-14 14:01:11 +01001405 ret = i915_gem_obj_ggtt_pin(reg_bo, PAGE_SIZE, PIN_MAPPABLE);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001406 if (ret) {
Akshay Joshi0206e352011-08-16 15:34:10 -04001407 DRM_ERROR("failed to pin overlay register bo\n");
1408 goto out_free_bo;
1409 }
Ben Widawskyf343c5f2013-07-05 14:41:04 -07001410 overlay->flip_addr = i915_gem_obj_ggtt_offset(reg_bo);
Chris Wilson0ddc1282010-08-12 09:35:00 +01001411
1412 ret = i915_gem_object_set_to_gtt_domain(reg_bo, true);
1413 if (ret) {
Akshay Joshi0206e352011-08-16 15:34:10 -04001414 DRM_ERROR("failed to move overlay register bo into the GTT\n");
1415 goto out_unpin_bo;
1416 }
Daniel Vetter02e792f2009-09-15 22:57:34 +02001417 }
1418
1419 /* init all values */
1420 overlay->color_key = 0x0101fe;
Chris Wilsonea9da4e2015-04-02 10:35:08 +01001421 overlay->color_key_enabled = true;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001422 overlay->brightness = -19;
1423 overlay->contrast = 75;
1424 overlay->saturation = 146;
1425
Chris Wilson8d74f652010-08-12 10:35:26 +01001426 regs = intel_overlay_map_regs(overlay);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001427 if (!regs)
Chris Wilson79d24272011-06-28 11:27:47 +01001428 goto out_unpin_bo;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001429
Ben Widawsky75020bc2012-04-16 14:07:43 -07001430 memset_io(regs, 0, sizeof(struct overlay_registers));
Daniel Vetter02e792f2009-09-15 22:57:34 +02001431 update_polyphase_filter(regs);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001432 update_reg_attrs(overlay, regs);
1433
Chris Wilson9bb2ff72010-08-12 12:02:11 +01001434 intel_overlay_unmap_regs(overlay, regs);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001435
1436 dev_priv->overlay = overlay;
Chris Wilson1ee8da62016-05-12 12:43:23 +01001437 mutex_unlock(&dev_priv->dev->struct_mutex);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001438 DRM_INFO("initialized overlay support\n");
1439 return;
1440
Chris Wilson0ddc1282010-08-12 09:35:00 +01001441out_unpin_bo:
Chris Wilson1ee8da62016-05-12 12:43:23 +01001442 if (!OVERLAY_NEEDS_PHYSICAL(dev_priv))
Ben Widawskyd7f46fc2013-12-06 14:10:55 -08001443 i915_gem_object_ggtt_unpin(reg_bo);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001444out_free_bo:
Chris Wilson05394f32010-11-08 19:18:58 +00001445 drm_gem_object_unreference(&reg_bo->base);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001446out_free:
Chris Wilson1ee8da62016-05-12 12:43:23 +01001447 mutex_unlock(&dev_priv->dev->struct_mutex);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001448 kfree(overlay);
1449 return;
1450}
1451
Chris Wilson1ee8da62016-05-12 12:43:23 +01001452void intel_cleanup_overlay(struct drm_i915_private *dev_priv)
Daniel Vetter02e792f2009-09-15 22:57:34 +02001453{
Chris Wilson62cf4e62010-08-12 10:50:36 +01001454 if (!dev_priv->overlay)
1455 return;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001456
Chris Wilson62cf4e62010-08-12 10:50:36 +01001457 /* The bo's should be free'd by the generic code already.
1458 * Furthermore modesetting teardown happens beforehand so the
1459 * hardware should be off already */
Ville Syrjälä77589f52015-03-31 10:37:22 +03001460 WARN_ON(dev_priv->overlay->active);
Chris Wilson62cf4e62010-08-12 10:50:36 +01001461
1462 drm_gem_object_unreference_unlocked(&dev_priv->overlay->reg_bo->base);
1463 kfree(dev_priv->overlay);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001464}
Chris Wilson6ef3d422010-08-04 20:26:07 +01001465
1466struct intel_overlay_error_state {
1467 struct overlay_registers regs;
1468 unsigned long base;
1469 u32 dovsta;
1470 u32 isr;
1471};
1472
Ben Widawsky75020bc2012-04-16 14:07:43 -07001473static struct overlay_registers __iomem *
Linus Torvaldsc48c43e2010-10-26 18:57:59 -07001474intel_overlay_map_regs_atomic(struct intel_overlay *overlay)
Chris Wilson3bd3c932010-08-19 08:19:30 +01001475{
Chris Wilson1ee8da62016-05-12 12:43:23 +01001476 struct drm_i915_private *dev_priv = overlay->i915;
Ben Widawsky75020bc2012-04-16 14:07:43 -07001477 struct overlay_registers __iomem *regs;
Chris Wilson3bd3c932010-08-19 08:19:30 +01001478
Chris Wilson1ee8da62016-05-12 12:43:23 +01001479 if (OVERLAY_NEEDS_PHYSICAL(dev_priv))
Ben Widawsky75020bc2012-04-16 14:07:43 -07001480 /* Cast to make sparse happy, but it's wc memory anyway, so
1481 * equivalent to the wc io mapping on X86. */
1482 regs = (struct overlay_registers __iomem *)
Chris Wilson00731152014-05-21 12:42:56 +01001483 overlay->reg_bo->phys_handle->vaddr;
Chris Wilson3bd3c932010-08-19 08:19:30 +01001484 else
Chris Wilson1ee8da62016-05-12 12:43:23 +01001485 regs = io_mapping_map_atomic_wc(dev_priv->ggtt.mappable,
Chris Wilsonda6ca032016-04-28 09:56:36 +01001486 overlay->flip_addr);
Chris Wilson3bd3c932010-08-19 08:19:30 +01001487
1488 return regs;
1489}
1490
1491static void intel_overlay_unmap_regs_atomic(struct intel_overlay *overlay,
Ben Widawsky75020bc2012-04-16 14:07:43 -07001492 struct overlay_registers __iomem *regs)
Chris Wilson3bd3c932010-08-19 08:19:30 +01001493{
Chris Wilson1ee8da62016-05-12 12:43:23 +01001494 if (!OVERLAY_NEEDS_PHYSICAL(overlay->i915))
Linus Torvaldsc48c43e2010-10-26 18:57:59 -07001495 io_mapping_unmap_atomic(regs);
Chris Wilson3bd3c932010-08-19 08:19:30 +01001496}
1497
Chris Wilson6ef3d422010-08-04 20:26:07 +01001498struct intel_overlay_error_state *
Chris Wilsonc0336662016-05-06 15:40:21 +01001499intel_overlay_capture_error_state(struct drm_i915_private *dev_priv)
Chris Wilson6ef3d422010-08-04 20:26:07 +01001500{
Chris Wilson6ef3d422010-08-04 20:26:07 +01001501 struct intel_overlay *overlay = dev_priv->overlay;
1502 struct intel_overlay_error_state *error;
1503 struct overlay_registers __iomem *regs;
1504
1505 if (!overlay || !overlay->active)
1506 return NULL;
1507
1508 error = kmalloc(sizeof(*error), GFP_ATOMIC);
1509 if (error == NULL)
1510 return NULL;
1511
1512 error->dovsta = I915_READ(DOVSTA);
1513 error->isr = I915_READ(ISR);
Chris Wilsonda6ca032016-04-28 09:56:36 +01001514 error->base = overlay->flip_addr;
Chris Wilson6ef3d422010-08-04 20:26:07 +01001515
1516 regs = intel_overlay_map_regs_atomic(overlay);
1517 if (!regs)
1518 goto err;
1519
1520 memcpy_fromio(&error->regs, regs, sizeof(struct overlay_registers));
Linus Torvaldsc48c43e2010-10-26 18:57:59 -07001521 intel_overlay_unmap_regs_atomic(overlay, regs);
Chris Wilson6ef3d422010-08-04 20:26:07 +01001522
1523 return error;
1524
1525err:
1526 kfree(error);
1527 return NULL;
1528}
1529
1530void
Mika Kuoppalaedc3d882013-05-23 13:55:35 +03001531intel_overlay_print_error_state(struct drm_i915_error_state_buf *m,
1532 struct intel_overlay_error_state *error)
Chris Wilson6ef3d422010-08-04 20:26:07 +01001533{
Mika Kuoppalaedc3d882013-05-23 13:55:35 +03001534 i915_error_printf(m, "Overlay, status: 0x%08x, interrupt: 0x%08x\n",
1535 error->dovsta, error->isr);
1536 i915_error_printf(m, " Register file at 0x%08lx:\n",
1537 error->base);
Chris Wilson6ef3d422010-08-04 20:26:07 +01001538
Mika Kuoppalaedc3d882013-05-23 13:55:35 +03001539#define P(x) i915_error_printf(m, " " #x ": 0x%08x\n", error->regs.x)
Chris Wilson6ef3d422010-08-04 20:26:07 +01001540 P(OBUF_0Y);
1541 P(OBUF_1Y);
1542 P(OBUF_0U);
1543 P(OBUF_0V);
1544 P(OBUF_1U);
1545 P(OBUF_1V);
1546 P(OSTRIDE);
1547 P(YRGB_VPH);
1548 P(UV_VPH);
1549 P(HORZ_PH);
1550 P(INIT_PHS);
1551 P(DWINPOS);
1552 P(DWINSZ);
1553 P(SWIDTH);
1554 P(SWIDTHSW);
1555 P(SHEIGHT);
1556 P(YRGBSCALE);
1557 P(UVSCALE);
1558 P(OCLRC0);
1559 P(OCLRC1);
1560 P(DCLRKV);
1561 P(DCLRKM);
1562 P(SCLRKVH);
1563 P(SCLRKVL);
1564 P(SCLRKEN);
1565 P(OCONFIG);
1566 P(OCMD);
1567 P(OSTART_0Y);
1568 P(OSTART_1Y);
1569 P(OSTART_0U);
1570 P(OSTART_0V);
1571 P(OSTART_1U);
1572 P(OSTART_1V);
1573 P(OTILEOFF_0Y);
1574 P(OTILEOFF_1Y);
1575 P(OTILEOFF_0U);
1576 P(OTILEOFF_0V);
1577 P(OTILEOFF_1U);
1578 P(OTILEOFF_1V);
1579 P(FASTHSCALE);
1580 P(UVSCALEV);
1581#undef P
1582}