blob: 8f1d4d9ef345758776c0d1d998b9f52ec4bce675 [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;
Chris Wilson7e37f882016-08-02 22:50:21 +0100238 struct intel_ring *ring;
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);
Chris Wilson1ee8da62016-05-12 12:43:23 +0100242 WARN_ON(IS_I830(dev_priv) && !(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) {
Chris Wilsonaa9b7812016-04-13 17:35:15 +0100250 i915_add_request_no_flush(req);
John Harrisondad540c2015-05-29 17:43:47 +0100251 return ret;
252 }
253
Ville Syrjälä1c7c4302015-03-31 10:37:24 +0300254 overlay->active = true;
255
Chris Wilson1dae2df2016-08-02 22:50:19 +0100256 ring = req->ring;
Chris Wilsonb5321f32016-08-02 22:50:18 +0100257 intel_ring_emit(ring, MI_OVERLAY_FLIP | MI_OVERLAY_ON);
258 intel_ring_emit(ring, overlay->flip_addr | OFC_UPDATE);
259 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | MI_WAIT_FOR_OVERLAY_FLIP);
260 intel_ring_emit(ring, MI_NOOP);
261 intel_ring_advance(ring);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200262
John Harrisondad540c2015-05-29 17:43:47 +0100263 return intel_overlay_do_wait_request(overlay, req, NULL);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200264}
265
266/* overlay needs to be enabled in OCMD reg */
Chris Wilson8dc5d142010-08-12 12:36:12 +0100267static int intel_overlay_continue(struct intel_overlay *overlay,
268 bool load_polyphase_filter)
Daniel Vetter02e792f2009-09-15 22:57:34 +0200269{
Chris Wilson1ee8da62016-05-12 12:43:23 +0100270 struct drm_i915_private *dev_priv = overlay->i915;
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;
Chris Wilson7e37f882016-08-02 22:50:21 +0100273 struct intel_ring *ring;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200274 u32 flip_addr = overlay->flip_addr;
275 u32 tmp;
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100276 int ret;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200277
Ville Syrjälä77589f52015-03-31 10:37:22 +0300278 WARN_ON(!overlay->active);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200279
280 if (load_polyphase_filter)
281 flip_addr |= OFC_UPDATE;
282
283 /* check for underruns */
284 tmp = I915_READ(DOVSTA);
285 if (tmp & (1 << 17))
286 DRM_DEBUG("overlay underrun, DOVSTA: %x\n", tmp);
287
Tvrtko Ursuline2f80392016-03-16 11:00:36 +0000288 req = i915_gem_request_alloc(engine, NULL);
Dave Gordon26827082016-01-19 19:02:53 +0000289 if (IS_ERR(req))
290 return PTR_ERR(req);
Chris Wilsonacb868d2012-09-26 13:47:30 +0100291
John Harrison5fb9de12015-05-29 17:44:07 +0100292 ret = intel_ring_begin(req, 2);
John Harrisondad540c2015-05-29 17:43:47 +0100293 if (ret) {
Chris Wilsonaa9b7812016-04-13 17:35:15 +0100294 i915_add_request_no_flush(req);
John Harrisondad540c2015-05-29 17:43:47 +0100295 return ret;
296 }
297
Chris Wilson1dae2df2016-08-02 22:50:19 +0100298 ring = req->ring;
Chris Wilsonb5321f32016-08-02 22:50:18 +0100299 intel_ring_emit(ring, MI_OVERLAY_FLIP | MI_OVERLAY_CONTINUE);
300 intel_ring_emit(ring, flip_addr);
301 intel_ring_advance(ring);
Daniel Vetter5a5a0c62009-09-15 22:57:36 +0200302
John Harrison9bfc01a2014-11-24 18:49:31 +0000303 WARN_ON(overlay->last_flip_req);
John Harrisondad540c2015-05-29 17:43:47 +0100304 i915_gem_request_assign(&overlay->last_flip_req, req);
John Harrison75289872015-05-29 17:43:49 +0100305 i915_add_request(req);
John Harrisonbf7dc5b2015-05-29 17:43:24 +0100306
307 return 0;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200308}
309
Chris Wilsonb303cf92010-08-12 14:03:48 +0100310static void intel_overlay_release_old_vid_tail(struct intel_overlay *overlay)
Daniel Vetter02e792f2009-09-15 22:57:34 +0200311{
Chris Wilson05394f32010-11-08 19:18:58 +0000312 struct drm_i915_gem_object *obj = overlay->old_vid_bo;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200313
Ben Widawskyd7f46fc2013-12-06 14:10:55 -0800314 i915_gem_object_ggtt_unpin(obj);
Chris Wilsonf8c417c2016-07-20 13:31:53 +0100315 i915_gem_object_put(obj);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200316
Chris Wilsonb303cf92010-08-12 14:03:48 +0100317 overlay->old_vid_bo = NULL;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200318}
319
Daniel Vetter12ca45f2037-04-25 10:08:26 +0200320static void intel_overlay_off_tail(struct intel_overlay *overlay)
321{
Chris Wilson05394f32010-11-08 19:18:58 +0000322 struct drm_i915_gem_object *obj = overlay->vid_bo;
Daniel Vetter12ca45f2037-04-25 10:08:26 +0200323
324 /* never have the overlay hw on without showing a frame */
Ville Syrjälä77589f52015-03-31 10:37:22 +0300325 if (WARN_ON(!obj))
326 return;
Daniel Vetter12ca45f2037-04-25 10:08:26 +0200327
Ben Widawskyd7f46fc2013-12-06 14:10:55 -0800328 i915_gem_object_ggtt_unpin(obj);
Chris Wilsonf8c417c2016-07-20 13:31:53 +0100329 i915_gem_object_put(obj);
Daniel Vetter12ca45f2037-04-25 10:08:26 +0200330 overlay->vid_bo = NULL;
331
332 overlay->crtc->overlay = NULL;
333 overlay->crtc = NULL;
Ville Syrjälä209c2a52015-03-31 10:37:23 +0300334 overlay->active = false;
Daniel Vetter12ca45f2037-04-25 10:08:26 +0200335}
336
Daniel Vetter02e792f2009-09-15 22:57:34 +0200337/* overlay needs to be disabled in OCMD reg */
Chris Wilsonce453d82011-02-21 14:43:56 +0000338static int intel_overlay_off(struct intel_overlay *overlay)
Daniel Vetter03f77ea2009-09-15 22:57:37 +0200339{
Chris Wilson1ee8da62016-05-12 12:43:23 +0100340 struct drm_i915_private *dev_priv = overlay->i915;
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +0000341 struct intel_engine_cs *engine = &dev_priv->engine[RCS];
John Harrisondad540c2015-05-29 17:43:47 +0100342 struct drm_i915_gem_request *req;
Chris Wilson7e37f882016-08-02 22:50:21 +0100343 struct intel_ring *ring;
Chris Wilson8dc5d142010-08-12 12:36:12 +0100344 u32 flip_addr = overlay->flip_addr;
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100345 int ret;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200346
Ville Syrjälä77589f52015-03-31 10:37:22 +0300347 WARN_ON(!overlay->active);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200348
349 /* According to intel docs the overlay hw may hang (when switching
350 * off) without loading the filter coeffs. It is however unclear whether
351 * this applies to the disabling of the overlay or to the switching off
352 * of the hw. Do it in both cases */
353 flip_addr |= OFC_UPDATE;
354
Tvrtko Ursuline2f80392016-03-16 11:00:36 +0000355 req = i915_gem_request_alloc(engine, NULL);
Dave Gordon26827082016-01-19 19:02:53 +0000356 if (IS_ERR(req))
357 return PTR_ERR(req);
Chris Wilsonacb868d2012-09-26 13:47:30 +0100358
John Harrison5fb9de12015-05-29 17:44:07 +0100359 ret = intel_ring_begin(req, 6);
John Harrisondad540c2015-05-29 17:43:47 +0100360 if (ret) {
Chris Wilsonaa9b7812016-04-13 17:35:15 +0100361 i915_add_request_no_flush(req);
John Harrisondad540c2015-05-29 17:43:47 +0100362 return ret;
363 }
364
Chris Wilson1dae2df2016-08-02 22:50:19 +0100365 ring = req->ring;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200366 /* wait for overlay to go idle */
Chris Wilsonb5321f32016-08-02 22:50:18 +0100367 intel_ring_emit(ring, MI_OVERLAY_FLIP | MI_OVERLAY_CONTINUE);
368 intel_ring_emit(ring, flip_addr);
369 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | MI_WAIT_FOR_OVERLAY_FLIP);
Chris Wilson722506f2010-08-12 09:28:50 +0100370 /* turn overlay off */
Chris Wilson1ee8da62016-05-12 12:43:23 +0100371 if (IS_I830(dev_priv)) {
Daniel Vettera9193982012-10-22 12:55:55 +0200372 /* Workaround: Don't disable the overlay fully, since otherwise
373 * it dies on the next OVERLAY_ON cmd. */
Chris Wilsonb5321f32016-08-02 22:50:18 +0100374 intel_ring_emit(ring, MI_NOOP);
375 intel_ring_emit(ring, MI_NOOP);
376 intel_ring_emit(ring, MI_NOOP);
Daniel Vettera9193982012-10-22 12:55:55 +0200377 } else {
Chris Wilsonb5321f32016-08-02 22:50:18 +0100378 intel_ring_emit(ring, MI_OVERLAY_FLIP | MI_OVERLAY_OFF);
379 intel_ring_emit(ring, flip_addr);
380 intel_ring_emit(ring,
Tvrtko Ursuline2f80392016-03-16 11:00:36 +0000381 MI_WAIT_FOR_EVENT | MI_WAIT_FOR_OVERLAY_FLIP);
Daniel Vettera9193982012-10-22 12:55:55 +0200382 }
Chris Wilsonb5321f32016-08-02 22:50:18 +0100383 intel_ring_advance(ring);
Chris Wilson722506f2010-08-12 09:28:50 +0100384
John Harrisondad540c2015-05-29 17:43:47 +0100385 return intel_overlay_do_wait_request(overlay, req, intel_overlay_off_tail);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200386}
387
Daniel Vetter03f77ea2009-09-15 22:57:37 +0200388/* recover from an interruption due to a signal
389 * We have to be careful not to repeat work forever an make forward progess. */
Chris Wilsonce453d82011-02-21 14:43:56 +0000390static int intel_overlay_recover_from_interrupt(struct intel_overlay *overlay)
Daniel Vetter03f77ea2009-09-15 22:57:37 +0200391{
Daniel Vetter03f77ea2009-09-15 22:57:37 +0200392 int ret;
Daniel Vetter03f77ea2009-09-15 22:57:37 +0200393
John Harrison9bfc01a2014-11-24 18:49:31 +0000394 if (overlay->last_flip_req == NULL)
Chris Wilsonb303cf92010-08-12 14:03:48 +0100395 return 0;
Daniel Vetter03f77ea2009-09-15 22:57:37 +0200396
Daniel Vettera4b3a572014-11-26 14:17:05 +0100397 ret = i915_wait_request(overlay->last_flip_req);
Chris Wilsonb6c028e2010-08-12 11:55:08 +0100398 if (ret)
Daniel Vetter03f77ea2009-09-15 22:57:37 +0200399 return ret;
400
Chris Wilsonb303cf92010-08-12 14:03:48 +0100401 if (overlay->flip_tail)
402 overlay->flip_tail(overlay);
Daniel Vetter03f77ea2009-09-15 22:57:37 +0200403
John Harrison9bfc01a2014-11-24 18:49:31 +0000404 i915_gem_request_assign(&overlay->last_flip_req, NULL);
Daniel Vetter03f77ea2009-09-15 22:57:37 +0200405 return 0;
406}
407
Daniel Vetter5a5a0c62009-09-15 22:57:36 +0200408/* Wait for pending overlay flip and release old frame.
409 * Needs to be called before the overlay register are changed
Chris Wilson8d74f652010-08-12 10:35:26 +0100410 * via intel_overlay_(un)map_regs
411 */
Daniel Vetter02e792f2009-09-15 22:57:34 +0200412static int intel_overlay_release_old_vid(struct intel_overlay *overlay)
413{
Chris Wilson1ee8da62016-05-12 12:43:23 +0100414 struct drm_i915_private *dev_priv = overlay->i915;
Tvrtko Ursulin4a570db2016-03-16 11:00:38 +0000415 struct intel_engine_cs *engine = &dev_priv->engine[RCS];
Daniel Vetter02e792f2009-09-15 22:57:34 +0200416 int ret;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200417
Chris Wilson91c8a322016-07-05 10:40:23 +0100418 lockdep_assert_held(&dev_priv->drm.struct_mutex);
Ville Syrjälä1362b772014-11-26 17:07:29 +0200419
Chris Wilson5cd68c92010-08-12 12:21:54 +0100420 /* Only wait if there is actually an old frame to release to
421 * guarantee forward progress.
422 */
Daniel Vetter03f77ea2009-09-15 22:57:37 +0200423 if (!overlay->old_vid_bo)
424 return 0;
425
Chris Wilson5cd68c92010-08-12 12:21:54 +0100426 if (I915_READ(ISR) & I915_OVERLAY_PLANE_FLIP_PENDING_INTERRUPT) {
427 /* synchronous slowpath */
John Harrisondad540c2015-05-29 17:43:47 +0100428 struct drm_i915_gem_request *req;
Chris Wilson7e37f882016-08-02 22:50:21 +0100429 struct intel_ring *ring;
John Harrisondad540c2015-05-29 17:43:47 +0100430
Tvrtko Ursuline2f80392016-03-16 11:00:36 +0000431 req = i915_gem_request_alloc(engine, NULL);
Dave Gordon26827082016-01-19 19:02:53 +0000432 if (IS_ERR(req))
433 return PTR_ERR(req);
Chris Wilsone1f99ce2010-10-27 12:45:26 +0100434
John Harrison5fb9de12015-05-29 17:44:07 +0100435 ret = intel_ring_begin(req, 2);
John Harrisondad540c2015-05-29 17:43:47 +0100436 if (ret) {
Chris Wilsonaa9b7812016-04-13 17:35:15 +0100437 i915_add_request_no_flush(req);
John Harrisondad540c2015-05-29 17:43:47 +0100438 return ret;
439 }
440
Chris Wilson1dae2df2016-08-02 22:50:19 +0100441 ring = req->ring;
Chris Wilsonb5321f32016-08-02 22:50:18 +0100442 intel_ring_emit(ring,
Tvrtko Ursuline2f80392016-03-16 11:00:36 +0000443 MI_WAIT_FOR_EVENT | MI_WAIT_FOR_OVERLAY_FLIP);
Chris Wilsonb5321f32016-08-02 22:50:18 +0100444 intel_ring_emit(ring, MI_NOOP);
445 intel_ring_advance(ring);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200446
John Harrisondad540c2015-05-29 17:43:47 +0100447 ret = intel_overlay_do_wait_request(overlay, req,
Chris Wilsonb303cf92010-08-12 14:03:48 +0100448 intel_overlay_release_old_vid_tail);
Chris Wilson5cd68c92010-08-12 12:21:54 +0100449 if (ret)
450 return ret;
451 }
Daniel Vetter02e792f2009-09-15 22:57:34 +0200452
Chris Wilson5cd68c92010-08-12 12:21:54 +0100453 intel_overlay_release_old_vid_tail(overlay);
Daniel Vettera071fa02014-06-18 23:28:09 +0200454
455
456 i915_gem_track_fb(overlay->old_vid_bo, NULL,
457 INTEL_FRONTBUFFER_OVERLAY(overlay->crtc->pipe));
Daniel Vetter02e792f2009-09-15 22:57:34 +0200458 return 0;
459}
460
Ville Syrjälä1362b772014-11-26 17:07:29 +0200461void intel_overlay_reset(struct drm_i915_private *dev_priv)
462{
463 struct intel_overlay *overlay = dev_priv->overlay;
464
465 if (!overlay)
466 return;
467
468 intel_overlay_release_old_vid(overlay);
469
470 overlay->last_flip_req = NULL;
471 overlay->old_xscale = 0;
472 overlay->old_yscale = 0;
473 overlay->crtc = NULL;
474 overlay->active = false;
475}
476
Daniel Vetter02e792f2009-09-15 22:57:34 +0200477struct put_image_params {
478 int format;
479 short dst_x;
480 short dst_y;
481 short dst_w;
482 short dst_h;
483 short src_w;
484 short src_scan_h;
485 short src_scan_w;
486 short src_h;
487 short stride_Y;
488 short stride_UV;
489 int offset_Y;
490 int offset_U;
491 int offset_V;
492};
493
494static int packed_depth_bytes(u32 format)
495{
496 switch (format & I915_OVERLAY_DEPTH_MASK) {
Chris Wilson722506f2010-08-12 09:28:50 +0100497 case I915_OVERLAY_YUV422:
498 return 4;
499 case I915_OVERLAY_YUV411:
500 /* return 6; not implemented */
501 default:
502 return -EINVAL;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200503 }
504}
505
506static int packed_width_bytes(u32 format, short width)
507{
508 switch (format & I915_OVERLAY_DEPTH_MASK) {
Chris Wilson722506f2010-08-12 09:28:50 +0100509 case I915_OVERLAY_YUV422:
510 return width << 1;
511 default:
512 return -EINVAL;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200513 }
514}
515
516static int uv_hsubsampling(u32 format)
517{
518 switch (format & I915_OVERLAY_DEPTH_MASK) {
Chris Wilson722506f2010-08-12 09:28:50 +0100519 case I915_OVERLAY_YUV422:
520 case I915_OVERLAY_YUV420:
521 return 2;
522 case I915_OVERLAY_YUV411:
523 case I915_OVERLAY_YUV410:
524 return 4;
525 default:
526 return -EINVAL;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200527 }
528}
529
530static int uv_vsubsampling(u32 format)
531{
532 switch (format & I915_OVERLAY_DEPTH_MASK) {
Chris Wilson722506f2010-08-12 09:28:50 +0100533 case I915_OVERLAY_YUV420:
534 case I915_OVERLAY_YUV410:
535 return 2;
536 case I915_OVERLAY_YUV422:
537 case I915_OVERLAY_YUV411:
538 return 1;
539 default:
540 return -EINVAL;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200541 }
542}
543
Chris Wilson1ee8da62016-05-12 12:43:23 +0100544static u32 calc_swidthsw(struct drm_i915_private *dev_priv, u32 offset, u32 width)
Daniel Vetter02e792f2009-09-15 22:57:34 +0200545{
546 u32 mask, shift, ret;
Chris Wilson1ee8da62016-05-12 12:43:23 +0100547 if (IS_GEN2(dev_priv)) {
Daniel Vetter02e792f2009-09-15 22:57:34 +0200548 mask = 0x1f;
549 shift = 5;
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100550 } else {
551 mask = 0x3f;
552 shift = 6;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200553 }
554 ret = ((offset + width + mask) >> shift) - (offset >> shift);
Chris Wilson1ee8da62016-05-12 12:43:23 +0100555 if (!IS_GEN2(dev_priv))
Daniel Vetter02e792f2009-09-15 22:57:34 +0200556 ret <<= 1;
Akshay Joshi0206e352011-08-16 15:34:10 -0400557 ret -= 1;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200558 return ret << 2;
559}
560
561static const u16 y_static_hcoeffs[N_HORIZ_Y_TAPS * N_PHASES] = {
562 0x3000, 0xb4a0, 0x1930, 0x1920, 0xb4a0,
563 0x3000, 0xb500, 0x19d0, 0x1880, 0xb440,
564 0x3000, 0xb540, 0x1a88, 0x2f80, 0xb3e0,
565 0x3000, 0xb580, 0x1b30, 0x2e20, 0xb380,
566 0x3000, 0xb5c0, 0x1bd8, 0x2cc0, 0xb320,
567 0x3020, 0xb5e0, 0x1c60, 0x2b80, 0xb2c0,
568 0x3020, 0xb5e0, 0x1cf8, 0x2a20, 0xb260,
569 0x3020, 0xb5e0, 0x1d80, 0x28e0, 0xb200,
570 0x3020, 0xb5c0, 0x1e08, 0x3f40, 0xb1c0,
571 0x3020, 0xb580, 0x1e78, 0x3ce0, 0xb160,
572 0x3040, 0xb520, 0x1ed8, 0x3aa0, 0xb120,
573 0x3040, 0xb4a0, 0x1f30, 0x3880, 0xb0e0,
574 0x3040, 0xb400, 0x1f78, 0x3680, 0xb0a0,
575 0x3020, 0xb340, 0x1fb8, 0x34a0, 0xb060,
576 0x3020, 0xb240, 0x1fe0, 0x32e0, 0xb040,
577 0x3020, 0xb140, 0x1ff8, 0x3160, 0xb020,
Chris Wilson722506f2010-08-12 09:28:50 +0100578 0xb000, 0x3000, 0x0800, 0x3000, 0xb000
579};
580
Daniel Vetter02e792f2009-09-15 22:57:34 +0200581static const u16 uv_static_hcoeffs[N_HORIZ_UV_TAPS * N_PHASES] = {
582 0x3000, 0x1800, 0x1800, 0xb000, 0x18d0, 0x2e60,
583 0xb000, 0x1990, 0x2ce0, 0xb020, 0x1a68, 0x2b40,
584 0xb040, 0x1b20, 0x29e0, 0xb060, 0x1bd8, 0x2880,
585 0xb080, 0x1c88, 0x3e60, 0xb0a0, 0x1d28, 0x3c00,
586 0xb0c0, 0x1db8, 0x39e0, 0xb0e0, 0x1e40, 0x37e0,
587 0xb100, 0x1eb8, 0x3620, 0xb100, 0x1f18, 0x34a0,
588 0xb100, 0x1f68, 0x3360, 0xb0e0, 0x1fa8, 0x3240,
589 0xb0c0, 0x1fe0, 0x3140, 0xb060, 0x1ff0, 0x30a0,
Chris Wilson722506f2010-08-12 09:28:50 +0100590 0x3000, 0x0800, 0x3000
591};
Daniel Vetter02e792f2009-09-15 22:57:34 +0200592
Ben Widawsky75020bc2012-04-16 14:07:43 -0700593static void update_polyphase_filter(struct overlay_registers __iomem *regs)
Daniel Vetter02e792f2009-09-15 22:57:34 +0200594{
Ben Widawsky75020bc2012-04-16 14:07:43 -0700595 memcpy_toio(regs->Y_HCOEFS, y_static_hcoeffs, sizeof(y_static_hcoeffs));
596 memcpy_toio(regs->UV_HCOEFS, uv_static_hcoeffs,
597 sizeof(uv_static_hcoeffs));
Daniel Vetter02e792f2009-09-15 22:57:34 +0200598}
599
600static bool update_scaling_factors(struct intel_overlay *overlay,
Ben Widawsky75020bc2012-04-16 14:07:43 -0700601 struct overlay_registers __iomem *regs,
Daniel Vetter02e792f2009-09-15 22:57:34 +0200602 struct put_image_params *params)
603{
604 /* fixed point with a 12 bit shift */
605 u32 xscale, yscale, xscale_UV, yscale_UV;
606#define FP_SHIFT 12
607#define FRACT_MASK 0xfff
608 bool scale_changed = false;
609 int uv_hscale = uv_hsubsampling(params->format);
610 int uv_vscale = uv_vsubsampling(params->format);
611
612 if (params->dst_w > 1)
613 xscale = ((params->src_scan_w - 1) << FP_SHIFT)
614 /(params->dst_w);
615 else
616 xscale = 1 << FP_SHIFT;
617
618 if (params->dst_h > 1)
619 yscale = ((params->src_scan_h - 1) << FP_SHIFT)
620 /(params->dst_h);
621 else
622 yscale = 1 << FP_SHIFT;
623
624 /*if (params->format & I915_OVERLAY_YUV_PLANAR) {*/
Chris Wilson722506f2010-08-12 09:28:50 +0100625 xscale_UV = xscale/uv_hscale;
626 yscale_UV = yscale/uv_vscale;
627 /* make the Y scale to UV scale ratio an exact multiply */
628 xscale = xscale_UV * uv_hscale;
629 yscale = yscale_UV * uv_vscale;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200630 /*} else {
Chris Wilson722506f2010-08-12 09:28:50 +0100631 xscale_UV = 0;
632 yscale_UV = 0;
633 }*/
Daniel Vetter02e792f2009-09-15 22:57:34 +0200634
635 if (xscale != overlay->old_xscale || yscale != overlay->old_yscale)
636 scale_changed = true;
637 overlay->old_xscale = xscale;
638 overlay->old_yscale = yscale;
639
Ben Widawsky75020bc2012-04-16 14:07:43 -0700640 iowrite32(((yscale & FRACT_MASK) << 20) |
641 ((xscale >> FP_SHIFT) << 16) |
642 ((xscale & FRACT_MASK) << 3),
643 &regs->YRGBSCALE);
Chris Wilson722506f2010-08-12 09:28:50 +0100644
Ben Widawsky75020bc2012-04-16 14:07:43 -0700645 iowrite32(((yscale_UV & FRACT_MASK) << 20) |
646 ((xscale_UV >> FP_SHIFT) << 16) |
647 ((xscale_UV & FRACT_MASK) << 3),
648 &regs->UVSCALE);
Chris Wilson722506f2010-08-12 09:28:50 +0100649
Ben Widawsky75020bc2012-04-16 14:07:43 -0700650 iowrite32((((yscale >> FP_SHIFT) << 16) |
651 ((yscale_UV >> FP_SHIFT) << 0)),
652 &regs->UVSCALEV);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200653
654 if (scale_changed)
655 update_polyphase_filter(regs);
656
657 return scale_changed;
658}
659
660static void update_colorkey(struct intel_overlay *overlay,
Ben Widawsky75020bc2012-04-16 14:07:43 -0700661 struct overlay_registers __iomem *regs)
Daniel Vetter02e792f2009-09-15 22:57:34 +0200662{
663 u32 key = overlay->color_key;
Chris Wilsonea9da4e2015-04-02 10:35:08 +0100664 u32 flags;
665
666 flags = 0;
667 if (overlay->color_key_enabled)
668 flags |= DST_KEY_ENABLE;
Chris Wilson6ba3ddd2010-08-12 09:30:58 +0100669
Matt Roperf4510a22014-04-01 15:22:40 -0700670 switch (overlay->crtc->base.primary->fb->bits_per_pixel) {
Chris Wilson722506f2010-08-12 09:28:50 +0100671 case 8:
Chris Wilsonea9da4e2015-04-02 10:35:08 +0100672 key = 0;
673 flags |= CLK_RGB8I_MASK;
Chris Wilson6ba3ddd2010-08-12 09:30:58 +0100674 break;
675
Chris Wilson722506f2010-08-12 09:28:50 +0100676 case 16:
Matt Roperf4510a22014-04-01 15:22:40 -0700677 if (overlay->crtc->base.primary->fb->depth == 15) {
Chris Wilsonea9da4e2015-04-02 10:35:08 +0100678 key = RGB15_TO_COLORKEY(key);
679 flags |= CLK_RGB15_MASK;
Chris Wilson722506f2010-08-12 09:28:50 +0100680 } else {
Chris Wilsonea9da4e2015-04-02 10:35:08 +0100681 key = RGB16_TO_COLORKEY(key);
682 flags |= CLK_RGB16_MASK;
Chris Wilson722506f2010-08-12 09:28:50 +0100683 }
Chris Wilson6ba3ddd2010-08-12 09:30:58 +0100684 break;
685
Chris Wilson722506f2010-08-12 09:28:50 +0100686 case 24:
687 case 32:
Chris Wilsonea9da4e2015-04-02 10:35:08 +0100688 flags |= CLK_RGB24_MASK;
Chris Wilson6ba3ddd2010-08-12 09:30:58 +0100689 break;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200690 }
Chris Wilsonea9da4e2015-04-02 10:35:08 +0100691
692 iowrite32(key, &regs->DCLRKV);
693 iowrite32(flags, &regs->DCLRKM);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200694}
695
696static u32 overlay_cmd_reg(struct put_image_params *params)
697{
698 u32 cmd = OCMD_ENABLE | OCMD_BUF_TYPE_FRAME | OCMD_BUFFER0;
699
700 if (params->format & I915_OVERLAY_YUV_PLANAR) {
701 switch (params->format & I915_OVERLAY_DEPTH_MASK) {
Chris Wilson722506f2010-08-12 09:28:50 +0100702 case I915_OVERLAY_YUV422:
703 cmd |= OCMD_YUV_422_PLANAR;
704 break;
705 case I915_OVERLAY_YUV420:
706 cmd |= OCMD_YUV_420_PLANAR;
707 break;
708 case I915_OVERLAY_YUV411:
709 case I915_OVERLAY_YUV410:
710 cmd |= OCMD_YUV_410_PLANAR;
711 break;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200712 }
713 } else { /* YUV packed */
714 switch (params->format & I915_OVERLAY_DEPTH_MASK) {
Chris Wilson722506f2010-08-12 09:28:50 +0100715 case I915_OVERLAY_YUV422:
716 cmd |= OCMD_YUV_422_PACKED;
717 break;
718 case I915_OVERLAY_YUV411:
719 cmd |= OCMD_YUV_411_PACKED;
720 break;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200721 }
722
723 switch (params->format & I915_OVERLAY_SWAP_MASK) {
Chris Wilson722506f2010-08-12 09:28:50 +0100724 case I915_OVERLAY_NO_SWAP:
725 break;
726 case I915_OVERLAY_UV_SWAP:
727 cmd |= OCMD_UV_SWAP;
728 break;
729 case I915_OVERLAY_Y_SWAP:
730 cmd |= OCMD_Y_SWAP;
731 break;
732 case I915_OVERLAY_Y_AND_UV_SWAP:
733 cmd |= OCMD_Y_AND_UV_SWAP;
734 break;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200735 }
736 }
737
738 return cmd;
739}
740
Chris Wilson5fe82c52010-08-12 12:38:21 +0100741static int intel_overlay_do_put_image(struct intel_overlay *overlay,
Chris Wilson05394f32010-11-08 19:18:58 +0000742 struct drm_i915_gem_object *new_bo,
Chris Wilson5fe82c52010-08-12 12:38:21 +0100743 struct put_image_params *params)
Daniel Vetter02e792f2009-09-15 22:57:34 +0200744{
745 int ret, tmp_width;
Ben Widawsky75020bc2012-04-16 14:07:43 -0700746 struct overlay_registers __iomem *regs;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200747 bool scale_changed = false;
Chris Wilson1ee8da62016-05-12 12:43:23 +0100748 struct drm_i915_private *dev_priv = overlay->i915;
Ben Widawsky75020bc2012-04-16 14:07:43 -0700749 u32 swidth, swidthsw, sheight, ostride;
Daniel Vettera071fa02014-06-18 23:28:09 +0200750 enum pipe pipe = overlay->crtc->pipe;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200751
Chris Wilson91c8a322016-07-05 10:40:23 +0100752 lockdep_assert_held(&dev_priv->drm.struct_mutex);
753 WARN_ON(!drm_modeset_is_locked(&dev_priv->drm.mode_config.connection_mutex));
Daniel Vetter02e792f2009-09-15 22:57:34 +0200754
Daniel Vetter02e792f2009-09-15 22:57:34 +0200755 ret = intel_overlay_release_old_vid(overlay);
756 if (ret != 0)
757 return ret;
758
Maarten Lankhorst7580d772015-08-18 13:40:06 +0200759 ret = i915_gem_object_pin_to_display_plane(new_bo, 0,
Tvrtko Ursuline6617332015-03-23 11:10:33 +0000760 &i915_ggtt_view_normal);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200761 if (ret != 0)
762 return ret;
763
Chris Wilsond9e86c02010-11-10 16:40:20 +0000764 ret = i915_gem_object_put_fence(new_bo);
765 if (ret)
766 goto out_unpin;
767
Daniel Vetter02e792f2009-09-15 22:57:34 +0200768 if (!overlay->active) {
Ben Widawsky75020bc2012-04-16 14:07:43 -0700769 u32 oconfig;
Chris Wilson8d74f652010-08-12 10:35:26 +0100770 regs = intel_overlay_map_regs(overlay);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200771 if (!regs) {
772 ret = -ENOMEM;
773 goto out_unpin;
774 }
Ben Widawsky75020bc2012-04-16 14:07:43 -0700775 oconfig = OCONF_CC_OUT_8BIT;
Chris Wilson1ee8da62016-05-12 12:43:23 +0100776 if (IS_GEN4(dev_priv))
Ben Widawsky75020bc2012-04-16 14:07:43 -0700777 oconfig |= OCONF_CSC_MODE_BT709;
Daniel Vettera071fa02014-06-18 23:28:09 +0200778 oconfig |= pipe == 0 ?
Daniel Vetter02e792f2009-09-15 22:57:34 +0200779 OCONF_PIPE_A : OCONF_PIPE_B;
Ben Widawsky75020bc2012-04-16 14:07:43 -0700780 iowrite32(oconfig, &regs->OCONFIG);
Chris Wilson9bb2ff72010-08-12 12:02:11 +0100781 intel_overlay_unmap_regs(overlay, regs);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200782
783 ret = intel_overlay_on(overlay);
784 if (ret != 0)
785 goto out_unpin;
786 }
787
Chris Wilson8d74f652010-08-12 10:35:26 +0100788 regs = intel_overlay_map_regs(overlay);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200789 if (!regs) {
790 ret = -ENOMEM;
791 goto out_unpin;
792 }
793
Ben Widawsky75020bc2012-04-16 14:07:43 -0700794 iowrite32((params->dst_y << 16) | params->dst_x, &regs->DWINPOS);
795 iowrite32((params->dst_h << 16) | params->dst_w, &regs->DWINSZ);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200796
797 if (params->format & I915_OVERLAY_YUV_PACKED)
798 tmp_width = packed_width_bytes(params->format, params->src_w);
799 else
800 tmp_width = params->src_w;
801
Ben Widawsky75020bc2012-04-16 14:07:43 -0700802 swidth = params->src_w;
Chris Wilson1ee8da62016-05-12 12:43:23 +0100803 swidthsw = calc_swidthsw(dev_priv, params->offset_Y, tmp_width);
Ben Widawsky75020bc2012-04-16 14:07:43 -0700804 sheight = params->src_h;
Ben Widawskyf343c5f2013-07-05 14:41:04 -0700805 iowrite32(i915_gem_obj_ggtt_offset(new_bo) + params->offset_Y, &regs->OBUF_0Y);
Ben Widawsky75020bc2012-04-16 14:07:43 -0700806 ostride = params->stride_Y;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200807
808 if (params->format & I915_OVERLAY_YUV_PLANAR) {
809 int uv_hscale = uv_hsubsampling(params->format);
810 int uv_vscale = uv_vsubsampling(params->format);
811 u32 tmp_U, tmp_V;
Ben Widawsky75020bc2012-04-16 14:07:43 -0700812 swidth |= (params->src_w/uv_hscale) << 16;
Chris Wilson1ee8da62016-05-12 12:43:23 +0100813 tmp_U = calc_swidthsw(dev_priv, params->offset_U,
Chris Wilson722506f2010-08-12 09:28:50 +0100814 params->src_w/uv_hscale);
Chris Wilson1ee8da62016-05-12 12:43:23 +0100815 tmp_V = calc_swidthsw(dev_priv, params->offset_V,
Chris Wilson722506f2010-08-12 09:28:50 +0100816 params->src_w/uv_hscale);
Ben Widawsky75020bc2012-04-16 14:07:43 -0700817 swidthsw |= max_t(u32, tmp_U, tmp_V) << 16;
818 sheight |= (params->src_h/uv_vscale) << 16;
Ben Widawskyf343c5f2013-07-05 14:41:04 -0700819 iowrite32(i915_gem_obj_ggtt_offset(new_bo) + params->offset_U, &regs->OBUF_0U);
820 iowrite32(i915_gem_obj_ggtt_offset(new_bo) + params->offset_V, &regs->OBUF_0V);
Ben Widawsky75020bc2012-04-16 14:07:43 -0700821 ostride |= params->stride_UV << 16;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200822 }
823
Ben Widawsky75020bc2012-04-16 14:07:43 -0700824 iowrite32(swidth, &regs->SWIDTH);
825 iowrite32(swidthsw, &regs->SWIDTHSW);
826 iowrite32(sheight, &regs->SHEIGHT);
827 iowrite32(ostride, &regs->OSTRIDE);
828
Daniel Vetter02e792f2009-09-15 22:57:34 +0200829 scale_changed = update_scaling_factors(overlay, regs, params);
830
831 update_colorkey(overlay, regs);
832
Ben Widawsky75020bc2012-04-16 14:07:43 -0700833 iowrite32(overlay_cmd_reg(params), &regs->OCMD);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200834
Chris Wilson9bb2ff72010-08-12 12:02:11 +0100835 intel_overlay_unmap_regs(overlay, regs);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200836
Chris Wilson8dc5d142010-08-12 12:36:12 +0100837 ret = intel_overlay_continue(overlay, scale_changed);
838 if (ret)
839 goto out_unpin;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200840
Daniel Vettera071fa02014-06-18 23:28:09 +0200841 i915_gem_track_fb(overlay->vid_bo, new_bo,
842 INTEL_FRONTBUFFER_OVERLAY(pipe));
843
Daniel Vetter02e792f2009-09-15 22:57:34 +0200844 overlay->old_vid_bo = overlay->vid_bo;
Chris Wilson05394f32010-11-08 19:18:58 +0000845 overlay->vid_bo = new_bo;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200846
Chris Wilson91c8a322016-07-05 10:40:23 +0100847 intel_frontbuffer_flip(&dev_priv->drm,
848 INTEL_FRONTBUFFER_OVERLAY(pipe));
Daniel Vetterf99d7062014-06-19 16:01:59 +0200849
Daniel Vetter02e792f2009-09-15 22:57:34 +0200850 return 0;
851
852out_unpin:
Ben Widawskyd7f46fc2013-12-06 14:10:55 -0800853 i915_gem_object_ggtt_unpin(new_bo);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200854 return ret;
855}
856
Chris Wilsonce453d82011-02-21 14:43:56 +0000857int intel_overlay_switch_off(struct intel_overlay *overlay)
Daniel Vetter02e792f2009-09-15 22:57:34 +0200858{
Chris Wilson1ee8da62016-05-12 12:43:23 +0100859 struct drm_i915_private *dev_priv = overlay->i915;
Ben Widawsky75020bc2012-04-16 14:07:43 -0700860 struct overlay_registers __iomem *regs;
Chris Wilson5dcdbcb2010-08-12 13:50:28 +0100861 int ret;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200862
Chris Wilson91c8a322016-07-05 10:40:23 +0100863 lockdep_assert_held(&dev_priv->drm.struct_mutex);
864 WARN_ON(!drm_modeset_is_locked(&dev_priv->drm.mode_config.connection_mutex));
Daniel Vetter02e792f2009-09-15 22:57:34 +0200865
Chris Wilsonce453d82011-02-21 14:43:56 +0000866 ret = intel_overlay_recover_from_interrupt(overlay);
Chris Wilsonb303cf92010-08-12 14:03:48 +0100867 if (ret != 0)
868 return ret;
Daniel Vetter9bedb972009-11-30 15:55:49 +0100869
Daniel Vetter02e792f2009-09-15 22:57:34 +0200870 if (!overlay->active)
871 return 0;
872
Daniel Vetter02e792f2009-09-15 22:57:34 +0200873 ret = intel_overlay_release_old_vid(overlay);
874 if (ret != 0)
875 return ret;
876
Chris Wilson8d74f652010-08-12 10:35:26 +0100877 regs = intel_overlay_map_regs(overlay);
Ben Widawsky75020bc2012-04-16 14:07:43 -0700878 iowrite32(0, &regs->OCMD);
Chris Wilson9bb2ff72010-08-12 12:02:11 +0100879 intel_overlay_unmap_regs(overlay, regs);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200880
Chris Wilsonce453d82011-02-21 14:43:56 +0000881 ret = intel_overlay_off(overlay);
Daniel Vetter03f77ea2009-09-15 22:57:37 +0200882 if (ret != 0)
883 return ret;
884
Daniel Vetter12ca45f2037-04-25 10:08:26 +0200885 intel_overlay_off_tail(overlay);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200886 return 0;
887}
888
889static int check_overlay_possible_on_crtc(struct intel_overlay *overlay,
890 struct intel_crtc *crtc)
891{
Chris Wilsonf7abfe82010-09-13 14:19:16 +0100892 if (!crtc->active)
Daniel Vetter02e792f2009-09-15 22:57:34 +0200893 return -EINVAL;
894
Daniel Vetter02e792f2009-09-15 22:57:34 +0200895 /* can't use the overlay with double wide pipe */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +0200896 if (crtc->config->double_wide)
Daniel Vetter02e792f2009-09-15 22:57:34 +0200897 return -EINVAL;
898
899 return 0;
900}
901
902static void update_pfit_vscale_ratio(struct intel_overlay *overlay)
903{
Chris Wilson1ee8da62016-05-12 12:43:23 +0100904 struct drm_i915_private *dev_priv = overlay->i915;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200905 u32 pfit_control = I915_READ(PFIT_CONTROL);
Chris Wilson446d2182010-08-12 11:15:58 +0100906 u32 ratio;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200907
908 /* XXX: This is not the same logic as in the xorg driver, but more in
Chris Wilson446d2182010-08-12 11:15:58 +0100909 * line with the intel documentation for the i965
910 */
Chris Wilson1ee8da62016-05-12 12:43:23 +0100911 if (INTEL_GEN(dev_priv) >= 4) {
Akshay Joshi0206e352011-08-16 15:34:10 -0400912 /* on i965 use the PGM reg to read out the autoscaler values */
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100913 ratio = I915_READ(PFIT_PGM_RATIOS) >> PFIT_VERT_SCALE_SHIFT_965;
914 } else {
Chris Wilson446d2182010-08-12 11:15:58 +0100915 if (pfit_control & VERT_AUTO_SCALE)
916 ratio = I915_READ(PFIT_AUTO_RATIOS);
Daniel Vetter02e792f2009-09-15 22:57:34 +0200917 else
Chris Wilson446d2182010-08-12 11:15:58 +0100918 ratio = I915_READ(PFIT_PGM_RATIOS);
919 ratio >>= PFIT_VERT_SCALE_SHIFT;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200920 }
921
922 overlay->pfit_vscale_ratio = ratio;
923}
924
925static int check_overlay_dst(struct intel_overlay *overlay,
926 struct drm_intel_overlay_put_image *rec)
927{
928 struct drm_display_mode *mode = &overlay->crtc->base.mode;
929
Daniel Vetter75c13992012-01-28 23:48:46 +0100930 if (rec->dst_x < mode->hdisplay &&
931 rec->dst_x + rec->dst_width <= mode->hdisplay &&
932 rec->dst_y < mode->vdisplay &&
933 rec->dst_y + rec->dst_height <= mode->vdisplay)
Daniel Vetter02e792f2009-09-15 22:57:34 +0200934 return 0;
935 else
936 return -EINVAL;
937}
938
939static int check_overlay_scaling(struct put_image_params *rec)
940{
941 u32 tmp;
942
943 /* downscaling limit is 8.0 */
944 tmp = ((rec->src_scan_h << 16) / rec->dst_h) >> 16;
945 if (tmp > 7)
946 return -EINVAL;
947 tmp = ((rec->src_scan_w << 16) / rec->dst_w) >> 16;
948 if (tmp > 7)
949 return -EINVAL;
950
951 return 0;
952}
953
Chris Wilson1ee8da62016-05-12 12:43:23 +0100954static int check_overlay_src(struct drm_i915_private *dev_priv,
Daniel Vetter02e792f2009-09-15 22:57:34 +0200955 struct drm_intel_overlay_put_image *rec,
Chris Wilson05394f32010-11-08 19:18:58 +0000956 struct drm_i915_gem_object *new_bo)
Daniel Vetter02e792f2009-09-15 22:57:34 +0200957{
Daniel Vetter02e792f2009-09-15 22:57:34 +0200958 int uv_hscale = uv_hsubsampling(rec->flags);
959 int uv_vscale = uv_vsubsampling(rec->flags);
Dan Carpenter8f28f542010-10-27 23:17:25 +0200960 u32 stride_mask;
961 int depth;
962 u32 tmp;
Daniel Vetter02e792f2009-09-15 22:57:34 +0200963
964 /* check src dimensions */
Chris Wilson1ee8da62016-05-12 12:43:23 +0100965 if (IS_845G(dev_priv) || IS_I830(dev_priv)) {
Chris Wilson722506f2010-08-12 09:28:50 +0100966 if (rec->src_height > IMAGE_MAX_HEIGHT_LEGACY ||
Chris Wilson9f7c3f42010-08-12 11:29:34 +0100967 rec->src_width > IMAGE_MAX_WIDTH_LEGACY)
Daniel Vetter02e792f2009-09-15 22:57:34 +0200968 return -EINVAL;
969 } else {
Chris Wilson722506f2010-08-12 09:28:50 +0100970 if (rec->src_height > IMAGE_MAX_HEIGHT ||
Chris Wilson9f7c3f42010-08-12 11:29:34 +0100971 rec->src_width > IMAGE_MAX_WIDTH)
Daniel Vetter02e792f2009-09-15 22:57:34 +0200972 return -EINVAL;
973 }
Chris Wilson9f7c3f42010-08-12 11:29:34 +0100974
Daniel Vetter02e792f2009-09-15 22:57:34 +0200975 /* better safe than sorry, use 4 as the maximal subsampling ratio */
Chris Wilson722506f2010-08-12 09:28:50 +0100976 if (rec->src_height < N_VERT_Y_TAPS*4 ||
Chris Wilson9f7c3f42010-08-12 11:29:34 +0100977 rec->src_width < N_HORIZ_Y_TAPS*4)
Daniel Vetter02e792f2009-09-15 22:57:34 +0200978 return -EINVAL;
979
Chris Wilsona1efd142010-07-12 19:35:38 +0100980 /* check alignment constraints */
Daniel Vetter02e792f2009-09-15 22:57:34 +0200981 switch (rec->flags & I915_OVERLAY_TYPE_MASK) {
Chris Wilson722506f2010-08-12 09:28:50 +0100982 case I915_OVERLAY_RGB:
983 /* not implemented */
984 return -EINVAL;
Chris Wilson9f7c3f42010-08-12 11:29:34 +0100985
Chris Wilson722506f2010-08-12 09:28:50 +0100986 case I915_OVERLAY_YUV_PACKED:
Chris Wilson722506f2010-08-12 09:28:50 +0100987 if (uv_vscale != 1)
Daniel Vetter02e792f2009-09-15 22:57:34 +0200988 return -EINVAL;
Chris Wilson9f7c3f42010-08-12 11:29:34 +0100989
990 depth = packed_depth_bytes(rec->flags);
Chris Wilson722506f2010-08-12 09:28:50 +0100991 if (depth < 0)
992 return depth;
Chris Wilson9f7c3f42010-08-12 11:29:34 +0100993
Chris Wilson722506f2010-08-12 09:28:50 +0100994 /* ignore UV planes */
995 rec->stride_UV = 0;
996 rec->offset_U = 0;
997 rec->offset_V = 0;
998 /* check pixel alignment */
999 if (rec->offset_Y % depth)
Daniel Vetter02e792f2009-09-15 22:57:34 +02001000 return -EINVAL;
Chris Wilson722506f2010-08-12 09:28:50 +01001001 break;
Chris Wilson9f7c3f42010-08-12 11:29:34 +01001002
Chris Wilson722506f2010-08-12 09:28:50 +01001003 case I915_OVERLAY_YUV_PLANAR:
1004 if (uv_vscale < 0 || uv_hscale < 0)
1005 return -EINVAL;
1006 /* no offset restrictions for planar formats */
1007 break;
Chris Wilson9f7c3f42010-08-12 11:29:34 +01001008
Chris Wilson722506f2010-08-12 09:28:50 +01001009 default:
1010 return -EINVAL;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001011 }
1012
1013 if (rec->src_width % uv_hscale)
1014 return -EINVAL;
1015
1016 /* stride checking */
Chris Wilson1ee8da62016-05-12 12:43:23 +01001017 if (IS_I830(dev_priv) || IS_845G(dev_priv))
Chris Wilsona1efd142010-07-12 19:35:38 +01001018 stride_mask = 255;
1019 else
1020 stride_mask = 63;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001021
1022 if (rec->stride_Y & stride_mask || rec->stride_UV & stride_mask)
1023 return -EINVAL;
Chris Wilson1ee8da62016-05-12 12:43:23 +01001024 if (IS_GEN4(dev_priv) && rec->stride_Y < 512)
Daniel Vetter02e792f2009-09-15 22:57:34 +02001025 return -EINVAL;
1026
1027 tmp = (rec->flags & I915_OVERLAY_TYPE_MASK) == I915_OVERLAY_YUV_PLANAR ?
Chris Wilson9f7c3f42010-08-12 11:29:34 +01001028 4096 : 8192;
1029 if (rec->stride_Y > tmp || rec->stride_UV > 2*1024)
Daniel Vetter02e792f2009-09-15 22:57:34 +02001030 return -EINVAL;
1031
1032 /* check buffer dimensions */
1033 switch (rec->flags & I915_OVERLAY_TYPE_MASK) {
Chris Wilson722506f2010-08-12 09:28:50 +01001034 case I915_OVERLAY_RGB:
1035 case I915_OVERLAY_YUV_PACKED:
1036 /* always 4 Y values per depth pixels */
1037 if (packed_width_bytes(rec->flags, rec->src_width) > rec->stride_Y)
1038 return -EINVAL;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001039
Chris Wilson722506f2010-08-12 09:28:50 +01001040 tmp = rec->stride_Y*rec->src_height;
Chris Wilson05394f32010-11-08 19:18:58 +00001041 if (rec->offset_Y + tmp > new_bo->base.size)
Chris Wilson722506f2010-08-12 09:28:50 +01001042 return -EINVAL;
1043 break;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001044
Chris Wilson722506f2010-08-12 09:28:50 +01001045 case I915_OVERLAY_YUV_PLANAR:
1046 if (rec->src_width > rec->stride_Y)
1047 return -EINVAL;
1048 if (rec->src_width/uv_hscale > rec->stride_UV)
1049 return -EINVAL;
1050
Chris Wilson9f7c3f42010-08-12 11:29:34 +01001051 tmp = rec->stride_Y * rec->src_height;
Chris Wilson05394f32010-11-08 19:18:58 +00001052 if (rec->offset_Y + tmp > new_bo->base.size)
Chris Wilson722506f2010-08-12 09:28:50 +01001053 return -EINVAL;
Chris Wilson9f7c3f42010-08-12 11:29:34 +01001054
1055 tmp = rec->stride_UV * (rec->src_height / uv_vscale);
Chris Wilson05394f32010-11-08 19:18:58 +00001056 if (rec->offset_U + tmp > new_bo->base.size ||
1057 rec->offset_V + tmp > new_bo->base.size)
Chris Wilson722506f2010-08-12 09:28:50 +01001058 return -EINVAL;
1059 break;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001060 }
1061
1062 return 0;
1063}
1064
Chris Wilsone9e331a2010-09-13 01:16:10 +01001065/**
1066 * Return the pipe currently connected to the panel fitter,
1067 * or -1 if the panel fitter is not present or not in use
1068 */
Chris Wilson1ee8da62016-05-12 12:43:23 +01001069static int intel_panel_fitter_pipe(struct drm_i915_private *dev_priv)
Chris Wilsone9e331a2010-09-13 01:16:10 +01001070{
Chris Wilsone9e331a2010-09-13 01:16:10 +01001071 u32 pfit_control;
1072
1073 /* i830 doesn't have a panel fitter */
Chris Wilson1ee8da62016-05-12 12:43:23 +01001074 if (INTEL_GEN(dev_priv) <= 3 &&
1075 (IS_I830(dev_priv) || !IS_MOBILE(dev_priv)))
Chris Wilsone9e331a2010-09-13 01:16:10 +01001076 return -1;
1077
1078 pfit_control = I915_READ(PFIT_CONTROL);
1079
1080 /* See if the panel fitter is in use */
1081 if ((pfit_control & PFIT_ENABLE) == 0)
1082 return -1;
1083
1084 /* 965 can place panel fitter on either pipe */
Chris Wilson1ee8da62016-05-12 12:43:23 +01001085 if (IS_GEN4(dev_priv))
Chris Wilsone9e331a2010-09-13 01:16:10 +01001086 return (pfit_control >> 29) & 0x3;
1087
1088 /* older chips can only use pipe 1 */
1089 return 1;
1090}
1091
Chris Wilson1ee8da62016-05-12 12:43:23 +01001092int intel_overlay_put_image_ioctl(struct drm_device *dev, void *data,
1093 struct drm_file *file_priv)
Daniel Vetter02e792f2009-09-15 22:57:34 +02001094{
1095 struct drm_intel_overlay_put_image *put_image_rec = data;
Chris Wilsonfac5e232016-07-04 11:34:36 +01001096 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001097 struct intel_overlay *overlay;
Rob Clark7707e652014-07-17 23:30:04 -04001098 struct drm_crtc *drmmode_crtc;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001099 struct intel_crtc *crtc;
Chris Wilson05394f32010-11-08 19:18:58 +00001100 struct drm_i915_gem_object *new_bo;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001101 struct put_image_params *params;
1102 int ret;
1103
Daniel Vetter02e792f2009-09-15 22:57:34 +02001104 overlay = dev_priv->overlay;
1105 if (!overlay) {
1106 DRM_DEBUG("userspace bug: no overlay\n");
1107 return -ENODEV;
1108 }
1109
1110 if (!(put_image_rec->flags & I915_OVERLAY_ENABLE)) {
Daniel Vettera0e99e62012-12-02 01:05:46 +01001111 drm_modeset_lock_all(dev);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001112 mutex_lock(&dev->struct_mutex);
1113
Chris Wilsonce453d82011-02-21 14:43:56 +00001114 ret = intel_overlay_switch_off(overlay);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001115
1116 mutex_unlock(&dev->struct_mutex);
Daniel Vettera0e99e62012-12-02 01:05:46 +01001117 drm_modeset_unlock_all(dev);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001118
1119 return ret;
1120 }
1121
Daniel Vetterb14c5672013-09-19 12:18:32 +02001122 params = kmalloc(sizeof(*params), GFP_KERNEL);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001123 if (!params)
1124 return -ENOMEM;
1125
Rob Clark7707e652014-07-17 23:30:04 -04001126 drmmode_crtc = drm_crtc_find(dev, put_image_rec->crtc_id);
1127 if (!drmmode_crtc) {
Dan Carpenter915a4282010-03-06 14:05:39 +03001128 ret = -ENOENT;
1129 goto out_free;
1130 }
Rob Clark7707e652014-07-17 23:30:04 -04001131 crtc = to_intel_crtc(drmmode_crtc);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001132
Chris Wilson03ac0642016-07-20 13:31:51 +01001133 new_bo = i915_gem_object_lookup(file_priv, put_image_rec->bo_handle);
1134 if (!new_bo) {
Dan Carpenter915a4282010-03-06 14:05:39 +03001135 ret = -ENOENT;
1136 goto out_free;
1137 }
Daniel Vetter02e792f2009-09-15 22:57:34 +02001138
Daniel Vettera0e99e62012-12-02 01:05:46 +01001139 drm_modeset_lock_all(dev);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001140 mutex_lock(&dev->struct_mutex);
1141
Chris Wilsond9e86c02010-11-10 16:40:20 +00001142 if (new_bo->tiling_mode) {
Daniel Vetter3b25b312014-02-14 14:06:06 +01001143 DRM_DEBUG_KMS("buffer used for overlay image can not be tiled\n");
Chris Wilsond9e86c02010-11-10 16:40:20 +00001144 ret = -EINVAL;
1145 goto out_unlock;
1146 }
1147
Chris Wilsonce453d82011-02-21 14:43:56 +00001148 ret = intel_overlay_recover_from_interrupt(overlay);
Chris Wilsonb303cf92010-08-12 14:03:48 +01001149 if (ret != 0)
1150 goto out_unlock;
Daniel Vetter03f77ea2009-09-15 22:57:37 +02001151
Daniel Vetter02e792f2009-09-15 22:57:34 +02001152 if (overlay->crtc != crtc) {
1153 struct drm_display_mode *mode = &crtc->base.mode;
Chris Wilsonce453d82011-02-21 14:43:56 +00001154 ret = intel_overlay_switch_off(overlay);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001155 if (ret != 0)
1156 goto out_unlock;
1157
1158 ret = check_overlay_possible_on_crtc(overlay, crtc);
1159 if (ret != 0)
1160 goto out_unlock;
1161
1162 overlay->crtc = crtc;
1163 crtc->overlay = overlay;
1164
Chris Wilsone9e331a2010-09-13 01:16:10 +01001165 /* line too wide, i.e. one-line-mode */
1166 if (mode->hdisplay > 1024 &&
Chris Wilson1ee8da62016-05-12 12:43:23 +01001167 intel_panel_fitter_pipe(dev_priv) == crtc->pipe) {
Ville Syrjälä209c2a52015-03-31 10:37:23 +03001168 overlay->pfit_active = true;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001169 update_pfit_vscale_ratio(overlay);
1170 } else
Ville Syrjälä209c2a52015-03-31 10:37:23 +03001171 overlay->pfit_active = false;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001172 }
1173
1174 ret = check_overlay_dst(overlay, put_image_rec);
1175 if (ret != 0)
1176 goto out_unlock;
1177
1178 if (overlay->pfit_active) {
1179 params->dst_y = ((((u32)put_image_rec->dst_y) << 12) /
Chris Wilson722506f2010-08-12 09:28:50 +01001180 overlay->pfit_vscale_ratio);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001181 /* shifting right rounds downwards, so add 1 */
1182 params->dst_h = ((((u32)put_image_rec->dst_height) << 12) /
Chris Wilson722506f2010-08-12 09:28:50 +01001183 overlay->pfit_vscale_ratio) + 1;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001184 } else {
1185 params->dst_y = put_image_rec->dst_y;
1186 params->dst_h = put_image_rec->dst_height;
1187 }
1188 params->dst_x = put_image_rec->dst_x;
1189 params->dst_w = put_image_rec->dst_width;
1190
1191 params->src_w = put_image_rec->src_width;
1192 params->src_h = put_image_rec->src_height;
1193 params->src_scan_w = put_image_rec->src_scan_width;
1194 params->src_scan_h = put_image_rec->src_scan_height;
Chris Wilson722506f2010-08-12 09:28:50 +01001195 if (params->src_scan_h > params->src_h ||
1196 params->src_scan_w > params->src_w) {
Daniel Vetter02e792f2009-09-15 22:57:34 +02001197 ret = -EINVAL;
1198 goto out_unlock;
1199 }
1200
Chris Wilson1ee8da62016-05-12 12:43:23 +01001201 ret = check_overlay_src(dev_priv, put_image_rec, new_bo);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001202 if (ret != 0)
1203 goto out_unlock;
1204 params->format = put_image_rec->flags & ~I915_OVERLAY_FLAGS_MASK;
1205 params->stride_Y = put_image_rec->stride_Y;
1206 params->stride_UV = put_image_rec->stride_UV;
1207 params->offset_Y = put_image_rec->offset_Y;
1208 params->offset_U = put_image_rec->offset_U;
1209 params->offset_V = put_image_rec->offset_V;
1210
1211 /* Check scaling after src size to prevent a divide-by-zero. */
1212 ret = check_overlay_scaling(params);
1213 if (ret != 0)
1214 goto out_unlock;
1215
1216 ret = intel_overlay_do_put_image(overlay, new_bo, params);
1217 if (ret != 0)
1218 goto out_unlock;
1219
1220 mutex_unlock(&dev->struct_mutex);
Daniel Vettera0e99e62012-12-02 01:05:46 +01001221 drm_modeset_unlock_all(dev);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001222
1223 kfree(params);
1224
1225 return 0;
1226
1227out_unlock:
1228 mutex_unlock(&dev->struct_mutex);
Daniel Vettera0e99e62012-12-02 01:05:46 +01001229 drm_modeset_unlock_all(dev);
Dave Gordon13f17b22016-07-21 18:39:38 +01001230 i915_gem_object_put_unlocked(new_bo);
Dan Carpenter915a4282010-03-06 14:05:39 +03001231out_free:
Daniel Vetter02e792f2009-09-15 22:57:34 +02001232 kfree(params);
1233
1234 return ret;
1235}
1236
1237static void update_reg_attrs(struct intel_overlay *overlay,
Ben Widawsky75020bc2012-04-16 14:07:43 -07001238 struct overlay_registers __iomem *regs)
Daniel Vetter02e792f2009-09-15 22:57:34 +02001239{
Ben Widawsky75020bc2012-04-16 14:07:43 -07001240 iowrite32((overlay->contrast << 18) | (overlay->brightness & 0xff),
1241 &regs->OCLRC0);
1242 iowrite32(overlay->saturation, &regs->OCLRC1);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001243}
1244
1245static bool check_gamma_bounds(u32 gamma1, u32 gamma2)
1246{
1247 int i;
1248
1249 if (gamma1 & 0xff000000 || gamma2 & 0xff000000)
1250 return false;
1251
1252 for (i = 0; i < 3; i++) {
Chris Wilson722506f2010-08-12 09:28:50 +01001253 if (((gamma1 >> i*8) & 0xff) >= ((gamma2 >> i*8) & 0xff))
Daniel Vetter02e792f2009-09-15 22:57:34 +02001254 return false;
1255 }
1256
1257 return true;
1258}
1259
1260static bool check_gamma5_errata(u32 gamma5)
1261{
1262 int i;
1263
1264 for (i = 0; i < 3; i++) {
1265 if (((gamma5 >> i*8) & 0xff) == 0x80)
1266 return false;
1267 }
1268
1269 return true;
1270}
1271
1272static int check_gamma(struct drm_intel_overlay_attrs *attrs)
1273{
Chris Wilson722506f2010-08-12 09:28:50 +01001274 if (!check_gamma_bounds(0, attrs->gamma0) ||
1275 !check_gamma_bounds(attrs->gamma0, attrs->gamma1) ||
1276 !check_gamma_bounds(attrs->gamma1, attrs->gamma2) ||
1277 !check_gamma_bounds(attrs->gamma2, attrs->gamma3) ||
1278 !check_gamma_bounds(attrs->gamma3, attrs->gamma4) ||
1279 !check_gamma_bounds(attrs->gamma4, attrs->gamma5) ||
1280 !check_gamma_bounds(attrs->gamma5, 0x00ffffff))
Daniel Vetter02e792f2009-09-15 22:57:34 +02001281 return -EINVAL;
Chris Wilson722506f2010-08-12 09:28:50 +01001282
Daniel Vetter02e792f2009-09-15 22:57:34 +02001283 if (!check_gamma5_errata(attrs->gamma5))
1284 return -EINVAL;
Chris Wilson722506f2010-08-12 09:28:50 +01001285
Daniel Vetter02e792f2009-09-15 22:57:34 +02001286 return 0;
1287}
1288
Chris Wilson1ee8da62016-05-12 12:43:23 +01001289int intel_overlay_attrs_ioctl(struct drm_device *dev, void *data,
1290 struct drm_file *file_priv)
Daniel Vetter02e792f2009-09-15 22:57:34 +02001291{
1292 struct drm_intel_overlay_attrs *attrs = data;
Chris Wilsonfac5e232016-07-04 11:34:36 +01001293 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001294 struct intel_overlay *overlay;
Ben Widawsky75020bc2012-04-16 14:07:43 -07001295 struct overlay_registers __iomem *regs;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001296 int ret;
1297
Daniel Vetter02e792f2009-09-15 22:57:34 +02001298 overlay = dev_priv->overlay;
1299 if (!overlay) {
1300 DRM_DEBUG("userspace bug: no overlay\n");
1301 return -ENODEV;
1302 }
1303
Daniel Vettera0e99e62012-12-02 01:05:46 +01001304 drm_modeset_lock_all(dev);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001305 mutex_lock(&dev->struct_mutex);
1306
Chris Wilson60fc3322010-08-12 10:44:45 +01001307 ret = -EINVAL;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001308 if (!(attrs->flags & I915_OVERLAY_UPDATE_ATTRS)) {
Chris Wilson60fc3322010-08-12 10:44:45 +01001309 attrs->color_key = overlay->color_key;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001310 attrs->brightness = overlay->brightness;
Chris Wilson60fc3322010-08-12 10:44:45 +01001311 attrs->contrast = overlay->contrast;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001312 attrs->saturation = overlay->saturation;
1313
Chris Wilson1ee8da62016-05-12 12:43:23 +01001314 if (!IS_GEN2(dev_priv)) {
Daniel Vetter02e792f2009-09-15 22:57:34 +02001315 attrs->gamma0 = I915_READ(OGAMC0);
1316 attrs->gamma1 = I915_READ(OGAMC1);
1317 attrs->gamma2 = I915_READ(OGAMC2);
1318 attrs->gamma3 = I915_READ(OGAMC3);
1319 attrs->gamma4 = I915_READ(OGAMC4);
1320 attrs->gamma5 = I915_READ(OGAMC5);
1321 }
Daniel Vetter02e792f2009-09-15 22:57:34 +02001322 } else {
Chris Wilson60fc3322010-08-12 10:44:45 +01001323 if (attrs->brightness < -128 || attrs->brightness > 127)
Daniel Vetter02e792f2009-09-15 22:57:34 +02001324 goto out_unlock;
Chris Wilson60fc3322010-08-12 10:44:45 +01001325 if (attrs->contrast > 255)
Daniel Vetter02e792f2009-09-15 22:57:34 +02001326 goto out_unlock;
Chris Wilson60fc3322010-08-12 10:44:45 +01001327 if (attrs->saturation > 1023)
Daniel Vetter02e792f2009-09-15 22:57:34 +02001328 goto out_unlock;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001329
Chris Wilson60fc3322010-08-12 10:44:45 +01001330 overlay->color_key = attrs->color_key;
1331 overlay->brightness = attrs->brightness;
1332 overlay->contrast = attrs->contrast;
1333 overlay->saturation = attrs->saturation;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001334
Chris Wilson8d74f652010-08-12 10:35:26 +01001335 regs = intel_overlay_map_regs(overlay);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001336 if (!regs) {
1337 ret = -ENOMEM;
1338 goto out_unlock;
1339 }
1340
1341 update_reg_attrs(overlay, regs);
1342
Chris Wilson9bb2ff72010-08-12 12:02:11 +01001343 intel_overlay_unmap_regs(overlay, regs);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001344
1345 if (attrs->flags & I915_OVERLAY_UPDATE_GAMMA) {
Chris Wilson1ee8da62016-05-12 12:43:23 +01001346 if (IS_GEN2(dev_priv))
Daniel Vetter02e792f2009-09-15 22:57:34 +02001347 goto out_unlock;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001348
1349 if (overlay->active) {
1350 ret = -EBUSY;
1351 goto out_unlock;
1352 }
1353
1354 ret = check_gamma(attrs);
Chris Wilson60fc3322010-08-12 10:44:45 +01001355 if (ret)
Daniel Vetter02e792f2009-09-15 22:57:34 +02001356 goto out_unlock;
1357
1358 I915_WRITE(OGAMC0, attrs->gamma0);
1359 I915_WRITE(OGAMC1, attrs->gamma1);
1360 I915_WRITE(OGAMC2, attrs->gamma2);
1361 I915_WRITE(OGAMC3, attrs->gamma3);
1362 I915_WRITE(OGAMC4, attrs->gamma4);
1363 I915_WRITE(OGAMC5, attrs->gamma5);
1364 }
Daniel Vetter02e792f2009-09-15 22:57:34 +02001365 }
Chris Wilsonea9da4e2015-04-02 10:35:08 +01001366 overlay->color_key_enabled = (attrs->flags & I915_OVERLAY_DISABLE_DEST_COLORKEY) == 0;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001367
Chris Wilson60fc3322010-08-12 10:44:45 +01001368 ret = 0;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001369out_unlock:
1370 mutex_unlock(&dev->struct_mutex);
Daniel Vettera0e99e62012-12-02 01:05:46 +01001371 drm_modeset_unlock_all(dev);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001372
1373 return ret;
1374}
1375
Chris Wilson1ee8da62016-05-12 12:43:23 +01001376void intel_setup_overlay(struct drm_i915_private *dev_priv)
Daniel Vetter02e792f2009-09-15 22:57:34 +02001377{
Daniel Vetter02e792f2009-09-15 22:57:34 +02001378 struct intel_overlay *overlay;
Chris Wilson05394f32010-11-08 19:18:58 +00001379 struct drm_i915_gem_object *reg_bo;
Ben Widawsky75020bc2012-04-16 14:07:43 -07001380 struct overlay_registers __iomem *regs;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001381 int ret;
1382
Chris Wilson1ee8da62016-05-12 12:43:23 +01001383 if (!HAS_OVERLAY(dev_priv))
Daniel Vetter02e792f2009-09-15 22:57:34 +02001384 return;
1385
Daniel Vetterb14c5672013-09-19 12:18:32 +02001386 overlay = kzalloc(sizeof(*overlay), GFP_KERNEL);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001387 if (!overlay)
1388 return;
Chris Wilson79d24272011-06-28 11:27:47 +01001389
Chris Wilson91c8a322016-07-05 10:40:23 +01001390 mutex_lock(&dev_priv->drm.struct_mutex);
Chris Wilson79d24272011-06-28 11:27:47 +01001391 if (WARN_ON(dev_priv->overlay))
1392 goto out_free;
1393
Chris Wilson1ee8da62016-05-12 12:43:23 +01001394 overlay->i915 = dev_priv;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001395
Daniel Vetterf63a4842013-07-23 19:24:38 +02001396 reg_bo = NULL;
Chris Wilson1ee8da62016-05-12 12:43:23 +01001397 if (!OVERLAY_NEEDS_PHYSICAL(dev_priv))
Chris Wilson91c8a322016-07-05 10:40:23 +01001398 reg_bo = i915_gem_object_create_stolen(&dev_priv->drm,
1399 PAGE_SIZE);
Chris Wilson80405132012-11-15 11:32:29 +00001400 if (reg_bo == NULL)
Chris Wilson91c8a322016-07-05 10:40:23 +01001401 reg_bo = i915_gem_object_create(&dev_priv->drm, PAGE_SIZE);
Chris Wilsonfe3db792016-04-25 13:32:13 +01001402 if (IS_ERR(reg_bo))
Daniel Vetter02e792f2009-09-15 22:57:34 +02001403 goto out_free;
Chris Wilson05394f32010-11-08 19:18:58 +00001404 overlay->reg_bo = reg_bo;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001405
Chris Wilson1ee8da62016-05-12 12:43:23 +01001406 if (OVERLAY_NEEDS_PHYSICAL(dev_priv)) {
Chris Wilson00731152014-05-21 12:42:56 +01001407 ret = i915_gem_object_attach_phys(reg_bo, PAGE_SIZE);
Akshay Joshi0206e352011-08-16 15:34:10 -04001408 if (ret) {
1409 DRM_ERROR("failed to attach phys overlay regs\n");
1410 goto out_free_bo;
1411 }
Chris Wilson00731152014-05-21 12:42:56 +01001412 overlay->flip_addr = reg_bo->phys_handle->busaddr;
Chris Wilson315781482010-08-12 09:42:51 +01001413 } else {
Daniel Vetter1ec9e262014-02-14 14:01:11 +01001414 ret = i915_gem_obj_ggtt_pin(reg_bo, PAGE_SIZE, PIN_MAPPABLE);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001415 if (ret) {
Akshay Joshi0206e352011-08-16 15:34:10 -04001416 DRM_ERROR("failed to pin overlay register bo\n");
1417 goto out_free_bo;
1418 }
Ben Widawskyf343c5f2013-07-05 14:41:04 -07001419 overlay->flip_addr = i915_gem_obj_ggtt_offset(reg_bo);
Chris Wilson0ddc1282010-08-12 09:35:00 +01001420
1421 ret = i915_gem_object_set_to_gtt_domain(reg_bo, true);
1422 if (ret) {
Akshay Joshi0206e352011-08-16 15:34:10 -04001423 DRM_ERROR("failed to move overlay register bo into the GTT\n");
1424 goto out_unpin_bo;
1425 }
Daniel Vetter02e792f2009-09-15 22:57:34 +02001426 }
1427
1428 /* init all values */
1429 overlay->color_key = 0x0101fe;
Chris Wilsonea9da4e2015-04-02 10:35:08 +01001430 overlay->color_key_enabled = true;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001431 overlay->brightness = -19;
1432 overlay->contrast = 75;
1433 overlay->saturation = 146;
1434
Chris Wilson8d74f652010-08-12 10:35:26 +01001435 regs = intel_overlay_map_regs(overlay);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001436 if (!regs)
Chris Wilson79d24272011-06-28 11:27:47 +01001437 goto out_unpin_bo;
Daniel Vetter02e792f2009-09-15 22:57:34 +02001438
Ben Widawsky75020bc2012-04-16 14:07:43 -07001439 memset_io(regs, 0, sizeof(struct overlay_registers));
Daniel Vetter02e792f2009-09-15 22:57:34 +02001440 update_polyphase_filter(regs);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001441 update_reg_attrs(overlay, regs);
1442
Chris Wilson9bb2ff72010-08-12 12:02:11 +01001443 intel_overlay_unmap_regs(overlay, regs);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001444
1445 dev_priv->overlay = overlay;
Chris Wilson91c8a322016-07-05 10:40:23 +01001446 mutex_unlock(&dev_priv->drm.struct_mutex);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001447 DRM_INFO("initialized overlay support\n");
1448 return;
1449
Chris Wilson0ddc1282010-08-12 09:35:00 +01001450out_unpin_bo:
Chris Wilson1ee8da62016-05-12 12:43:23 +01001451 if (!OVERLAY_NEEDS_PHYSICAL(dev_priv))
Ben Widawskyd7f46fc2013-12-06 14:10:55 -08001452 i915_gem_object_ggtt_unpin(reg_bo);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001453out_free_bo:
Chris Wilsonf8c417c2016-07-20 13:31:53 +01001454 i915_gem_object_put(reg_bo);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001455out_free:
Chris Wilson91c8a322016-07-05 10:40:23 +01001456 mutex_unlock(&dev_priv->drm.struct_mutex);
Daniel Vetter02e792f2009-09-15 22:57:34 +02001457 kfree(overlay);
1458 return;
1459}
1460
Chris Wilson1ee8da62016-05-12 12:43:23 +01001461void intel_cleanup_overlay(struct drm_i915_private *dev_priv)
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
Chris Wilson34911fd2016-07-20 13:31:54 +01001471 i915_gem_object_put_unlocked(dev_priv->overlay->reg_bo);
Chris Wilson62cf4e62010-08-12 10:50:36 +01001472 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{
Chris Wilson1ee8da62016-05-12 12:43:23 +01001485 struct drm_i915_private *dev_priv = overlay->i915;
Ben Widawsky75020bc2012-04-16 14:07:43 -07001486 struct overlay_registers __iomem *regs;
Chris Wilson3bd3c932010-08-19 08:19:30 +01001487
Chris Wilson1ee8da62016-05-12 12:43:23 +01001488 if (OVERLAY_NEEDS_PHYSICAL(dev_priv))
Ben Widawsky75020bc2012-04-16 14:07:43 -07001489 /* Cast to make sparse happy, but it's wc memory anyway, so
1490 * equivalent to the wc io mapping on X86. */
1491 regs = (struct overlay_registers __iomem *)
Chris Wilson00731152014-05-21 12:42:56 +01001492 overlay->reg_bo->phys_handle->vaddr;
Chris Wilson3bd3c932010-08-19 08:19:30 +01001493 else
Chris Wilson1ee8da62016-05-12 12:43:23 +01001494 regs = io_mapping_map_atomic_wc(dev_priv->ggtt.mappable,
Chris Wilsonda6ca032016-04-28 09:56:36 +01001495 overlay->flip_addr);
Chris Wilson3bd3c932010-08-19 08:19:30 +01001496
1497 return regs;
1498}
1499
1500static void intel_overlay_unmap_regs_atomic(struct intel_overlay *overlay,
Ben Widawsky75020bc2012-04-16 14:07:43 -07001501 struct overlay_registers __iomem *regs)
Chris Wilson3bd3c932010-08-19 08:19:30 +01001502{
Chris Wilson1ee8da62016-05-12 12:43:23 +01001503 if (!OVERLAY_NEEDS_PHYSICAL(overlay->i915))
Linus Torvaldsc48c43e2010-10-26 18:57:59 -07001504 io_mapping_unmap_atomic(regs);
Chris Wilson3bd3c932010-08-19 08:19:30 +01001505}
1506
Chris Wilson6ef3d422010-08-04 20:26:07 +01001507struct intel_overlay_error_state *
Chris Wilsonc0336662016-05-06 15:40:21 +01001508intel_overlay_capture_error_state(struct drm_i915_private *dev_priv)
Chris Wilson6ef3d422010-08-04 20:26:07 +01001509{
Chris Wilson6ef3d422010-08-04 20:26:07 +01001510 struct intel_overlay *overlay = dev_priv->overlay;
1511 struct intel_overlay_error_state *error;
1512 struct overlay_registers __iomem *regs;
1513
1514 if (!overlay || !overlay->active)
1515 return NULL;
1516
1517 error = kmalloc(sizeof(*error), GFP_ATOMIC);
1518 if (error == NULL)
1519 return NULL;
1520
1521 error->dovsta = I915_READ(DOVSTA);
1522 error->isr = I915_READ(ISR);
Chris Wilsonda6ca032016-04-28 09:56:36 +01001523 error->base = overlay->flip_addr;
Chris Wilson6ef3d422010-08-04 20:26:07 +01001524
1525 regs = intel_overlay_map_regs_atomic(overlay);
1526 if (!regs)
1527 goto err;
1528
1529 memcpy_fromio(&error->regs, regs, sizeof(struct overlay_registers));
Linus Torvaldsc48c43e2010-10-26 18:57:59 -07001530 intel_overlay_unmap_regs_atomic(overlay, regs);
Chris Wilson6ef3d422010-08-04 20:26:07 +01001531
1532 return error;
1533
1534err:
1535 kfree(error);
1536 return NULL;
1537}
1538
1539void
Mika Kuoppalaedc3d882013-05-23 13:55:35 +03001540intel_overlay_print_error_state(struct drm_i915_error_state_buf *m,
1541 struct intel_overlay_error_state *error)
Chris Wilson6ef3d422010-08-04 20:26:07 +01001542{
Mika Kuoppalaedc3d882013-05-23 13:55:35 +03001543 i915_error_printf(m, "Overlay, status: 0x%08x, interrupt: 0x%08x\n",
1544 error->dovsta, error->isr);
1545 i915_error_printf(m, " Register file at 0x%08lx:\n",
1546 error->base);
Chris Wilson6ef3d422010-08-04 20:26:07 +01001547
Mika Kuoppalaedc3d882013-05-23 13:55:35 +03001548#define P(x) i915_error_printf(m, " " #x ": 0x%08x\n", error->regs.x)
Chris Wilson6ef3d422010-08-04 20:26:07 +01001549 P(OBUF_0Y);
1550 P(OBUF_1Y);
1551 P(OBUF_0U);
1552 P(OBUF_0V);
1553 P(OBUF_1U);
1554 P(OBUF_1V);
1555 P(OSTRIDE);
1556 P(YRGB_VPH);
1557 P(UV_VPH);
1558 P(HORZ_PH);
1559 P(INIT_PHS);
1560 P(DWINPOS);
1561 P(DWINSZ);
1562 P(SWIDTH);
1563 P(SWIDTHSW);
1564 P(SHEIGHT);
1565 P(YRGBSCALE);
1566 P(UVSCALE);
1567 P(OCLRC0);
1568 P(OCLRC1);
1569 P(DCLRKV);
1570 P(DCLRKM);
1571 P(SCLRKVH);
1572 P(SCLRKVL);
1573 P(SCLRKEN);
1574 P(OCONFIG);
1575 P(OCMD);
1576 P(OSTART_0Y);
1577 P(OSTART_1Y);
1578 P(OSTART_0U);
1579 P(OSTART_0V);
1580 P(OSTART_1U);
1581 P(OSTART_1V);
1582 P(OTILEOFF_0Y);
1583 P(OTILEOFF_1Y);
1584 P(OTILEOFF_0U);
1585 P(OTILEOFF_0V);
1586 P(OTILEOFF_1U);
1587 P(OTILEOFF_1V);
1588 P(FASTHSCALE);
1589 P(UVSCALEV);
1590#undef P
1591}