xref: /dflybsd-src/sys/dev/drm/i915/intel_overlay.c (revision 3f2dd94a569761201b5b0a18b2f697f97fe1b9dc)
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 
i830_overlay_clock_gating(struct drm_i915_private * dev_priv,bool enable)190a85cb24fSFrançois Tigeot static void i830_overlay_clock_gating(struct drm_i915_private *dev_priv,
191a85cb24fSFrançois Tigeot 				      bool enable)
192a85cb24fSFrançois Tigeot {
193a85cb24fSFrançois Tigeot 	struct pci_dev *pdev = dev_priv->drm.pdev;
194a85cb24fSFrançois Tigeot 	u8 val;
195a85cb24fSFrançois Tigeot 
196a85cb24fSFrançois Tigeot 	/* WA_OVERLAY_CLKGATE:alm */
197a85cb24fSFrançois Tigeot 	if (enable)
198a85cb24fSFrançois Tigeot 		I915_WRITE(DSPCLK_GATE_D, 0);
199a85cb24fSFrançois Tigeot 	else
200a85cb24fSFrançois Tigeot 		I915_WRITE(DSPCLK_GATE_D, OVRUNIT_CLOCK_GATE_DISABLE);
201a85cb24fSFrançois Tigeot 
202a85cb24fSFrançois Tigeot 	/* WA_DISABLE_L2CACHE_CLOCK_GATING:alm */
203a85cb24fSFrançois Tigeot 	pci_bus_read_config_byte(pdev->bus,
204a85cb24fSFrançois Tigeot 				 PCI_DEVFN(0, 0), I830_CLOCK_GATE, &val);
205a85cb24fSFrançois Tigeot 	if (enable)
206a85cb24fSFrançois Tigeot 		val &= ~I830_L2_CACHE_CLOCK_GATE_DISABLE;
207a85cb24fSFrançois Tigeot 	else
208a85cb24fSFrançois Tigeot 		val |= I830_L2_CACHE_CLOCK_GATE_DISABLE;
209a85cb24fSFrançois Tigeot #if 0
210a85cb24fSFrançois Tigeot 	pci_bus_write_config_byte(pdev->bus,
211a85cb24fSFrançois Tigeot 				  PCI_DEVFN(0, 0), I830_CLOCK_GATE, val);
212a85cb24fSFrançois Tigeot #endif
213a85cb24fSFrançois Tigeot }
214a85cb24fSFrançois Tigeot 
215e3440f96SFrançois Tigeot static struct overlay_registers __iomem *
intel_overlay_map_regs(struct intel_overlay * overlay)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 
intel_overlay_unmap_regs(struct intel_overlay * overlay,struct overlay_registers __iomem * regs)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 
intel_overlay_submit_request(struct intel_overlay * overlay,struct drm_i915_gem_request * req,i915_gem_retire_fn retire)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 
intel_overlay_do_wait_request(struct intel_overlay * overlay,struct drm_i915_gem_request * req,i915_gem_retire_fn retire)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 
alloc_request(struct intel_overlay * overlay)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 */
intel_overlay_on(struct intel_overlay * overlay)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;
272a85cb24fSFrançois Tigeot 	u32 *cs;
273e3adcf8fSFrançois Tigeot 
27419c468b4SFrançois Tigeot 	WARN_ON(overlay->active);
275e3adcf8fSFrançois Tigeot 
27671f41f3eSFrançois Tigeot 	req = alloc_request(overlay);
277c0e85e96SFrançois Tigeot 	if (IS_ERR(req))
278c0e85e96SFrançois Tigeot 		return PTR_ERR(req);
279f192107fSFrançois Tigeot 
280a85cb24fSFrançois Tigeot 	cs = intel_ring_begin(req, 4);
281a85cb24fSFrançois Tigeot 	if (IS_ERR(cs)) {
282a85cb24fSFrançois Tigeot 		i915_add_request(req);
283a85cb24fSFrançois Tigeot 		return PTR_ERR(cs);
284a05eeebfSFrançois Tigeot 	}
285a05eeebfSFrançois Tigeot 
28619c468b4SFrançois Tigeot 	overlay->active = true;
28719c468b4SFrançois Tigeot 
288a85cb24fSFrançois Tigeot 	if (IS_I830(dev_priv))
289a85cb24fSFrançois Tigeot 		i830_overlay_clock_gating(dev_priv, false);
290a85cb24fSFrançois Tigeot 
291a85cb24fSFrançois Tigeot 	*cs++ = MI_OVERLAY_FLIP | MI_OVERLAY_ON;
292a85cb24fSFrançois Tigeot 	*cs++ = overlay->flip_addr | OFC_UPDATE;
293a85cb24fSFrançois Tigeot 	*cs++ = MI_WAIT_FOR_EVENT | MI_WAIT_FOR_OVERLAY_FLIP;
294a85cb24fSFrançois Tigeot 	*cs++ = MI_NOOP;
295a85cb24fSFrançois Tigeot 	intel_ring_advance(req, cs);
296f192107fSFrançois Tigeot 
297a05eeebfSFrançois Tigeot 	return intel_overlay_do_wait_request(overlay, req, NULL);
298e3adcf8fSFrançois Tigeot }
299e3adcf8fSFrançois Tigeot 
intel_overlay_flip_prepare(struct intel_overlay * overlay,struct i915_vma * vma)300a85cb24fSFrançois Tigeot static void intel_overlay_flip_prepare(struct intel_overlay *overlay,
301a85cb24fSFrançois Tigeot 				       struct i915_vma *vma)
302a85cb24fSFrançois Tigeot {
303a85cb24fSFrançois Tigeot 	enum i915_pipe pipe = overlay->crtc->pipe;
304a85cb24fSFrançois Tigeot 
305a85cb24fSFrançois Tigeot 	WARN_ON(overlay->old_vma);
306a85cb24fSFrançois Tigeot 
307a85cb24fSFrançois Tigeot 	i915_gem_track_fb(overlay->vma ? overlay->vma->obj : NULL,
308a85cb24fSFrançois Tigeot 			  vma ? vma->obj : NULL,
309a85cb24fSFrançois Tigeot 			  INTEL_FRONTBUFFER_OVERLAY(pipe));
310a85cb24fSFrançois Tigeot 
311a85cb24fSFrançois Tigeot 	intel_frontbuffer_flip_prepare(overlay->i915,
312a85cb24fSFrançois Tigeot 				       INTEL_FRONTBUFFER_OVERLAY(pipe));
313a85cb24fSFrançois Tigeot 
314a85cb24fSFrançois Tigeot 	overlay->old_vma = overlay->vma;
315a85cb24fSFrançois Tigeot 	if (vma)
316a85cb24fSFrançois Tigeot 		overlay->vma = i915_vma_get(vma);
317a85cb24fSFrançois Tigeot 	else
318a85cb24fSFrançois Tigeot 		overlay->vma = NULL;
319a85cb24fSFrançois Tigeot }
320a85cb24fSFrançois Tigeot 
321e3adcf8fSFrançois Tigeot /* overlay needs to be enabled in OCMD reg */
intel_overlay_continue(struct intel_overlay * overlay,struct i915_vma * vma,bool load_polyphase_filter)322e3adcf8fSFrançois Tigeot static int intel_overlay_continue(struct intel_overlay *overlay,
323a85cb24fSFrançois Tigeot 				  struct i915_vma *vma,
324e3adcf8fSFrançois Tigeot 				  bool load_polyphase_filter)
325e3adcf8fSFrançois Tigeot {
3261487f786SFrançois Tigeot 	struct drm_i915_private *dev_priv = overlay->i915;
327a05eeebfSFrançois Tigeot 	struct drm_i915_gem_request *req;
328e3adcf8fSFrançois Tigeot 	u32 flip_addr = overlay->flip_addr;
329a85cb24fSFrançois Tigeot 	u32 tmp, *cs;
330e3adcf8fSFrançois Tigeot 
33119c468b4SFrançois Tigeot 	WARN_ON(!overlay->active);
332e3adcf8fSFrançois Tigeot 
333e3adcf8fSFrançois Tigeot 	if (load_polyphase_filter)
334e3adcf8fSFrançois Tigeot 		flip_addr |= OFC_UPDATE;
335e3adcf8fSFrançois Tigeot 
336e3adcf8fSFrançois Tigeot 	/* check for underruns */
337e3adcf8fSFrançois Tigeot 	tmp = I915_READ(DOVSTA);
338e3adcf8fSFrançois Tigeot 	if (tmp & (1 << 17))
339e3adcf8fSFrançois Tigeot 		DRM_DEBUG("overlay underrun, DOVSTA: %x\n", tmp);
340e3adcf8fSFrançois Tigeot 
34171f41f3eSFrançois Tigeot 	req = alloc_request(overlay);
342c0e85e96SFrançois Tigeot 	if (IS_ERR(req))
343c0e85e96SFrançois Tigeot 		return PTR_ERR(req);
344e3adcf8fSFrançois Tigeot 
345a85cb24fSFrançois Tigeot 	cs = intel_ring_begin(req, 2);
346a85cb24fSFrançois Tigeot 	if (IS_ERR(cs)) {
347a85cb24fSFrançois Tigeot 		i915_add_request(req);
348a85cb24fSFrançois Tigeot 		return PTR_ERR(cs);
349a05eeebfSFrançois Tigeot 	}
350a05eeebfSFrançois Tigeot 
351a85cb24fSFrançois Tigeot 	*cs++ = MI_OVERLAY_FLIP | MI_OVERLAY_CONTINUE;
352a85cb24fSFrançois Tigeot 	*cs++ = flip_addr;
353a85cb24fSFrançois Tigeot 	intel_ring_advance(req, cs);
354a85cb24fSFrançois Tigeot 
355a85cb24fSFrançois Tigeot 	intel_overlay_flip_prepare(overlay, vma);
356e3adcf8fSFrançois Tigeot 
35771f41f3eSFrançois Tigeot 	intel_overlay_submit_request(overlay, req, NULL);
358a05eeebfSFrançois Tigeot 
359a05eeebfSFrançois Tigeot 	return 0;
360e3adcf8fSFrançois Tigeot }
361e3adcf8fSFrançois Tigeot 
intel_overlay_release_old_vma(struct intel_overlay * overlay)362a85cb24fSFrançois Tigeot static void intel_overlay_release_old_vma(struct intel_overlay *overlay)
363a85cb24fSFrançois Tigeot {
364a85cb24fSFrançois Tigeot 	struct i915_vma *vma;
365a85cb24fSFrançois Tigeot 
366a85cb24fSFrançois Tigeot 	vma = fetch_and_zero(&overlay->old_vma);
367a85cb24fSFrançois Tigeot 	if (WARN_ON(!vma))
368a85cb24fSFrançois Tigeot 		return;
369a85cb24fSFrançois Tigeot 
370a85cb24fSFrançois Tigeot 	intel_frontbuffer_flip_complete(overlay->i915,
371a85cb24fSFrançois Tigeot 					INTEL_FRONTBUFFER_OVERLAY(overlay->crtc->pipe));
372a85cb24fSFrançois Tigeot 
373a85cb24fSFrançois Tigeot 	i915_gem_object_unpin_from_display_plane(vma);
374a85cb24fSFrançois Tigeot 	i915_vma_put(vma);
375a85cb24fSFrançois Tigeot }
376a85cb24fSFrançois Tigeot 
intel_overlay_release_old_vid_tail(struct i915_gem_active * active,struct drm_i915_gem_request * req)37771f41f3eSFrançois Tigeot static void intel_overlay_release_old_vid_tail(struct i915_gem_active *active,
37871f41f3eSFrançois Tigeot 					       struct drm_i915_gem_request *req)
379e3adcf8fSFrançois Tigeot {
38071f41f3eSFrançois Tigeot 	struct intel_overlay *overlay =
38171f41f3eSFrançois Tigeot 		container_of(active, typeof(*overlay), last_flip);
382e3adcf8fSFrançois Tigeot 
383a85cb24fSFrançois Tigeot 	intel_overlay_release_old_vma(overlay);
384e3adcf8fSFrançois Tigeot }
385e3adcf8fSFrançois Tigeot 
intel_overlay_off_tail(struct i915_gem_active * active,struct drm_i915_gem_request * req)38671f41f3eSFrançois Tigeot static void intel_overlay_off_tail(struct i915_gem_active *active,
38771f41f3eSFrançois Tigeot 				   struct drm_i915_gem_request *req)
388e3adcf8fSFrançois Tigeot {
38971f41f3eSFrançois Tigeot 	struct intel_overlay *overlay =
39071f41f3eSFrançois Tigeot 		container_of(active, typeof(*overlay), last_flip);
391a85cb24fSFrançois Tigeot 	struct drm_i915_private *dev_priv = overlay->i915;
392e3adcf8fSFrançois Tigeot 
393a85cb24fSFrançois Tigeot 	intel_overlay_release_old_vma(overlay);
394e3adcf8fSFrançois Tigeot 
395e3adcf8fSFrançois Tigeot 	overlay->crtc->overlay = NULL;
396e3adcf8fSFrançois Tigeot 	overlay->crtc = NULL;
39719c468b4SFrançois Tigeot 	overlay->active = false;
398a85cb24fSFrançois Tigeot 
399a85cb24fSFrançois Tigeot 	if (IS_I830(dev_priv))
400a85cb24fSFrançois Tigeot 		i830_overlay_clock_gating(dev_priv, true);
401e3adcf8fSFrançois Tigeot }
402e3adcf8fSFrançois Tigeot 
403e3adcf8fSFrançois Tigeot /* overlay needs to be disabled in OCMD reg */
intel_overlay_off(struct intel_overlay * overlay)404e3adcf8fSFrançois Tigeot static int intel_overlay_off(struct intel_overlay *overlay)
405e3adcf8fSFrançois Tigeot {
406a05eeebfSFrançois Tigeot 	struct drm_i915_gem_request *req;
407a85cb24fSFrançois Tigeot 	u32 *cs, flip_addr = overlay->flip_addr;
408e3adcf8fSFrançois Tigeot 
40919c468b4SFrançois Tigeot 	WARN_ON(!overlay->active);
410e3adcf8fSFrançois Tigeot 
411e3adcf8fSFrançois Tigeot 	/* According to intel docs the overlay hw may hang (when switching
412e3adcf8fSFrançois Tigeot 	 * off) without loading the filter coeffs. It is however unclear whether
413e3adcf8fSFrançois Tigeot 	 * this applies to the disabling of the overlay or to the switching off
414e3adcf8fSFrançois Tigeot 	 * of the hw. Do it in both cases */
415e3adcf8fSFrançois Tigeot 	flip_addr |= OFC_UPDATE;
416e3adcf8fSFrançois Tigeot 
41771f41f3eSFrançois Tigeot 	req = alloc_request(overlay);
418c0e85e96SFrançois Tigeot 	if (IS_ERR(req))
419c0e85e96SFrançois Tigeot 		return PTR_ERR(req);
420e3adcf8fSFrançois Tigeot 
421a85cb24fSFrançois Tigeot 	cs = intel_ring_begin(req, 6);
422a85cb24fSFrançois Tigeot 	if (IS_ERR(cs)) {
423a85cb24fSFrançois Tigeot 		i915_add_request(req);
424a85cb24fSFrançois Tigeot 		return PTR_ERR(cs);
425a05eeebfSFrançois Tigeot 	}
426a05eeebfSFrançois Tigeot 
427f192107fSFrançois Tigeot 	/* wait for overlay to go idle */
428a85cb24fSFrançois Tigeot 	*cs++ = MI_OVERLAY_FLIP | MI_OVERLAY_CONTINUE;
429a85cb24fSFrançois Tigeot 	*cs++ = flip_addr;
430a85cb24fSFrançois Tigeot 	*cs++ = MI_WAIT_FOR_EVENT | MI_WAIT_FOR_OVERLAY_FLIP;
431a85cb24fSFrançois Tigeot 
432f192107fSFrançois Tigeot 	/* turn overlay off */
433a85cb24fSFrançois Tigeot 	*cs++ = MI_OVERLAY_FLIP | MI_OVERLAY_OFF;
434a85cb24fSFrançois Tigeot 	*cs++ = flip_addr;
435a85cb24fSFrançois Tigeot 	*cs++ = MI_WAIT_FOR_EVENT | MI_WAIT_FOR_OVERLAY_FLIP;
436a85cb24fSFrançois Tigeot 
437a85cb24fSFrançois Tigeot 	intel_ring_advance(req, cs);
438a85cb24fSFrançois Tigeot 
439a85cb24fSFrançois Tigeot 	intel_overlay_flip_prepare(overlay, NULL);
440f192107fSFrançois Tigeot 
44171f41f3eSFrançois Tigeot 	return intel_overlay_do_wait_request(overlay, req,
44271f41f3eSFrançois Tigeot 					     intel_overlay_off_tail);
443e3adcf8fSFrançois Tigeot }
444e3adcf8fSFrançois Tigeot 
445e3adcf8fSFrançois Tigeot /* recover from an interruption due to a signal
446e3adcf8fSFrançois Tigeot  * We have to be careful not to repeat work forever an make forward progess. */
intel_overlay_recover_from_interrupt(struct intel_overlay * overlay)447e3adcf8fSFrançois Tigeot static int intel_overlay_recover_from_interrupt(struct intel_overlay *overlay)
448e3adcf8fSFrançois Tigeot {
44971f41f3eSFrançois Tigeot 	return i915_gem_active_retire(&overlay->last_flip,
45071f41f3eSFrançois Tigeot 				      &overlay->i915->drm.struct_mutex);
451e3adcf8fSFrançois Tigeot }
452e3adcf8fSFrançois Tigeot 
453e3adcf8fSFrançois Tigeot /* Wait for pending overlay flip and release old frame.
454e3adcf8fSFrançois Tigeot  * Needs to be called before the overlay register are changed
455e3adcf8fSFrançois Tigeot  * via intel_overlay_(un)map_regs
456e3adcf8fSFrançois Tigeot  */
intel_overlay_release_old_vid(struct intel_overlay * overlay)457e3adcf8fSFrançois Tigeot static int intel_overlay_release_old_vid(struct intel_overlay *overlay)
458e3adcf8fSFrançois Tigeot {
4591487f786SFrançois Tigeot 	struct drm_i915_private *dev_priv = overlay->i915;
460a85cb24fSFrançois Tigeot 	u32 *cs;
461e3adcf8fSFrançois Tigeot 	int ret;
462e3adcf8fSFrançois Tigeot 
463303bf270SFrançois Tigeot 	lockdep_assert_held(&dev_priv->drm.struct_mutex);
4642c9916cdSFrançois Tigeot 
465e3adcf8fSFrançois Tigeot 	/* Only wait if there is actually an old frame to release to
466e3adcf8fSFrançois Tigeot 	 * guarantee forward progress.
467e3adcf8fSFrançois Tigeot 	 */
4681e12ee3bSFrançois Tigeot 	if (!overlay->old_vma)
469e3adcf8fSFrançois Tigeot 		return 0;
470e3adcf8fSFrançois Tigeot 
471e3adcf8fSFrançois Tigeot 	if (I915_READ(ISR) & I915_OVERLAY_PLANE_FLIP_PENDING_INTERRUPT) {
472e3adcf8fSFrançois Tigeot 		/* synchronous slowpath */
473a05eeebfSFrançois Tigeot 		struct drm_i915_gem_request *req;
474a05eeebfSFrançois Tigeot 
47571f41f3eSFrançois Tigeot 		req = alloc_request(overlay);
476c0e85e96SFrançois Tigeot 		if (IS_ERR(req))
477c0e85e96SFrançois Tigeot 			return PTR_ERR(req);
478e3adcf8fSFrançois Tigeot 
479a85cb24fSFrançois Tigeot 		cs = intel_ring_begin(req, 2);
480a85cb24fSFrançois Tigeot 		if (IS_ERR(cs)) {
481a85cb24fSFrançois Tigeot 			i915_add_request(req);
482a85cb24fSFrançois Tigeot 			return PTR_ERR(cs);
483a05eeebfSFrançois Tigeot 		}
484a05eeebfSFrançois Tigeot 
485a85cb24fSFrançois Tigeot 		*cs++ = MI_WAIT_FOR_EVENT | MI_WAIT_FOR_OVERLAY_FLIP;
486a85cb24fSFrançois Tigeot 		*cs++ = MI_NOOP;
487a85cb24fSFrançois Tigeot 		intel_ring_advance(req, cs);
488e3adcf8fSFrançois Tigeot 
489a05eeebfSFrançois Tigeot 		ret = intel_overlay_do_wait_request(overlay, req,
490e3adcf8fSFrançois Tigeot 						    intel_overlay_release_old_vid_tail);
491e3adcf8fSFrançois Tigeot 		if (ret)
492e3adcf8fSFrançois Tigeot 			return ret;
49371f41f3eSFrançois Tigeot 	} else
49471f41f3eSFrançois Tigeot 		intel_overlay_release_old_vid_tail(&overlay->last_flip, NULL);
495e3adcf8fSFrançois Tigeot 
496e3adcf8fSFrançois Tigeot 	return 0;
497e3adcf8fSFrançois Tigeot }
498e3adcf8fSFrançois Tigeot 
intel_overlay_reset(struct drm_i915_private * dev_priv)4992c9916cdSFrançois Tigeot void intel_overlay_reset(struct drm_i915_private *dev_priv)
5002c9916cdSFrançois Tigeot {
5012c9916cdSFrançois Tigeot 	struct intel_overlay *overlay = dev_priv->overlay;
5022c9916cdSFrançois Tigeot 
5032c9916cdSFrançois Tigeot 	if (!overlay)
5042c9916cdSFrançois Tigeot 		return;
5052c9916cdSFrançois Tigeot 
5062c9916cdSFrançois Tigeot 	intel_overlay_release_old_vid(overlay);
5072c9916cdSFrançois Tigeot 
5082c9916cdSFrançois Tigeot 	overlay->old_xscale = 0;
5092c9916cdSFrançois Tigeot 	overlay->old_yscale = 0;
5102c9916cdSFrançois Tigeot 	overlay->crtc = NULL;
5112c9916cdSFrançois Tigeot 	overlay->active = false;
5122c9916cdSFrançois Tigeot }
5132c9916cdSFrançois Tigeot 
514e3adcf8fSFrançois Tigeot struct put_image_params {
515e3adcf8fSFrançois Tigeot 	int format;
516e3adcf8fSFrançois Tigeot 	short dst_x;
517e3adcf8fSFrançois Tigeot 	short dst_y;
518e3adcf8fSFrançois Tigeot 	short dst_w;
519e3adcf8fSFrançois Tigeot 	short dst_h;
520e3adcf8fSFrançois Tigeot 	short src_w;
521e3adcf8fSFrançois Tigeot 	short src_scan_h;
522e3adcf8fSFrançois Tigeot 	short src_scan_w;
523e3adcf8fSFrançois Tigeot 	short src_h;
524e3adcf8fSFrançois Tigeot 	short stride_Y;
525e3adcf8fSFrançois Tigeot 	short stride_UV;
526e3adcf8fSFrançois Tigeot 	int offset_Y;
527e3adcf8fSFrançois Tigeot 	int offset_U;
528e3adcf8fSFrançois Tigeot 	int offset_V;
529e3adcf8fSFrançois Tigeot };
530e3adcf8fSFrançois Tigeot 
packed_depth_bytes(u32 format)531e3adcf8fSFrançois Tigeot static int packed_depth_bytes(u32 format)
532e3adcf8fSFrançois Tigeot {
533e3adcf8fSFrançois Tigeot 	switch (format & I915_OVERLAY_DEPTH_MASK) {
534e3adcf8fSFrançois Tigeot 	case I915_OVERLAY_YUV422:
535e3adcf8fSFrançois Tigeot 		return 4;
536e3adcf8fSFrançois Tigeot 	case I915_OVERLAY_YUV411:
537e3adcf8fSFrançois Tigeot 		/* return 6; not implemented */
538e3adcf8fSFrançois Tigeot 	default:
539e3adcf8fSFrançois Tigeot 		return -EINVAL;
540e3adcf8fSFrançois Tigeot 	}
541e3adcf8fSFrançois Tigeot }
542e3adcf8fSFrançois Tigeot 
packed_width_bytes(u32 format,short width)543e3adcf8fSFrançois Tigeot static int packed_width_bytes(u32 format, short width)
544e3adcf8fSFrançois Tigeot {
545e3adcf8fSFrançois Tigeot 	switch (format & I915_OVERLAY_DEPTH_MASK) {
546e3adcf8fSFrançois Tigeot 	case I915_OVERLAY_YUV422:
547e3adcf8fSFrançois Tigeot 		return width << 1;
548e3adcf8fSFrançois Tigeot 	default:
549e3adcf8fSFrançois Tigeot 		return -EINVAL;
550e3adcf8fSFrançois Tigeot 	}
551e3adcf8fSFrançois Tigeot }
552e3adcf8fSFrançois Tigeot 
uv_hsubsampling(u32 format)553e3adcf8fSFrançois Tigeot static int uv_hsubsampling(u32 format)
554e3adcf8fSFrançois Tigeot {
555e3adcf8fSFrançois Tigeot 	switch (format & I915_OVERLAY_DEPTH_MASK) {
556e3adcf8fSFrançois Tigeot 	case I915_OVERLAY_YUV422:
557e3adcf8fSFrançois Tigeot 	case I915_OVERLAY_YUV420:
558e3adcf8fSFrançois Tigeot 		return 2;
559e3adcf8fSFrançois Tigeot 	case I915_OVERLAY_YUV411:
560e3adcf8fSFrançois Tigeot 	case I915_OVERLAY_YUV410:
561e3adcf8fSFrançois Tigeot 		return 4;
562e3adcf8fSFrançois Tigeot 	default:
563e3adcf8fSFrançois Tigeot 		return -EINVAL;
564e3adcf8fSFrançois Tigeot 	}
565e3adcf8fSFrançois Tigeot }
566e3adcf8fSFrançois Tigeot 
uv_vsubsampling(u32 format)567e3adcf8fSFrançois Tigeot static int uv_vsubsampling(u32 format)
568e3adcf8fSFrançois Tigeot {
569e3adcf8fSFrançois Tigeot 	switch (format & I915_OVERLAY_DEPTH_MASK) {
570e3adcf8fSFrançois Tigeot 	case I915_OVERLAY_YUV420:
571e3adcf8fSFrançois Tigeot 	case I915_OVERLAY_YUV410:
572e3adcf8fSFrançois Tigeot 		return 2;
573e3adcf8fSFrançois Tigeot 	case I915_OVERLAY_YUV422:
574e3adcf8fSFrançois Tigeot 	case I915_OVERLAY_YUV411:
575e3adcf8fSFrançois Tigeot 		return 1;
576e3adcf8fSFrançois Tigeot 	default:
577e3adcf8fSFrançois Tigeot 		return -EINVAL;
578e3adcf8fSFrançois Tigeot 	}
579e3adcf8fSFrançois Tigeot }
580e3adcf8fSFrançois Tigeot 
calc_swidthsw(struct drm_i915_private * dev_priv,u32 offset,u32 width)5811487f786SFrançois Tigeot static u32 calc_swidthsw(struct drm_i915_private *dev_priv, u32 offset, u32 width)
582e3adcf8fSFrançois Tigeot {
583a85cb24fSFrançois Tigeot 	u32 sw;
584a85cb24fSFrançois Tigeot 
585a85cb24fSFrançois Tigeot 	if (IS_GEN2(dev_priv))
586a85cb24fSFrançois Tigeot 		sw = ALIGN((offset & 31) + width, 32);
587a85cb24fSFrançois Tigeot 	else
588a85cb24fSFrançois Tigeot 		sw = ALIGN((offset & 63) + width, 64);
589a85cb24fSFrançois Tigeot 
590a85cb24fSFrançois Tigeot 	if (sw == 0)
591a85cb24fSFrançois Tigeot 		return 0;
592a85cb24fSFrançois Tigeot 
593a85cb24fSFrançois Tigeot 	return (sw - 32) >> 3;
594e3adcf8fSFrançois Tigeot }
595e3adcf8fSFrançois Tigeot 
596a85cb24fSFrançois Tigeot static const u16 y_static_hcoeffs[N_PHASES][N_HORIZ_Y_TAPS] = {
597a85cb24fSFrançois Tigeot 	[ 0] = { 0x3000, 0xb4a0, 0x1930, 0x1920, 0xb4a0, },
598a85cb24fSFrançois Tigeot 	[ 1] = { 0x3000, 0xb500, 0x19d0, 0x1880, 0xb440, },
599a85cb24fSFrançois Tigeot 	[ 2] = { 0x3000, 0xb540, 0x1a88, 0x2f80, 0xb3e0, },
600a85cb24fSFrançois Tigeot 	[ 3] = { 0x3000, 0xb580, 0x1b30, 0x2e20, 0xb380, },
601a85cb24fSFrançois Tigeot 	[ 4] = { 0x3000, 0xb5c0, 0x1bd8, 0x2cc0, 0xb320, },
602a85cb24fSFrançois Tigeot 	[ 5] = { 0x3020, 0xb5e0, 0x1c60, 0x2b80, 0xb2c0, },
603a85cb24fSFrançois Tigeot 	[ 6] = { 0x3020, 0xb5e0, 0x1cf8, 0x2a20, 0xb260, },
604a85cb24fSFrançois Tigeot 	[ 7] = { 0x3020, 0xb5e0, 0x1d80, 0x28e0, 0xb200, },
605a85cb24fSFrançois Tigeot 	[ 8] = { 0x3020, 0xb5c0, 0x1e08, 0x3f40, 0xb1c0, },
606a85cb24fSFrançois Tigeot 	[ 9] = { 0x3020, 0xb580, 0x1e78, 0x3ce0, 0xb160, },
607a85cb24fSFrançois Tigeot 	[10] = { 0x3040, 0xb520, 0x1ed8, 0x3aa0, 0xb120, },
608a85cb24fSFrançois Tigeot 	[11] = { 0x3040, 0xb4a0, 0x1f30, 0x3880, 0xb0e0, },
609a85cb24fSFrançois Tigeot 	[12] = { 0x3040, 0xb400, 0x1f78, 0x3680, 0xb0a0, },
610a85cb24fSFrançois Tigeot 	[13] = { 0x3020, 0xb340, 0x1fb8, 0x34a0, 0xb060, },
611a85cb24fSFrançois Tigeot 	[14] = { 0x3020, 0xb240, 0x1fe0, 0x32e0, 0xb040, },
612a85cb24fSFrançois Tigeot 	[15] = { 0x3020, 0xb140, 0x1ff8, 0x3160, 0xb020, },
613a85cb24fSFrançois Tigeot 	[16] = { 0xb000, 0x3000, 0x0800, 0x3000, 0xb000, },
614e3adcf8fSFrançois Tigeot };
615e3adcf8fSFrançois Tigeot 
616a85cb24fSFrançois Tigeot static const u16 uv_static_hcoeffs[N_PHASES][N_HORIZ_UV_TAPS] = {
617a85cb24fSFrançois Tigeot 	[ 0] = { 0x3000, 0x1800, 0x1800, },
618a85cb24fSFrançois Tigeot 	[ 1] = { 0xb000, 0x18d0, 0x2e60, },
619a85cb24fSFrançois Tigeot 	[ 2] = { 0xb000, 0x1990, 0x2ce0, },
620a85cb24fSFrançois Tigeot 	[ 3] = { 0xb020, 0x1a68, 0x2b40, },
621a85cb24fSFrançois Tigeot 	[ 4] = { 0xb040, 0x1b20, 0x29e0, },
622a85cb24fSFrançois Tigeot 	[ 5] = { 0xb060, 0x1bd8, 0x2880, },
623a85cb24fSFrançois Tigeot 	[ 6] = { 0xb080, 0x1c88, 0x3e60, },
624a85cb24fSFrançois Tigeot 	[ 7] = { 0xb0a0, 0x1d28, 0x3c00, },
625a85cb24fSFrançois Tigeot 	[ 8] = { 0xb0c0, 0x1db8, 0x39e0, },
626a85cb24fSFrançois Tigeot 	[ 9] = { 0xb0e0, 0x1e40, 0x37e0, },
627a85cb24fSFrançois Tigeot 	[10] = { 0xb100, 0x1eb8, 0x3620, },
628a85cb24fSFrançois Tigeot 	[11] = { 0xb100, 0x1f18, 0x34a0, },
629a85cb24fSFrançois Tigeot 	[12] = { 0xb100, 0x1f68, 0x3360, },
630a85cb24fSFrançois Tigeot 	[13] = { 0xb0e0, 0x1fa8, 0x3240, },
631a85cb24fSFrançois Tigeot 	[14] = { 0xb0c0, 0x1fe0, 0x3140, },
632a85cb24fSFrançois Tigeot 	[15] = { 0xb060, 0x1ff0, 0x30a0, },
633a85cb24fSFrançois Tigeot 	[16] = { 0x3000, 0x0800, 0x3000, },
634e3adcf8fSFrançois Tigeot };
635e3adcf8fSFrançois Tigeot 
update_polyphase_filter(struct overlay_registers __iomem * regs)636e3440f96SFrançois Tigeot static void update_polyphase_filter(struct overlay_registers __iomem *regs)
637e3adcf8fSFrançois Tigeot {
638e3440f96SFrançois Tigeot 	memcpy_toio(regs->Y_HCOEFS, y_static_hcoeffs, sizeof(y_static_hcoeffs));
639e3440f96SFrançois Tigeot 	memcpy_toio(regs->UV_HCOEFS, uv_static_hcoeffs,
640e3440f96SFrançois Tigeot 		    sizeof(uv_static_hcoeffs));
641e3adcf8fSFrançois Tigeot }
642e3adcf8fSFrançois Tigeot 
update_scaling_factors(struct intel_overlay * overlay,struct overlay_registers __iomem * regs,struct put_image_params * params)643e3adcf8fSFrançois Tigeot static bool update_scaling_factors(struct intel_overlay *overlay,
644e3440f96SFrançois Tigeot 				   struct overlay_registers __iomem *regs,
645e3adcf8fSFrançois Tigeot 				   struct put_image_params *params)
646e3adcf8fSFrançois Tigeot {
647e3adcf8fSFrançois Tigeot 	/* fixed point with a 12 bit shift */
648e3adcf8fSFrançois Tigeot 	u32 xscale, yscale, xscale_UV, yscale_UV;
649e3adcf8fSFrançois Tigeot #define FP_SHIFT 12
650e3adcf8fSFrançois Tigeot #define FRACT_MASK 0xfff
651e3adcf8fSFrançois Tigeot 	bool scale_changed = false;
652e3adcf8fSFrançois Tigeot 	int uv_hscale = uv_hsubsampling(params->format);
653e3adcf8fSFrançois Tigeot 	int uv_vscale = uv_vsubsampling(params->format);
654e3adcf8fSFrançois Tigeot 
655e3adcf8fSFrançois Tigeot 	if (params->dst_w > 1)
656e3adcf8fSFrançois Tigeot 		xscale = ((params->src_scan_w - 1) << FP_SHIFT)
657e3adcf8fSFrançois Tigeot 			/(params->dst_w);
658e3adcf8fSFrançois Tigeot 	else
659e3adcf8fSFrançois Tigeot 		xscale = 1 << FP_SHIFT;
660e3adcf8fSFrançois Tigeot 
661e3adcf8fSFrançois Tigeot 	if (params->dst_h > 1)
662e3adcf8fSFrançois Tigeot 		yscale = ((params->src_scan_h - 1) << FP_SHIFT)
663e3adcf8fSFrançois Tigeot 			/(params->dst_h);
664e3adcf8fSFrançois Tigeot 	else
665e3adcf8fSFrançois Tigeot 		yscale = 1 << FP_SHIFT;
666e3adcf8fSFrançois Tigeot 
667e3adcf8fSFrançois Tigeot 	/*if (params->format & I915_OVERLAY_YUV_PLANAR) {*/
668e3adcf8fSFrançois Tigeot 	xscale_UV = xscale/uv_hscale;
669e3adcf8fSFrançois Tigeot 	yscale_UV = yscale/uv_vscale;
670e3adcf8fSFrançois Tigeot 	/* make the Y scale to UV scale ratio an exact multiply */
671e3adcf8fSFrançois Tigeot 	xscale = xscale_UV * uv_hscale;
672e3adcf8fSFrançois Tigeot 	yscale = yscale_UV * uv_vscale;
673e3adcf8fSFrançois Tigeot 	/*} else {
674e3adcf8fSFrançois Tigeot 	  xscale_UV = 0;
675e3adcf8fSFrançois Tigeot 	  yscale_UV = 0;
676e3adcf8fSFrançois Tigeot 	  }*/
677e3adcf8fSFrançois Tigeot 
678e3adcf8fSFrançois Tigeot 	if (xscale != overlay->old_xscale || yscale != overlay->old_yscale)
679e3adcf8fSFrançois Tigeot 		scale_changed = true;
680e3adcf8fSFrançois Tigeot 	overlay->old_xscale = xscale;
681e3adcf8fSFrançois Tigeot 	overlay->old_yscale = yscale;
682e3adcf8fSFrançois Tigeot 
683e3440f96SFrançois Tigeot 	iowrite32(((yscale & FRACT_MASK) << 20) |
684e3adcf8fSFrançois Tigeot 		  ((xscale >> FP_SHIFT)  << 16) |
685e3440f96SFrançois Tigeot 		  ((xscale & FRACT_MASK) << 3),
686e3440f96SFrançois Tigeot 		 &regs->YRGBSCALE);
687e3adcf8fSFrançois Tigeot 
688e3440f96SFrançois Tigeot 	iowrite32(((yscale_UV & FRACT_MASK) << 20) |
689e3adcf8fSFrançois Tigeot 		  ((xscale_UV >> FP_SHIFT)  << 16) |
690e3440f96SFrançois Tigeot 		  ((xscale_UV & FRACT_MASK) << 3),
691e3440f96SFrançois Tigeot 		 &regs->UVSCALE);
692e3adcf8fSFrançois Tigeot 
693e3440f96SFrançois Tigeot 	iowrite32((((yscale    >> FP_SHIFT) << 16) |
694e3440f96SFrançois Tigeot 		   ((yscale_UV >> FP_SHIFT) << 0)),
695e3440f96SFrançois Tigeot 		 &regs->UVSCALEV);
696e3adcf8fSFrançois Tigeot 
697e3adcf8fSFrançois Tigeot 	if (scale_changed)
698e3adcf8fSFrançois Tigeot 		update_polyphase_filter(regs);
699e3adcf8fSFrançois Tigeot 
700e3adcf8fSFrançois Tigeot 	return scale_changed;
701e3adcf8fSFrançois Tigeot }
702e3adcf8fSFrançois Tigeot 
update_colorkey(struct intel_overlay * overlay,struct overlay_registers __iomem * regs)703e3adcf8fSFrançois Tigeot static void update_colorkey(struct intel_overlay *overlay,
704e3440f96SFrançois Tigeot 			    struct overlay_registers __iomem *regs)
705e3adcf8fSFrançois Tigeot {
706a85cb24fSFrançois Tigeot 	const struct intel_plane_state *state =
707a85cb24fSFrançois Tigeot 		to_intel_plane_state(overlay->crtc->base.primary->state);
708e3adcf8fSFrançois Tigeot 	u32 key = overlay->color_key;
709a85cb24fSFrançois Tigeot 	u32 format = 0;
710a85cb24fSFrançois Tigeot 	u32 flags = 0;
71119c468b4SFrançois Tigeot 
71219c468b4SFrançois Tigeot 	if (overlay->color_key_enabled)
71319c468b4SFrançois Tigeot 		flags |= DST_KEY_ENABLE;
714e3adcf8fSFrançois Tigeot 
715a85cb24fSFrançois Tigeot 	if (state->base.visible)
716a85cb24fSFrançois Tigeot 		format = state->base.fb->format->format;
717a85cb24fSFrançois Tigeot 
718a85cb24fSFrançois Tigeot 	switch (format) {
719a85cb24fSFrançois Tigeot 	case DRM_FORMAT_C8:
72019c468b4SFrançois Tigeot 		key = 0;
72119c468b4SFrançois Tigeot 		flags |= CLK_RGB8I_MASK;
722e3adcf8fSFrançois Tigeot 		break;
723a85cb24fSFrançois Tigeot 	case DRM_FORMAT_XRGB1555:
72419c468b4SFrançois Tigeot 		key = RGB15_TO_COLORKEY(key);
72519c468b4SFrançois Tigeot 		flags |= CLK_RGB15_MASK;
726a85cb24fSFrançois Tigeot 		break;
727a85cb24fSFrançois Tigeot 	case DRM_FORMAT_RGB565:
72819c468b4SFrançois Tigeot 		key = RGB16_TO_COLORKEY(key);
72919c468b4SFrançois Tigeot 		flags |= CLK_RGB16_MASK;
730e3adcf8fSFrançois Tigeot 		break;
731a85cb24fSFrançois Tigeot 	default:
73219c468b4SFrançois Tigeot 		flags |= CLK_RGB24_MASK;
733e3adcf8fSFrançois Tigeot 		break;
734e3adcf8fSFrançois Tigeot 	}
73519c468b4SFrançois Tigeot 
73619c468b4SFrançois Tigeot 	iowrite32(key, &regs->DCLRKV);
73719c468b4SFrançois Tigeot 	iowrite32(flags, &regs->DCLRKM);
738e3adcf8fSFrançois Tigeot }
739e3adcf8fSFrançois Tigeot 
overlay_cmd_reg(struct put_image_params * params)740e3adcf8fSFrançois Tigeot static u32 overlay_cmd_reg(struct put_image_params *params)
741e3adcf8fSFrançois Tigeot {
742e3adcf8fSFrançois Tigeot 	u32 cmd = OCMD_ENABLE | OCMD_BUF_TYPE_FRAME | OCMD_BUFFER0;
743e3adcf8fSFrançois Tigeot 
744e3adcf8fSFrançois Tigeot 	if (params->format & I915_OVERLAY_YUV_PLANAR) {
745e3adcf8fSFrançois Tigeot 		switch (params->format & I915_OVERLAY_DEPTH_MASK) {
746e3adcf8fSFrançois Tigeot 		case I915_OVERLAY_YUV422:
747e3adcf8fSFrançois Tigeot 			cmd |= OCMD_YUV_422_PLANAR;
748e3adcf8fSFrançois Tigeot 			break;
749e3adcf8fSFrançois Tigeot 		case I915_OVERLAY_YUV420:
750e3adcf8fSFrançois Tigeot 			cmd |= OCMD_YUV_420_PLANAR;
751e3adcf8fSFrançois Tigeot 			break;
752e3adcf8fSFrançois Tigeot 		case I915_OVERLAY_YUV411:
753e3adcf8fSFrançois Tigeot 		case I915_OVERLAY_YUV410:
754e3adcf8fSFrançois Tigeot 			cmd |= OCMD_YUV_410_PLANAR;
755e3adcf8fSFrançois Tigeot 			break;
756e3adcf8fSFrançois Tigeot 		}
757e3adcf8fSFrançois Tigeot 	} else { /* YUV packed */
758e3adcf8fSFrançois Tigeot 		switch (params->format & I915_OVERLAY_DEPTH_MASK) {
759e3adcf8fSFrançois Tigeot 		case I915_OVERLAY_YUV422:
760e3adcf8fSFrançois Tigeot 			cmd |= OCMD_YUV_422_PACKED;
761e3adcf8fSFrançois Tigeot 			break;
762e3adcf8fSFrançois Tigeot 		case I915_OVERLAY_YUV411:
763e3adcf8fSFrançois Tigeot 			cmd |= OCMD_YUV_411_PACKED;
764e3adcf8fSFrançois Tigeot 			break;
765e3adcf8fSFrançois Tigeot 		}
766e3adcf8fSFrançois Tigeot 
767e3adcf8fSFrançois Tigeot 		switch (params->format & I915_OVERLAY_SWAP_MASK) {
768e3adcf8fSFrançois Tigeot 		case I915_OVERLAY_NO_SWAP:
769e3adcf8fSFrançois Tigeot 			break;
770e3adcf8fSFrançois Tigeot 		case I915_OVERLAY_UV_SWAP:
771e3adcf8fSFrançois Tigeot 			cmd |= OCMD_UV_SWAP;
772e3adcf8fSFrançois Tigeot 			break;
773e3adcf8fSFrançois Tigeot 		case I915_OVERLAY_Y_SWAP:
774e3adcf8fSFrançois Tigeot 			cmd |= OCMD_Y_SWAP;
775e3adcf8fSFrançois Tigeot 			break;
776e3adcf8fSFrançois Tigeot 		case I915_OVERLAY_Y_AND_UV_SWAP:
777e3adcf8fSFrançois Tigeot 			cmd |= OCMD_Y_AND_UV_SWAP;
778e3adcf8fSFrançois Tigeot 			break;
779e3adcf8fSFrançois Tigeot 		}
780e3adcf8fSFrançois Tigeot 	}
781e3adcf8fSFrançois Tigeot 
782e3adcf8fSFrançois Tigeot 	return cmd;
783e3adcf8fSFrançois Tigeot }
784e3adcf8fSFrançois Tigeot 
intel_overlay_do_put_image(struct intel_overlay * overlay,struct drm_i915_gem_object * new_bo,struct put_image_params * params)785e3adcf8fSFrançois Tigeot static int intel_overlay_do_put_image(struct intel_overlay *overlay,
786e3adcf8fSFrançois Tigeot 				      struct drm_i915_gem_object *new_bo,
787e3adcf8fSFrançois Tigeot 				      struct put_image_params *params)
788e3adcf8fSFrançois Tigeot {
789e3adcf8fSFrançois Tigeot 	int ret, tmp_width;
790e3440f96SFrançois Tigeot 	struct overlay_registers __iomem *regs;
791e3adcf8fSFrançois Tigeot 	bool scale_changed = false;
7921487f786SFrançois Tigeot 	struct drm_i915_private *dev_priv = overlay->i915;
793e3440f96SFrançois Tigeot 	u32 swidth, swidthsw, sheight, ostride;
79424edb884SFrançois Tigeot 	enum i915_pipe pipe = overlay->crtc->pipe;
7951e12ee3bSFrançois Tigeot 	struct i915_vma *vma;
796e3adcf8fSFrançois Tigeot 
797303bf270SFrançois Tigeot 	lockdep_assert_held(&dev_priv->drm.struct_mutex);
798303bf270SFrançois Tigeot 	WARN_ON(!drm_modeset_is_locked(&dev_priv->drm.mode_config.connection_mutex));
799e3adcf8fSFrançois Tigeot 
800e3adcf8fSFrançois Tigeot 	ret = intel_overlay_release_old_vid(overlay);
801e3adcf8fSFrançois Tigeot 	if (ret != 0)
802e3adcf8fSFrançois Tigeot 		return ret;
803e3adcf8fSFrançois Tigeot 
804*3f2dd94aSFrançois Tigeot 	atomic_inc(&dev_priv->gpu_error.pending_fb_pin);
805*3f2dd94aSFrançois Tigeot 
806a85cb24fSFrançois Tigeot 	vma = i915_gem_object_pin_to_display_plane(new_bo, 0, NULL);
807*3f2dd94aSFrançois Tigeot 	if (IS_ERR(vma)) {
808*3f2dd94aSFrançois Tigeot 		ret = PTR_ERR(vma);
809*3f2dd94aSFrançois Tigeot 		goto out_pin_section;
810*3f2dd94aSFrançois Tigeot 	}
811e3adcf8fSFrançois Tigeot 
8121e12ee3bSFrançois Tigeot 	ret = i915_vma_put_fence(vma);
813e3adcf8fSFrançois Tigeot 	if (ret)
814e3adcf8fSFrançois Tigeot 		goto out_unpin;
815e3adcf8fSFrançois Tigeot 
816e3adcf8fSFrançois Tigeot 	if (!overlay->active) {
817e3440f96SFrançois Tigeot 		u32 oconfig;
818e3adcf8fSFrançois Tigeot 		regs = intel_overlay_map_regs(overlay);
819e3adcf8fSFrançois Tigeot 		if (!regs) {
820e3adcf8fSFrançois Tigeot 			ret = -ENOMEM;
821e3adcf8fSFrançois Tigeot 			goto out_unpin;
822e3adcf8fSFrançois Tigeot 		}
823e3440f96SFrançois Tigeot 		oconfig = OCONF_CC_OUT_8BIT;
8241487f786SFrançois Tigeot 		if (IS_GEN4(dev_priv))
825e3440f96SFrançois Tigeot 			oconfig |= OCONF_CSC_MODE_BT709;
82624edb884SFrançois Tigeot 		oconfig |= pipe == 0 ?
827e3adcf8fSFrançois Tigeot 			OCONF_PIPE_A : OCONF_PIPE_B;
828e3440f96SFrançois Tigeot 		iowrite32(oconfig, &regs->OCONFIG);
829e3adcf8fSFrançois Tigeot 		intel_overlay_unmap_regs(overlay, regs);
830e3adcf8fSFrançois Tigeot 
831e3adcf8fSFrançois Tigeot 		ret = intel_overlay_on(overlay);
832e3adcf8fSFrançois Tigeot 		if (ret != 0)
833e3adcf8fSFrançois Tigeot 			goto out_unpin;
834e3adcf8fSFrançois Tigeot 	}
835e3adcf8fSFrançois Tigeot 
836e3adcf8fSFrançois Tigeot 	regs = intel_overlay_map_regs(overlay);
837e3adcf8fSFrançois Tigeot 	if (!regs) {
838e3adcf8fSFrançois Tigeot 		ret = -ENOMEM;
839e3adcf8fSFrançois Tigeot 		goto out_unpin;
840e3adcf8fSFrançois Tigeot 	}
841e3adcf8fSFrançois Tigeot 
842e3440f96SFrançois Tigeot 	iowrite32((params->dst_y << 16) | params->dst_x, &regs->DWINPOS);
843e3440f96SFrançois Tigeot 	iowrite32((params->dst_h << 16) | params->dst_w, &regs->DWINSZ);
844e3adcf8fSFrançois Tigeot 
845e3adcf8fSFrançois Tigeot 	if (params->format & I915_OVERLAY_YUV_PACKED)
846e3adcf8fSFrançois Tigeot 		tmp_width = packed_width_bytes(params->format, params->src_w);
847e3adcf8fSFrançois Tigeot 	else
848e3adcf8fSFrançois Tigeot 		tmp_width = params->src_w;
849e3adcf8fSFrançois Tigeot 
850e3440f96SFrançois Tigeot 	swidth = params->src_w;
8511487f786SFrançois Tigeot 	swidthsw = calc_swidthsw(dev_priv, params->offset_Y, tmp_width);
852e3440f96SFrançois Tigeot 	sheight = params->src_h;
8531e12ee3bSFrançois Tigeot 	iowrite32(i915_ggtt_offset(vma) + params->offset_Y, &regs->OBUF_0Y);
854e3440f96SFrançois Tigeot 	ostride = params->stride_Y;
855e3adcf8fSFrançois Tigeot 
856e3adcf8fSFrançois Tigeot 	if (params->format & I915_OVERLAY_YUV_PLANAR) {
857e3adcf8fSFrançois Tigeot 		int uv_hscale = uv_hsubsampling(params->format);
858e3adcf8fSFrançois Tigeot 		int uv_vscale = uv_vsubsampling(params->format);
859e3adcf8fSFrançois Tigeot 		u32 tmp_U, tmp_V;
860e3440f96SFrançois Tigeot 		swidth |= (params->src_w/uv_hscale) << 16;
8611487f786SFrançois Tigeot 		tmp_U = calc_swidthsw(dev_priv, params->offset_U,
862e3adcf8fSFrançois Tigeot 				      params->src_w/uv_hscale);
8631487f786SFrançois Tigeot 		tmp_V = calc_swidthsw(dev_priv, params->offset_V,
864e3adcf8fSFrançois Tigeot 				      params->src_w/uv_hscale);
865e3440f96SFrançois Tigeot 		swidthsw |= max_t(u32, tmp_U, tmp_V) << 16;
866e3440f96SFrançois Tigeot 		sheight |= (params->src_h/uv_vscale) << 16;
8671e12ee3bSFrançois Tigeot 		iowrite32(i915_ggtt_offset(vma) + params->offset_U,
8681e12ee3bSFrançois Tigeot 			  &regs->OBUF_0U);
8691e12ee3bSFrançois Tigeot 		iowrite32(i915_ggtt_offset(vma) + params->offset_V,
8701e12ee3bSFrançois Tigeot 			  &regs->OBUF_0V);
871e3440f96SFrançois Tigeot 		ostride |= params->stride_UV << 16;
872e3adcf8fSFrançois Tigeot 	}
873e3adcf8fSFrançois Tigeot 
874e3440f96SFrançois Tigeot 	iowrite32(swidth, &regs->SWIDTH);
875e3440f96SFrançois Tigeot 	iowrite32(swidthsw, &regs->SWIDTHSW);
876e3440f96SFrançois Tigeot 	iowrite32(sheight, &regs->SHEIGHT);
877e3440f96SFrançois Tigeot 	iowrite32(ostride, &regs->OSTRIDE);
878e3440f96SFrançois Tigeot 
879e3adcf8fSFrançois Tigeot 	scale_changed = update_scaling_factors(overlay, regs, params);
880e3adcf8fSFrançois Tigeot 
881e3adcf8fSFrançois Tigeot 	update_colorkey(overlay, regs);
882e3adcf8fSFrançois Tigeot 
883e3440f96SFrançois Tigeot 	iowrite32(overlay_cmd_reg(params), &regs->OCMD);
884e3adcf8fSFrançois Tigeot 
885e3adcf8fSFrançois Tigeot 	intel_overlay_unmap_regs(overlay, regs);
886e3adcf8fSFrançois Tigeot 
887a85cb24fSFrançois Tigeot 	ret = intel_overlay_continue(overlay, vma, scale_changed);
888e3adcf8fSFrançois Tigeot 	if (ret)
889e3adcf8fSFrançois Tigeot 		goto out_unpin;
890e3adcf8fSFrançois Tigeot 
891e3adcf8fSFrançois Tigeot 	return 0;
892e3adcf8fSFrançois Tigeot 
893e3adcf8fSFrançois Tigeot out_unpin:
8941e12ee3bSFrançois Tigeot 	i915_gem_object_unpin_from_display_plane(vma);
895*3f2dd94aSFrançois Tigeot out_pin_section:
896*3f2dd94aSFrançois Tigeot 	atomic_dec(&dev_priv->gpu_error.pending_fb_pin);
897*3f2dd94aSFrançois Tigeot 
898e3adcf8fSFrançois Tigeot 	return ret;
899e3adcf8fSFrançois Tigeot }
900e3adcf8fSFrançois Tigeot 
intel_overlay_switch_off(struct intel_overlay * overlay)901e3adcf8fSFrançois Tigeot int intel_overlay_switch_off(struct intel_overlay *overlay)
902e3adcf8fSFrançois Tigeot {
9031487f786SFrançois Tigeot 	struct drm_i915_private *dev_priv = overlay->i915;
904e3440f96SFrançois Tigeot 	struct overlay_registers __iomem *regs;
905e3adcf8fSFrançois Tigeot 	int ret;
906e3adcf8fSFrançois Tigeot 
907303bf270SFrançois Tigeot 	lockdep_assert_held(&dev_priv->drm.struct_mutex);
908303bf270SFrançois Tigeot 	WARN_ON(!drm_modeset_is_locked(&dev_priv->drm.mode_config.connection_mutex));
909e3adcf8fSFrançois Tigeot 
910e3adcf8fSFrançois Tigeot 	ret = intel_overlay_recover_from_interrupt(overlay);
911e3adcf8fSFrançois Tigeot 	if (ret != 0)
912e3adcf8fSFrançois Tigeot 		return ret;
913e3adcf8fSFrançois Tigeot 
914e3adcf8fSFrançois Tigeot 	if (!overlay->active)
915e3adcf8fSFrançois Tigeot 		return 0;
916e3adcf8fSFrançois Tigeot 
917e3adcf8fSFrançois Tigeot 	ret = intel_overlay_release_old_vid(overlay);
918e3adcf8fSFrançois Tigeot 	if (ret != 0)
919e3adcf8fSFrançois Tigeot 		return ret;
920e3adcf8fSFrançois Tigeot 
921e3adcf8fSFrançois Tigeot 	regs = intel_overlay_map_regs(overlay);
922e3440f96SFrançois Tigeot 	iowrite32(0, &regs->OCMD);
923e3adcf8fSFrançois Tigeot 	intel_overlay_unmap_regs(overlay, regs);
924e3adcf8fSFrançois Tigeot 
92571f41f3eSFrançois Tigeot 	return intel_overlay_off(overlay);
926e3adcf8fSFrançois Tigeot }
927e3adcf8fSFrançois Tigeot 
check_overlay_possible_on_crtc(struct intel_overlay * overlay,struct intel_crtc * crtc)928e3adcf8fSFrançois Tigeot static int check_overlay_possible_on_crtc(struct intel_overlay *overlay,
929e3adcf8fSFrançois Tigeot 					  struct intel_crtc *crtc)
930e3adcf8fSFrançois Tigeot {
931e3adcf8fSFrançois Tigeot 	if (!crtc->active)
932e3adcf8fSFrançois Tigeot 		return -EINVAL;
933e3adcf8fSFrançois Tigeot 
934e3adcf8fSFrançois Tigeot 	/* can't use the overlay with double wide pipe */
9352c9916cdSFrançois Tigeot 	if (crtc->config->double_wide)
936e3adcf8fSFrançois Tigeot 		return -EINVAL;
937e3adcf8fSFrançois Tigeot 
938e3adcf8fSFrançois Tigeot 	return 0;
939e3adcf8fSFrançois Tigeot }
940e3adcf8fSFrançois Tigeot 
update_pfit_vscale_ratio(struct intel_overlay * overlay)941e3adcf8fSFrançois Tigeot static void update_pfit_vscale_ratio(struct intel_overlay *overlay)
942e3adcf8fSFrançois Tigeot {
9431487f786SFrançois Tigeot 	struct drm_i915_private *dev_priv = overlay->i915;
944e3adcf8fSFrançois Tigeot 	u32 pfit_control = I915_READ(PFIT_CONTROL);
945e3adcf8fSFrançois Tigeot 	u32 ratio;
946e3adcf8fSFrançois Tigeot 
947e3adcf8fSFrançois Tigeot 	/* XXX: This is not the same logic as in the xorg driver, but more in
948e3adcf8fSFrançois Tigeot 	 * line with the intel documentation for the i965
949e3adcf8fSFrançois Tigeot 	 */
9501487f786SFrançois Tigeot 	if (INTEL_GEN(dev_priv) >= 4) {
951e3adcf8fSFrançois Tigeot 		/* on i965 use the PGM reg to read out the autoscaler values */
952e3adcf8fSFrançois Tigeot 		ratio = I915_READ(PFIT_PGM_RATIOS) >> PFIT_VERT_SCALE_SHIFT_965;
953e3adcf8fSFrançois Tigeot 	} else {
954e3adcf8fSFrançois Tigeot 		if (pfit_control & VERT_AUTO_SCALE)
955e3adcf8fSFrançois Tigeot 			ratio = I915_READ(PFIT_AUTO_RATIOS);
956e3adcf8fSFrançois Tigeot 		else
957e3adcf8fSFrançois Tigeot 			ratio = I915_READ(PFIT_PGM_RATIOS);
958e3adcf8fSFrançois Tigeot 		ratio >>= PFIT_VERT_SCALE_SHIFT;
959e3adcf8fSFrançois Tigeot 	}
960e3adcf8fSFrançois Tigeot 
961e3adcf8fSFrançois Tigeot 	overlay->pfit_vscale_ratio = ratio;
962e3adcf8fSFrançois Tigeot }
963e3adcf8fSFrançois Tigeot 
check_overlay_dst(struct intel_overlay * overlay,struct drm_intel_overlay_put_image * rec)964e3adcf8fSFrançois Tigeot static int check_overlay_dst(struct intel_overlay *overlay,
965e3adcf8fSFrançois Tigeot 			     struct drm_intel_overlay_put_image *rec)
966e3adcf8fSFrançois Tigeot {
967a85cb24fSFrançois Tigeot 	const struct intel_crtc_state *pipe_config =
968a85cb24fSFrançois Tigeot 		overlay->crtc->config;
969e3adcf8fSFrançois Tigeot 
970a85cb24fSFrançois Tigeot 	if (rec->dst_x < pipe_config->pipe_src_w &&
971a85cb24fSFrançois Tigeot 	    rec->dst_x + rec->dst_width <= pipe_config->pipe_src_w &&
972a85cb24fSFrançois Tigeot 	    rec->dst_y < pipe_config->pipe_src_h &&
973a85cb24fSFrançois Tigeot 	    rec->dst_y + rec->dst_height <= pipe_config->pipe_src_h)
974e3adcf8fSFrançois Tigeot 		return 0;
975e3adcf8fSFrançois Tigeot 	else
976e3adcf8fSFrançois Tigeot 		return -EINVAL;
977e3adcf8fSFrançois Tigeot }
978e3adcf8fSFrançois Tigeot 
check_overlay_scaling(struct put_image_params * rec)979e3adcf8fSFrançois Tigeot static int check_overlay_scaling(struct put_image_params *rec)
980e3adcf8fSFrançois Tigeot {
981e3adcf8fSFrançois Tigeot 	u32 tmp;
982e3adcf8fSFrançois Tigeot 
983e3adcf8fSFrançois Tigeot 	/* downscaling limit is 8.0 */
984e3adcf8fSFrançois Tigeot 	tmp = ((rec->src_scan_h << 16) / rec->dst_h) >> 16;
985e3adcf8fSFrançois Tigeot 	if (tmp > 7)
986e3adcf8fSFrançois Tigeot 		return -EINVAL;
987e3adcf8fSFrançois Tigeot 	tmp = ((rec->src_scan_w << 16) / rec->dst_w) >> 16;
988e3adcf8fSFrançois Tigeot 	if (tmp > 7)
989e3adcf8fSFrançois Tigeot 		return -EINVAL;
990e3adcf8fSFrançois Tigeot 
991e3adcf8fSFrançois Tigeot 	return 0;
992e3adcf8fSFrançois Tigeot }
993e3adcf8fSFrançois Tigeot 
check_overlay_src(struct drm_i915_private * dev_priv,struct drm_intel_overlay_put_image * rec,struct drm_i915_gem_object * new_bo)9941487f786SFrançois Tigeot static int check_overlay_src(struct drm_i915_private *dev_priv,
995e3adcf8fSFrançois Tigeot 			     struct drm_intel_overlay_put_image *rec,
996e3adcf8fSFrançois Tigeot 			     struct drm_i915_gem_object *new_bo)
997e3adcf8fSFrançois Tigeot {
998e3adcf8fSFrançois Tigeot 	int uv_hscale = uv_hsubsampling(rec->flags);
999e3adcf8fSFrançois Tigeot 	int uv_vscale = uv_vsubsampling(rec->flags);
1000e3adcf8fSFrançois Tigeot 	u32 stride_mask;
1001e3adcf8fSFrançois Tigeot 	int depth;
1002e3adcf8fSFrançois Tigeot 	u32 tmp;
1003e3adcf8fSFrançois Tigeot 
1004e3adcf8fSFrançois Tigeot 	/* check src dimensions */
1005a85cb24fSFrançois Tigeot 	if (IS_I845G(dev_priv) || IS_I830(dev_priv)) {
1006e3adcf8fSFrançois Tigeot 		if (rec->src_height > IMAGE_MAX_HEIGHT_LEGACY ||
1007e3adcf8fSFrançois Tigeot 		    rec->src_width  > IMAGE_MAX_WIDTH_LEGACY)
1008e3adcf8fSFrançois Tigeot 			return -EINVAL;
1009e3adcf8fSFrançois Tigeot 	} else {
1010e3adcf8fSFrançois Tigeot 		if (rec->src_height > IMAGE_MAX_HEIGHT ||
1011e3adcf8fSFrançois Tigeot 		    rec->src_width  > IMAGE_MAX_WIDTH)
1012e3adcf8fSFrançois Tigeot 			return -EINVAL;
1013e3adcf8fSFrançois Tigeot 	}
1014e3adcf8fSFrançois Tigeot 
1015e3adcf8fSFrançois Tigeot 	/* better safe than sorry, use 4 as the maximal subsampling ratio */
1016e3adcf8fSFrançois Tigeot 	if (rec->src_height < N_VERT_Y_TAPS*4 ||
1017e3adcf8fSFrançois Tigeot 	    rec->src_width  < N_HORIZ_Y_TAPS*4)
1018e3adcf8fSFrançois Tigeot 		return -EINVAL;
1019e3adcf8fSFrançois Tigeot 
1020e3adcf8fSFrançois Tigeot 	/* check alignment constraints */
1021e3adcf8fSFrançois Tigeot 	switch (rec->flags & I915_OVERLAY_TYPE_MASK) {
1022e3adcf8fSFrançois Tigeot 	case I915_OVERLAY_RGB:
1023e3adcf8fSFrançois Tigeot 		/* not implemented */
1024e3adcf8fSFrançois Tigeot 		return -EINVAL;
1025e3adcf8fSFrançois Tigeot 
1026e3adcf8fSFrançois Tigeot 	case I915_OVERLAY_YUV_PACKED:
1027e3adcf8fSFrançois Tigeot 		if (uv_vscale != 1)
1028e3adcf8fSFrançois Tigeot 			return -EINVAL;
1029e3adcf8fSFrançois Tigeot 
1030e3adcf8fSFrançois Tigeot 		depth = packed_depth_bytes(rec->flags);
1031e3adcf8fSFrançois Tigeot 		if (depth < 0)
1032e3adcf8fSFrançois Tigeot 			return depth;
1033e3adcf8fSFrançois Tigeot 
1034e3adcf8fSFrançois Tigeot 		/* ignore UV planes */
1035e3adcf8fSFrançois Tigeot 		rec->stride_UV = 0;
1036e3adcf8fSFrançois Tigeot 		rec->offset_U = 0;
1037e3adcf8fSFrançois Tigeot 		rec->offset_V = 0;
1038e3adcf8fSFrançois Tigeot 		/* check pixel alignment */
1039e3adcf8fSFrançois Tigeot 		if (rec->offset_Y % depth)
1040e3adcf8fSFrançois Tigeot 			return -EINVAL;
1041e3adcf8fSFrançois Tigeot 		break;
1042e3adcf8fSFrançois Tigeot 
1043e3adcf8fSFrançois Tigeot 	case I915_OVERLAY_YUV_PLANAR:
1044e3adcf8fSFrançois Tigeot 		if (uv_vscale < 0 || uv_hscale < 0)
1045e3adcf8fSFrançois Tigeot 			return -EINVAL;
1046e3adcf8fSFrançois Tigeot 		/* no offset restrictions for planar formats */
1047e3adcf8fSFrançois Tigeot 		break;
1048e3adcf8fSFrançois Tigeot 
1049e3adcf8fSFrançois Tigeot 	default:
1050e3adcf8fSFrançois Tigeot 		return -EINVAL;
1051e3adcf8fSFrançois Tigeot 	}
1052e3adcf8fSFrançois Tigeot 
1053e3adcf8fSFrançois Tigeot 	if (rec->src_width % uv_hscale)
1054e3adcf8fSFrançois Tigeot 		return -EINVAL;
1055e3adcf8fSFrançois Tigeot 
1056e3adcf8fSFrançois Tigeot 	/* stride checking */
1057a85cb24fSFrançois Tigeot 	if (IS_I830(dev_priv) || IS_I845G(dev_priv))
1058e3adcf8fSFrançois Tigeot 		stride_mask = 255;
1059e3adcf8fSFrançois Tigeot 	else
1060e3adcf8fSFrançois Tigeot 		stride_mask = 63;
1061e3adcf8fSFrançois Tigeot 
1062e3adcf8fSFrançois Tigeot 	if (rec->stride_Y & stride_mask || rec->stride_UV & stride_mask)
1063e3adcf8fSFrançois Tigeot 		return -EINVAL;
10641487f786SFrançois Tigeot 	if (IS_GEN4(dev_priv) && rec->stride_Y < 512)
1065e3adcf8fSFrançois Tigeot 		return -EINVAL;
1066e3adcf8fSFrançois Tigeot 
1067e3adcf8fSFrançois Tigeot 	tmp = (rec->flags & I915_OVERLAY_TYPE_MASK) == I915_OVERLAY_YUV_PLANAR ?
1068e3adcf8fSFrançois Tigeot 		4096 : 8192;
1069e3adcf8fSFrançois Tigeot 	if (rec->stride_Y > tmp || rec->stride_UV > 2*1024)
1070e3adcf8fSFrançois Tigeot 		return -EINVAL;
1071e3adcf8fSFrançois Tigeot 
1072e3adcf8fSFrançois Tigeot 	/* check buffer dimensions */
1073e3adcf8fSFrançois Tigeot 	switch (rec->flags & I915_OVERLAY_TYPE_MASK) {
1074e3adcf8fSFrançois Tigeot 	case I915_OVERLAY_RGB:
1075e3adcf8fSFrançois Tigeot 	case I915_OVERLAY_YUV_PACKED:
1076e3adcf8fSFrançois Tigeot 		/* always 4 Y values per depth pixels */
1077e3adcf8fSFrançois Tigeot 		if (packed_width_bytes(rec->flags, rec->src_width) > rec->stride_Y)
1078e3adcf8fSFrançois Tigeot 			return -EINVAL;
1079e3adcf8fSFrançois Tigeot 
1080e3adcf8fSFrançois Tigeot 		tmp = rec->stride_Y*rec->src_height;
1081e3adcf8fSFrançois Tigeot 		if (rec->offset_Y + tmp > new_bo->base.size)
1082e3adcf8fSFrançois Tigeot 			return -EINVAL;
1083e3adcf8fSFrançois Tigeot 		break;
1084e3adcf8fSFrançois Tigeot 
1085e3adcf8fSFrançois Tigeot 	case I915_OVERLAY_YUV_PLANAR:
1086e3adcf8fSFrançois Tigeot 		if (rec->src_width > rec->stride_Y)
1087e3adcf8fSFrançois Tigeot 			return -EINVAL;
1088e3adcf8fSFrançois Tigeot 		if (rec->src_width/uv_hscale > rec->stride_UV)
1089e3adcf8fSFrançois Tigeot 			return -EINVAL;
1090e3adcf8fSFrançois Tigeot 
1091e3adcf8fSFrançois Tigeot 		tmp = rec->stride_Y * rec->src_height;
1092e3adcf8fSFrançois Tigeot 		if (rec->offset_Y + tmp > new_bo->base.size)
1093e3adcf8fSFrançois Tigeot 			return -EINVAL;
1094e3adcf8fSFrançois Tigeot 
1095e3adcf8fSFrançois Tigeot 		tmp = rec->stride_UV * (rec->src_height / uv_vscale);
1096e3adcf8fSFrançois Tigeot 		if (rec->offset_U + tmp > new_bo->base.size ||
1097e3adcf8fSFrançois Tigeot 		    rec->offset_V + tmp > new_bo->base.size)
1098e3adcf8fSFrançois Tigeot 			return -EINVAL;
1099e3adcf8fSFrançois Tigeot 		break;
1100e3adcf8fSFrançois Tigeot 	}
1101e3adcf8fSFrançois Tigeot 
1102e3adcf8fSFrançois Tigeot 	return 0;
1103e3adcf8fSFrançois Tigeot }
1104e3adcf8fSFrançois Tigeot 
intel_overlay_put_image_ioctl(struct drm_device * dev,void * data,struct drm_file * file_priv)11051487f786SFrançois Tigeot int intel_overlay_put_image_ioctl(struct drm_device *dev, void *data,
1106e3adcf8fSFrançois Tigeot 				  struct drm_file *file_priv)
1107e3adcf8fSFrançois Tigeot {
1108e3adcf8fSFrançois Tigeot 	struct drm_intel_overlay_put_image *put_image_rec = data;
1109bf017597SFrançois Tigeot 	struct drm_i915_private *dev_priv = to_i915(dev);
1110e3adcf8fSFrançois Tigeot 	struct intel_overlay *overlay;
111124edb884SFrançois Tigeot 	struct drm_crtc *drmmode_crtc;
1112e3adcf8fSFrançois Tigeot 	struct intel_crtc *crtc;
1113e3adcf8fSFrançois Tigeot 	struct drm_i915_gem_object *new_bo;
1114e3adcf8fSFrançois Tigeot 	struct put_image_params *params;
1115e3adcf8fSFrançois Tigeot 	int ret;
1116e3adcf8fSFrançois Tigeot 
1117e3adcf8fSFrançois Tigeot 	overlay = dev_priv->overlay;
1118e3adcf8fSFrançois Tigeot 	if (!overlay) {
1119e3adcf8fSFrançois Tigeot 		DRM_DEBUG("userspace bug: no overlay\n");
1120e3adcf8fSFrançois Tigeot 		return -ENODEV;
1121e3adcf8fSFrançois Tigeot 	}
1122e3adcf8fSFrançois Tigeot 
1123e3adcf8fSFrançois Tigeot 	if (!(put_image_rec->flags & I915_OVERLAY_ENABLE)) {
1124a2fdbec6SFrançois Tigeot 		drm_modeset_lock_all(dev);
1125a2fdbec6SFrançois Tigeot 		mutex_lock(&dev->struct_mutex);
1126e3adcf8fSFrançois Tigeot 
1127e3adcf8fSFrançois Tigeot 		ret = intel_overlay_switch_off(overlay);
1128e3adcf8fSFrançois Tigeot 
1129a2fdbec6SFrançois Tigeot 		mutex_unlock(&dev->struct_mutex);
1130a2fdbec6SFrançois Tigeot 		drm_modeset_unlock_all(dev);
1131e3adcf8fSFrançois Tigeot 
1132e3adcf8fSFrançois Tigeot 		return ret;
1133e3adcf8fSFrançois Tigeot 	}
1134e3adcf8fSFrançois Tigeot 
1135bf017597SFrançois Tigeot 	params = kmalloc(sizeof(*params), M_DRM, GFP_KERNEL);
1136e3440f96SFrançois Tigeot 	if (!params)
1137e3440f96SFrançois Tigeot 		return -ENOMEM;
1138e3adcf8fSFrançois Tigeot 
1139*3f2dd94aSFrançois Tigeot 	drmmode_crtc = drm_crtc_find(dev, file_priv, put_image_rec->crtc_id);
114024edb884SFrançois Tigeot 	if (!drmmode_crtc) {
1141e3adcf8fSFrançois Tigeot 		ret = -ENOENT;
1142e3adcf8fSFrançois Tigeot 		goto out_free;
1143e3adcf8fSFrançois Tigeot 	}
114424edb884SFrançois Tigeot 	crtc = to_intel_crtc(drmmode_crtc);
1145e3adcf8fSFrançois Tigeot 
114687df8fc6SFrançois Tigeot 	new_bo = i915_gem_object_lookup(file_priv, put_image_rec->bo_handle);
114787df8fc6SFrançois Tigeot 	if (!new_bo) {
1148e3adcf8fSFrançois Tigeot 		ret = -ENOENT;
1149e3adcf8fSFrançois Tigeot 		goto out_free;
1150e3adcf8fSFrançois Tigeot 	}
1151e3adcf8fSFrançois Tigeot 
1152a2fdbec6SFrançois Tigeot 	drm_modeset_lock_all(dev);
1153a2fdbec6SFrançois Tigeot 	mutex_lock(&dev->struct_mutex);
1154e3adcf8fSFrançois Tigeot 
115571f41f3eSFrançois Tigeot 	if (i915_gem_object_is_tiled(new_bo)) {
1156ba55f2f5SFrançois Tigeot 		DRM_DEBUG_KMS("buffer used for overlay image can not be tiled\n");
1157e3adcf8fSFrançois Tigeot 		ret = -EINVAL;
1158e3adcf8fSFrançois Tigeot 		goto out_unlock;
1159e3adcf8fSFrançois Tigeot 	}
1160e3adcf8fSFrançois Tigeot 
1161e3adcf8fSFrançois Tigeot 	ret = intel_overlay_recover_from_interrupt(overlay);
1162e3adcf8fSFrançois Tigeot 	if (ret != 0)
1163e3adcf8fSFrançois Tigeot 		goto out_unlock;
1164e3adcf8fSFrançois Tigeot 
1165e3adcf8fSFrançois Tigeot 	if (overlay->crtc != crtc) {
1166e3adcf8fSFrançois Tigeot 		ret = intel_overlay_switch_off(overlay);
1167e3adcf8fSFrançois Tigeot 		if (ret != 0)
1168e3adcf8fSFrançois Tigeot 			goto out_unlock;
1169e3adcf8fSFrançois Tigeot 
1170e3adcf8fSFrançois Tigeot 		ret = check_overlay_possible_on_crtc(overlay, crtc);
1171e3adcf8fSFrançois Tigeot 		if (ret != 0)
1172e3adcf8fSFrançois Tigeot 			goto out_unlock;
1173e3adcf8fSFrançois Tigeot 
1174e3adcf8fSFrançois Tigeot 		overlay->crtc = crtc;
1175e3adcf8fSFrançois Tigeot 		crtc->overlay = overlay;
1176e3adcf8fSFrançois Tigeot 
1177e3adcf8fSFrançois Tigeot 		/* line too wide, i.e. one-line-mode */
1178a85cb24fSFrançois Tigeot 		if (crtc->config->pipe_src_w > 1024 &&
1179a85cb24fSFrançois Tigeot 		    crtc->config->gmch_pfit.control & PFIT_ENABLE) {
118019c468b4SFrançois Tigeot 			overlay->pfit_active = true;
1181e3adcf8fSFrançois Tigeot 			update_pfit_vscale_ratio(overlay);
1182e3adcf8fSFrançois Tigeot 		} else
118319c468b4SFrançois Tigeot 			overlay->pfit_active = false;
1184e3adcf8fSFrançois Tigeot 	}
1185e3adcf8fSFrançois Tigeot 
1186e3adcf8fSFrançois Tigeot 	ret = check_overlay_dst(overlay, put_image_rec);
1187e3adcf8fSFrançois Tigeot 	if (ret != 0)
1188e3adcf8fSFrançois Tigeot 		goto out_unlock;
1189e3adcf8fSFrançois Tigeot 
1190e3adcf8fSFrançois Tigeot 	if (overlay->pfit_active) {
1191e3adcf8fSFrançois Tigeot 		params->dst_y = ((((u32)put_image_rec->dst_y) << 12) /
1192e3adcf8fSFrançois Tigeot 				 overlay->pfit_vscale_ratio);
1193e3adcf8fSFrançois Tigeot 		/* shifting right rounds downwards, so add 1 */
1194e3adcf8fSFrançois Tigeot 		params->dst_h = ((((u32)put_image_rec->dst_height) << 12) /
1195e3adcf8fSFrançois Tigeot 				 overlay->pfit_vscale_ratio) + 1;
1196e3adcf8fSFrançois Tigeot 	} else {
1197e3adcf8fSFrançois Tigeot 		params->dst_y = put_image_rec->dst_y;
1198e3adcf8fSFrançois Tigeot 		params->dst_h = put_image_rec->dst_height;
1199e3adcf8fSFrançois Tigeot 	}
1200e3adcf8fSFrançois Tigeot 	params->dst_x = put_image_rec->dst_x;
1201e3adcf8fSFrançois Tigeot 	params->dst_w = put_image_rec->dst_width;
1202e3adcf8fSFrançois Tigeot 
1203e3adcf8fSFrançois Tigeot 	params->src_w = put_image_rec->src_width;
1204e3adcf8fSFrançois Tigeot 	params->src_h = put_image_rec->src_height;
1205e3adcf8fSFrançois Tigeot 	params->src_scan_w = put_image_rec->src_scan_width;
1206e3adcf8fSFrançois Tigeot 	params->src_scan_h = put_image_rec->src_scan_height;
1207e3adcf8fSFrançois Tigeot 	if (params->src_scan_h > params->src_h ||
1208e3adcf8fSFrançois Tigeot 	    params->src_scan_w > params->src_w) {
1209e3adcf8fSFrançois Tigeot 		ret = -EINVAL;
1210e3adcf8fSFrançois Tigeot 		goto out_unlock;
1211e3adcf8fSFrançois Tigeot 	}
1212e3adcf8fSFrançois Tigeot 
12131487f786SFrançois Tigeot 	ret = check_overlay_src(dev_priv, put_image_rec, new_bo);
1214e3adcf8fSFrançois Tigeot 	if (ret != 0)
1215e3adcf8fSFrançois Tigeot 		goto out_unlock;
1216e3adcf8fSFrançois Tigeot 	params->format = put_image_rec->flags & ~I915_OVERLAY_FLAGS_MASK;
1217e3adcf8fSFrançois Tigeot 	params->stride_Y = put_image_rec->stride_Y;
1218e3adcf8fSFrançois Tigeot 	params->stride_UV = put_image_rec->stride_UV;
1219e3adcf8fSFrançois Tigeot 	params->offset_Y = put_image_rec->offset_Y;
1220e3adcf8fSFrançois Tigeot 	params->offset_U = put_image_rec->offset_U;
1221e3adcf8fSFrançois Tigeot 	params->offset_V = put_image_rec->offset_V;
1222e3adcf8fSFrançois Tigeot 
1223e3adcf8fSFrançois Tigeot 	/* Check scaling after src size to prevent a divide-by-zero. */
1224e3adcf8fSFrançois Tigeot 	ret = check_overlay_scaling(params);
1225e3adcf8fSFrançois Tigeot 	if (ret != 0)
1226e3adcf8fSFrançois Tigeot 		goto out_unlock;
1227e3adcf8fSFrançois Tigeot 
1228e3adcf8fSFrançois Tigeot 	ret = intel_overlay_do_put_image(overlay, new_bo, params);
1229e3adcf8fSFrançois Tigeot 	if (ret != 0)
1230e3adcf8fSFrançois Tigeot 		goto out_unlock;
1231e3adcf8fSFrançois Tigeot 
1232a2fdbec6SFrançois Tigeot 	mutex_unlock(&dev->struct_mutex);
1233a2fdbec6SFrançois Tigeot 	drm_modeset_unlock_all(dev);
1234a85cb24fSFrançois Tigeot 	i915_gem_object_put(new_bo);
1235e3adcf8fSFrançois Tigeot 
1236158486a6SFrançois Tigeot 	kfree(params);
1237e3adcf8fSFrançois Tigeot 
1238e3adcf8fSFrançois Tigeot 	return 0;
1239e3adcf8fSFrançois Tigeot 
1240e3adcf8fSFrançois Tigeot out_unlock:
1241a2fdbec6SFrançois Tigeot 	mutex_unlock(&dev->struct_mutex);
1242a2fdbec6SFrançois Tigeot 	drm_modeset_unlock_all(dev);
12434be47400SFrançois Tigeot 	i915_gem_object_put(new_bo);
1244e3adcf8fSFrançois Tigeot out_free:
1245158486a6SFrançois Tigeot 	kfree(params);
1246e3adcf8fSFrançois Tigeot 
1247e3adcf8fSFrançois Tigeot 	return ret;
1248e3adcf8fSFrançois Tigeot }
1249e3adcf8fSFrançois Tigeot 
update_reg_attrs(struct intel_overlay * overlay,struct overlay_registers __iomem * regs)1250e3adcf8fSFrançois Tigeot static void update_reg_attrs(struct intel_overlay *overlay,
1251e3440f96SFrançois Tigeot 			     struct overlay_registers __iomem *regs)
1252e3adcf8fSFrançois Tigeot {
1253e3440f96SFrançois Tigeot 	iowrite32((overlay->contrast << 18) | (overlay->brightness & 0xff),
1254e3440f96SFrançois Tigeot 		  &regs->OCLRC0);
1255e3440f96SFrançois Tigeot 	iowrite32(overlay->saturation, &regs->OCLRC1);
1256e3adcf8fSFrançois Tigeot }
1257e3adcf8fSFrançois Tigeot 
check_gamma_bounds(u32 gamma1,u32 gamma2)1258e3adcf8fSFrançois Tigeot static bool check_gamma_bounds(u32 gamma1, u32 gamma2)
1259e3adcf8fSFrançois Tigeot {
1260e3adcf8fSFrançois Tigeot 	int i;
1261e3adcf8fSFrançois Tigeot 
1262e3adcf8fSFrançois Tigeot 	if (gamma1 & 0xff000000 || gamma2 & 0xff000000)
1263e3adcf8fSFrançois Tigeot 		return false;
1264e3adcf8fSFrançois Tigeot 
1265e3adcf8fSFrançois Tigeot 	for (i = 0; i < 3; i++) {
1266e3adcf8fSFrançois Tigeot 		if (((gamma1 >> i*8) & 0xff) >= ((gamma2 >> i*8) & 0xff))
1267e3adcf8fSFrançois Tigeot 			return false;
1268e3adcf8fSFrançois Tigeot 	}
1269e3adcf8fSFrançois Tigeot 
1270e3adcf8fSFrançois Tigeot 	return true;
1271e3adcf8fSFrançois Tigeot }
1272e3adcf8fSFrançois Tigeot 
check_gamma5_errata(u32 gamma5)1273e3adcf8fSFrançois Tigeot static bool check_gamma5_errata(u32 gamma5)
1274e3adcf8fSFrançois Tigeot {
1275e3adcf8fSFrançois Tigeot 	int i;
1276e3adcf8fSFrançois Tigeot 
1277e3adcf8fSFrançois Tigeot 	for (i = 0; i < 3; i++) {
1278e3adcf8fSFrançois Tigeot 		if (((gamma5 >> i*8) & 0xff) == 0x80)
1279e3adcf8fSFrançois Tigeot 			return false;
1280e3adcf8fSFrançois Tigeot 	}
1281e3adcf8fSFrançois Tigeot 
1282e3adcf8fSFrançois Tigeot 	return true;
1283e3adcf8fSFrançois Tigeot }
1284e3adcf8fSFrançois Tigeot 
check_gamma(struct drm_intel_overlay_attrs * attrs)1285e3adcf8fSFrançois Tigeot static int check_gamma(struct drm_intel_overlay_attrs *attrs)
1286e3adcf8fSFrançois Tigeot {
1287e3adcf8fSFrançois Tigeot 	if (!check_gamma_bounds(0, attrs->gamma0) ||
1288e3adcf8fSFrançois Tigeot 	    !check_gamma_bounds(attrs->gamma0, attrs->gamma1) ||
1289e3adcf8fSFrançois Tigeot 	    !check_gamma_bounds(attrs->gamma1, attrs->gamma2) ||
1290e3adcf8fSFrançois Tigeot 	    !check_gamma_bounds(attrs->gamma2, attrs->gamma3) ||
1291e3adcf8fSFrançois Tigeot 	    !check_gamma_bounds(attrs->gamma3, attrs->gamma4) ||
1292e3adcf8fSFrançois Tigeot 	    !check_gamma_bounds(attrs->gamma4, attrs->gamma5) ||
1293e3adcf8fSFrançois Tigeot 	    !check_gamma_bounds(attrs->gamma5, 0x00ffffff))
1294e3adcf8fSFrançois Tigeot 		return -EINVAL;
1295e3adcf8fSFrançois Tigeot 
1296e3adcf8fSFrançois Tigeot 	if (!check_gamma5_errata(attrs->gamma5))
1297e3adcf8fSFrançois Tigeot 		return -EINVAL;
1298e3adcf8fSFrançois Tigeot 
1299e3adcf8fSFrançois Tigeot 	return 0;
1300e3adcf8fSFrançois Tigeot }
1301e3adcf8fSFrançois Tigeot 
intel_overlay_attrs_ioctl(struct drm_device * dev,void * data,struct drm_file * file_priv)13021487f786SFrançois Tigeot int intel_overlay_attrs_ioctl(struct drm_device *dev, void *data,
1303e3adcf8fSFrançois Tigeot 			      struct drm_file *file_priv)
1304e3adcf8fSFrançois Tigeot {
1305e3adcf8fSFrançois Tigeot 	struct drm_intel_overlay_attrs *attrs = data;
1306bf017597SFrançois Tigeot 	struct drm_i915_private *dev_priv = to_i915(dev);
1307e3adcf8fSFrançois Tigeot 	struct intel_overlay *overlay;
1308e3440f96SFrançois Tigeot 	struct overlay_registers __iomem *regs;
1309e3adcf8fSFrançois Tigeot 	int ret;
1310e3adcf8fSFrançois Tigeot 
1311e3adcf8fSFrançois Tigeot 	overlay = dev_priv->overlay;
1312e3adcf8fSFrançois Tigeot 	if (!overlay) {
1313e3adcf8fSFrançois Tigeot 		DRM_DEBUG("userspace bug: no overlay\n");
1314e3adcf8fSFrançois Tigeot 		return -ENODEV;
1315e3adcf8fSFrançois Tigeot 	}
1316e3adcf8fSFrançois Tigeot 
1317a2fdbec6SFrançois Tigeot 	drm_modeset_lock_all(dev);
1318a2fdbec6SFrançois Tigeot 	mutex_lock(&dev->struct_mutex);
1319e3adcf8fSFrançois Tigeot 
1320e3adcf8fSFrançois Tigeot 	ret = -EINVAL;
1321e3adcf8fSFrançois Tigeot 	if (!(attrs->flags & I915_OVERLAY_UPDATE_ATTRS)) {
1322e3adcf8fSFrançois Tigeot 		attrs->color_key  = overlay->color_key;
1323e3adcf8fSFrançois Tigeot 		attrs->brightness = overlay->brightness;
1324e3adcf8fSFrançois Tigeot 		attrs->contrast   = overlay->contrast;
1325e3adcf8fSFrançois Tigeot 		attrs->saturation = overlay->saturation;
1326e3adcf8fSFrançois Tigeot 
13271487f786SFrançois Tigeot 		if (!IS_GEN2(dev_priv)) {
1328e3adcf8fSFrançois Tigeot 			attrs->gamma0 = I915_READ(OGAMC0);
1329e3adcf8fSFrançois Tigeot 			attrs->gamma1 = I915_READ(OGAMC1);
1330e3adcf8fSFrançois Tigeot 			attrs->gamma2 = I915_READ(OGAMC2);
1331e3adcf8fSFrançois Tigeot 			attrs->gamma3 = I915_READ(OGAMC3);
1332e3adcf8fSFrançois Tigeot 			attrs->gamma4 = I915_READ(OGAMC4);
1333e3adcf8fSFrançois Tigeot 			attrs->gamma5 = I915_READ(OGAMC5);
1334e3adcf8fSFrançois Tigeot 		}
1335e3adcf8fSFrançois Tigeot 	} else {
1336e3adcf8fSFrançois Tigeot 		if (attrs->brightness < -128 || attrs->brightness > 127)
1337e3adcf8fSFrançois Tigeot 			goto out_unlock;
1338e3adcf8fSFrançois Tigeot 		if (attrs->contrast > 255)
1339e3adcf8fSFrançois Tigeot 			goto out_unlock;
1340e3adcf8fSFrançois Tigeot 		if (attrs->saturation > 1023)
1341e3adcf8fSFrançois Tigeot 			goto out_unlock;
1342e3adcf8fSFrançois Tigeot 
1343e3adcf8fSFrançois Tigeot 		overlay->color_key  = attrs->color_key;
1344e3adcf8fSFrançois Tigeot 		overlay->brightness = attrs->brightness;
1345e3adcf8fSFrançois Tigeot 		overlay->contrast   = attrs->contrast;
1346e3adcf8fSFrançois Tigeot 		overlay->saturation = attrs->saturation;
1347e3adcf8fSFrançois Tigeot 
1348e3adcf8fSFrançois Tigeot 		regs = intel_overlay_map_regs(overlay);
1349e3adcf8fSFrançois Tigeot 		if (!regs) {
1350e3adcf8fSFrançois Tigeot 			ret = -ENOMEM;
1351e3adcf8fSFrançois Tigeot 			goto out_unlock;
1352e3adcf8fSFrançois Tigeot 		}
1353e3adcf8fSFrançois Tigeot 
1354e3adcf8fSFrançois Tigeot 		update_reg_attrs(overlay, regs);
1355e3adcf8fSFrançois Tigeot 
1356e3adcf8fSFrançois Tigeot 		intel_overlay_unmap_regs(overlay, regs);
1357e3adcf8fSFrançois Tigeot 
1358e3adcf8fSFrançois Tigeot 		if (attrs->flags & I915_OVERLAY_UPDATE_GAMMA) {
13591487f786SFrançois Tigeot 			if (IS_GEN2(dev_priv))
1360e3adcf8fSFrançois Tigeot 				goto out_unlock;
1361e3adcf8fSFrançois Tigeot 
1362e3adcf8fSFrançois Tigeot 			if (overlay->active) {
1363e3adcf8fSFrançois Tigeot 				ret = -EBUSY;
1364e3adcf8fSFrançois Tigeot 				goto out_unlock;
1365e3adcf8fSFrançois Tigeot 			}
1366e3adcf8fSFrançois Tigeot 
1367e3adcf8fSFrançois Tigeot 			ret = check_gamma(attrs);
1368e3adcf8fSFrançois Tigeot 			if (ret)
1369e3adcf8fSFrançois Tigeot 				goto out_unlock;
1370e3adcf8fSFrançois Tigeot 
1371e3adcf8fSFrançois Tigeot 			I915_WRITE(OGAMC0, attrs->gamma0);
1372e3adcf8fSFrançois Tigeot 			I915_WRITE(OGAMC1, attrs->gamma1);
1373e3adcf8fSFrançois Tigeot 			I915_WRITE(OGAMC2, attrs->gamma2);
1374e3adcf8fSFrançois Tigeot 			I915_WRITE(OGAMC3, attrs->gamma3);
1375e3adcf8fSFrançois Tigeot 			I915_WRITE(OGAMC4, attrs->gamma4);
1376e3adcf8fSFrançois Tigeot 			I915_WRITE(OGAMC5, attrs->gamma5);
1377e3adcf8fSFrançois Tigeot 		}
1378e3adcf8fSFrançois Tigeot 	}
137919c468b4SFrançois Tigeot 	overlay->color_key_enabled = (attrs->flags & I915_OVERLAY_DISABLE_DEST_COLORKEY) == 0;
1380e3adcf8fSFrançois Tigeot 
1381e3adcf8fSFrançois Tigeot 	ret = 0;
1382e3adcf8fSFrançois Tigeot out_unlock:
1383a2fdbec6SFrançois Tigeot 	mutex_unlock(&dev->struct_mutex);
1384a2fdbec6SFrançois Tigeot 	drm_modeset_unlock_all(dev);
1385e3adcf8fSFrançois Tigeot 
1386e3adcf8fSFrançois Tigeot 	return ret;
1387e3adcf8fSFrançois Tigeot }
1388e3adcf8fSFrançois Tigeot 
intel_setup_overlay(struct drm_i915_private * dev_priv)13891487f786SFrançois Tigeot void intel_setup_overlay(struct drm_i915_private *dev_priv)
1390e3adcf8fSFrançois Tigeot {
1391e3adcf8fSFrançois Tigeot 	struct intel_overlay *overlay;
1392e3adcf8fSFrançois Tigeot 	struct drm_i915_gem_object *reg_bo;
1393e3440f96SFrançois Tigeot 	struct overlay_registers __iomem *regs;
13941e12ee3bSFrançois Tigeot 	struct i915_vma *vma = NULL;
1395e3adcf8fSFrançois Tigeot 	int ret;
1396e3adcf8fSFrançois Tigeot 
13971487f786SFrançois Tigeot 	if (!HAS_OVERLAY(dev_priv))
1398e3adcf8fSFrançois Tigeot 		return;
1399e3adcf8fSFrançois Tigeot 
14009edbd4a0SFrançois Tigeot 	overlay = kzalloc(sizeof(*overlay), GFP_KERNEL);
1401e3440f96SFrançois Tigeot 	if (!overlay)
1402e3440f96SFrançois Tigeot 		return;
1403e3440f96SFrançois Tigeot 
1404303bf270SFrançois Tigeot 	mutex_lock(&dev_priv->drm.struct_mutex);
1405e3440f96SFrançois Tigeot 	if (WARN_ON(dev_priv->overlay))
1406e3adcf8fSFrançois Tigeot 		goto out_free;
1407e3440f96SFrançois Tigeot 
14081487f786SFrançois Tigeot 	overlay->i915 = dev_priv;
1409e3adcf8fSFrançois Tigeot 
14109edbd4a0SFrançois Tigeot 	reg_bo = NULL;
14111487f786SFrançois Tigeot 	if (!OVERLAY_NEEDS_PHYSICAL(dev_priv))
1412a85cb24fSFrançois Tigeot 		reg_bo = i915_gem_object_create_stolen(dev_priv, PAGE_SIZE);
14139edbd4a0SFrançois Tigeot 	if (reg_bo == NULL)
1414a85cb24fSFrançois Tigeot 		reg_bo = i915_gem_object_create(dev_priv, PAGE_SIZE);
14151487f786SFrançois Tigeot 	if (IS_ERR(reg_bo))
1416e3adcf8fSFrançois Tigeot 		goto out_free;
1417e3adcf8fSFrançois Tigeot 	overlay->reg_bo = reg_bo;
1418e3adcf8fSFrançois Tigeot 
14191487f786SFrançois Tigeot 	if (OVERLAY_NEEDS_PHYSICAL(dev_priv)) {
1420ba55f2f5SFrançois Tigeot 		ret = i915_gem_object_attach_phys(reg_bo, PAGE_SIZE);
1421e3adcf8fSFrançois Tigeot 		if (ret) {
1422e3adcf8fSFrançois Tigeot 			DRM_ERROR("failed to attach phys overlay regs\n");
1423e3adcf8fSFrançois Tigeot 			goto out_free_bo;
1424e3adcf8fSFrançois Tigeot 		}
1425ba55f2f5SFrançois Tigeot 		overlay->flip_addr = reg_bo->phys_handle->busaddr;
1426e3adcf8fSFrançois Tigeot 	} else {
14271e12ee3bSFrançois Tigeot 		vma = i915_gem_object_ggtt_pin(reg_bo, NULL,
142871f41f3eSFrançois Tigeot 					       0, PAGE_SIZE, PIN_MAPPABLE);
14291e12ee3bSFrançois Tigeot 		if (IS_ERR(vma)) {
1430e3adcf8fSFrançois Tigeot 			DRM_ERROR("failed to pin overlay register bo\n");
14311e12ee3bSFrançois Tigeot 			ret = PTR_ERR(vma);
1432e3adcf8fSFrançois Tigeot 			goto out_free_bo;
1433e3adcf8fSFrançois Tigeot 		}
14341e12ee3bSFrançois Tigeot 		overlay->flip_addr = i915_ggtt_offset(vma);
1435e3adcf8fSFrançois Tigeot 
1436e3adcf8fSFrançois Tigeot 		ret = i915_gem_object_set_to_gtt_domain(reg_bo, true);
1437e3adcf8fSFrançois Tigeot 		if (ret) {
1438e3adcf8fSFrançois Tigeot 			DRM_ERROR("failed to move overlay register bo into the GTT\n");
1439e3adcf8fSFrançois Tigeot 			goto out_unpin_bo;
1440e3adcf8fSFrançois Tigeot 		}
1441e3adcf8fSFrançois Tigeot 	}
1442e3adcf8fSFrançois Tigeot 
1443e3adcf8fSFrançois Tigeot 	/* init all values */
1444e3adcf8fSFrançois Tigeot 	overlay->color_key = 0x0101fe;
144519c468b4SFrançois Tigeot 	overlay->color_key_enabled = true;
1446e3adcf8fSFrançois Tigeot 	overlay->brightness = -19;
1447e3adcf8fSFrançois Tigeot 	overlay->contrast = 75;
1448e3adcf8fSFrançois Tigeot 	overlay->saturation = 146;
1449e3adcf8fSFrançois Tigeot 
14504be47400SFrançois Tigeot 	init_request_active(&overlay->last_flip, NULL);
14514be47400SFrançois Tigeot 
1452e3adcf8fSFrançois Tigeot 	regs = intel_overlay_map_regs(overlay);
1453e3adcf8fSFrançois Tigeot 	if (!regs)
1454e3adcf8fSFrançois Tigeot 		goto out_unpin_bo;
1455e3adcf8fSFrançois Tigeot 
1456e3440f96SFrançois Tigeot 	memset_io(regs, 0, sizeof(struct overlay_registers));
1457e3adcf8fSFrançois Tigeot 	update_polyphase_filter(regs);
1458e3adcf8fSFrançois Tigeot 	update_reg_attrs(overlay, regs);
1459e3adcf8fSFrançois Tigeot 
1460e3adcf8fSFrançois Tigeot 	intel_overlay_unmap_regs(overlay, regs);
1461e3adcf8fSFrançois Tigeot 
1462e3adcf8fSFrançois Tigeot 	dev_priv->overlay = overlay;
1463303bf270SFrançois Tigeot 	mutex_unlock(&dev_priv->drm.struct_mutex);
1464e3440f96SFrançois Tigeot 	DRM_INFO("initialized overlay support\n");
1465e3adcf8fSFrançois Tigeot 	return;
1466e3adcf8fSFrançois Tigeot 
1467e3adcf8fSFrançois Tigeot out_unpin_bo:
14681e12ee3bSFrançois Tigeot 	if (vma)
14691e12ee3bSFrançois Tigeot 		i915_vma_unpin(vma);
1470e3adcf8fSFrançois Tigeot out_free_bo:
147187df8fc6SFrançois Tigeot 	i915_gem_object_put(reg_bo);
1472e3adcf8fSFrançois Tigeot out_free:
1473303bf270SFrançois Tigeot 	mutex_unlock(&dev_priv->drm.struct_mutex);
1474158486a6SFrançois Tigeot 	kfree(overlay);
1475e3adcf8fSFrançois Tigeot 	return;
1476e3adcf8fSFrançois Tigeot }
1477e3adcf8fSFrançois Tigeot 
intel_cleanup_overlay(struct drm_i915_private * dev_priv)14781487f786SFrançois Tigeot void intel_cleanup_overlay(struct drm_i915_private *dev_priv)
1479e3adcf8fSFrançois Tigeot {
1480e3adcf8fSFrançois Tigeot 	if (!dev_priv->overlay)
1481e3adcf8fSFrançois Tigeot 		return;
1482e3adcf8fSFrançois Tigeot 
1483e3adcf8fSFrançois Tigeot 	/* The bo's should be free'd by the generic code already.
1484e3adcf8fSFrançois Tigeot 	 * Furthermore modesetting teardown happens beforehand so the
1485e3adcf8fSFrançois Tigeot 	 * hardware should be off already */
148619c468b4SFrançois Tigeot 	WARN_ON(dev_priv->overlay->active);
1487e3adcf8fSFrançois Tigeot 
14884be47400SFrançois Tigeot 	i915_gem_object_put(dev_priv->overlay->reg_bo);
1489158486a6SFrançois Tigeot 	kfree(dev_priv->overlay);
1490e3adcf8fSFrançois Tigeot }
1491e3adcf8fSFrançois Tigeot 
14921e12ee3bSFrançois Tigeot #if IS_ENABLED(CONFIG_DRM_I915_CAPTURE_ERROR)
14931e12ee3bSFrançois Tigeot 
1494e3adcf8fSFrançois Tigeot struct intel_overlay_error_state {
1495e3adcf8fSFrançois Tigeot 	struct overlay_registers regs;
1496e3adcf8fSFrançois Tigeot 	unsigned long base;
1497e3adcf8fSFrançois Tigeot 	u32 dovsta;
1498e3adcf8fSFrançois Tigeot 	u32 isr;
1499e3adcf8fSFrançois Tigeot };
1500e3adcf8fSFrançois Tigeot 
1501e3440f96SFrançois Tigeot static struct overlay_registers __iomem *
intel_overlay_map_regs_atomic(struct intel_overlay * overlay)1502e3440f96SFrançois Tigeot intel_overlay_map_regs_atomic(struct intel_overlay *overlay)
1503e3440f96SFrançois Tigeot {
15041487f786SFrançois Tigeot 	struct drm_i915_private *dev_priv = overlay->i915;
1505e3440f96SFrançois Tigeot 	struct overlay_registers __iomem *regs;
1506e3440f96SFrançois Tigeot 
15071487f786SFrançois Tigeot 	if (OVERLAY_NEEDS_PHYSICAL(dev_priv))
1508e3440f96SFrançois Tigeot 		/* Cast to make sparse happy, but it's wc memory anyway, so
1509e3440f96SFrançois Tigeot 		 * equivalent to the wc io mapping on X86. */
1510e3440f96SFrançois Tigeot 		regs = (struct overlay_registers __iomem *)
15112c9916cdSFrançois Tigeot 			overlay->reg_bo->phys_handle->vaddr;
1512e3440f96SFrançois Tigeot 	else
15131e12ee3bSFrançois Tigeot 		regs = io_mapping_map_atomic_wc(&dev_priv->ggtt.mappable,
15141487f786SFrançois Tigeot 						overlay->flip_addr);
1515e3440f96SFrançois Tigeot 
1516e3440f96SFrançois Tigeot 	return regs;
1517e3440f96SFrançois Tigeot }
1518e3440f96SFrançois Tigeot 
intel_overlay_unmap_regs_atomic(struct intel_overlay * overlay,struct overlay_registers __iomem * regs)1519e3440f96SFrançois Tigeot static void intel_overlay_unmap_regs_atomic(struct intel_overlay *overlay,
1520e3440f96SFrançois Tigeot 					struct overlay_registers __iomem *regs)
1521e3440f96SFrançois Tigeot {
15221487f786SFrançois Tigeot 	if (!OVERLAY_NEEDS_PHYSICAL(overlay->i915))
1523e3440f96SFrançois Tigeot 		io_mapping_unmap_atomic(regs);
1524e3440f96SFrançois Tigeot }
1525e3440f96SFrançois Tigeot 
1526e3adcf8fSFrançois Tigeot struct intel_overlay_error_state *
intel_overlay_capture_error_state(struct drm_i915_private * dev_priv)15271487f786SFrançois Tigeot intel_overlay_capture_error_state(struct drm_i915_private *dev_priv)
1528e3adcf8fSFrançois Tigeot {
1529e3adcf8fSFrançois Tigeot 	struct intel_overlay *overlay = dev_priv->overlay;
1530e3adcf8fSFrançois Tigeot 	struct intel_overlay_error_state *error;
1531e3adcf8fSFrançois Tigeot 	struct overlay_registers __iomem *regs;
1532e3adcf8fSFrançois Tigeot 
1533e3adcf8fSFrançois Tigeot 	if (!overlay || !overlay->active)
1534e3adcf8fSFrançois Tigeot 		return NULL;
1535e3adcf8fSFrançois Tigeot 
1536bf017597SFrançois Tigeot 	error = kmalloc(sizeof(*error), M_DRM, GFP_ATOMIC);
1537e3adcf8fSFrançois Tigeot 	if (error == NULL)
1538e3adcf8fSFrançois Tigeot 		return NULL;
1539e3adcf8fSFrançois Tigeot 
1540e3adcf8fSFrançois Tigeot 	error->dovsta = I915_READ(DOVSTA);
1541e3adcf8fSFrançois Tigeot 	error->isr = I915_READ(ISR);
15421487f786SFrançois Tigeot 	error->base = overlay->flip_addr;
1543e3adcf8fSFrançois Tigeot 
1544e3440f96SFrançois Tigeot 	regs = intel_overlay_map_regs_atomic(overlay);
1545e3adcf8fSFrançois Tigeot 	if (!regs)
1546e3adcf8fSFrançois Tigeot 		goto err;
1547e3adcf8fSFrançois Tigeot 
1548e3440f96SFrançois Tigeot 	memcpy_fromio(&error->regs, regs, sizeof(struct overlay_registers));
1549e3440f96SFrançois Tigeot 	intel_overlay_unmap_regs_atomic(overlay, regs);
1550e3adcf8fSFrançois Tigeot 
1551e3440f96SFrançois Tigeot 	return error;
1552e3adcf8fSFrançois Tigeot 
1553e3adcf8fSFrançois Tigeot err:
1554158486a6SFrançois Tigeot 	kfree(error);
1555e3440f96SFrançois Tigeot 	return NULL;
1556e3adcf8fSFrançois Tigeot }
1557e3adcf8fSFrançois Tigeot 
1558e3adcf8fSFrançois Tigeot void
intel_overlay_print_error_state(struct drm_i915_error_state_buf * m,struct intel_overlay_error_state * error)15595d0b1887SFrançois Tigeot intel_overlay_print_error_state(struct drm_i915_error_state_buf *m,
15605d0b1887SFrançois Tigeot 				struct intel_overlay_error_state *error)
1561e3adcf8fSFrançois Tigeot {
15625d0b1887SFrançois Tigeot 	i915_error_printf(m, "Overlay, status: 0x%08x, interrupt: 0x%08x\n",
1563e3adcf8fSFrançois Tigeot 			  error->dovsta, error->isr);
15645d0b1887SFrançois Tigeot 	i915_error_printf(m, "  Register file at 0x%08lx:\n",
1565e3adcf8fSFrançois Tigeot 			  error->base);
1566e3adcf8fSFrançois Tigeot 
15675d0b1887SFrançois Tigeot #define P(x) i915_error_printf(m, "    " #x ":	0x%08x\n", error->regs.x)
1568e3adcf8fSFrançois Tigeot 	P(OBUF_0Y);
1569e3adcf8fSFrançois Tigeot 	P(OBUF_1Y);
1570e3adcf8fSFrançois Tigeot 	P(OBUF_0U);
1571e3adcf8fSFrançois Tigeot 	P(OBUF_0V);
1572e3adcf8fSFrançois Tigeot 	P(OBUF_1U);
1573e3adcf8fSFrançois Tigeot 	P(OBUF_1V);
1574e3adcf8fSFrançois Tigeot 	P(OSTRIDE);
1575e3adcf8fSFrançois Tigeot 	P(YRGB_VPH);
1576e3adcf8fSFrançois Tigeot 	P(UV_VPH);
1577e3adcf8fSFrançois Tigeot 	P(HORZ_PH);
1578e3adcf8fSFrançois Tigeot 	P(INIT_PHS);
1579e3adcf8fSFrançois Tigeot 	P(DWINPOS);
1580e3adcf8fSFrançois Tigeot 	P(DWINSZ);
1581e3adcf8fSFrançois Tigeot 	P(SWIDTH);
1582e3adcf8fSFrançois Tigeot 	P(SWIDTHSW);
1583e3adcf8fSFrançois Tigeot 	P(SHEIGHT);
1584e3adcf8fSFrançois Tigeot 	P(YRGBSCALE);
1585e3adcf8fSFrançois Tigeot 	P(UVSCALE);
1586e3adcf8fSFrançois Tigeot 	P(OCLRC0);
1587e3adcf8fSFrançois Tigeot 	P(OCLRC1);
1588e3adcf8fSFrançois Tigeot 	P(DCLRKV);
1589e3adcf8fSFrançois Tigeot 	P(DCLRKM);
1590e3adcf8fSFrançois Tigeot 	P(SCLRKVH);
1591e3adcf8fSFrançois Tigeot 	P(SCLRKVL);
1592e3adcf8fSFrançois Tigeot 	P(SCLRKEN);
1593e3adcf8fSFrançois Tigeot 	P(OCONFIG);
1594e3adcf8fSFrançois Tigeot 	P(OCMD);
1595e3adcf8fSFrançois Tigeot 	P(OSTART_0Y);
1596e3adcf8fSFrançois Tigeot 	P(OSTART_1Y);
1597e3adcf8fSFrançois Tigeot 	P(OSTART_0U);
1598e3adcf8fSFrançois Tigeot 	P(OSTART_0V);
1599e3adcf8fSFrançois Tigeot 	P(OSTART_1U);
1600e3adcf8fSFrançois Tigeot 	P(OSTART_1V);
1601e3adcf8fSFrançois Tigeot 	P(OTILEOFF_0Y);
1602e3adcf8fSFrançois Tigeot 	P(OTILEOFF_1Y);
1603e3adcf8fSFrançois Tigeot 	P(OTILEOFF_0U);
1604e3adcf8fSFrançois Tigeot 	P(OTILEOFF_0V);
1605e3adcf8fSFrançois Tigeot 	P(OTILEOFF_1U);
1606e3adcf8fSFrançois Tigeot 	P(OTILEOFF_1V);
1607e3adcf8fSFrançois Tigeot 	P(FASTHSCALE);
1608e3adcf8fSFrançois Tigeot 	P(UVSCALEV);
1609e3adcf8fSFrançois Tigeot #undef P
1610e3adcf8fSFrançois Tigeot }
16111e12ee3bSFrançois Tigeot 
16121e12ee3bSFrançois Tigeot #endif
1613