xref: /dflybsd-src/sys/dev/drm/i915/intel_overlay.c (revision a85cb24f18e3804e75ab8bcda7692564d0563317)
1e3adcf8fSFrançois Tigeot /*
2e3adcf8fSFrançois Tigeot  * Copyright © 2009
3e3adcf8fSFrançois Tigeot  *
4e3adcf8fSFrançois Tigeot  * Permission is hereby granted, free of charge, to any person obtaining a
5e3adcf8fSFrançois Tigeot  * copy of this software and associated documentation files (the "Software"),
6e3adcf8fSFrançois Tigeot  * to deal in the Software without restriction, including without limitation
7e3adcf8fSFrançois Tigeot  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8e3adcf8fSFrançois Tigeot  * and/or sell copies of the Software, and to permit persons to whom the
9e3adcf8fSFrançois Tigeot  * Software is furnished to do so, subject to the following conditions:
10e3adcf8fSFrançois Tigeot  *
11e3adcf8fSFrançois Tigeot  * The above copyright notice and this permission notice (including the next
12e3adcf8fSFrançois Tigeot  * paragraph) shall be included in all copies or substantial portions of the
13e3adcf8fSFrançois Tigeot  * Software.
14e3adcf8fSFrançois Tigeot  *
15e3adcf8fSFrançois Tigeot  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16e3adcf8fSFrançois Tigeot  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17e3adcf8fSFrançois Tigeot  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18e3adcf8fSFrançois Tigeot  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19e3adcf8fSFrançois Tigeot  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20e3adcf8fSFrançois Tigeot  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21e3adcf8fSFrançois Tigeot  * SOFTWARE.
22e3adcf8fSFrançois Tigeot  *
23e3adcf8fSFrançois Tigeot  * Authors:
24e3adcf8fSFrançois Tigeot  *    Daniel Vetter <daniel@ffwll.ch>
25e3adcf8fSFrançois Tigeot  *
26e3adcf8fSFrançois Tigeot  * Derived from Xorg ddx, xf86-video-intel, src/i830_video.c
27e3adcf8fSFrançois Tigeot  */
2818e26a6dSFrançois Tigeot #include <drm/drmP.h>
295c6c6f23SFrançois Tigeot #include <drm/i915_drm.h>
30e3adcf8fSFrançois Tigeot #include "i915_drv.h"
31e3adcf8fSFrançois Tigeot #include "i915_reg.h"
32e3adcf8fSFrançois Tigeot #include "intel_drv.h"
3371f41f3eSFrançois Tigeot #include "intel_frontbuffer.h"
34e3adcf8fSFrançois Tigeot 
35e3adcf8fSFrançois Tigeot /* Limits for overlay size. According to intel doc, the real limits are:
36e3adcf8fSFrançois Tigeot  * Y width: 4095, UV width (planar): 2047, Y height: 2047,
37e3adcf8fSFrançois Tigeot  * UV width (planar): * 1023. But the xorg thinks 2048 for height and width. Use
38e3adcf8fSFrançois Tigeot  * the mininum of both.  */
39e3adcf8fSFrançois Tigeot #define IMAGE_MAX_WIDTH		2048
40e3adcf8fSFrançois Tigeot #define IMAGE_MAX_HEIGHT	2046 /* 2 * 1023 */
41e3adcf8fSFrançois Tigeot /* on 830 and 845 these large limits result in the card hanging */
42e3adcf8fSFrançois Tigeot #define IMAGE_MAX_WIDTH_LEGACY	1024
43e3adcf8fSFrançois Tigeot #define IMAGE_MAX_HEIGHT_LEGACY	1088
44e3adcf8fSFrançois Tigeot 
45e3adcf8fSFrançois Tigeot /* overlay register definitions */
46e3adcf8fSFrançois Tigeot /* OCMD register */
47e3adcf8fSFrançois Tigeot #define OCMD_TILED_SURFACE	(0x1<<19)
48e3adcf8fSFrançois Tigeot #define OCMD_MIRROR_MASK	(0x3<<17)
49e3adcf8fSFrançois Tigeot #define OCMD_MIRROR_MODE	(0x3<<17)
50e3adcf8fSFrançois Tigeot #define OCMD_MIRROR_HORIZONTAL	(0x1<<17)
51e3adcf8fSFrançois Tigeot #define OCMD_MIRROR_VERTICAL	(0x2<<17)
52e3adcf8fSFrançois Tigeot #define OCMD_MIRROR_BOTH	(0x3<<17)
53e3adcf8fSFrançois Tigeot #define OCMD_BYTEORDER_MASK	(0x3<<14) /* zero for YUYV or FOURCC YUY2 */
54e3adcf8fSFrançois Tigeot #define OCMD_UV_SWAP		(0x1<<14) /* YVYU */
55e3adcf8fSFrançois Tigeot #define OCMD_Y_SWAP		(0x2<<14) /* UYVY or FOURCC UYVY */
56e3adcf8fSFrançois Tigeot #define OCMD_Y_AND_UV_SWAP	(0x3<<14) /* VYUY */
57e3adcf8fSFrançois Tigeot #define OCMD_SOURCE_FORMAT_MASK (0xf<<10)
58e3adcf8fSFrançois Tigeot #define OCMD_RGB_888		(0x1<<10) /* not in i965 Intel docs */
59e3adcf8fSFrançois Tigeot #define OCMD_RGB_555		(0x2<<10) /* not in i965 Intel docs */
60e3adcf8fSFrançois Tigeot #define OCMD_RGB_565		(0x3<<10) /* not in i965 Intel docs */
61e3adcf8fSFrançois Tigeot #define OCMD_YUV_422_PACKED	(0x8<<10)
62e3adcf8fSFrançois Tigeot #define OCMD_YUV_411_PACKED	(0x9<<10) /* not in i965 Intel docs */
63e3adcf8fSFrançois Tigeot #define OCMD_YUV_420_PLANAR	(0xc<<10)
64e3adcf8fSFrançois Tigeot #define OCMD_YUV_422_PLANAR	(0xd<<10)
65e3adcf8fSFrançois Tigeot #define OCMD_YUV_410_PLANAR	(0xe<<10) /* also 411 */
66e3adcf8fSFrançois Tigeot #define OCMD_TVSYNCFLIP_PARITY	(0x1<<9)
67e3adcf8fSFrançois Tigeot #define OCMD_TVSYNCFLIP_ENABLE	(0x1<<7)
68e3adcf8fSFrançois Tigeot #define OCMD_BUF_TYPE_MASK	(0x1<<5)
69e3adcf8fSFrançois Tigeot #define OCMD_BUF_TYPE_FRAME	(0x0<<5)
70e3adcf8fSFrançois Tigeot #define OCMD_BUF_TYPE_FIELD	(0x1<<5)
71e3adcf8fSFrançois Tigeot #define OCMD_TEST_MODE		(0x1<<4)
72e3adcf8fSFrançois Tigeot #define OCMD_BUFFER_SELECT	(0x3<<2)
73e3adcf8fSFrançois Tigeot #define OCMD_BUFFER0		(0x0<<2)
74e3adcf8fSFrançois Tigeot #define OCMD_BUFFER1		(0x1<<2)
75e3adcf8fSFrançois Tigeot #define OCMD_FIELD_SELECT	(0x1<<2)
76e3adcf8fSFrançois Tigeot #define OCMD_FIELD0		(0x0<<1)
77e3adcf8fSFrançois Tigeot #define OCMD_FIELD1		(0x1<<1)
78e3adcf8fSFrançois Tigeot #define OCMD_ENABLE		(0x1<<0)
79e3adcf8fSFrançois Tigeot 
80e3adcf8fSFrançois Tigeot /* OCONFIG register */
81e3adcf8fSFrançois Tigeot #define OCONF_PIPE_MASK		(0x1<<18)
82e3adcf8fSFrançois Tigeot #define OCONF_PIPE_A		(0x0<<18)
83e3adcf8fSFrançois Tigeot #define OCONF_PIPE_B		(0x1<<18)
84e3adcf8fSFrançois Tigeot #define OCONF_GAMMA2_ENABLE	(0x1<<16)
85e3adcf8fSFrançois Tigeot #define OCONF_CSC_MODE_BT601	(0x0<<5)
86e3adcf8fSFrançois Tigeot #define OCONF_CSC_MODE_BT709	(0x1<<5)
87e3adcf8fSFrançois Tigeot #define OCONF_CSC_BYPASS	(0x1<<4)
88e3adcf8fSFrançois Tigeot #define OCONF_CC_OUT_8BIT	(0x1<<3)
89e3adcf8fSFrançois Tigeot #define OCONF_TEST_MODE		(0x1<<2)
90e3adcf8fSFrançois Tigeot #define OCONF_THREE_LINE_BUFFER	(0x1<<0)
91e3adcf8fSFrançois Tigeot #define OCONF_TWO_LINE_BUFFER	(0x0<<0)
92e3adcf8fSFrançois Tigeot 
93e3adcf8fSFrançois Tigeot /* DCLRKM (dst-key) register */
94e3adcf8fSFrançois Tigeot #define DST_KEY_ENABLE		(0x1<<31)
95e3adcf8fSFrançois Tigeot #define CLK_RGB24_MASK		0x0
96e3adcf8fSFrançois Tigeot #define CLK_RGB16_MASK		0x070307
97e3adcf8fSFrançois Tigeot #define CLK_RGB15_MASK		0x070707
98e3adcf8fSFrançois Tigeot #define CLK_RGB8I_MASK		0xffffff
99e3adcf8fSFrançois Tigeot 
100e3adcf8fSFrançois Tigeot #define RGB16_TO_COLORKEY(c) \
101e3adcf8fSFrançois Tigeot 	(((c & 0xF800) << 8) | ((c & 0x07E0) << 5) | ((c & 0x001F) << 3))
102e3adcf8fSFrançois Tigeot #define RGB15_TO_COLORKEY(c) \
103e3adcf8fSFrançois Tigeot 	(((c & 0x7c00) << 9) | ((c & 0x03E0) << 6) | ((c & 0x001F) << 3))
104e3adcf8fSFrançois Tigeot 
105e3adcf8fSFrançois Tigeot /* overlay flip addr flag */
106e3adcf8fSFrançois Tigeot #define OFC_UPDATE		0x1
107e3adcf8fSFrançois Tigeot 
108e3adcf8fSFrançois Tigeot /* polyphase filter coefficients */
109e3adcf8fSFrançois Tigeot #define N_HORIZ_Y_TAPS          5
110e3adcf8fSFrançois Tigeot #define N_VERT_Y_TAPS           3
111e3adcf8fSFrançois Tigeot #define N_HORIZ_UV_TAPS         3
112e3adcf8fSFrançois Tigeot #define N_VERT_UV_TAPS          3
113e3adcf8fSFrançois Tigeot #define N_PHASES                17
114e3adcf8fSFrançois Tigeot #define MAX_TAPS                5
115e3adcf8fSFrançois Tigeot 
116e3adcf8fSFrançois Tigeot /* memory bufferd overlay registers */
117e3adcf8fSFrançois Tigeot struct overlay_registers {
118e3adcf8fSFrançois Tigeot 	u32 OBUF_0Y;
119e3adcf8fSFrançois Tigeot 	u32 OBUF_1Y;
120e3adcf8fSFrançois Tigeot 	u32 OBUF_0U;
121e3adcf8fSFrançois Tigeot 	u32 OBUF_0V;
122e3adcf8fSFrançois Tigeot 	u32 OBUF_1U;
123e3adcf8fSFrançois Tigeot 	u32 OBUF_1V;
124e3adcf8fSFrançois Tigeot 	u32 OSTRIDE;
125e3adcf8fSFrançois Tigeot 	u32 YRGB_VPH;
126e3adcf8fSFrançois Tigeot 	u32 UV_VPH;
127e3adcf8fSFrançois Tigeot 	u32 HORZ_PH;
128e3adcf8fSFrançois Tigeot 	u32 INIT_PHS;
129e3adcf8fSFrançois Tigeot 	u32 DWINPOS;
130e3adcf8fSFrançois Tigeot 	u32 DWINSZ;
131e3adcf8fSFrançois Tigeot 	u32 SWIDTH;
132e3adcf8fSFrançois Tigeot 	u32 SWIDTHSW;
133e3adcf8fSFrançois Tigeot 	u32 SHEIGHT;
134e3adcf8fSFrançois Tigeot 	u32 YRGBSCALE;
135e3adcf8fSFrançois Tigeot 	u32 UVSCALE;
136e3adcf8fSFrançois Tigeot 	u32 OCLRC0;
137e3adcf8fSFrançois Tigeot 	u32 OCLRC1;
138e3adcf8fSFrançois Tigeot 	u32 DCLRKV;
139e3adcf8fSFrançois Tigeot 	u32 DCLRKM;
140e3adcf8fSFrançois Tigeot 	u32 SCLRKVH;
141e3adcf8fSFrançois Tigeot 	u32 SCLRKVL;
142e3adcf8fSFrançois Tigeot 	u32 SCLRKEN;
143e3adcf8fSFrançois Tigeot 	u32 OCONFIG;
144e3adcf8fSFrançois Tigeot 	u32 OCMD;
145e3adcf8fSFrançois Tigeot 	u32 RESERVED1; /* 0x6C */
146e3adcf8fSFrançois Tigeot 	u32 OSTART_0Y;
147e3adcf8fSFrançois Tigeot 	u32 OSTART_1Y;
148e3adcf8fSFrançois Tigeot 	u32 OSTART_0U;
149e3adcf8fSFrançois Tigeot 	u32 OSTART_0V;
150e3adcf8fSFrançois Tigeot 	u32 OSTART_1U;
151e3adcf8fSFrançois Tigeot 	u32 OSTART_1V;
152e3adcf8fSFrançois Tigeot 	u32 OTILEOFF_0Y;
153e3adcf8fSFrançois Tigeot 	u32 OTILEOFF_1Y;
154e3adcf8fSFrançois Tigeot 	u32 OTILEOFF_0U;
155e3adcf8fSFrançois Tigeot 	u32 OTILEOFF_0V;
156e3adcf8fSFrançois Tigeot 	u32 OTILEOFF_1U;
157e3adcf8fSFrançois Tigeot 	u32 OTILEOFF_1V;
158e3adcf8fSFrançois Tigeot 	u32 FASTHSCALE; /* 0xA0 */
159e3adcf8fSFrançois Tigeot 	u32 UVSCALEV; /* 0xA4 */
160e3adcf8fSFrançois Tigeot 	u32 RESERVEDC[(0x200 - 0xA8) / 4]; /* 0xA8 - 0x1FC */
161e3adcf8fSFrançois Tigeot 	u16 Y_VCOEFS[N_VERT_Y_TAPS * N_PHASES]; /* 0x200 */
162e3adcf8fSFrançois Tigeot 	u16 RESERVEDD[0x100 / 2 - N_VERT_Y_TAPS * N_PHASES];
163e3adcf8fSFrançois Tigeot 	u16 Y_HCOEFS[N_HORIZ_Y_TAPS * N_PHASES]; /* 0x300 */
164e3adcf8fSFrançois Tigeot 	u16 RESERVEDE[0x200 / 2 - N_HORIZ_Y_TAPS * N_PHASES];
165e3adcf8fSFrançois Tigeot 	u16 UV_VCOEFS[N_VERT_UV_TAPS * N_PHASES]; /* 0x500 */
166e3adcf8fSFrançois Tigeot 	u16 RESERVEDF[0x100 / 2 - N_VERT_UV_TAPS * N_PHASES];
167e3adcf8fSFrançois Tigeot 	u16 UV_HCOEFS[N_HORIZ_UV_TAPS * N_PHASES]; /* 0x600 */
168e3adcf8fSFrançois Tigeot 	u16 RESERVEDG[0x100 / 2 - N_HORIZ_UV_TAPS * N_PHASES];
169e3adcf8fSFrançois Tigeot };
170e3adcf8fSFrançois Tigeot 
171e3adcf8fSFrançois Tigeot struct intel_overlay {
1721487f786SFrançois Tigeot 	struct drm_i915_private *i915;
173e3adcf8fSFrançois Tigeot 	struct intel_crtc *crtc;
1741e12ee3bSFrançois Tigeot 	struct i915_vma *vma;
1751e12ee3bSFrançois Tigeot 	struct i915_vma *old_vma;
17619c468b4SFrançois Tigeot 	bool active;
17719c468b4SFrançois Tigeot 	bool pfit_active;
178e3adcf8fSFrançois Tigeot 	u32 pfit_vscale_ratio; /* shifted-point number, (1<<12) == 1.0 */
17919c468b4SFrançois Tigeot 	u32 color_key:24;
18019c468b4SFrançois Tigeot 	u32 color_key_enabled:1;
181e3adcf8fSFrançois Tigeot 	u32 brightness, contrast, saturation;
182e3adcf8fSFrançois Tigeot 	u32 old_xscale, old_yscale;
183e3adcf8fSFrançois Tigeot 	/* register access */
184e3adcf8fSFrançois Tigeot 	u32 flip_addr;
185e3adcf8fSFrançois Tigeot 	struct drm_i915_gem_object *reg_bo;
186e3adcf8fSFrançois Tigeot 	/* flip handling */
18771f41f3eSFrançois Tigeot 	struct i915_gem_active last_flip;
188e3adcf8fSFrançois Tigeot };
189e3adcf8fSFrançois Tigeot 
190*a85cb24fSFrançois Tigeot static void i830_overlay_clock_gating(struct drm_i915_private *dev_priv,
191*a85cb24fSFrançois Tigeot 				      bool enable)
192*a85cb24fSFrançois Tigeot {
193*a85cb24fSFrançois Tigeot 	struct pci_dev *pdev = dev_priv->drm.pdev;
194*a85cb24fSFrançois Tigeot 	u8 val;
195*a85cb24fSFrançois Tigeot 
196*a85cb24fSFrançois Tigeot 	/* WA_OVERLAY_CLKGATE:alm */
197*a85cb24fSFrançois Tigeot 	if (enable)
198*a85cb24fSFrançois Tigeot 		I915_WRITE(DSPCLK_GATE_D, 0);
199*a85cb24fSFrançois Tigeot 	else
200*a85cb24fSFrançois Tigeot 		I915_WRITE(DSPCLK_GATE_D, OVRUNIT_CLOCK_GATE_DISABLE);
201*a85cb24fSFrançois Tigeot 
202*a85cb24fSFrançois Tigeot 	/* WA_DISABLE_L2CACHE_CLOCK_GATING:alm */
203*a85cb24fSFrançois Tigeot 	pci_bus_read_config_byte(pdev->bus,
204*a85cb24fSFrançois Tigeot 				 PCI_DEVFN(0, 0), I830_CLOCK_GATE, &val);
205*a85cb24fSFrançois Tigeot 	if (enable)
206*a85cb24fSFrançois Tigeot 		val &= ~I830_L2_CACHE_CLOCK_GATE_DISABLE;
207*a85cb24fSFrançois Tigeot 	else
208*a85cb24fSFrançois Tigeot 		val |= I830_L2_CACHE_CLOCK_GATE_DISABLE;
209*a85cb24fSFrançois Tigeot #if 0
210*a85cb24fSFrançois Tigeot 	pci_bus_write_config_byte(pdev->bus,
211*a85cb24fSFrançois Tigeot 				  PCI_DEVFN(0, 0), I830_CLOCK_GATE, val);
212*a85cb24fSFrançois Tigeot #endif
213*a85cb24fSFrançois Tigeot }
214*a85cb24fSFrançois Tigeot 
215e3440f96SFrançois Tigeot static struct overlay_registers __iomem *
216e3adcf8fSFrançois Tigeot intel_overlay_map_regs(struct intel_overlay *overlay)
217e3adcf8fSFrançois Tigeot {
2181487f786SFrançois Tigeot 	struct drm_i915_private *dev_priv = overlay->i915;
219e3440f96SFrançois Tigeot 	struct overlay_registers __iomem *regs;
220e3adcf8fSFrançois Tigeot 
2211487f786SFrançois Tigeot 	if (OVERLAY_NEEDS_PHYSICAL(dev_priv))
222ba55f2f5SFrançois Tigeot 		regs = (struct overlay_registers __iomem *)overlay->reg_bo->phys_handle->vaddr;
223e3440f96SFrançois Tigeot 	else
2241e12ee3bSFrançois Tigeot 		regs = io_mapping_map_wc(&dev_priv->ggtt.mappable,
2251487f786SFrançois Tigeot 					 overlay->flip_addr,
2261487f786SFrançois Tigeot 					 PAGE_SIZE);
227e3440f96SFrançois Tigeot 
228e3440f96SFrançois Tigeot 	return regs;
229e3adcf8fSFrançois Tigeot }
230e3adcf8fSFrançois Tigeot 
231e3adcf8fSFrançois Tigeot static void intel_overlay_unmap_regs(struct intel_overlay *overlay,
232e3440f96SFrançois Tigeot 				     struct overlay_registers __iomem *regs)
233e3adcf8fSFrançois Tigeot {
2341487f786SFrançois Tigeot 	if (!OVERLAY_NEEDS_PHYSICAL(overlay->i915))
235bf017597SFrançois Tigeot 		io_mapping_unmap(regs);
236e3adcf8fSFrançois Tigeot }
237e3adcf8fSFrançois Tigeot 
23871f41f3eSFrançois Tigeot static void intel_overlay_submit_request(struct intel_overlay *overlay,
23971f41f3eSFrançois Tigeot 					 struct drm_i915_gem_request *req,
24071f41f3eSFrançois Tigeot 					 i915_gem_retire_fn retire)
24171f41f3eSFrançois Tigeot {
24271f41f3eSFrançois Tigeot 	GEM_BUG_ON(i915_gem_active_peek(&overlay->last_flip,
24371f41f3eSFrançois Tigeot 					&overlay->i915->drm.struct_mutex));
2444be47400SFrançois Tigeot 	i915_gem_active_set_retire_fn(&overlay->last_flip, retire,
2454be47400SFrançois Tigeot 				      &overlay->i915->drm.struct_mutex);
24671f41f3eSFrançois Tigeot 	i915_gem_active_set(&overlay->last_flip, req);
24771f41f3eSFrançois Tigeot 	i915_add_request(req);
24871f41f3eSFrançois Tigeot }
24971f41f3eSFrançois Tigeot 
250e3adcf8fSFrançois Tigeot static int intel_overlay_do_wait_request(struct intel_overlay *overlay,
251a05eeebfSFrançois Tigeot 					 struct drm_i915_gem_request *req,
25271f41f3eSFrançois Tigeot 					 i915_gem_retire_fn retire)
253e3adcf8fSFrançois Tigeot {
25471f41f3eSFrançois Tigeot 	intel_overlay_submit_request(overlay, req, retire);
25571f41f3eSFrançois Tigeot 	return i915_gem_active_retire(&overlay->last_flip,
25671f41f3eSFrançois Tigeot 				      &overlay->i915->drm.struct_mutex);
25771f41f3eSFrançois Tigeot }
258e3adcf8fSFrançois Tigeot 
25971f41f3eSFrançois Tigeot static struct drm_i915_gem_request *alloc_request(struct intel_overlay *overlay)
26071f41f3eSFrançois Tigeot {
26171f41f3eSFrançois Tigeot 	struct drm_i915_private *dev_priv = overlay->i915;
2621e12ee3bSFrançois Tigeot 	struct intel_engine_cs *engine = dev_priv->engine[RCS];
263f192107fSFrançois Tigeot 
26471f41f3eSFrançois Tigeot 	return i915_gem_request_alloc(engine, dev_priv->kernel_context);
265e3adcf8fSFrançois Tigeot }
266e3adcf8fSFrançois Tigeot 
267e3adcf8fSFrançois Tigeot /* overlay needs to be disable in OCMD reg */
268e3adcf8fSFrançois Tigeot static int intel_overlay_on(struct intel_overlay *overlay)
269e3adcf8fSFrançois Tigeot {
2701487f786SFrançois Tigeot 	struct drm_i915_private *dev_priv = overlay->i915;
271a05eeebfSFrançois Tigeot 	struct drm_i915_gem_request *req;
272*a85cb24fSFrançois Tigeot 	u32 *cs;
273e3adcf8fSFrançois Tigeot 
27419c468b4SFrançois Tigeot 	WARN_ON(overlay->active);
2751487f786SFrançois Tigeot 	WARN_ON(IS_I830(dev_priv) && !(dev_priv->quirks & QUIRK_PIPEA_FORCE));
276e3adcf8fSFrançois Tigeot 
27771f41f3eSFrançois Tigeot 	req = alloc_request(overlay);
278c0e85e96SFrançois Tigeot 	if (IS_ERR(req))
279c0e85e96SFrançois Tigeot 		return PTR_ERR(req);
280f192107fSFrançois Tigeot 
281*a85cb24fSFrançois Tigeot 	cs = intel_ring_begin(req, 4);
282*a85cb24fSFrançois Tigeot 	if (IS_ERR(cs)) {
283*a85cb24fSFrançois Tigeot 		i915_add_request(req);
284*a85cb24fSFrançois Tigeot 		return PTR_ERR(cs);
285a05eeebfSFrançois Tigeot 	}
286a05eeebfSFrançois Tigeot 
28719c468b4SFrançois Tigeot 	overlay->active = true;
28819c468b4SFrançois Tigeot 
289*a85cb24fSFrançois Tigeot 	if (IS_I830(dev_priv))
290*a85cb24fSFrançois Tigeot 		i830_overlay_clock_gating(dev_priv, false);
291*a85cb24fSFrançois Tigeot 
292*a85cb24fSFrançois Tigeot 	*cs++ = MI_OVERLAY_FLIP | MI_OVERLAY_ON;
293*a85cb24fSFrançois Tigeot 	*cs++ = overlay->flip_addr | OFC_UPDATE;
294*a85cb24fSFrançois Tigeot 	*cs++ = MI_WAIT_FOR_EVENT | MI_WAIT_FOR_OVERLAY_FLIP;
295*a85cb24fSFrançois Tigeot 	*cs++ = MI_NOOP;
296*a85cb24fSFrançois Tigeot 	intel_ring_advance(req, cs);
297f192107fSFrançois Tigeot 
298a05eeebfSFrançois Tigeot 	return intel_overlay_do_wait_request(overlay, req, NULL);
299e3adcf8fSFrançois Tigeot }
300e3adcf8fSFrançois Tigeot 
301*a85cb24fSFrançois Tigeot static void intel_overlay_flip_prepare(struct intel_overlay *overlay,
302*a85cb24fSFrançois Tigeot 				       struct i915_vma *vma)
303*a85cb24fSFrançois Tigeot {
304*a85cb24fSFrançois Tigeot 	enum i915_pipe pipe = overlay->crtc->pipe;
305*a85cb24fSFrançois Tigeot 
306*a85cb24fSFrançois Tigeot 	WARN_ON(overlay->old_vma);
307*a85cb24fSFrançois Tigeot 
308*a85cb24fSFrançois Tigeot 	i915_gem_track_fb(overlay->vma ? overlay->vma->obj : NULL,
309*a85cb24fSFrançois Tigeot 			  vma ? vma->obj : NULL,
310*a85cb24fSFrançois Tigeot 			  INTEL_FRONTBUFFER_OVERLAY(pipe));
311*a85cb24fSFrançois Tigeot 
312*a85cb24fSFrançois Tigeot 	intel_frontbuffer_flip_prepare(overlay->i915,
313*a85cb24fSFrançois Tigeot 				       INTEL_FRONTBUFFER_OVERLAY(pipe));
314*a85cb24fSFrançois Tigeot 
315*a85cb24fSFrançois Tigeot 	overlay->old_vma = overlay->vma;
316*a85cb24fSFrançois Tigeot 	if (vma)
317*a85cb24fSFrançois Tigeot 		overlay->vma = i915_vma_get(vma);
318*a85cb24fSFrançois Tigeot 	else
319*a85cb24fSFrançois Tigeot 		overlay->vma = NULL;
320*a85cb24fSFrançois Tigeot }
321*a85cb24fSFrançois Tigeot 
322e3adcf8fSFrançois Tigeot /* overlay needs to be enabled in OCMD reg */
323e3adcf8fSFrançois Tigeot static int intel_overlay_continue(struct intel_overlay *overlay,
324*a85cb24fSFrançois Tigeot 				  struct i915_vma *vma,
325e3adcf8fSFrançois Tigeot 				  bool load_polyphase_filter)
326e3adcf8fSFrançois Tigeot {
3271487f786SFrançois Tigeot 	struct drm_i915_private *dev_priv = overlay->i915;
328a05eeebfSFrançois Tigeot 	struct drm_i915_gem_request *req;
329e3adcf8fSFrançois Tigeot 	u32 flip_addr = overlay->flip_addr;
330*a85cb24fSFrançois Tigeot 	u32 tmp, *cs;
331e3adcf8fSFrançois Tigeot 
33219c468b4SFrançois Tigeot 	WARN_ON(!overlay->active);
333e3adcf8fSFrançois Tigeot 
334e3adcf8fSFrançois Tigeot 	if (load_polyphase_filter)
335e3adcf8fSFrançois Tigeot 		flip_addr |= OFC_UPDATE;
336e3adcf8fSFrançois Tigeot 
337e3adcf8fSFrançois Tigeot 	/* check for underruns */
338e3adcf8fSFrançois Tigeot 	tmp = I915_READ(DOVSTA);
339e3adcf8fSFrançois Tigeot 	if (tmp & (1 << 17))
340e3adcf8fSFrançois Tigeot 		DRM_DEBUG("overlay underrun, DOVSTA: %x\n", tmp);
341e3adcf8fSFrançois Tigeot 
34271f41f3eSFrançois Tigeot 	req = alloc_request(overlay);
343c0e85e96SFrançois Tigeot 	if (IS_ERR(req))
344c0e85e96SFrançois Tigeot 		return PTR_ERR(req);
345e3adcf8fSFrançois Tigeot 
346*a85cb24fSFrançois Tigeot 	cs = intel_ring_begin(req, 2);
347*a85cb24fSFrançois Tigeot 	if (IS_ERR(cs)) {
348*a85cb24fSFrançois Tigeot 		i915_add_request(req);
349*a85cb24fSFrançois Tigeot 		return PTR_ERR(cs);
350a05eeebfSFrançois Tigeot 	}
351a05eeebfSFrançois Tigeot 
352*a85cb24fSFrançois Tigeot 	*cs++ = MI_OVERLAY_FLIP | MI_OVERLAY_CONTINUE;
353*a85cb24fSFrançois Tigeot 	*cs++ = flip_addr;
354*a85cb24fSFrançois Tigeot 	intel_ring_advance(req, cs);
355*a85cb24fSFrançois Tigeot 
356*a85cb24fSFrançois Tigeot 	intel_overlay_flip_prepare(overlay, vma);
357e3adcf8fSFrançois Tigeot 
35871f41f3eSFrançois Tigeot 	intel_overlay_submit_request(overlay, req, NULL);
359a05eeebfSFrançois Tigeot 
360a05eeebfSFrançois Tigeot 	return 0;
361e3adcf8fSFrançois Tigeot }
362e3adcf8fSFrançois Tigeot 
363*a85cb24fSFrançois Tigeot static void intel_overlay_release_old_vma(struct intel_overlay *overlay)
364*a85cb24fSFrançois Tigeot {
365*a85cb24fSFrançois Tigeot 	struct i915_vma *vma;
366*a85cb24fSFrançois Tigeot 
367*a85cb24fSFrançois Tigeot 	vma = fetch_and_zero(&overlay->old_vma);
368*a85cb24fSFrançois Tigeot 	if (WARN_ON(!vma))
369*a85cb24fSFrançois Tigeot 		return;
370*a85cb24fSFrançois Tigeot 
371*a85cb24fSFrançois Tigeot 	intel_frontbuffer_flip_complete(overlay->i915,
372*a85cb24fSFrançois Tigeot 					INTEL_FRONTBUFFER_OVERLAY(overlay->crtc->pipe));
373*a85cb24fSFrançois Tigeot 
374*a85cb24fSFrançois Tigeot 	i915_gem_object_unpin_from_display_plane(vma);
375*a85cb24fSFrançois Tigeot 	i915_vma_put(vma);
376*a85cb24fSFrançois Tigeot }
377*a85cb24fSFrançois Tigeot 
37871f41f3eSFrançois Tigeot static void intel_overlay_release_old_vid_tail(struct i915_gem_active *active,
37971f41f3eSFrançois Tigeot 					       struct drm_i915_gem_request *req)
380e3adcf8fSFrançois Tigeot {
38171f41f3eSFrançois Tigeot 	struct intel_overlay *overlay =
38271f41f3eSFrançois Tigeot 		container_of(active, typeof(*overlay), last_flip);
383e3adcf8fSFrançois Tigeot 
384*a85cb24fSFrançois Tigeot 	intel_overlay_release_old_vma(overlay);
385e3adcf8fSFrançois Tigeot }
386e3adcf8fSFrançois Tigeot 
38771f41f3eSFrançois Tigeot static void intel_overlay_off_tail(struct i915_gem_active *active,
38871f41f3eSFrançois Tigeot 				   struct drm_i915_gem_request *req)
389e3adcf8fSFrançois Tigeot {
39071f41f3eSFrançois Tigeot 	struct intel_overlay *overlay =
39171f41f3eSFrançois Tigeot 		container_of(active, typeof(*overlay), last_flip);
392*a85cb24fSFrançois Tigeot 	struct drm_i915_private *dev_priv = overlay->i915;
393e3adcf8fSFrançois Tigeot 
394*a85cb24fSFrançois Tigeot 	intel_overlay_release_old_vma(overlay);
395e3adcf8fSFrançois Tigeot 
396e3adcf8fSFrançois Tigeot 	overlay->crtc->overlay = NULL;
397e3adcf8fSFrançois Tigeot 	overlay->crtc = NULL;
39819c468b4SFrançois Tigeot 	overlay->active = false;
399*a85cb24fSFrançois Tigeot 
400*a85cb24fSFrançois Tigeot 	if (IS_I830(dev_priv))
401*a85cb24fSFrançois Tigeot 		i830_overlay_clock_gating(dev_priv, true);
402e3adcf8fSFrançois Tigeot }
403e3adcf8fSFrançois Tigeot 
404e3adcf8fSFrançois Tigeot /* overlay needs to be disabled in OCMD reg */
405e3adcf8fSFrançois Tigeot static int intel_overlay_off(struct intel_overlay *overlay)
406e3adcf8fSFrançois Tigeot {
407a05eeebfSFrançois Tigeot 	struct drm_i915_gem_request *req;
408*a85cb24fSFrançois Tigeot 	u32 *cs, flip_addr = overlay->flip_addr;
409e3adcf8fSFrançois Tigeot 
41019c468b4SFrançois Tigeot 	WARN_ON(!overlay->active);
411e3adcf8fSFrançois Tigeot 
412e3adcf8fSFrançois Tigeot 	/* According to intel docs the overlay hw may hang (when switching
413e3adcf8fSFrançois Tigeot 	 * off) without loading the filter coeffs. It is however unclear whether
414e3adcf8fSFrançois Tigeot 	 * this applies to the disabling of the overlay or to the switching off
415e3adcf8fSFrançois Tigeot 	 * of the hw. Do it in both cases */
416e3adcf8fSFrançois Tigeot 	flip_addr |= OFC_UPDATE;
417e3adcf8fSFrançois Tigeot 
41871f41f3eSFrançois Tigeot 	req = alloc_request(overlay);
419c0e85e96SFrançois Tigeot 	if (IS_ERR(req))
420c0e85e96SFrançois Tigeot 		return PTR_ERR(req);
421e3adcf8fSFrançois Tigeot 
422*a85cb24fSFrançois Tigeot 	cs = intel_ring_begin(req, 6);
423*a85cb24fSFrançois Tigeot 	if (IS_ERR(cs)) {
424*a85cb24fSFrançois Tigeot 		i915_add_request(req);
425*a85cb24fSFrançois Tigeot 		return PTR_ERR(cs);
426a05eeebfSFrançois Tigeot 	}
427a05eeebfSFrançois Tigeot 
428f192107fSFrançois Tigeot 	/* wait for overlay to go idle */
429*a85cb24fSFrançois Tigeot 	*cs++ = MI_OVERLAY_FLIP | MI_OVERLAY_CONTINUE;
430*a85cb24fSFrançois Tigeot 	*cs++ = flip_addr;
431*a85cb24fSFrançois Tigeot 	*cs++ = MI_WAIT_FOR_EVENT | MI_WAIT_FOR_OVERLAY_FLIP;
432*a85cb24fSFrançois Tigeot 
433f192107fSFrançois Tigeot 	/* turn overlay off */
434*a85cb24fSFrançois Tigeot 	*cs++ = MI_OVERLAY_FLIP | MI_OVERLAY_OFF;
435*a85cb24fSFrançois Tigeot 	*cs++ = flip_addr;
436*a85cb24fSFrançois Tigeot 	*cs++ = MI_WAIT_FOR_EVENT | MI_WAIT_FOR_OVERLAY_FLIP;
437*a85cb24fSFrançois Tigeot 
438*a85cb24fSFrançois Tigeot 	intel_ring_advance(req, cs);
439*a85cb24fSFrançois Tigeot 
440*a85cb24fSFrançois Tigeot 	intel_overlay_flip_prepare(overlay, NULL);
441f192107fSFrançois Tigeot 
44271f41f3eSFrançois Tigeot 	return intel_overlay_do_wait_request(overlay, req,
44371f41f3eSFrançois Tigeot 					     intel_overlay_off_tail);
444e3adcf8fSFrançois Tigeot }
445e3adcf8fSFrançois Tigeot 
446e3adcf8fSFrançois Tigeot /* recover from an interruption due to a signal
447e3adcf8fSFrançois Tigeot  * We have to be careful not to repeat work forever an make forward progess. */
448e3adcf8fSFrançois Tigeot static int intel_overlay_recover_from_interrupt(struct intel_overlay *overlay)
449e3adcf8fSFrançois Tigeot {
45071f41f3eSFrançois Tigeot 	return i915_gem_active_retire(&overlay->last_flip,
45171f41f3eSFrançois Tigeot 				      &overlay->i915->drm.struct_mutex);
452e3adcf8fSFrançois Tigeot }
453e3adcf8fSFrançois Tigeot 
454e3adcf8fSFrançois Tigeot /* Wait for pending overlay flip and release old frame.
455e3adcf8fSFrançois Tigeot  * Needs to be called before the overlay register are changed
456e3adcf8fSFrançois Tigeot  * via intel_overlay_(un)map_regs
457e3adcf8fSFrançois Tigeot  */
458e3adcf8fSFrançois Tigeot static int intel_overlay_release_old_vid(struct intel_overlay *overlay)
459e3adcf8fSFrançois Tigeot {
4601487f786SFrançois Tigeot 	struct drm_i915_private *dev_priv = overlay->i915;
461*a85cb24fSFrançois Tigeot 	u32 *cs;
462e3adcf8fSFrançois Tigeot 	int ret;
463e3adcf8fSFrançois Tigeot 
464303bf270SFrançois Tigeot 	lockdep_assert_held(&dev_priv->drm.struct_mutex);
4652c9916cdSFrançois Tigeot 
466e3adcf8fSFrançois Tigeot 	/* Only wait if there is actually an old frame to release to
467e3adcf8fSFrançois Tigeot 	 * guarantee forward progress.
468e3adcf8fSFrançois Tigeot 	 */
4691e12ee3bSFrançois Tigeot 	if (!overlay->old_vma)
470e3adcf8fSFrançois Tigeot 		return 0;
471e3adcf8fSFrançois Tigeot 
472e3adcf8fSFrançois Tigeot 	if (I915_READ(ISR) & I915_OVERLAY_PLANE_FLIP_PENDING_INTERRUPT) {
473e3adcf8fSFrançois Tigeot 		/* synchronous slowpath */
474a05eeebfSFrançois Tigeot 		struct drm_i915_gem_request *req;
475a05eeebfSFrançois Tigeot 
47671f41f3eSFrançois Tigeot 		req = alloc_request(overlay);
477c0e85e96SFrançois Tigeot 		if (IS_ERR(req))
478c0e85e96SFrançois Tigeot 			return PTR_ERR(req);
479e3adcf8fSFrançois Tigeot 
480*a85cb24fSFrançois Tigeot 		cs = intel_ring_begin(req, 2);
481*a85cb24fSFrançois Tigeot 		if (IS_ERR(cs)) {
482*a85cb24fSFrançois Tigeot 			i915_add_request(req);
483*a85cb24fSFrançois Tigeot 			return PTR_ERR(cs);
484a05eeebfSFrançois Tigeot 		}
485a05eeebfSFrançois Tigeot 
486*a85cb24fSFrançois Tigeot 		*cs++ = MI_WAIT_FOR_EVENT | MI_WAIT_FOR_OVERLAY_FLIP;
487*a85cb24fSFrançois Tigeot 		*cs++ = MI_NOOP;
488*a85cb24fSFrançois Tigeot 		intel_ring_advance(req, cs);
489e3adcf8fSFrançois Tigeot 
490a05eeebfSFrançois Tigeot 		ret = intel_overlay_do_wait_request(overlay, req,
491e3adcf8fSFrançois Tigeot 						    intel_overlay_release_old_vid_tail);
492e3adcf8fSFrançois Tigeot 		if (ret)
493e3adcf8fSFrançois Tigeot 			return ret;
49471f41f3eSFrançois Tigeot 	} else
49571f41f3eSFrançois Tigeot 		intel_overlay_release_old_vid_tail(&overlay->last_flip, NULL);
496e3adcf8fSFrançois Tigeot 
497e3adcf8fSFrançois Tigeot 	return 0;
498e3adcf8fSFrançois Tigeot }
499e3adcf8fSFrançois Tigeot 
5002c9916cdSFrançois Tigeot void intel_overlay_reset(struct drm_i915_private *dev_priv)
5012c9916cdSFrançois Tigeot {
5022c9916cdSFrançois Tigeot 	struct intel_overlay *overlay = dev_priv->overlay;
5032c9916cdSFrançois Tigeot 
5042c9916cdSFrançois Tigeot 	if (!overlay)
5052c9916cdSFrançois Tigeot 		return;
5062c9916cdSFrançois Tigeot 
5072c9916cdSFrançois Tigeot 	intel_overlay_release_old_vid(overlay);
5082c9916cdSFrançois Tigeot 
5092c9916cdSFrançois Tigeot 	overlay->old_xscale = 0;
5102c9916cdSFrançois Tigeot 	overlay->old_yscale = 0;
5112c9916cdSFrançois Tigeot 	overlay->crtc = NULL;
5122c9916cdSFrançois Tigeot 	overlay->active = false;
5132c9916cdSFrançois Tigeot }
5142c9916cdSFrançois Tigeot 
515e3adcf8fSFrançois Tigeot struct put_image_params {
516e3adcf8fSFrançois Tigeot 	int format;
517e3adcf8fSFrançois Tigeot 	short dst_x;
518e3adcf8fSFrançois Tigeot 	short dst_y;
519e3adcf8fSFrançois Tigeot 	short dst_w;
520e3adcf8fSFrançois Tigeot 	short dst_h;
521e3adcf8fSFrançois Tigeot 	short src_w;
522e3adcf8fSFrançois Tigeot 	short src_scan_h;
523e3adcf8fSFrançois Tigeot 	short src_scan_w;
524e3adcf8fSFrançois Tigeot 	short src_h;
525e3adcf8fSFrançois Tigeot 	short stride_Y;
526e3adcf8fSFrançois Tigeot 	short stride_UV;
527e3adcf8fSFrançois Tigeot 	int offset_Y;
528e3adcf8fSFrançois Tigeot 	int offset_U;
529e3adcf8fSFrançois Tigeot 	int offset_V;
530e3adcf8fSFrançois Tigeot };
531e3adcf8fSFrançois Tigeot 
532e3adcf8fSFrançois Tigeot static int packed_depth_bytes(u32 format)
533e3adcf8fSFrançois Tigeot {
534e3adcf8fSFrançois Tigeot 	switch (format & I915_OVERLAY_DEPTH_MASK) {
535e3adcf8fSFrançois Tigeot 	case I915_OVERLAY_YUV422:
536e3adcf8fSFrançois Tigeot 		return 4;
537e3adcf8fSFrançois Tigeot 	case I915_OVERLAY_YUV411:
538e3adcf8fSFrançois Tigeot 		/* return 6; not implemented */
539e3adcf8fSFrançois Tigeot 	default:
540e3adcf8fSFrançois Tigeot 		return -EINVAL;
541e3adcf8fSFrançois Tigeot 	}
542e3adcf8fSFrançois Tigeot }
543e3adcf8fSFrançois Tigeot 
544e3adcf8fSFrançois Tigeot static int packed_width_bytes(u32 format, short width)
545e3adcf8fSFrançois Tigeot {
546e3adcf8fSFrançois Tigeot 	switch (format & I915_OVERLAY_DEPTH_MASK) {
547e3adcf8fSFrançois Tigeot 	case I915_OVERLAY_YUV422:
548e3adcf8fSFrançois Tigeot 		return width << 1;
549e3adcf8fSFrançois Tigeot 	default:
550e3adcf8fSFrançois Tigeot 		return -EINVAL;
551e3adcf8fSFrançois Tigeot 	}
552e3adcf8fSFrançois Tigeot }
553e3adcf8fSFrançois Tigeot 
554e3adcf8fSFrançois Tigeot static int uv_hsubsampling(u32 format)
555e3adcf8fSFrançois Tigeot {
556e3adcf8fSFrançois Tigeot 	switch (format & I915_OVERLAY_DEPTH_MASK) {
557e3adcf8fSFrançois Tigeot 	case I915_OVERLAY_YUV422:
558e3adcf8fSFrançois Tigeot 	case I915_OVERLAY_YUV420:
559e3adcf8fSFrançois Tigeot 		return 2;
560e3adcf8fSFrançois Tigeot 	case I915_OVERLAY_YUV411:
561e3adcf8fSFrançois Tigeot 	case I915_OVERLAY_YUV410:
562e3adcf8fSFrançois Tigeot 		return 4;
563e3adcf8fSFrançois Tigeot 	default:
564e3adcf8fSFrançois Tigeot 		return -EINVAL;
565e3adcf8fSFrançois Tigeot 	}
566e3adcf8fSFrançois Tigeot }
567e3adcf8fSFrançois Tigeot 
568e3adcf8fSFrançois Tigeot static int uv_vsubsampling(u32 format)
569e3adcf8fSFrançois Tigeot {
570e3adcf8fSFrançois Tigeot 	switch (format & I915_OVERLAY_DEPTH_MASK) {
571e3adcf8fSFrançois Tigeot 	case I915_OVERLAY_YUV420:
572e3adcf8fSFrançois Tigeot 	case I915_OVERLAY_YUV410:
573e3adcf8fSFrançois Tigeot 		return 2;
574e3adcf8fSFrançois Tigeot 	case I915_OVERLAY_YUV422:
575e3adcf8fSFrançois Tigeot 	case I915_OVERLAY_YUV411:
576e3adcf8fSFrançois Tigeot 		return 1;
577e3adcf8fSFrançois Tigeot 	default:
578e3adcf8fSFrançois Tigeot 		return -EINVAL;
579e3adcf8fSFrançois Tigeot 	}
580e3adcf8fSFrançois Tigeot }
581e3adcf8fSFrançois Tigeot 
5821487f786SFrançois Tigeot static u32 calc_swidthsw(struct drm_i915_private *dev_priv, u32 offset, u32 width)
583e3adcf8fSFrançois Tigeot {
584*a85cb24fSFrançois Tigeot 	u32 sw;
585*a85cb24fSFrançois Tigeot 
586*a85cb24fSFrançois Tigeot 	if (IS_GEN2(dev_priv))
587*a85cb24fSFrançois Tigeot 		sw = ALIGN((offset & 31) + width, 32);
588*a85cb24fSFrançois Tigeot 	else
589*a85cb24fSFrançois Tigeot 		sw = ALIGN((offset & 63) + width, 64);
590*a85cb24fSFrançois Tigeot 
591*a85cb24fSFrançois Tigeot 	if (sw == 0)
592*a85cb24fSFrançois Tigeot 		return 0;
593*a85cb24fSFrançois Tigeot 
594*a85cb24fSFrançois Tigeot 	return (sw - 32) >> 3;
595e3adcf8fSFrançois Tigeot }
596e3adcf8fSFrançois Tigeot 
597*a85cb24fSFrançois Tigeot static const u16 y_static_hcoeffs[N_PHASES][N_HORIZ_Y_TAPS] = {
598*a85cb24fSFrançois Tigeot 	[ 0] = { 0x3000, 0xb4a0, 0x1930, 0x1920, 0xb4a0, },
599*a85cb24fSFrançois Tigeot 	[ 1] = { 0x3000, 0xb500, 0x19d0, 0x1880, 0xb440, },
600*a85cb24fSFrançois Tigeot 	[ 2] = { 0x3000, 0xb540, 0x1a88, 0x2f80, 0xb3e0, },
601*a85cb24fSFrançois Tigeot 	[ 3] = { 0x3000, 0xb580, 0x1b30, 0x2e20, 0xb380, },
602*a85cb24fSFrançois Tigeot 	[ 4] = { 0x3000, 0xb5c0, 0x1bd8, 0x2cc0, 0xb320, },
603*a85cb24fSFrançois Tigeot 	[ 5] = { 0x3020, 0xb5e0, 0x1c60, 0x2b80, 0xb2c0, },
604*a85cb24fSFrançois Tigeot 	[ 6] = { 0x3020, 0xb5e0, 0x1cf8, 0x2a20, 0xb260, },
605*a85cb24fSFrançois Tigeot 	[ 7] = { 0x3020, 0xb5e0, 0x1d80, 0x28e0, 0xb200, },
606*a85cb24fSFrançois Tigeot 	[ 8] = { 0x3020, 0xb5c0, 0x1e08, 0x3f40, 0xb1c0, },
607*a85cb24fSFrançois Tigeot 	[ 9] = { 0x3020, 0xb580, 0x1e78, 0x3ce0, 0xb160, },
608*a85cb24fSFrançois Tigeot 	[10] = { 0x3040, 0xb520, 0x1ed8, 0x3aa0, 0xb120, },
609*a85cb24fSFrançois Tigeot 	[11] = { 0x3040, 0xb4a0, 0x1f30, 0x3880, 0xb0e0, },
610*a85cb24fSFrançois Tigeot 	[12] = { 0x3040, 0xb400, 0x1f78, 0x3680, 0xb0a0, },
611*a85cb24fSFrançois Tigeot 	[13] = { 0x3020, 0xb340, 0x1fb8, 0x34a0, 0xb060, },
612*a85cb24fSFrançois Tigeot 	[14] = { 0x3020, 0xb240, 0x1fe0, 0x32e0, 0xb040, },
613*a85cb24fSFrançois Tigeot 	[15] = { 0x3020, 0xb140, 0x1ff8, 0x3160, 0xb020, },
614*a85cb24fSFrançois Tigeot 	[16] = { 0xb000, 0x3000, 0x0800, 0x3000, 0xb000, },
615e3adcf8fSFrançois Tigeot };
616e3adcf8fSFrançois Tigeot 
617*a85cb24fSFrançois Tigeot static const u16 uv_static_hcoeffs[N_PHASES][N_HORIZ_UV_TAPS] = {
618*a85cb24fSFrançois Tigeot 	[ 0] = { 0x3000, 0x1800, 0x1800, },
619*a85cb24fSFrançois Tigeot 	[ 1] = { 0xb000, 0x18d0, 0x2e60, },
620*a85cb24fSFrançois Tigeot 	[ 2] = { 0xb000, 0x1990, 0x2ce0, },
621*a85cb24fSFrançois Tigeot 	[ 3] = { 0xb020, 0x1a68, 0x2b40, },
622*a85cb24fSFrançois Tigeot 	[ 4] = { 0xb040, 0x1b20, 0x29e0, },
623*a85cb24fSFrançois Tigeot 	[ 5] = { 0xb060, 0x1bd8, 0x2880, },
624*a85cb24fSFrançois Tigeot 	[ 6] = { 0xb080, 0x1c88, 0x3e60, },
625*a85cb24fSFrançois Tigeot 	[ 7] = { 0xb0a0, 0x1d28, 0x3c00, },
626*a85cb24fSFrançois Tigeot 	[ 8] = { 0xb0c0, 0x1db8, 0x39e0, },
627*a85cb24fSFrançois Tigeot 	[ 9] = { 0xb0e0, 0x1e40, 0x37e0, },
628*a85cb24fSFrançois Tigeot 	[10] = { 0xb100, 0x1eb8, 0x3620, },
629*a85cb24fSFrançois Tigeot 	[11] = { 0xb100, 0x1f18, 0x34a0, },
630*a85cb24fSFrançois Tigeot 	[12] = { 0xb100, 0x1f68, 0x3360, },
631*a85cb24fSFrançois Tigeot 	[13] = { 0xb0e0, 0x1fa8, 0x3240, },
632*a85cb24fSFrançois Tigeot 	[14] = { 0xb0c0, 0x1fe0, 0x3140, },
633*a85cb24fSFrançois Tigeot 	[15] = { 0xb060, 0x1ff0, 0x30a0, },
634*a85cb24fSFrançois Tigeot 	[16] = { 0x3000, 0x0800, 0x3000, },
635e3adcf8fSFrançois Tigeot };
636e3adcf8fSFrançois Tigeot 
637e3440f96SFrançois Tigeot static void update_polyphase_filter(struct overlay_registers __iomem *regs)
638e3adcf8fSFrançois Tigeot {
639e3440f96SFrançois Tigeot 	memcpy_toio(regs->Y_HCOEFS, y_static_hcoeffs, sizeof(y_static_hcoeffs));
640e3440f96SFrançois Tigeot 	memcpy_toio(regs->UV_HCOEFS, uv_static_hcoeffs,
641e3440f96SFrançois Tigeot 		    sizeof(uv_static_hcoeffs));
642e3adcf8fSFrançois Tigeot }
643e3adcf8fSFrançois Tigeot 
644e3adcf8fSFrançois Tigeot static bool update_scaling_factors(struct intel_overlay *overlay,
645e3440f96SFrançois Tigeot 				   struct overlay_registers __iomem *regs,
646e3adcf8fSFrançois Tigeot 				   struct put_image_params *params)
647e3adcf8fSFrançois Tigeot {
648e3adcf8fSFrançois Tigeot 	/* fixed point with a 12 bit shift */
649e3adcf8fSFrançois Tigeot 	u32 xscale, yscale, xscale_UV, yscale_UV;
650e3adcf8fSFrançois Tigeot #define FP_SHIFT 12
651e3adcf8fSFrançois Tigeot #define FRACT_MASK 0xfff
652e3adcf8fSFrançois Tigeot 	bool scale_changed = false;
653e3adcf8fSFrançois Tigeot 	int uv_hscale = uv_hsubsampling(params->format);
654e3adcf8fSFrançois Tigeot 	int uv_vscale = uv_vsubsampling(params->format);
655e3adcf8fSFrançois Tigeot 
656e3adcf8fSFrançois Tigeot 	if (params->dst_w > 1)
657e3adcf8fSFrançois Tigeot 		xscale = ((params->src_scan_w - 1) << FP_SHIFT)
658e3adcf8fSFrançois Tigeot 			/(params->dst_w);
659e3adcf8fSFrançois Tigeot 	else
660e3adcf8fSFrançois Tigeot 		xscale = 1 << FP_SHIFT;
661e3adcf8fSFrançois Tigeot 
662e3adcf8fSFrançois Tigeot 	if (params->dst_h > 1)
663e3adcf8fSFrançois Tigeot 		yscale = ((params->src_scan_h - 1) << FP_SHIFT)
664e3adcf8fSFrançois Tigeot 			/(params->dst_h);
665e3adcf8fSFrançois Tigeot 	else
666e3adcf8fSFrançois Tigeot 		yscale = 1 << FP_SHIFT;
667e3adcf8fSFrançois Tigeot 
668e3adcf8fSFrançois Tigeot 	/*if (params->format & I915_OVERLAY_YUV_PLANAR) {*/
669e3adcf8fSFrançois Tigeot 	xscale_UV = xscale/uv_hscale;
670e3adcf8fSFrançois Tigeot 	yscale_UV = yscale/uv_vscale;
671e3adcf8fSFrançois Tigeot 	/* make the Y scale to UV scale ratio an exact multiply */
672e3adcf8fSFrançois Tigeot 	xscale = xscale_UV * uv_hscale;
673e3adcf8fSFrançois Tigeot 	yscale = yscale_UV * uv_vscale;
674e3adcf8fSFrançois Tigeot 	/*} else {
675e3adcf8fSFrançois Tigeot 	  xscale_UV = 0;
676e3adcf8fSFrançois Tigeot 	  yscale_UV = 0;
677e3adcf8fSFrançois Tigeot 	  }*/
678e3adcf8fSFrançois Tigeot 
679e3adcf8fSFrançois Tigeot 	if (xscale != overlay->old_xscale || yscale != overlay->old_yscale)
680e3adcf8fSFrançois Tigeot 		scale_changed = true;
681e3adcf8fSFrançois Tigeot 	overlay->old_xscale = xscale;
682e3adcf8fSFrançois Tigeot 	overlay->old_yscale = yscale;
683e3adcf8fSFrançois Tigeot 
684e3440f96SFrançois Tigeot 	iowrite32(((yscale & FRACT_MASK) << 20) |
685e3adcf8fSFrançois Tigeot 		  ((xscale >> FP_SHIFT)  << 16) |
686e3440f96SFrançois Tigeot 		  ((xscale & FRACT_MASK) << 3),
687e3440f96SFrançois Tigeot 		 &regs->YRGBSCALE);
688e3adcf8fSFrançois Tigeot 
689e3440f96SFrançois Tigeot 	iowrite32(((yscale_UV & FRACT_MASK) << 20) |
690e3adcf8fSFrançois Tigeot 		  ((xscale_UV >> FP_SHIFT)  << 16) |
691e3440f96SFrançois Tigeot 		  ((xscale_UV & FRACT_MASK) << 3),
692e3440f96SFrançois Tigeot 		 &regs->UVSCALE);
693e3adcf8fSFrançois Tigeot 
694e3440f96SFrançois Tigeot 	iowrite32((((yscale    >> FP_SHIFT) << 16) |
695e3440f96SFrançois Tigeot 		   ((yscale_UV >> FP_SHIFT) << 0)),
696e3440f96SFrançois Tigeot 		 &regs->UVSCALEV);
697e3adcf8fSFrançois Tigeot 
698e3adcf8fSFrançois Tigeot 	if (scale_changed)
699e3adcf8fSFrançois Tigeot 		update_polyphase_filter(regs);
700e3adcf8fSFrançois Tigeot 
701e3adcf8fSFrançois Tigeot 	return scale_changed;
702e3adcf8fSFrançois Tigeot }
703e3adcf8fSFrançois Tigeot 
704e3adcf8fSFrançois Tigeot static void update_colorkey(struct intel_overlay *overlay,
705e3440f96SFrançois Tigeot 			    struct overlay_registers __iomem *regs)
706e3adcf8fSFrançois Tigeot {
707*a85cb24fSFrançois Tigeot 	const struct intel_plane_state *state =
708*a85cb24fSFrançois Tigeot 		to_intel_plane_state(overlay->crtc->base.primary->state);
709e3adcf8fSFrançois Tigeot 	u32 key = overlay->color_key;
710*a85cb24fSFrançois Tigeot 	u32 format = 0;
711*a85cb24fSFrançois Tigeot 	u32 flags = 0;
71219c468b4SFrançois Tigeot 
71319c468b4SFrançois Tigeot 	if (overlay->color_key_enabled)
71419c468b4SFrançois Tigeot 		flags |= DST_KEY_ENABLE;
715e3adcf8fSFrançois Tigeot 
716*a85cb24fSFrançois Tigeot 	if (state->base.visible)
717*a85cb24fSFrançois Tigeot 		format = state->base.fb->format->format;
718*a85cb24fSFrançois Tigeot 
719*a85cb24fSFrançois Tigeot 	switch (format) {
720*a85cb24fSFrançois Tigeot 	case DRM_FORMAT_C8:
72119c468b4SFrançois Tigeot 		key = 0;
72219c468b4SFrançois Tigeot 		flags |= CLK_RGB8I_MASK;
723e3adcf8fSFrançois Tigeot 		break;
724*a85cb24fSFrançois Tigeot 	case DRM_FORMAT_XRGB1555:
72519c468b4SFrançois Tigeot 		key = RGB15_TO_COLORKEY(key);
72619c468b4SFrançois Tigeot 		flags |= CLK_RGB15_MASK;
727*a85cb24fSFrançois Tigeot 		break;
728*a85cb24fSFrançois Tigeot 	case DRM_FORMAT_RGB565:
72919c468b4SFrançois Tigeot 		key = RGB16_TO_COLORKEY(key);
73019c468b4SFrançois Tigeot 		flags |= CLK_RGB16_MASK;
731e3adcf8fSFrançois Tigeot 		break;
732*a85cb24fSFrançois Tigeot 	default:
73319c468b4SFrançois Tigeot 		flags |= CLK_RGB24_MASK;
734e3adcf8fSFrançois Tigeot 		break;
735e3adcf8fSFrançois Tigeot 	}
73619c468b4SFrançois Tigeot 
73719c468b4SFrançois Tigeot 	iowrite32(key, &regs->DCLRKV);
73819c468b4SFrançois Tigeot 	iowrite32(flags, &regs->DCLRKM);
739e3adcf8fSFrançois Tigeot }
740e3adcf8fSFrançois Tigeot 
741e3adcf8fSFrançois Tigeot static u32 overlay_cmd_reg(struct put_image_params *params)
742e3adcf8fSFrançois Tigeot {
743e3adcf8fSFrançois Tigeot 	u32 cmd = OCMD_ENABLE | OCMD_BUF_TYPE_FRAME | OCMD_BUFFER0;
744e3adcf8fSFrançois Tigeot 
745e3adcf8fSFrançois Tigeot 	if (params->format & I915_OVERLAY_YUV_PLANAR) {
746e3adcf8fSFrançois Tigeot 		switch (params->format & I915_OVERLAY_DEPTH_MASK) {
747e3adcf8fSFrançois Tigeot 		case I915_OVERLAY_YUV422:
748e3adcf8fSFrançois Tigeot 			cmd |= OCMD_YUV_422_PLANAR;
749e3adcf8fSFrançois Tigeot 			break;
750e3adcf8fSFrançois Tigeot 		case I915_OVERLAY_YUV420:
751e3adcf8fSFrançois Tigeot 			cmd |= OCMD_YUV_420_PLANAR;
752e3adcf8fSFrançois Tigeot 			break;
753e3adcf8fSFrançois Tigeot 		case I915_OVERLAY_YUV411:
754e3adcf8fSFrançois Tigeot 		case I915_OVERLAY_YUV410:
755e3adcf8fSFrançois Tigeot 			cmd |= OCMD_YUV_410_PLANAR;
756e3adcf8fSFrançois Tigeot 			break;
757e3adcf8fSFrançois Tigeot 		}
758e3adcf8fSFrançois Tigeot 	} else { /* YUV packed */
759e3adcf8fSFrançois Tigeot 		switch (params->format & I915_OVERLAY_DEPTH_MASK) {
760e3adcf8fSFrançois Tigeot 		case I915_OVERLAY_YUV422:
761e3adcf8fSFrançois Tigeot 			cmd |= OCMD_YUV_422_PACKED;
762e3adcf8fSFrançois Tigeot 			break;
763e3adcf8fSFrançois Tigeot 		case I915_OVERLAY_YUV411:
764e3adcf8fSFrançois Tigeot 			cmd |= OCMD_YUV_411_PACKED;
765e3adcf8fSFrançois Tigeot 			break;
766e3adcf8fSFrançois Tigeot 		}
767e3adcf8fSFrançois Tigeot 
768e3adcf8fSFrançois Tigeot 		switch (params->format & I915_OVERLAY_SWAP_MASK) {
769e3adcf8fSFrançois Tigeot 		case I915_OVERLAY_NO_SWAP:
770e3adcf8fSFrançois Tigeot 			break;
771e3adcf8fSFrançois Tigeot 		case I915_OVERLAY_UV_SWAP:
772e3adcf8fSFrançois Tigeot 			cmd |= OCMD_UV_SWAP;
773e3adcf8fSFrançois Tigeot 			break;
774e3adcf8fSFrançois Tigeot 		case I915_OVERLAY_Y_SWAP:
775e3adcf8fSFrançois Tigeot 			cmd |= OCMD_Y_SWAP;
776e3adcf8fSFrançois Tigeot 			break;
777e3adcf8fSFrançois Tigeot 		case I915_OVERLAY_Y_AND_UV_SWAP:
778e3adcf8fSFrançois Tigeot 			cmd |= OCMD_Y_AND_UV_SWAP;
779e3adcf8fSFrançois Tigeot 			break;
780e3adcf8fSFrançois Tigeot 		}
781e3adcf8fSFrançois Tigeot 	}
782e3adcf8fSFrançois Tigeot 
783e3adcf8fSFrançois Tigeot 	return cmd;
784e3adcf8fSFrançois Tigeot }
785e3adcf8fSFrançois Tigeot 
786e3adcf8fSFrançois Tigeot static int intel_overlay_do_put_image(struct intel_overlay *overlay,
787e3adcf8fSFrançois Tigeot 				      struct drm_i915_gem_object *new_bo,
788e3adcf8fSFrançois Tigeot 				      struct put_image_params *params)
789e3adcf8fSFrançois Tigeot {
790e3adcf8fSFrançois Tigeot 	int ret, tmp_width;
791e3440f96SFrançois Tigeot 	struct overlay_registers __iomem *regs;
792e3adcf8fSFrançois Tigeot 	bool scale_changed = false;
7931487f786SFrançois Tigeot 	struct drm_i915_private *dev_priv = overlay->i915;
794e3440f96SFrançois Tigeot 	u32 swidth, swidthsw, sheight, ostride;
79524edb884SFrançois Tigeot 	enum i915_pipe pipe = overlay->crtc->pipe;
7961e12ee3bSFrançois Tigeot 	struct i915_vma *vma;
797e3adcf8fSFrançois Tigeot 
798303bf270SFrançois Tigeot 	lockdep_assert_held(&dev_priv->drm.struct_mutex);
799303bf270SFrançois Tigeot 	WARN_ON(!drm_modeset_is_locked(&dev_priv->drm.mode_config.connection_mutex));
800e3adcf8fSFrançois Tigeot 
801e3adcf8fSFrançois Tigeot 	ret = intel_overlay_release_old_vid(overlay);
802e3adcf8fSFrançois Tigeot 	if (ret != 0)
803e3adcf8fSFrançois Tigeot 		return ret;
804e3adcf8fSFrançois Tigeot 
805*a85cb24fSFrançois Tigeot 	vma = i915_gem_object_pin_to_display_plane(new_bo, 0, NULL);
8061e12ee3bSFrançois Tigeot 	if (IS_ERR(vma))
8071e12ee3bSFrançois Tigeot 		return PTR_ERR(vma);
808e3adcf8fSFrançois Tigeot 
8091e12ee3bSFrançois Tigeot 	ret = i915_vma_put_fence(vma);
810e3adcf8fSFrançois Tigeot 	if (ret)
811e3adcf8fSFrançois Tigeot 		goto out_unpin;
812e3adcf8fSFrançois Tigeot 
813e3adcf8fSFrançois Tigeot 	if (!overlay->active) {
814e3440f96SFrançois Tigeot 		u32 oconfig;
815e3adcf8fSFrançois Tigeot 		regs = intel_overlay_map_regs(overlay);
816e3adcf8fSFrançois Tigeot 		if (!regs) {
817e3adcf8fSFrançois Tigeot 			ret = -ENOMEM;
818e3adcf8fSFrançois Tigeot 			goto out_unpin;
819e3adcf8fSFrançois Tigeot 		}
820e3440f96SFrançois Tigeot 		oconfig = OCONF_CC_OUT_8BIT;
8211487f786SFrançois Tigeot 		if (IS_GEN4(dev_priv))
822e3440f96SFrançois Tigeot 			oconfig |= OCONF_CSC_MODE_BT709;
82324edb884SFrançois Tigeot 		oconfig |= pipe == 0 ?
824e3adcf8fSFrançois Tigeot 			OCONF_PIPE_A : OCONF_PIPE_B;
825e3440f96SFrançois Tigeot 		iowrite32(oconfig, &regs->OCONFIG);
826e3adcf8fSFrançois Tigeot 		intel_overlay_unmap_regs(overlay, regs);
827e3adcf8fSFrançois Tigeot 
828e3adcf8fSFrançois Tigeot 		ret = intel_overlay_on(overlay);
829e3adcf8fSFrançois Tigeot 		if (ret != 0)
830e3adcf8fSFrançois Tigeot 			goto out_unpin;
831e3adcf8fSFrançois Tigeot 	}
832e3adcf8fSFrançois Tigeot 
833e3adcf8fSFrançois Tigeot 	regs = intel_overlay_map_regs(overlay);
834e3adcf8fSFrançois Tigeot 	if (!regs) {
835e3adcf8fSFrançois Tigeot 		ret = -ENOMEM;
836e3adcf8fSFrançois Tigeot 		goto out_unpin;
837e3adcf8fSFrançois Tigeot 	}
838e3adcf8fSFrançois Tigeot 
839e3440f96SFrançois Tigeot 	iowrite32((params->dst_y << 16) | params->dst_x, &regs->DWINPOS);
840e3440f96SFrançois Tigeot 	iowrite32((params->dst_h << 16) | params->dst_w, &regs->DWINSZ);
841e3adcf8fSFrançois Tigeot 
842e3adcf8fSFrançois Tigeot 	if (params->format & I915_OVERLAY_YUV_PACKED)
843e3adcf8fSFrançois Tigeot 		tmp_width = packed_width_bytes(params->format, params->src_w);
844e3adcf8fSFrançois Tigeot 	else
845e3adcf8fSFrançois Tigeot 		tmp_width = params->src_w;
846e3adcf8fSFrançois Tigeot 
847e3440f96SFrançois Tigeot 	swidth = params->src_w;
8481487f786SFrançois Tigeot 	swidthsw = calc_swidthsw(dev_priv, params->offset_Y, tmp_width);
849e3440f96SFrançois Tigeot 	sheight = params->src_h;
8501e12ee3bSFrançois Tigeot 	iowrite32(i915_ggtt_offset(vma) + params->offset_Y, &regs->OBUF_0Y);
851e3440f96SFrançois Tigeot 	ostride = params->stride_Y;
852e3adcf8fSFrançois Tigeot 
853e3adcf8fSFrançois Tigeot 	if (params->format & I915_OVERLAY_YUV_PLANAR) {
854e3adcf8fSFrançois Tigeot 		int uv_hscale = uv_hsubsampling(params->format);
855e3adcf8fSFrançois Tigeot 		int uv_vscale = uv_vsubsampling(params->format);
856e3adcf8fSFrançois Tigeot 		u32 tmp_U, tmp_V;
857e3440f96SFrançois Tigeot 		swidth |= (params->src_w/uv_hscale) << 16;
8581487f786SFrançois Tigeot 		tmp_U = calc_swidthsw(dev_priv, params->offset_U,
859e3adcf8fSFrançois Tigeot 				      params->src_w/uv_hscale);
8601487f786SFrançois Tigeot 		tmp_V = calc_swidthsw(dev_priv, params->offset_V,
861e3adcf8fSFrançois Tigeot 				      params->src_w/uv_hscale);
862e3440f96SFrançois Tigeot 		swidthsw |= max_t(u32, tmp_U, tmp_V) << 16;
863e3440f96SFrançois Tigeot 		sheight |= (params->src_h/uv_vscale) << 16;
8641e12ee3bSFrançois Tigeot 		iowrite32(i915_ggtt_offset(vma) + params->offset_U,
8651e12ee3bSFrançois Tigeot 			  &regs->OBUF_0U);
8661e12ee3bSFrançois Tigeot 		iowrite32(i915_ggtt_offset(vma) + params->offset_V,
8671e12ee3bSFrançois Tigeot 			  &regs->OBUF_0V);
868e3440f96SFrançois Tigeot 		ostride |= params->stride_UV << 16;
869e3adcf8fSFrançois Tigeot 	}
870e3adcf8fSFrançois Tigeot 
871e3440f96SFrançois Tigeot 	iowrite32(swidth, &regs->SWIDTH);
872e3440f96SFrançois Tigeot 	iowrite32(swidthsw, &regs->SWIDTHSW);
873e3440f96SFrançois Tigeot 	iowrite32(sheight, &regs->SHEIGHT);
874e3440f96SFrançois Tigeot 	iowrite32(ostride, &regs->OSTRIDE);
875e3440f96SFrançois Tigeot 
876e3adcf8fSFrançois Tigeot 	scale_changed = update_scaling_factors(overlay, regs, params);
877e3adcf8fSFrançois Tigeot 
878e3adcf8fSFrançois Tigeot 	update_colorkey(overlay, regs);
879e3adcf8fSFrançois Tigeot 
880e3440f96SFrançois Tigeot 	iowrite32(overlay_cmd_reg(params), &regs->OCMD);
881e3adcf8fSFrançois Tigeot 
882e3adcf8fSFrançois Tigeot 	intel_overlay_unmap_regs(overlay, regs);
883e3adcf8fSFrançois Tigeot 
884*a85cb24fSFrançois Tigeot 	ret = intel_overlay_continue(overlay, vma, scale_changed);
885e3adcf8fSFrançois Tigeot 	if (ret)
886e3adcf8fSFrançois Tigeot 		goto out_unpin;
887e3adcf8fSFrançois Tigeot 
888e3adcf8fSFrançois Tigeot 	return 0;
889e3adcf8fSFrançois Tigeot 
890e3adcf8fSFrançois Tigeot out_unpin:
8911e12ee3bSFrançois Tigeot 	i915_gem_object_unpin_from_display_plane(vma);
892e3adcf8fSFrançois Tigeot 	return ret;
893e3adcf8fSFrançois Tigeot }
894e3adcf8fSFrançois Tigeot 
895e3adcf8fSFrançois Tigeot int intel_overlay_switch_off(struct intel_overlay *overlay)
896e3adcf8fSFrançois Tigeot {
8971487f786SFrançois Tigeot 	struct drm_i915_private *dev_priv = overlay->i915;
898e3440f96SFrançois Tigeot 	struct overlay_registers __iomem *regs;
899e3adcf8fSFrançois Tigeot 	int ret;
900e3adcf8fSFrançois Tigeot 
901303bf270SFrançois Tigeot 	lockdep_assert_held(&dev_priv->drm.struct_mutex);
902303bf270SFrançois Tigeot 	WARN_ON(!drm_modeset_is_locked(&dev_priv->drm.mode_config.connection_mutex));
903e3adcf8fSFrançois Tigeot 
904e3adcf8fSFrançois Tigeot 	ret = intel_overlay_recover_from_interrupt(overlay);
905e3adcf8fSFrançois Tigeot 	if (ret != 0)
906e3adcf8fSFrançois Tigeot 		return ret;
907e3adcf8fSFrançois Tigeot 
908e3adcf8fSFrançois Tigeot 	if (!overlay->active)
909e3adcf8fSFrançois Tigeot 		return 0;
910e3adcf8fSFrançois Tigeot 
911e3adcf8fSFrançois Tigeot 	ret = intel_overlay_release_old_vid(overlay);
912e3adcf8fSFrançois Tigeot 	if (ret != 0)
913e3adcf8fSFrançois Tigeot 		return ret;
914e3adcf8fSFrançois Tigeot 
915e3adcf8fSFrançois Tigeot 	regs = intel_overlay_map_regs(overlay);
916e3440f96SFrançois Tigeot 	iowrite32(0, &regs->OCMD);
917e3adcf8fSFrançois Tigeot 	intel_overlay_unmap_regs(overlay, regs);
918e3adcf8fSFrançois Tigeot 
91971f41f3eSFrançois Tigeot 	return intel_overlay_off(overlay);
920e3adcf8fSFrançois Tigeot }
921e3adcf8fSFrançois Tigeot 
922e3adcf8fSFrançois Tigeot static int check_overlay_possible_on_crtc(struct intel_overlay *overlay,
923e3adcf8fSFrançois Tigeot 					  struct intel_crtc *crtc)
924e3adcf8fSFrançois Tigeot {
925e3adcf8fSFrançois Tigeot 	if (!crtc->active)
926e3adcf8fSFrançois Tigeot 		return -EINVAL;
927e3adcf8fSFrançois Tigeot 
928e3adcf8fSFrançois Tigeot 	/* can't use the overlay with double wide pipe */
9292c9916cdSFrançois Tigeot 	if (crtc->config->double_wide)
930e3adcf8fSFrançois Tigeot 		return -EINVAL;
931e3adcf8fSFrançois Tigeot 
932e3adcf8fSFrançois Tigeot 	return 0;
933e3adcf8fSFrançois Tigeot }
934e3adcf8fSFrançois Tigeot 
935e3adcf8fSFrançois Tigeot static void update_pfit_vscale_ratio(struct intel_overlay *overlay)
936e3adcf8fSFrançois Tigeot {
9371487f786SFrançois Tigeot 	struct drm_i915_private *dev_priv = overlay->i915;
938e3adcf8fSFrançois Tigeot 	u32 pfit_control = I915_READ(PFIT_CONTROL);
939e3adcf8fSFrançois Tigeot 	u32 ratio;
940e3adcf8fSFrançois Tigeot 
941e3adcf8fSFrançois Tigeot 	/* XXX: This is not the same logic as in the xorg driver, but more in
942e3adcf8fSFrançois Tigeot 	 * line with the intel documentation for the i965
943e3adcf8fSFrançois Tigeot 	 */
9441487f786SFrançois Tigeot 	if (INTEL_GEN(dev_priv) >= 4) {
945e3adcf8fSFrançois Tigeot 		/* on i965 use the PGM reg to read out the autoscaler values */
946e3adcf8fSFrançois Tigeot 		ratio = I915_READ(PFIT_PGM_RATIOS) >> PFIT_VERT_SCALE_SHIFT_965;
947e3adcf8fSFrançois Tigeot 	} else {
948e3adcf8fSFrançois Tigeot 		if (pfit_control & VERT_AUTO_SCALE)
949e3adcf8fSFrançois Tigeot 			ratio = I915_READ(PFIT_AUTO_RATIOS);
950e3adcf8fSFrançois Tigeot 		else
951e3adcf8fSFrançois Tigeot 			ratio = I915_READ(PFIT_PGM_RATIOS);
952e3adcf8fSFrançois Tigeot 		ratio >>= PFIT_VERT_SCALE_SHIFT;
953e3adcf8fSFrançois Tigeot 	}
954e3adcf8fSFrançois Tigeot 
955e3adcf8fSFrançois Tigeot 	overlay->pfit_vscale_ratio = ratio;
956e3adcf8fSFrançois Tigeot }
957e3adcf8fSFrançois Tigeot 
958e3adcf8fSFrançois Tigeot static int check_overlay_dst(struct intel_overlay *overlay,
959e3adcf8fSFrançois Tigeot 			     struct drm_intel_overlay_put_image *rec)
960e3adcf8fSFrançois Tigeot {
961*a85cb24fSFrançois Tigeot 	const struct intel_crtc_state *pipe_config =
962*a85cb24fSFrançois Tigeot 		overlay->crtc->config;
963e3adcf8fSFrançois Tigeot 
964*a85cb24fSFrançois Tigeot 	if (rec->dst_x < pipe_config->pipe_src_w &&
965*a85cb24fSFrançois Tigeot 	    rec->dst_x + rec->dst_width <= pipe_config->pipe_src_w &&
966*a85cb24fSFrançois Tigeot 	    rec->dst_y < pipe_config->pipe_src_h &&
967*a85cb24fSFrançois Tigeot 	    rec->dst_y + rec->dst_height <= pipe_config->pipe_src_h)
968e3adcf8fSFrançois Tigeot 		return 0;
969e3adcf8fSFrançois Tigeot 	else
970e3adcf8fSFrançois Tigeot 		return -EINVAL;
971e3adcf8fSFrançois Tigeot }
972e3adcf8fSFrançois Tigeot 
973e3adcf8fSFrançois Tigeot static int check_overlay_scaling(struct put_image_params *rec)
974e3adcf8fSFrançois Tigeot {
975e3adcf8fSFrançois Tigeot 	u32 tmp;
976e3adcf8fSFrançois Tigeot 
977e3adcf8fSFrançois Tigeot 	/* downscaling limit is 8.0 */
978e3adcf8fSFrançois Tigeot 	tmp = ((rec->src_scan_h << 16) / rec->dst_h) >> 16;
979e3adcf8fSFrançois Tigeot 	if (tmp > 7)
980e3adcf8fSFrançois Tigeot 		return -EINVAL;
981e3adcf8fSFrançois Tigeot 	tmp = ((rec->src_scan_w << 16) / rec->dst_w) >> 16;
982e3adcf8fSFrançois Tigeot 	if (tmp > 7)
983e3adcf8fSFrançois Tigeot 		return -EINVAL;
984e3adcf8fSFrançois Tigeot 
985e3adcf8fSFrançois Tigeot 	return 0;
986e3adcf8fSFrançois Tigeot }
987e3adcf8fSFrançois Tigeot 
9881487f786SFrançois Tigeot static int check_overlay_src(struct drm_i915_private *dev_priv,
989e3adcf8fSFrançois Tigeot 			     struct drm_intel_overlay_put_image *rec,
990e3adcf8fSFrançois Tigeot 			     struct drm_i915_gem_object *new_bo)
991e3adcf8fSFrançois Tigeot {
992e3adcf8fSFrançois Tigeot 	int uv_hscale = uv_hsubsampling(rec->flags);
993e3adcf8fSFrançois Tigeot 	int uv_vscale = uv_vsubsampling(rec->flags);
994e3adcf8fSFrançois Tigeot 	u32 stride_mask;
995e3adcf8fSFrançois Tigeot 	int depth;
996e3adcf8fSFrançois Tigeot 	u32 tmp;
997e3adcf8fSFrançois Tigeot 
998e3adcf8fSFrançois Tigeot 	/* check src dimensions */
999*a85cb24fSFrançois Tigeot 	if (IS_I845G(dev_priv) || IS_I830(dev_priv)) {
1000e3adcf8fSFrançois Tigeot 		if (rec->src_height > IMAGE_MAX_HEIGHT_LEGACY ||
1001e3adcf8fSFrançois Tigeot 		    rec->src_width  > IMAGE_MAX_WIDTH_LEGACY)
1002e3adcf8fSFrançois Tigeot 			return -EINVAL;
1003e3adcf8fSFrançois Tigeot 	} else {
1004e3adcf8fSFrançois Tigeot 		if (rec->src_height > IMAGE_MAX_HEIGHT ||
1005e3adcf8fSFrançois Tigeot 		    rec->src_width  > IMAGE_MAX_WIDTH)
1006e3adcf8fSFrançois Tigeot 			return -EINVAL;
1007e3adcf8fSFrançois Tigeot 	}
1008e3adcf8fSFrançois Tigeot 
1009e3adcf8fSFrançois Tigeot 	/* better safe than sorry, use 4 as the maximal subsampling ratio */
1010e3adcf8fSFrançois Tigeot 	if (rec->src_height < N_VERT_Y_TAPS*4 ||
1011e3adcf8fSFrançois Tigeot 	    rec->src_width  < N_HORIZ_Y_TAPS*4)
1012e3adcf8fSFrançois Tigeot 		return -EINVAL;
1013e3adcf8fSFrançois Tigeot 
1014e3adcf8fSFrançois Tigeot 	/* check alignment constraints */
1015e3adcf8fSFrançois Tigeot 	switch (rec->flags & I915_OVERLAY_TYPE_MASK) {
1016e3adcf8fSFrançois Tigeot 	case I915_OVERLAY_RGB:
1017e3adcf8fSFrançois Tigeot 		/* not implemented */
1018e3adcf8fSFrançois Tigeot 		return -EINVAL;
1019e3adcf8fSFrançois Tigeot 
1020e3adcf8fSFrançois Tigeot 	case I915_OVERLAY_YUV_PACKED:
1021e3adcf8fSFrançois Tigeot 		if (uv_vscale != 1)
1022e3adcf8fSFrançois Tigeot 			return -EINVAL;
1023e3adcf8fSFrançois Tigeot 
1024e3adcf8fSFrançois Tigeot 		depth = packed_depth_bytes(rec->flags);
1025e3adcf8fSFrançois Tigeot 		if (depth < 0)
1026e3adcf8fSFrançois Tigeot 			return depth;
1027e3adcf8fSFrançois Tigeot 
1028e3adcf8fSFrançois Tigeot 		/* ignore UV planes */
1029e3adcf8fSFrançois Tigeot 		rec->stride_UV = 0;
1030e3adcf8fSFrançois Tigeot 		rec->offset_U = 0;
1031e3adcf8fSFrançois Tigeot 		rec->offset_V = 0;
1032e3adcf8fSFrançois Tigeot 		/* check pixel alignment */
1033e3adcf8fSFrançois Tigeot 		if (rec->offset_Y % depth)
1034e3adcf8fSFrançois Tigeot 			return -EINVAL;
1035e3adcf8fSFrançois Tigeot 		break;
1036e3adcf8fSFrançois Tigeot 
1037e3adcf8fSFrançois Tigeot 	case I915_OVERLAY_YUV_PLANAR:
1038e3adcf8fSFrançois Tigeot 		if (uv_vscale < 0 || uv_hscale < 0)
1039e3adcf8fSFrançois Tigeot 			return -EINVAL;
1040e3adcf8fSFrançois Tigeot 		/* no offset restrictions for planar formats */
1041e3adcf8fSFrançois Tigeot 		break;
1042e3adcf8fSFrançois Tigeot 
1043e3adcf8fSFrançois Tigeot 	default:
1044e3adcf8fSFrançois Tigeot 		return -EINVAL;
1045e3adcf8fSFrançois Tigeot 	}
1046e3adcf8fSFrançois Tigeot 
1047e3adcf8fSFrançois Tigeot 	if (rec->src_width % uv_hscale)
1048e3adcf8fSFrançois Tigeot 		return -EINVAL;
1049e3adcf8fSFrançois Tigeot 
1050e3adcf8fSFrançois Tigeot 	/* stride checking */
1051*a85cb24fSFrançois Tigeot 	if (IS_I830(dev_priv) || IS_I845G(dev_priv))
1052e3adcf8fSFrançois Tigeot 		stride_mask = 255;
1053e3adcf8fSFrançois Tigeot 	else
1054e3adcf8fSFrançois Tigeot 		stride_mask = 63;
1055e3adcf8fSFrançois Tigeot 
1056e3adcf8fSFrançois Tigeot 	if (rec->stride_Y & stride_mask || rec->stride_UV & stride_mask)
1057e3adcf8fSFrançois Tigeot 		return -EINVAL;
10581487f786SFrançois Tigeot 	if (IS_GEN4(dev_priv) && rec->stride_Y < 512)
1059e3adcf8fSFrançois Tigeot 		return -EINVAL;
1060e3adcf8fSFrançois Tigeot 
1061e3adcf8fSFrançois Tigeot 	tmp = (rec->flags & I915_OVERLAY_TYPE_MASK) == I915_OVERLAY_YUV_PLANAR ?
1062e3adcf8fSFrançois Tigeot 		4096 : 8192;
1063e3adcf8fSFrançois Tigeot 	if (rec->stride_Y > tmp || rec->stride_UV > 2*1024)
1064e3adcf8fSFrançois Tigeot 		return -EINVAL;
1065e3adcf8fSFrançois Tigeot 
1066e3adcf8fSFrançois Tigeot 	/* check buffer dimensions */
1067e3adcf8fSFrançois Tigeot 	switch (rec->flags & I915_OVERLAY_TYPE_MASK) {
1068e3adcf8fSFrançois Tigeot 	case I915_OVERLAY_RGB:
1069e3adcf8fSFrançois Tigeot 	case I915_OVERLAY_YUV_PACKED:
1070e3adcf8fSFrançois Tigeot 		/* always 4 Y values per depth pixels */
1071e3adcf8fSFrançois Tigeot 		if (packed_width_bytes(rec->flags, rec->src_width) > rec->stride_Y)
1072e3adcf8fSFrançois Tigeot 			return -EINVAL;
1073e3adcf8fSFrançois Tigeot 
1074e3adcf8fSFrançois Tigeot 		tmp = rec->stride_Y*rec->src_height;
1075e3adcf8fSFrançois Tigeot 		if (rec->offset_Y + tmp > new_bo->base.size)
1076e3adcf8fSFrançois Tigeot 			return -EINVAL;
1077e3adcf8fSFrançois Tigeot 		break;
1078e3adcf8fSFrançois Tigeot 
1079e3adcf8fSFrançois Tigeot 	case I915_OVERLAY_YUV_PLANAR:
1080e3adcf8fSFrançois Tigeot 		if (rec->src_width > rec->stride_Y)
1081e3adcf8fSFrançois Tigeot 			return -EINVAL;
1082e3adcf8fSFrançois Tigeot 		if (rec->src_width/uv_hscale > rec->stride_UV)
1083e3adcf8fSFrançois Tigeot 			return -EINVAL;
1084e3adcf8fSFrançois Tigeot 
1085e3adcf8fSFrançois Tigeot 		tmp = rec->stride_Y * rec->src_height;
1086e3adcf8fSFrançois Tigeot 		if (rec->offset_Y + tmp > new_bo->base.size)
1087e3adcf8fSFrançois Tigeot 			return -EINVAL;
1088e3adcf8fSFrançois Tigeot 
1089e3adcf8fSFrançois Tigeot 		tmp = rec->stride_UV * (rec->src_height / uv_vscale);
1090e3adcf8fSFrançois Tigeot 		if (rec->offset_U + tmp > new_bo->base.size ||
1091e3adcf8fSFrançois Tigeot 		    rec->offset_V + tmp > new_bo->base.size)
1092e3adcf8fSFrançois Tigeot 			return -EINVAL;
1093e3adcf8fSFrançois Tigeot 		break;
1094e3adcf8fSFrançois Tigeot 	}
1095e3adcf8fSFrançois Tigeot 
1096e3adcf8fSFrançois Tigeot 	return 0;
1097e3adcf8fSFrançois Tigeot }
1098e3adcf8fSFrançois Tigeot 
10991487f786SFrançois Tigeot int intel_overlay_put_image_ioctl(struct drm_device *dev, void *data,
1100e3adcf8fSFrançois Tigeot 				  struct drm_file *file_priv)
1101e3adcf8fSFrançois Tigeot {
1102e3adcf8fSFrançois Tigeot 	struct drm_intel_overlay_put_image *put_image_rec = data;
1103bf017597SFrançois Tigeot 	struct drm_i915_private *dev_priv = to_i915(dev);
1104e3adcf8fSFrançois Tigeot 	struct intel_overlay *overlay;
110524edb884SFrançois Tigeot 	struct drm_crtc *drmmode_crtc;
1106e3adcf8fSFrançois Tigeot 	struct intel_crtc *crtc;
1107e3adcf8fSFrançois Tigeot 	struct drm_i915_gem_object *new_bo;
1108e3adcf8fSFrançois Tigeot 	struct put_image_params *params;
1109e3adcf8fSFrançois Tigeot 	int ret;
1110e3adcf8fSFrançois Tigeot 
1111e3adcf8fSFrançois Tigeot 	overlay = dev_priv->overlay;
1112e3adcf8fSFrançois Tigeot 	if (!overlay) {
1113e3adcf8fSFrançois Tigeot 		DRM_DEBUG("userspace bug: no overlay\n");
1114e3adcf8fSFrançois Tigeot 		return -ENODEV;
1115e3adcf8fSFrançois Tigeot 	}
1116e3adcf8fSFrançois Tigeot 
1117e3adcf8fSFrançois Tigeot 	if (!(put_image_rec->flags & I915_OVERLAY_ENABLE)) {
1118a2fdbec6SFrançois Tigeot 		drm_modeset_lock_all(dev);
1119a2fdbec6SFrançois Tigeot 		mutex_lock(&dev->struct_mutex);
1120e3adcf8fSFrançois Tigeot 
1121e3adcf8fSFrançois Tigeot 		ret = intel_overlay_switch_off(overlay);
1122e3adcf8fSFrançois Tigeot 
1123a2fdbec6SFrançois Tigeot 		mutex_unlock(&dev->struct_mutex);
1124a2fdbec6SFrançois Tigeot 		drm_modeset_unlock_all(dev);
1125e3adcf8fSFrançois Tigeot 
1126e3adcf8fSFrançois Tigeot 		return ret;
1127e3adcf8fSFrançois Tigeot 	}
1128e3adcf8fSFrançois Tigeot 
1129bf017597SFrançois Tigeot 	params = kmalloc(sizeof(*params), M_DRM, GFP_KERNEL);
1130e3440f96SFrançois Tigeot 	if (!params)
1131e3440f96SFrançois Tigeot 		return -ENOMEM;
1132e3adcf8fSFrançois Tigeot 
113324edb884SFrançois Tigeot 	drmmode_crtc = drm_crtc_find(dev, put_image_rec->crtc_id);
113424edb884SFrançois Tigeot 	if (!drmmode_crtc) {
1135e3adcf8fSFrançois Tigeot 		ret = -ENOENT;
1136e3adcf8fSFrançois Tigeot 		goto out_free;
1137e3adcf8fSFrançois Tigeot 	}
113824edb884SFrançois Tigeot 	crtc = to_intel_crtc(drmmode_crtc);
1139e3adcf8fSFrançois Tigeot 
114087df8fc6SFrançois Tigeot 	new_bo = i915_gem_object_lookup(file_priv, put_image_rec->bo_handle);
114187df8fc6SFrançois Tigeot 	if (!new_bo) {
1142e3adcf8fSFrançois Tigeot 		ret = -ENOENT;
1143e3adcf8fSFrançois Tigeot 		goto out_free;
1144e3adcf8fSFrançois Tigeot 	}
1145e3adcf8fSFrançois Tigeot 
1146a2fdbec6SFrançois Tigeot 	drm_modeset_lock_all(dev);
1147a2fdbec6SFrançois Tigeot 	mutex_lock(&dev->struct_mutex);
1148e3adcf8fSFrançois Tigeot 
114971f41f3eSFrançois Tigeot 	if (i915_gem_object_is_tiled(new_bo)) {
1150ba55f2f5SFrançois Tigeot 		DRM_DEBUG_KMS("buffer used for overlay image can not be tiled\n");
1151e3adcf8fSFrançois Tigeot 		ret = -EINVAL;
1152e3adcf8fSFrançois Tigeot 		goto out_unlock;
1153e3adcf8fSFrançois Tigeot 	}
1154e3adcf8fSFrançois Tigeot 
1155e3adcf8fSFrançois Tigeot 	ret = intel_overlay_recover_from_interrupt(overlay);
1156e3adcf8fSFrançois Tigeot 	if (ret != 0)
1157e3adcf8fSFrançois Tigeot 		goto out_unlock;
1158e3adcf8fSFrançois Tigeot 
1159e3adcf8fSFrançois Tigeot 	if (overlay->crtc != crtc) {
1160e3adcf8fSFrançois Tigeot 		ret = intel_overlay_switch_off(overlay);
1161e3adcf8fSFrançois Tigeot 		if (ret != 0)
1162e3adcf8fSFrançois Tigeot 			goto out_unlock;
1163e3adcf8fSFrançois Tigeot 
1164e3adcf8fSFrançois Tigeot 		ret = check_overlay_possible_on_crtc(overlay, crtc);
1165e3adcf8fSFrançois Tigeot 		if (ret != 0)
1166e3adcf8fSFrançois Tigeot 			goto out_unlock;
1167e3adcf8fSFrançois Tigeot 
1168e3adcf8fSFrançois Tigeot 		overlay->crtc = crtc;
1169e3adcf8fSFrançois Tigeot 		crtc->overlay = overlay;
1170e3adcf8fSFrançois Tigeot 
1171e3adcf8fSFrançois Tigeot 		/* line too wide, i.e. one-line-mode */
1172*a85cb24fSFrançois Tigeot 		if (crtc->config->pipe_src_w > 1024 &&
1173*a85cb24fSFrançois Tigeot 		    crtc->config->gmch_pfit.control & PFIT_ENABLE) {
117419c468b4SFrançois Tigeot 			overlay->pfit_active = true;
1175e3adcf8fSFrançois Tigeot 			update_pfit_vscale_ratio(overlay);
1176e3adcf8fSFrançois Tigeot 		} else
117719c468b4SFrançois Tigeot 			overlay->pfit_active = false;
1178e3adcf8fSFrançois Tigeot 	}
1179e3adcf8fSFrançois Tigeot 
1180e3adcf8fSFrançois Tigeot 	ret = check_overlay_dst(overlay, put_image_rec);
1181e3adcf8fSFrançois Tigeot 	if (ret != 0)
1182e3adcf8fSFrançois Tigeot 		goto out_unlock;
1183e3adcf8fSFrançois Tigeot 
1184e3adcf8fSFrançois Tigeot 	if (overlay->pfit_active) {
1185e3adcf8fSFrançois Tigeot 		params->dst_y = ((((u32)put_image_rec->dst_y) << 12) /
1186e3adcf8fSFrançois Tigeot 				 overlay->pfit_vscale_ratio);
1187e3adcf8fSFrançois Tigeot 		/* shifting right rounds downwards, so add 1 */
1188e3adcf8fSFrançois Tigeot 		params->dst_h = ((((u32)put_image_rec->dst_height) << 12) /
1189e3adcf8fSFrançois Tigeot 				 overlay->pfit_vscale_ratio) + 1;
1190e3adcf8fSFrançois Tigeot 	} else {
1191e3adcf8fSFrançois Tigeot 		params->dst_y = put_image_rec->dst_y;
1192e3adcf8fSFrançois Tigeot 		params->dst_h = put_image_rec->dst_height;
1193e3adcf8fSFrançois Tigeot 	}
1194e3adcf8fSFrançois Tigeot 	params->dst_x = put_image_rec->dst_x;
1195e3adcf8fSFrançois Tigeot 	params->dst_w = put_image_rec->dst_width;
1196e3adcf8fSFrançois Tigeot 
1197e3adcf8fSFrançois Tigeot 	params->src_w = put_image_rec->src_width;
1198e3adcf8fSFrançois Tigeot 	params->src_h = put_image_rec->src_height;
1199e3adcf8fSFrançois Tigeot 	params->src_scan_w = put_image_rec->src_scan_width;
1200e3adcf8fSFrançois Tigeot 	params->src_scan_h = put_image_rec->src_scan_height;
1201e3adcf8fSFrançois Tigeot 	if (params->src_scan_h > params->src_h ||
1202e3adcf8fSFrançois Tigeot 	    params->src_scan_w > params->src_w) {
1203e3adcf8fSFrançois Tigeot 		ret = -EINVAL;
1204e3adcf8fSFrançois Tigeot 		goto out_unlock;
1205e3adcf8fSFrançois Tigeot 	}
1206e3adcf8fSFrançois Tigeot 
12071487f786SFrançois Tigeot 	ret = check_overlay_src(dev_priv, put_image_rec, new_bo);
1208e3adcf8fSFrançois Tigeot 	if (ret != 0)
1209e3adcf8fSFrançois Tigeot 		goto out_unlock;
1210e3adcf8fSFrançois Tigeot 	params->format = put_image_rec->flags & ~I915_OVERLAY_FLAGS_MASK;
1211e3adcf8fSFrançois Tigeot 	params->stride_Y = put_image_rec->stride_Y;
1212e3adcf8fSFrançois Tigeot 	params->stride_UV = put_image_rec->stride_UV;
1213e3adcf8fSFrançois Tigeot 	params->offset_Y = put_image_rec->offset_Y;
1214e3adcf8fSFrançois Tigeot 	params->offset_U = put_image_rec->offset_U;
1215e3adcf8fSFrançois Tigeot 	params->offset_V = put_image_rec->offset_V;
1216e3adcf8fSFrançois Tigeot 
1217e3adcf8fSFrançois Tigeot 	/* Check scaling after src size to prevent a divide-by-zero. */
1218e3adcf8fSFrançois Tigeot 	ret = check_overlay_scaling(params);
1219e3adcf8fSFrançois Tigeot 	if (ret != 0)
1220e3adcf8fSFrançois Tigeot 		goto out_unlock;
1221e3adcf8fSFrançois Tigeot 
1222e3adcf8fSFrançois Tigeot 	ret = intel_overlay_do_put_image(overlay, new_bo, params);
1223e3adcf8fSFrançois Tigeot 	if (ret != 0)
1224e3adcf8fSFrançois Tigeot 		goto out_unlock;
1225e3adcf8fSFrançois Tigeot 
1226a2fdbec6SFrançois Tigeot 	mutex_unlock(&dev->struct_mutex);
1227a2fdbec6SFrançois Tigeot 	drm_modeset_unlock_all(dev);
1228*a85cb24fSFrançois Tigeot 	i915_gem_object_put(new_bo);
1229e3adcf8fSFrançois Tigeot 
1230158486a6SFrançois Tigeot 	kfree(params);
1231e3adcf8fSFrançois Tigeot 
1232e3adcf8fSFrançois Tigeot 	return 0;
1233e3adcf8fSFrançois Tigeot 
1234e3adcf8fSFrançois Tigeot out_unlock:
1235a2fdbec6SFrançois Tigeot 	mutex_unlock(&dev->struct_mutex);
1236a2fdbec6SFrançois Tigeot 	drm_modeset_unlock_all(dev);
12374be47400SFrançois Tigeot 	i915_gem_object_put(new_bo);
1238e3adcf8fSFrançois Tigeot out_free:
1239158486a6SFrançois Tigeot 	kfree(params);
1240e3adcf8fSFrançois Tigeot 
1241e3adcf8fSFrançois Tigeot 	return ret;
1242e3adcf8fSFrançois Tigeot }
1243e3adcf8fSFrançois Tigeot 
1244e3adcf8fSFrançois Tigeot static void update_reg_attrs(struct intel_overlay *overlay,
1245e3440f96SFrançois Tigeot 			     struct overlay_registers __iomem *regs)
1246e3adcf8fSFrançois Tigeot {
1247e3440f96SFrançois Tigeot 	iowrite32((overlay->contrast << 18) | (overlay->brightness & 0xff),
1248e3440f96SFrançois Tigeot 		  &regs->OCLRC0);
1249e3440f96SFrançois Tigeot 	iowrite32(overlay->saturation, &regs->OCLRC1);
1250e3adcf8fSFrançois Tigeot }
1251e3adcf8fSFrançois Tigeot 
1252e3adcf8fSFrançois Tigeot static bool check_gamma_bounds(u32 gamma1, u32 gamma2)
1253e3adcf8fSFrançois Tigeot {
1254e3adcf8fSFrançois Tigeot 	int i;
1255e3adcf8fSFrançois Tigeot 
1256e3adcf8fSFrançois Tigeot 	if (gamma1 & 0xff000000 || gamma2 & 0xff000000)
1257e3adcf8fSFrançois Tigeot 		return false;
1258e3adcf8fSFrançois Tigeot 
1259e3adcf8fSFrançois Tigeot 	for (i = 0; i < 3; i++) {
1260e3adcf8fSFrançois Tigeot 		if (((gamma1 >> i*8) & 0xff) >= ((gamma2 >> i*8) & 0xff))
1261e3adcf8fSFrançois Tigeot 			return false;
1262e3adcf8fSFrançois Tigeot 	}
1263e3adcf8fSFrançois Tigeot 
1264e3adcf8fSFrançois Tigeot 	return true;
1265e3adcf8fSFrançois Tigeot }
1266e3adcf8fSFrançois Tigeot 
1267e3adcf8fSFrançois Tigeot static bool check_gamma5_errata(u32 gamma5)
1268e3adcf8fSFrançois Tigeot {
1269e3adcf8fSFrançois Tigeot 	int i;
1270e3adcf8fSFrançois Tigeot 
1271e3adcf8fSFrançois Tigeot 	for (i = 0; i < 3; i++) {
1272e3adcf8fSFrançois Tigeot 		if (((gamma5 >> i*8) & 0xff) == 0x80)
1273e3adcf8fSFrançois Tigeot 			return false;
1274e3adcf8fSFrançois Tigeot 	}
1275e3adcf8fSFrançois Tigeot 
1276e3adcf8fSFrançois Tigeot 	return true;
1277e3adcf8fSFrançois Tigeot }
1278e3adcf8fSFrançois Tigeot 
1279e3adcf8fSFrançois Tigeot static int check_gamma(struct drm_intel_overlay_attrs *attrs)
1280e3adcf8fSFrançois Tigeot {
1281e3adcf8fSFrançois Tigeot 	if (!check_gamma_bounds(0, attrs->gamma0) ||
1282e3adcf8fSFrançois Tigeot 	    !check_gamma_bounds(attrs->gamma0, attrs->gamma1) ||
1283e3adcf8fSFrançois Tigeot 	    !check_gamma_bounds(attrs->gamma1, attrs->gamma2) ||
1284e3adcf8fSFrançois Tigeot 	    !check_gamma_bounds(attrs->gamma2, attrs->gamma3) ||
1285e3adcf8fSFrançois Tigeot 	    !check_gamma_bounds(attrs->gamma3, attrs->gamma4) ||
1286e3adcf8fSFrançois Tigeot 	    !check_gamma_bounds(attrs->gamma4, attrs->gamma5) ||
1287e3adcf8fSFrançois Tigeot 	    !check_gamma_bounds(attrs->gamma5, 0x00ffffff))
1288e3adcf8fSFrançois Tigeot 		return -EINVAL;
1289e3adcf8fSFrançois Tigeot 
1290e3adcf8fSFrançois Tigeot 	if (!check_gamma5_errata(attrs->gamma5))
1291e3adcf8fSFrançois Tigeot 		return -EINVAL;
1292e3adcf8fSFrançois Tigeot 
1293e3adcf8fSFrançois Tigeot 	return 0;
1294e3adcf8fSFrançois Tigeot }
1295e3adcf8fSFrançois Tigeot 
12961487f786SFrançois Tigeot int intel_overlay_attrs_ioctl(struct drm_device *dev, void *data,
1297e3adcf8fSFrançois Tigeot 			      struct drm_file *file_priv)
1298e3adcf8fSFrançois Tigeot {
1299e3adcf8fSFrançois Tigeot 	struct drm_intel_overlay_attrs *attrs = data;
1300bf017597SFrançois Tigeot 	struct drm_i915_private *dev_priv = to_i915(dev);
1301e3adcf8fSFrançois Tigeot 	struct intel_overlay *overlay;
1302e3440f96SFrançois Tigeot 	struct overlay_registers __iomem *regs;
1303e3adcf8fSFrançois Tigeot 	int ret;
1304e3adcf8fSFrançois Tigeot 
1305e3adcf8fSFrançois Tigeot 	overlay = dev_priv->overlay;
1306e3adcf8fSFrançois Tigeot 	if (!overlay) {
1307e3adcf8fSFrançois Tigeot 		DRM_DEBUG("userspace bug: no overlay\n");
1308e3adcf8fSFrançois Tigeot 		return -ENODEV;
1309e3adcf8fSFrançois Tigeot 	}
1310e3adcf8fSFrançois Tigeot 
1311a2fdbec6SFrançois Tigeot 	drm_modeset_lock_all(dev);
1312a2fdbec6SFrançois Tigeot 	mutex_lock(&dev->struct_mutex);
1313e3adcf8fSFrançois Tigeot 
1314e3adcf8fSFrançois Tigeot 	ret = -EINVAL;
1315e3adcf8fSFrançois Tigeot 	if (!(attrs->flags & I915_OVERLAY_UPDATE_ATTRS)) {
1316e3adcf8fSFrançois Tigeot 		attrs->color_key  = overlay->color_key;
1317e3adcf8fSFrançois Tigeot 		attrs->brightness = overlay->brightness;
1318e3adcf8fSFrançois Tigeot 		attrs->contrast   = overlay->contrast;
1319e3adcf8fSFrançois Tigeot 		attrs->saturation = overlay->saturation;
1320e3adcf8fSFrançois Tigeot 
13211487f786SFrançois Tigeot 		if (!IS_GEN2(dev_priv)) {
1322e3adcf8fSFrançois Tigeot 			attrs->gamma0 = I915_READ(OGAMC0);
1323e3adcf8fSFrançois Tigeot 			attrs->gamma1 = I915_READ(OGAMC1);
1324e3adcf8fSFrançois Tigeot 			attrs->gamma2 = I915_READ(OGAMC2);
1325e3adcf8fSFrançois Tigeot 			attrs->gamma3 = I915_READ(OGAMC3);
1326e3adcf8fSFrançois Tigeot 			attrs->gamma4 = I915_READ(OGAMC4);
1327e3adcf8fSFrançois Tigeot 			attrs->gamma5 = I915_READ(OGAMC5);
1328e3adcf8fSFrançois Tigeot 		}
1329e3adcf8fSFrançois Tigeot 	} else {
1330e3adcf8fSFrançois Tigeot 		if (attrs->brightness < -128 || attrs->brightness > 127)
1331e3adcf8fSFrançois Tigeot 			goto out_unlock;
1332e3adcf8fSFrançois Tigeot 		if (attrs->contrast > 255)
1333e3adcf8fSFrançois Tigeot 			goto out_unlock;
1334e3adcf8fSFrançois Tigeot 		if (attrs->saturation > 1023)
1335e3adcf8fSFrançois Tigeot 			goto out_unlock;
1336e3adcf8fSFrançois Tigeot 
1337e3adcf8fSFrançois Tigeot 		overlay->color_key  = attrs->color_key;
1338e3adcf8fSFrançois Tigeot 		overlay->brightness = attrs->brightness;
1339e3adcf8fSFrançois Tigeot 		overlay->contrast   = attrs->contrast;
1340e3adcf8fSFrançois Tigeot 		overlay->saturation = attrs->saturation;
1341e3adcf8fSFrançois Tigeot 
1342e3adcf8fSFrançois Tigeot 		regs = intel_overlay_map_regs(overlay);
1343e3adcf8fSFrançois Tigeot 		if (!regs) {
1344e3adcf8fSFrançois Tigeot 			ret = -ENOMEM;
1345e3adcf8fSFrançois Tigeot 			goto out_unlock;
1346e3adcf8fSFrançois Tigeot 		}
1347e3adcf8fSFrançois Tigeot 
1348e3adcf8fSFrançois Tigeot 		update_reg_attrs(overlay, regs);
1349e3adcf8fSFrançois Tigeot 
1350e3adcf8fSFrançois Tigeot 		intel_overlay_unmap_regs(overlay, regs);
1351e3adcf8fSFrançois Tigeot 
1352e3adcf8fSFrançois Tigeot 		if (attrs->flags & I915_OVERLAY_UPDATE_GAMMA) {
13531487f786SFrançois Tigeot 			if (IS_GEN2(dev_priv))
1354e3adcf8fSFrançois Tigeot 				goto out_unlock;
1355e3adcf8fSFrançois Tigeot 
1356e3adcf8fSFrançois Tigeot 			if (overlay->active) {
1357e3adcf8fSFrançois Tigeot 				ret = -EBUSY;
1358e3adcf8fSFrançois Tigeot 				goto out_unlock;
1359e3adcf8fSFrançois Tigeot 			}
1360e3adcf8fSFrançois Tigeot 
1361e3adcf8fSFrançois Tigeot 			ret = check_gamma(attrs);
1362e3adcf8fSFrançois Tigeot 			if (ret)
1363e3adcf8fSFrançois Tigeot 				goto out_unlock;
1364e3adcf8fSFrançois Tigeot 
1365e3adcf8fSFrançois Tigeot 			I915_WRITE(OGAMC0, attrs->gamma0);
1366e3adcf8fSFrançois Tigeot 			I915_WRITE(OGAMC1, attrs->gamma1);
1367e3adcf8fSFrançois Tigeot 			I915_WRITE(OGAMC2, attrs->gamma2);
1368e3adcf8fSFrançois Tigeot 			I915_WRITE(OGAMC3, attrs->gamma3);
1369e3adcf8fSFrançois Tigeot 			I915_WRITE(OGAMC4, attrs->gamma4);
1370e3adcf8fSFrançois Tigeot 			I915_WRITE(OGAMC5, attrs->gamma5);
1371e3adcf8fSFrançois Tigeot 		}
1372e3adcf8fSFrançois Tigeot 	}
137319c468b4SFrançois Tigeot 	overlay->color_key_enabled = (attrs->flags & I915_OVERLAY_DISABLE_DEST_COLORKEY) == 0;
1374e3adcf8fSFrançois Tigeot 
1375e3adcf8fSFrançois Tigeot 	ret = 0;
1376e3adcf8fSFrançois Tigeot out_unlock:
1377a2fdbec6SFrançois Tigeot 	mutex_unlock(&dev->struct_mutex);
1378a2fdbec6SFrançois Tigeot 	drm_modeset_unlock_all(dev);
1379e3adcf8fSFrançois Tigeot 
1380e3adcf8fSFrançois Tigeot 	return ret;
1381e3adcf8fSFrançois Tigeot }
1382e3adcf8fSFrançois Tigeot 
13831487f786SFrançois Tigeot void intel_setup_overlay(struct drm_i915_private *dev_priv)
1384e3adcf8fSFrançois Tigeot {
1385e3adcf8fSFrançois Tigeot 	struct intel_overlay *overlay;
1386e3adcf8fSFrançois Tigeot 	struct drm_i915_gem_object *reg_bo;
1387e3440f96SFrançois Tigeot 	struct overlay_registers __iomem *regs;
13881e12ee3bSFrançois Tigeot 	struct i915_vma *vma = NULL;
1389e3adcf8fSFrançois Tigeot 	int ret;
1390e3adcf8fSFrançois Tigeot 
13911487f786SFrançois Tigeot 	if (!HAS_OVERLAY(dev_priv))
1392e3adcf8fSFrançois Tigeot 		return;
1393e3adcf8fSFrançois Tigeot 
13949edbd4a0SFrançois Tigeot 	overlay = kzalloc(sizeof(*overlay), GFP_KERNEL);
1395e3440f96SFrançois Tigeot 	if (!overlay)
1396e3440f96SFrançois Tigeot 		return;
1397e3440f96SFrançois Tigeot 
1398303bf270SFrançois Tigeot 	mutex_lock(&dev_priv->drm.struct_mutex);
1399e3440f96SFrançois Tigeot 	if (WARN_ON(dev_priv->overlay))
1400e3adcf8fSFrançois Tigeot 		goto out_free;
1401e3440f96SFrançois Tigeot 
14021487f786SFrançois Tigeot 	overlay->i915 = dev_priv;
1403e3adcf8fSFrançois Tigeot 
14049edbd4a0SFrançois Tigeot 	reg_bo = NULL;
14051487f786SFrançois Tigeot 	if (!OVERLAY_NEEDS_PHYSICAL(dev_priv))
1406*a85cb24fSFrançois Tigeot 		reg_bo = i915_gem_object_create_stolen(dev_priv, PAGE_SIZE);
14079edbd4a0SFrançois Tigeot 	if (reg_bo == NULL)
1408*a85cb24fSFrançois Tigeot 		reg_bo = i915_gem_object_create(dev_priv, PAGE_SIZE);
14091487f786SFrançois Tigeot 	if (IS_ERR(reg_bo))
1410e3adcf8fSFrançois Tigeot 		goto out_free;
1411e3adcf8fSFrançois Tigeot 	overlay->reg_bo = reg_bo;
1412e3adcf8fSFrançois Tigeot 
14131487f786SFrançois Tigeot 	if (OVERLAY_NEEDS_PHYSICAL(dev_priv)) {
1414ba55f2f5SFrançois Tigeot 		ret = i915_gem_object_attach_phys(reg_bo, PAGE_SIZE);
1415e3adcf8fSFrançois Tigeot 		if (ret) {
1416e3adcf8fSFrançois Tigeot 			DRM_ERROR("failed to attach phys overlay regs\n");
1417e3adcf8fSFrançois Tigeot 			goto out_free_bo;
1418e3adcf8fSFrançois Tigeot 		}
1419ba55f2f5SFrançois Tigeot 		overlay->flip_addr = reg_bo->phys_handle->busaddr;
1420e3adcf8fSFrançois Tigeot 	} else {
14211e12ee3bSFrançois Tigeot 		vma = i915_gem_object_ggtt_pin(reg_bo, NULL,
142271f41f3eSFrançois Tigeot 					       0, PAGE_SIZE, PIN_MAPPABLE);
14231e12ee3bSFrançois Tigeot 		if (IS_ERR(vma)) {
1424e3adcf8fSFrançois Tigeot 			DRM_ERROR("failed to pin overlay register bo\n");
14251e12ee3bSFrançois Tigeot 			ret = PTR_ERR(vma);
1426e3adcf8fSFrançois Tigeot 			goto out_free_bo;
1427e3adcf8fSFrançois Tigeot 		}
14281e12ee3bSFrançois Tigeot 		overlay->flip_addr = i915_ggtt_offset(vma);
1429e3adcf8fSFrançois Tigeot 
1430e3adcf8fSFrançois Tigeot 		ret = i915_gem_object_set_to_gtt_domain(reg_bo, true);
1431e3adcf8fSFrançois Tigeot 		if (ret) {
1432e3adcf8fSFrançois Tigeot 			DRM_ERROR("failed to move overlay register bo into the GTT\n");
1433e3adcf8fSFrançois Tigeot 			goto out_unpin_bo;
1434e3adcf8fSFrançois Tigeot 		}
1435e3adcf8fSFrançois Tigeot 	}
1436e3adcf8fSFrançois Tigeot 
1437e3adcf8fSFrançois Tigeot 	/* init all values */
1438e3adcf8fSFrançois Tigeot 	overlay->color_key = 0x0101fe;
143919c468b4SFrançois Tigeot 	overlay->color_key_enabled = true;
1440e3adcf8fSFrançois Tigeot 	overlay->brightness = -19;
1441e3adcf8fSFrançois Tigeot 	overlay->contrast = 75;
1442e3adcf8fSFrançois Tigeot 	overlay->saturation = 146;
1443e3adcf8fSFrançois Tigeot 
14444be47400SFrançois Tigeot 	init_request_active(&overlay->last_flip, NULL);
14454be47400SFrançois Tigeot 
1446e3adcf8fSFrançois Tigeot 	regs = intel_overlay_map_regs(overlay);
1447e3adcf8fSFrançois Tigeot 	if (!regs)
1448e3adcf8fSFrançois Tigeot 		goto out_unpin_bo;
1449e3adcf8fSFrançois Tigeot 
1450e3440f96SFrançois Tigeot 	memset_io(regs, 0, sizeof(struct overlay_registers));
1451e3adcf8fSFrançois Tigeot 	update_polyphase_filter(regs);
1452e3adcf8fSFrançois Tigeot 	update_reg_attrs(overlay, regs);
1453e3adcf8fSFrançois Tigeot 
1454e3adcf8fSFrançois Tigeot 	intel_overlay_unmap_regs(overlay, regs);
1455e3adcf8fSFrançois Tigeot 
1456e3adcf8fSFrançois Tigeot 	dev_priv->overlay = overlay;
1457303bf270SFrançois Tigeot 	mutex_unlock(&dev_priv->drm.struct_mutex);
1458e3440f96SFrançois Tigeot 	DRM_INFO("initialized overlay support\n");
1459e3adcf8fSFrançois Tigeot 	return;
1460e3adcf8fSFrançois Tigeot 
1461e3adcf8fSFrançois Tigeot out_unpin_bo:
14621e12ee3bSFrançois Tigeot 	if (vma)
14631e12ee3bSFrançois Tigeot 		i915_vma_unpin(vma);
1464e3adcf8fSFrançois Tigeot out_free_bo:
146587df8fc6SFrançois Tigeot 	i915_gem_object_put(reg_bo);
1466e3adcf8fSFrançois Tigeot out_free:
1467303bf270SFrançois Tigeot 	mutex_unlock(&dev_priv->drm.struct_mutex);
1468158486a6SFrançois Tigeot 	kfree(overlay);
1469e3adcf8fSFrançois Tigeot 	return;
1470e3adcf8fSFrançois Tigeot }
1471e3adcf8fSFrançois Tigeot 
14721487f786SFrançois Tigeot void intel_cleanup_overlay(struct drm_i915_private *dev_priv)
1473e3adcf8fSFrançois Tigeot {
1474e3adcf8fSFrançois Tigeot 	if (!dev_priv->overlay)
1475e3adcf8fSFrançois Tigeot 		return;
1476e3adcf8fSFrançois Tigeot 
1477e3adcf8fSFrançois Tigeot 	/* The bo's should be free'd by the generic code already.
1478e3adcf8fSFrançois Tigeot 	 * Furthermore modesetting teardown happens beforehand so the
1479e3adcf8fSFrançois Tigeot 	 * hardware should be off already */
148019c468b4SFrançois Tigeot 	WARN_ON(dev_priv->overlay->active);
1481e3adcf8fSFrançois Tigeot 
14824be47400SFrançois Tigeot 	i915_gem_object_put(dev_priv->overlay->reg_bo);
1483158486a6SFrançois Tigeot 	kfree(dev_priv->overlay);
1484e3adcf8fSFrançois Tigeot }
1485e3adcf8fSFrançois Tigeot 
14861e12ee3bSFrançois Tigeot #if IS_ENABLED(CONFIG_DRM_I915_CAPTURE_ERROR)
14871e12ee3bSFrançois Tigeot 
1488e3adcf8fSFrançois Tigeot struct intel_overlay_error_state {
1489e3adcf8fSFrançois Tigeot 	struct overlay_registers regs;
1490e3adcf8fSFrançois Tigeot 	unsigned long base;
1491e3adcf8fSFrançois Tigeot 	u32 dovsta;
1492e3adcf8fSFrançois Tigeot 	u32 isr;
1493e3adcf8fSFrançois Tigeot };
1494e3adcf8fSFrançois Tigeot 
1495e3440f96SFrançois Tigeot static struct overlay_registers __iomem *
1496e3440f96SFrançois Tigeot intel_overlay_map_regs_atomic(struct intel_overlay *overlay)
1497e3440f96SFrançois Tigeot {
14981487f786SFrançois Tigeot 	struct drm_i915_private *dev_priv = overlay->i915;
1499e3440f96SFrançois Tigeot 	struct overlay_registers __iomem *regs;
1500e3440f96SFrançois Tigeot 
15011487f786SFrançois Tigeot 	if (OVERLAY_NEEDS_PHYSICAL(dev_priv))
1502e3440f96SFrançois Tigeot 		/* Cast to make sparse happy, but it's wc memory anyway, so
1503e3440f96SFrançois Tigeot 		 * equivalent to the wc io mapping on X86. */
1504e3440f96SFrançois Tigeot 		regs = (struct overlay_registers __iomem *)
15052c9916cdSFrançois Tigeot 			overlay->reg_bo->phys_handle->vaddr;
1506e3440f96SFrançois Tigeot 	else
15071e12ee3bSFrançois Tigeot 		regs = io_mapping_map_atomic_wc(&dev_priv->ggtt.mappable,
15081487f786SFrançois Tigeot 						overlay->flip_addr);
1509e3440f96SFrançois Tigeot 
1510e3440f96SFrançois Tigeot 	return regs;
1511e3440f96SFrançois Tigeot }
1512e3440f96SFrançois Tigeot 
1513e3440f96SFrançois Tigeot static void intel_overlay_unmap_regs_atomic(struct intel_overlay *overlay,
1514e3440f96SFrançois Tigeot 					struct overlay_registers __iomem *regs)
1515e3440f96SFrançois Tigeot {
15161487f786SFrançois Tigeot 	if (!OVERLAY_NEEDS_PHYSICAL(overlay->i915))
1517e3440f96SFrançois Tigeot 		io_mapping_unmap_atomic(regs);
1518e3440f96SFrançois Tigeot }
1519e3440f96SFrançois Tigeot 
1520e3adcf8fSFrançois Tigeot struct intel_overlay_error_state *
15211487f786SFrançois Tigeot intel_overlay_capture_error_state(struct drm_i915_private *dev_priv)
1522e3adcf8fSFrançois Tigeot {
1523e3adcf8fSFrançois Tigeot 	struct intel_overlay *overlay = dev_priv->overlay;
1524e3adcf8fSFrançois Tigeot 	struct intel_overlay_error_state *error;
1525e3adcf8fSFrançois Tigeot 	struct overlay_registers __iomem *regs;
1526e3adcf8fSFrançois Tigeot 
1527e3adcf8fSFrançois Tigeot 	if (!overlay || !overlay->active)
1528e3adcf8fSFrançois Tigeot 		return NULL;
1529e3adcf8fSFrançois Tigeot 
1530bf017597SFrançois Tigeot 	error = kmalloc(sizeof(*error), M_DRM, GFP_ATOMIC);
1531e3adcf8fSFrançois Tigeot 	if (error == NULL)
1532e3adcf8fSFrançois Tigeot 		return NULL;
1533e3adcf8fSFrançois Tigeot 
1534e3adcf8fSFrançois Tigeot 	error->dovsta = I915_READ(DOVSTA);
1535e3adcf8fSFrançois Tigeot 	error->isr = I915_READ(ISR);
15361487f786SFrançois Tigeot 	error->base = overlay->flip_addr;
1537e3adcf8fSFrançois Tigeot 
1538e3440f96SFrançois Tigeot 	regs = intel_overlay_map_regs_atomic(overlay);
1539e3adcf8fSFrançois Tigeot 	if (!regs)
1540e3adcf8fSFrançois Tigeot 		goto err;
1541e3adcf8fSFrançois Tigeot 
1542e3440f96SFrançois Tigeot 	memcpy_fromio(&error->regs, regs, sizeof(struct overlay_registers));
1543e3440f96SFrançois Tigeot 	intel_overlay_unmap_regs_atomic(overlay, regs);
1544e3adcf8fSFrançois Tigeot 
1545e3440f96SFrançois Tigeot 	return error;
1546e3adcf8fSFrançois Tigeot 
1547e3adcf8fSFrançois Tigeot err:
1548158486a6SFrançois Tigeot 	kfree(error);
1549e3440f96SFrançois Tigeot 	return NULL;
1550e3adcf8fSFrançois Tigeot }
1551e3adcf8fSFrançois Tigeot 
1552e3adcf8fSFrançois Tigeot void
15535d0b1887SFrançois Tigeot intel_overlay_print_error_state(struct drm_i915_error_state_buf *m,
15545d0b1887SFrançois Tigeot 				struct intel_overlay_error_state *error)
1555e3adcf8fSFrançois Tigeot {
15565d0b1887SFrançois Tigeot 	i915_error_printf(m, "Overlay, status: 0x%08x, interrupt: 0x%08x\n",
1557e3adcf8fSFrançois Tigeot 			  error->dovsta, error->isr);
15585d0b1887SFrançois Tigeot 	i915_error_printf(m, "  Register file at 0x%08lx:\n",
1559e3adcf8fSFrançois Tigeot 			  error->base);
1560e3adcf8fSFrançois Tigeot 
15615d0b1887SFrançois Tigeot #define P(x) i915_error_printf(m, "    " #x ":	0x%08x\n", error->regs.x)
1562e3adcf8fSFrançois Tigeot 	P(OBUF_0Y);
1563e3adcf8fSFrançois Tigeot 	P(OBUF_1Y);
1564e3adcf8fSFrançois Tigeot 	P(OBUF_0U);
1565e3adcf8fSFrançois Tigeot 	P(OBUF_0V);
1566e3adcf8fSFrançois Tigeot 	P(OBUF_1U);
1567e3adcf8fSFrançois Tigeot 	P(OBUF_1V);
1568e3adcf8fSFrançois Tigeot 	P(OSTRIDE);
1569e3adcf8fSFrançois Tigeot 	P(YRGB_VPH);
1570e3adcf8fSFrançois Tigeot 	P(UV_VPH);
1571e3adcf8fSFrançois Tigeot 	P(HORZ_PH);
1572e3adcf8fSFrançois Tigeot 	P(INIT_PHS);
1573e3adcf8fSFrançois Tigeot 	P(DWINPOS);
1574e3adcf8fSFrançois Tigeot 	P(DWINSZ);
1575e3adcf8fSFrançois Tigeot 	P(SWIDTH);
1576e3adcf8fSFrançois Tigeot 	P(SWIDTHSW);
1577e3adcf8fSFrançois Tigeot 	P(SHEIGHT);
1578e3adcf8fSFrançois Tigeot 	P(YRGBSCALE);
1579e3adcf8fSFrançois Tigeot 	P(UVSCALE);
1580e3adcf8fSFrançois Tigeot 	P(OCLRC0);
1581e3adcf8fSFrançois Tigeot 	P(OCLRC1);
1582e3adcf8fSFrançois Tigeot 	P(DCLRKV);
1583e3adcf8fSFrançois Tigeot 	P(DCLRKM);
1584e3adcf8fSFrançois Tigeot 	P(SCLRKVH);
1585e3adcf8fSFrançois Tigeot 	P(SCLRKVL);
1586e3adcf8fSFrançois Tigeot 	P(SCLRKEN);
1587e3adcf8fSFrançois Tigeot 	P(OCONFIG);
1588e3adcf8fSFrançois Tigeot 	P(OCMD);
1589e3adcf8fSFrançois Tigeot 	P(OSTART_0Y);
1590e3adcf8fSFrançois Tigeot 	P(OSTART_1Y);
1591e3adcf8fSFrançois Tigeot 	P(OSTART_0U);
1592e3adcf8fSFrançois Tigeot 	P(OSTART_0V);
1593e3adcf8fSFrançois Tigeot 	P(OSTART_1U);
1594e3adcf8fSFrançois Tigeot 	P(OSTART_1V);
1595e3adcf8fSFrançois Tigeot 	P(OTILEOFF_0Y);
1596e3adcf8fSFrançois Tigeot 	P(OTILEOFF_1Y);
1597e3adcf8fSFrançois Tigeot 	P(OTILEOFF_0U);
1598e3adcf8fSFrançois Tigeot 	P(OTILEOFF_0V);
1599e3adcf8fSFrançois Tigeot 	P(OTILEOFF_1U);
1600e3adcf8fSFrançois Tigeot 	P(OTILEOFF_1V);
1601e3adcf8fSFrançois Tigeot 	P(FASTHSCALE);
1602e3adcf8fSFrançois Tigeot 	P(UVSCALEV);
1603e3adcf8fSFrançois Tigeot #undef P
1604e3adcf8fSFrançois Tigeot }
16051e12ee3bSFrançois Tigeot 
16061e12ee3bSFrançois Tigeot #endif
1607