xref: /dflybsd-src/sys/dev/drm/i915/intel_sprite.c (revision 83b4b9b96d578e400ab5890b64f60d69d4429e75)
1e3adcf8fSFrançois Tigeot /*
2e3adcf8fSFrançois Tigeot  * Copyright © 2011 Intel Corporation
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  *   Jesse Barnes <jbarnes@virtuousgeek.org>
25e3adcf8fSFrançois Tigeot  *
26e3adcf8fSFrançois Tigeot  * New plane/sprite handling.
27e3adcf8fSFrançois Tigeot  *
28e3adcf8fSFrançois Tigeot  * The older chips had a separate interface for programming plane related
29e3adcf8fSFrançois Tigeot  * registers; newer ones are much simpler and we can use the new DRM plane
30e3adcf8fSFrançois Tigeot  * support.
31e3adcf8fSFrançois Tigeot  */
3218e26a6dSFrançois Tigeot #include <drm/drmP.h>
3318e26a6dSFrançois Tigeot #include <drm/drm_crtc.h>
34*83b4b9b9SFrançois Tigeot #include <drm/drm_fourcc.h>
355d0b1887SFrançois Tigeot #include <drm/drm_rect.h>
3619c468b4SFrançois Tigeot #include <drm/drm_atomic.h>
372c9916cdSFrançois Tigeot #include <drm/drm_plane_helper.h>
3818e26a6dSFrançois Tigeot #include "intel_drv.h"
395c6c6f23SFrançois Tigeot #include <drm/i915_drm.h>
40e3adcf8fSFrançois Tigeot #include "i915_drv.h"
41e3adcf8fSFrançois Tigeot 
422c9916cdSFrançois Tigeot static bool
432c9916cdSFrançois Tigeot format_is_yuv(uint32_t format)
442c9916cdSFrançois Tigeot {
452c9916cdSFrançois Tigeot 	switch (format) {
462c9916cdSFrançois Tigeot 	case DRM_FORMAT_YUYV:
472c9916cdSFrançois Tigeot 	case DRM_FORMAT_UYVY:
482c9916cdSFrançois Tigeot 	case DRM_FORMAT_VYUY:
492c9916cdSFrançois Tigeot 	case DRM_FORMAT_YVYU:
502c9916cdSFrançois Tigeot 		return true;
512c9916cdSFrançois Tigeot 	default:
522c9916cdSFrançois Tigeot 		return false;
532c9916cdSFrançois Tigeot 	}
542c9916cdSFrançois Tigeot }
552c9916cdSFrançois Tigeot 
56352ff8bdSFrançois Tigeot static int usecs_to_scanlines(const struct drm_display_mode *adjusted_mode,
57352ff8bdSFrançois Tigeot 			      int usecs)
58ba55f2f5SFrançois Tigeot {
59ba55f2f5SFrançois Tigeot 	/* paranoia */
60352ff8bdSFrançois Tigeot 	if (!adjusted_mode->crtc_htotal)
61ba55f2f5SFrançois Tigeot 		return 1;
62ba55f2f5SFrançois Tigeot 
63352ff8bdSFrançois Tigeot 	return DIV_ROUND_UP(usecs * adjusted_mode->crtc_clock,
64352ff8bdSFrançois Tigeot 			    1000 * adjusted_mode->crtc_htotal);
65ba55f2f5SFrançois Tigeot }
66ba55f2f5SFrançois Tigeot 
672c9916cdSFrançois Tigeot /**
682c9916cdSFrançois Tigeot  * intel_pipe_update_start() - start update of a set of display registers
692c9916cdSFrançois Tigeot  * @crtc: the crtc of which the registers are going to be updated
702c9916cdSFrançois Tigeot  * @start_vbl_count: vblank counter return pointer used for error checking
712c9916cdSFrançois Tigeot  *
722c9916cdSFrançois Tigeot  * Mark the start of an update to pipe registers that should be updated
732c9916cdSFrançois Tigeot  * atomically regarding vblank. If the next vblank will happens within
742c9916cdSFrançois Tigeot  * the next 100 us, this function waits until the vblank passes.
752c9916cdSFrançois Tigeot  *
762c9916cdSFrançois Tigeot  * After a successful call to this function, interrupts will be disabled
772c9916cdSFrançois Tigeot  * until a subsequent call to intel_pipe_update_end(). That is done to
782c9916cdSFrançois Tigeot  * avoid random delays. The value written to @start_vbl_count should be
792c9916cdSFrançois Tigeot  * supplied to intel_pipe_update_end() for error checking.
802c9916cdSFrançois Tigeot  */
81352ff8bdSFrançois Tigeot void intel_pipe_update_start(struct intel_crtc *crtc)
82ba55f2f5SFrançois Tigeot {
83ba55f2f5SFrançois Tigeot 	struct drm_device *dev = crtc->base.dev;
84352ff8bdSFrançois Tigeot 	const struct drm_display_mode *adjusted_mode = &crtc->config->base.adjusted_mode;
85ba55f2f5SFrançois Tigeot 	enum i915_pipe pipe = crtc->pipe;
86ba55f2f5SFrançois Tigeot 	long timeout = msecs_to_jiffies_timeout(1);
87ba55f2f5SFrançois Tigeot 	int scanline, min, max, vblank_start;
881b13d190SFrançois Tigeot 	wait_queue_head_t *wq = drm_crtc_vblank_waitqueue(&crtc->base);
89ba55f2f5SFrançois Tigeot 	DEFINE_WAIT(wait);
90ba55f2f5SFrançois Tigeot 
91352ff8bdSFrançois Tigeot 	vblank_start = adjusted_mode->crtc_vblank_start;
92352ff8bdSFrançois Tigeot 	if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
93ba55f2f5SFrançois Tigeot 		vblank_start = DIV_ROUND_UP(vblank_start, 2);
94ba55f2f5SFrançois Tigeot 
95ba55f2f5SFrançois Tigeot 	/* FIXME needs to be calibrated sensibly */
96352ff8bdSFrançois Tigeot 	min = vblank_start - usecs_to_scanlines(adjusted_mode, 100);
97ba55f2f5SFrançois Tigeot 	max = vblank_start - 1;
98ba55f2f5SFrançois Tigeot 
99a05eeebfSFrançois Tigeot 	local_irq_disable();
100a05eeebfSFrançois Tigeot 
101ba55f2f5SFrançois Tigeot 	if (min <= 0 || max <= 0)
102a05eeebfSFrançois Tigeot 		return;
103ba55f2f5SFrançois Tigeot 
104477eb7f9SFrançois Tigeot 	if (WARN_ON(drm_crtc_vblank_get(&crtc->base)))
105a05eeebfSFrançois Tigeot 		return;
106ba55f2f5SFrançois Tigeot 
107352ff8bdSFrançois Tigeot 	crtc->debug.min_vbl = min;
108352ff8bdSFrançois Tigeot 	crtc->debug.max_vbl = max;
109352ff8bdSFrançois Tigeot 	trace_i915_pipe_update_start(crtc);
110ba55f2f5SFrançois Tigeot 
111ba55f2f5SFrançois Tigeot 	for (;;) {
112ba55f2f5SFrançois Tigeot 		/*
113ba55f2f5SFrançois Tigeot 		 * prepare_to_wait() has a memory barrier, which guarantees
114ba55f2f5SFrançois Tigeot 		 * other CPUs can see the task state update by the time we
115ba55f2f5SFrançois Tigeot 		 * read the scanline.
116ba55f2f5SFrançois Tigeot 		 */
1171b13d190SFrançois Tigeot 		prepare_to_wait(wq, &wait, TASK_UNINTERRUPTIBLE);
118ba55f2f5SFrançois Tigeot 
119ba55f2f5SFrançois Tigeot 		scanline = intel_get_crtc_scanline(crtc);
120ba55f2f5SFrançois Tigeot 		if (scanline < min || scanline > max)
121ba55f2f5SFrançois Tigeot 			break;
122ba55f2f5SFrançois Tigeot 
123ba55f2f5SFrançois Tigeot 		if (timeout <= 0) {
124ba55f2f5SFrançois Tigeot 			DRM_ERROR("Potential atomic update failure on pipe %c\n",
125ba55f2f5SFrançois Tigeot 				  pipe_name(crtc->pipe));
126ba55f2f5SFrançois Tigeot 			break;
127ba55f2f5SFrançois Tigeot 		}
128ba55f2f5SFrançois Tigeot 
129ba55f2f5SFrançois Tigeot 		local_irq_enable();
130ba55f2f5SFrançois Tigeot 
131ba55f2f5SFrançois Tigeot 		timeout = schedule_timeout(timeout);
132ba55f2f5SFrançois Tigeot 
133ba55f2f5SFrançois Tigeot 		local_irq_disable();
134ba55f2f5SFrançois Tigeot 	}
135ba55f2f5SFrançois Tigeot 
1361b13d190SFrançois Tigeot 	finish_wait(wq, &wait);
137ba55f2f5SFrançois Tigeot 
138477eb7f9SFrançois Tigeot 	drm_crtc_vblank_put(&crtc->base);
139ba55f2f5SFrançois Tigeot 
140352ff8bdSFrançois Tigeot 	crtc->debug.scanline_start = scanline;
141352ff8bdSFrançois Tigeot 	crtc->debug.start_vbl_time = ktime_get();
142352ff8bdSFrançois Tigeot 	crtc->debug.start_vbl_count =
143352ff8bdSFrançois Tigeot 		dev->driver->get_vblank_counter(dev, pipe);
144ba55f2f5SFrançois Tigeot 
145352ff8bdSFrançois Tigeot 	trace_i915_pipe_update_vblank_evaded(crtc);
146ba55f2f5SFrançois Tigeot }
147ba55f2f5SFrançois Tigeot 
1482c9916cdSFrançois Tigeot /**
1492c9916cdSFrançois Tigeot  * intel_pipe_update_end() - end update of a set of display registers
1502c9916cdSFrançois Tigeot  * @crtc: the crtc of which the registers were updated
1512c9916cdSFrançois Tigeot  * @start_vbl_count: start vblank counter (used for error checking)
1522c9916cdSFrançois Tigeot  *
1532c9916cdSFrançois Tigeot  * Mark the end of an update started with intel_pipe_update_start(). This
1542c9916cdSFrançois Tigeot  * re-enables interrupts and verifies the update was actually completed
1552c9916cdSFrançois Tigeot  * before a vblank using the value of @start_vbl_count.
1562c9916cdSFrançois Tigeot  */
157352ff8bdSFrançois Tigeot void intel_pipe_update_end(struct intel_crtc *crtc)
158ba55f2f5SFrançois Tigeot {
159ba55f2f5SFrançois Tigeot 	struct drm_device *dev = crtc->base.dev;
160ba55f2f5SFrançois Tigeot 	enum i915_pipe pipe = crtc->pipe;
161352ff8bdSFrançois Tigeot 	int scanline_end = intel_get_crtc_scanline(crtc);
162ba55f2f5SFrançois Tigeot 	u32 end_vbl_count = dev->driver->get_vblank_counter(dev, pipe);
163352ff8bdSFrançois Tigeot 	ktime_t end_vbl_time = ktime_get();
164ba55f2f5SFrançois Tigeot 
165352ff8bdSFrançois Tigeot 	trace_i915_pipe_update_end(crtc, end_vbl_count, scanline_end);
166ba55f2f5SFrançois Tigeot 
167ba55f2f5SFrançois Tigeot 	local_irq_enable();
168ba55f2f5SFrançois Tigeot 
169352ff8bdSFrançois Tigeot 	if (crtc->debug.start_vbl_count &&
170352ff8bdSFrançois Tigeot 	    crtc->debug.start_vbl_count != end_vbl_count) {
171352ff8bdSFrançois Tigeot 		DRM_ERROR("Atomic update failure on pipe %c (start=%u end=%u) time %lld us, min %d, max %d, scanline start %d, end %d\n",
172352ff8bdSFrançois Tigeot 			  pipe_name(pipe), crtc->debug.start_vbl_count,
173352ff8bdSFrançois Tigeot 			  end_vbl_count,
174352ff8bdSFrançois Tigeot 			  ktime_us_delta(end_vbl_time, crtc->debug.start_vbl_time),
175352ff8bdSFrançois Tigeot 			  crtc->debug.min_vbl, crtc->debug.max_vbl,
176352ff8bdSFrançois Tigeot 			  crtc->debug.scanline_start, scanline_end);
177352ff8bdSFrançois Tigeot 	}
178ba55f2f5SFrançois Tigeot }
179ba55f2f5SFrançois Tigeot 
180e3adcf8fSFrançois Tigeot static void
181c0e85e96SFrançois Tigeot skl_update_plane(struct drm_plane *drm_plane,
182c0e85e96SFrançois Tigeot 		 const struct intel_crtc_state *crtc_state,
183c0e85e96SFrançois Tigeot 		 const struct intel_plane_state *plane_state)
1842c9916cdSFrançois Tigeot {
1852c9916cdSFrançois Tigeot 	struct drm_device *dev = drm_plane->dev;
1862c9916cdSFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
1872c9916cdSFrançois Tigeot 	struct intel_plane *intel_plane = to_intel_plane(drm_plane);
188c0e85e96SFrançois Tigeot 	struct drm_framebuffer *fb = plane_state->base.fb;
189477eb7f9SFrançois Tigeot 	struct drm_i915_gem_object *obj = intel_fb_obj(fb);
1902c9916cdSFrançois Tigeot 	const int pipe = intel_plane->pipe;
1912c9916cdSFrançois Tigeot 	const int plane = intel_plane->plane + 1;
19219c468b4SFrançois Tigeot 	u32 plane_ctl, stride_div, stride;
193c0e85e96SFrançois Tigeot 	const struct drm_intel_sprite_colorkey *key = &plane_state->ckey;
194aee94f86SFrançois Tigeot 	u32 surf_addr;
19519c468b4SFrançois Tigeot 	u32 tile_height, plane_offset, plane_size;
1968621f407SFrançois Tigeot 	unsigned int rotation = plane_state->base.rotation;
19719c468b4SFrançois Tigeot 	int x_offset, y_offset;
198c0e85e96SFrançois Tigeot 	int crtc_x = plane_state->dst.x1;
199c0e85e96SFrançois Tigeot 	int crtc_y = plane_state->dst.y1;
200c0e85e96SFrançois Tigeot 	uint32_t crtc_w = drm_rect_width(&plane_state->dst);
201c0e85e96SFrançois Tigeot 	uint32_t crtc_h = drm_rect_height(&plane_state->dst);
202c0e85e96SFrançois Tigeot 	uint32_t x = plane_state->src.x1 >> 16;
203c0e85e96SFrançois Tigeot 	uint32_t y = plane_state->src.y1 >> 16;
204c0e85e96SFrançois Tigeot 	uint32_t src_w = drm_rect_width(&plane_state->src) >> 16;
205c0e85e96SFrançois Tigeot 	uint32_t src_h = drm_rect_height(&plane_state->src) >> 16;
206c0e85e96SFrançois Tigeot 	const struct intel_scaler *scaler =
207c0e85e96SFrançois Tigeot 		&crtc_state->scaler_state.scalers[plane_state->scaler_id];
2082c9916cdSFrançois Tigeot 
209477eb7f9SFrançois Tigeot 	plane_ctl = PLANE_CTL_ENABLE |
210352ff8bdSFrançois Tigeot 		PLANE_CTL_PIPE_GAMMA_ENABLE |
211477eb7f9SFrançois Tigeot 		PLANE_CTL_PIPE_CSC_ENABLE;
2122c9916cdSFrançois Tigeot 
21319c468b4SFrançois Tigeot 	plane_ctl |= skl_plane_ctl_format(fb->pixel_format);
21419c468b4SFrançois Tigeot 	plane_ctl |= skl_plane_ctl_tiling(fb->modifier[0]);
2152c9916cdSFrançois Tigeot 
21619c468b4SFrançois Tigeot 	plane_ctl |= skl_plane_ctl_rotation(rotation);
2172c9916cdSFrançois Tigeot 
218c0e85e96SFrançois Tigeot 	stride_div = intel_fb_stride_alignment(dev_priv, fb->modifier[0],
219477eb7f9SFrançois Tigeot 					       fb->pixel_format);
220477eb7f9SFrançois Tigeot 
2212c9916cdSFrançois Tigeot 	/* Sizes are 0 based */
2222c9916cdSFrançois Tigeot 	src_w--;
2232c9916cdSFrançois Tigeot 	src_h--;
2242c9916cdSFrançois Tigeot 	crtc_w--;
2252c9916cdSFrançois Tigeot 	crtc_h--;
2262c9916cdSFrançois Tigeot 
227477eb7f9SFrançois Tigeot 	if (key->flags) {
228477eb7f9SFrançois Tigeot 		I915_WRITE(PLANE_KEYVAL(pipe, plane), key->min_value);
229477eb7f9SFrançois Tigeot 		I915_WRITE(PLANE_KEYMAX(pipe, plane), key->max_value);
230477eb7f9SFrançois Tigeot 		I915_WRITE(PLANE_KEYMSK(pipe, plane), key->channel_mask);
231477eb7f9SFrançois Tigeot 	}
232477eb7f9SFrançois Tigeot 
233477eb7f9SFrançois Tigeot 	if (key->flags & I915_SET_COLORKEY_DESTINATION)
234477eb7f9SFrançois Tigeot 		plane_ctl |= PLANE_CTL_KEY_ENABLE_DESTINATION;
235477eb7f9SFrançois Tigeot 	else if (key->flags & I915_SET_COLORKEY_SOURCE)
236477eb7f9SFrançois Tigeot 		plane_ctl |= PLANE_CTL_KEY_ENABLE_SOURCE;
237477eb7f9SFrançois Tigeot 
238352ff8bdSFrançois Tigeot 	surf_addr = intel_plane_obj_offset(intel_plane, obj, 0);
239477eb7f9SFrançois Tigeot 
24019c468b4SFrançois Tigeot 	if (intel_rotation_90_or_270(rotation)) {
241c0e85e96SFrançois Tigeot 		int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
242c0e85e96SFrançois Tigeot 
24319c468b4SFrançois Tigeot 		/* stride: Surface height in tiles */
244c0e85e96SFrançois Tigeot 		tile_height = intel_tile_height(dev_priv, fb->modifier[0], cpp);
24519c468b4SFrançois Tigeot 		stride = DIV_ROUND_UP(fb->height, tile_height);
24619c468b4SFrançois Tigeot 		plane_size = (src_w << 16) | src_h;
24719c468b4SFrançois Tigeot 		x_offset = stride * tile_height - y - (src_h + 1);
24819c468b4SFrançois Tigeot 		y_offset = x;
24919c468b4SFrançois Tigeot 	} else {
25019c468b4SFrançois Tigeot 		stride = fb->pitches[0] / stride_div;
25119c468b4SFrançois Tigeot 		plane_size = (src_h << 16) | src_w;
25219c468b4SFrançois Tigeot 		x_offset = x;
25319c468b4SFrançois Tigeot 		y_offset = y;
25419c468b4SFrançois Tigeot 	}
25519c468b4SFrançois Tigeot 	plane_offset = y_offset << 16 | x_offset;
25619c468b4SFrançois Tigeot 
25719c468b4SFrançois Tigeot 	I915_WRITE(PLANE_OFFSET(pipe, plane), plane_offset);
25819c468b4SFrançois Tigeot 	I915_WRITE(PLANE_STRIDE(pipe, plane), stride);
25919c468b4SFrançois Tigeot 	I915_WRITE(PLANE_SIZE(pipe, plane), plane_size);
26019c468b4SFrançois Tigeot 
26119c468b4SFrançois Tigeot 	/* program plane scaler */
262c0e85e96SFrançois Tigeot 	if (plane_state->scaler_id >= 0) {
26319c468b4SFrançois Tigeot 		uint32_t ps_ctrl = 0;
264c0e85e96SFrançois Tigeot 		int scaler_id = plane_state->scaler_id;
26519c468b4SFrançois Tigeot 
26619c468b4SFrançois Tigeot 		DRM_DEBUG_KMS("plane = %d PS_PLANE_SEL(plane) = 0x%x\n", plane,
26719c468b4SFrançois Tigeot 			PS_PLANE_SEL(plane));
268c0e85e96SFrançois Tigeot 		ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(plane) | scaler->mode;
26919c468b4SFrançois Tigeot 		I915_WRITE(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
27019c468b4SFrançois Tigeot 		I915_WRITE(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
27119c468b4SFrançois Tigeot 		I915_WRITE(SKL_PS_WIN_POS(pipe, scaler_id), (crtc_x << 16) | crtc_y);
27219c468b4SFrançois Tigeot 		I915_WRITE(SKL_PS_WIN_SZ(pipe, scaler_id),
27319c468b4SFrançois Tigeot 			((crtc_w + 1) << 16)|(crtc_h + 1));
27419c468b4SFrançois Tigeot 
27519c468b4SFrançois Tigeot 		I915_WRITE(PLANE_POS(pipe, plane), 0);
27619c468b4SFrançois Tigeot 	} else {
2772c9916cdSFrançois Tigeot 		I915_WRITE(PLANE_POS(pipe, plane), (crtc_y << 16) | crtc_x);
27819c468b4SFrançois Tigeot 	}
27919c468b4SFrançois Tigeot 
2802c9916cdSFrançois Tigeot 	I915_WRITE(PLANE_CTL(pipe, plane), plane_ctl);
281477eb7f9SFrançois Tigeot 	I915_WRITE(PLANE_SURF(pipe, plane), surf_addr);
2822c9916cdSFrançois Tigeot 	POSTING_READ(PLANE_SURF(pipe, plane));
2832c9916cdSFrançois Tigeot }
2842c9916cdSFrançois Tigeot 
2852c9916cdSFrançois Tigeot static void
286a05eeebfSFrançois Tigeot skl_disable_plane(struct drm_plane *dplane, struct drm_crtc *crtc)
2872c9916cdSFrançois Tigeot {
28819c468b4SFrançois Tigeot 	struct drm_device *dev = dplane->dev;
2892c9916cdSFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
29019c468b4SFrançois Tigeot 	struct intel_plane *intel_plane = to_intel_plane(dplane);
2912c9916cdSFrançois Tigeot 	const int pipe = intel_plane->pipe;
2922c9916cdSFrançois Tigeot 	const int plane = intel_plane->plane + 1;
2932c9916cdSFrançois Tigeot 
294477eb7f9SFrançois Tigeot 	I915_WRITE(PLANE_CTL(pipe, plane), 0);
2952c9916cdSFrançois Tigeot 
296477eb7f9SFrançois Tigeot 	I915_WRITE(PLANE_SURF(pipe, plane), 0);
297477eb7f9SFrançois Tigeot 	POSTING_READ(PLANE_SURF(pipe, plane));
2982c9916cdSFrançois Tigeot }
2992c9916cdSFrançois Tigeot 
3002c9916cdSFrançois Tigeot static void
3012c9916cdSFrançois Tigeot chv_update_csc(struct intel_plane *intel_plane, uint32_t format)
3022c9916cdSFrançois Tigeot {
3032c9916cdSFrançois Tigeot 	struct drm_i915_private *dev_priv = intel_plane->base.dev->dev_private;
3042c9916cdSFrançois Tigeot 	int plane = intel_plane->plane;
3052c9916cdSFrançois Tigeot 
3062c9916cdSFrançois Tigeot 	/* Seems RGB data bypasses the CSC always */
3072c9916cdSFrançois Tigeot 	if (!format_is_yuv(format))
3082c9916cdSFrançois Tigeot 		return;
3092c9916cdSFrançois Tigeot 
3102c9916cdSFrançois Tigeot 	/*
3112c9916cdSFrançois Tigeot 	 * BT.601 limited range YCbCr -> full range RGB
3122c9916cdSFrançois Tigeot 	 *
3132c9916cdSFrançois Tigeot 	 * |r|   | 6537 4769     0|   |cr  |
3142c9916cdSFrançois Tigeot 	 * |g| = |-3330 4769 -1605| x |y-64|
3152c9916cdSFrançois Tigeot 	 * |b|   |    0 4769  8263|   |cb  |
3162c9916cdSFrançois Tigeot 	 *
3172c9916cdSFrançois Tigeot 	 * Cb and Cr apparently come in as signed already, so no
3182c9916cdSFrançois Tigeot 	 * need for any offset. For Y we need to remove the offset.
3192c9916cdSFrançois Tigeot 	 */
3202c9916cdSFrançois Tigeot 	I915_WRITE(SPCSCYGOFF(plane), SPCSC_OOFF(0) | SPCSC_IOFF(-64));
3212c9916cdSFrançois Tigeot 	I915_WRITE(SPCSCCBOFF(plane), SPCSC_OOFF(0) | SPCSC_IOFF(0));
3222c9916cdSFrançois Tigeot 	I915_WRITE(SPCSCCROFF(plane), SPCSC_OOFF(0) | SPCSC_IOFF(0));
3232c9916cdSFrançois Tigeot 
3242c9916cdSFrançois Tigeot 	I915_WRITE(SPCSCC01(plane), SPCSC_C1(4769) | SPCSC_C0(6537));
3252c9916cdSFrançois Tigeot 	I915_WRITE(SPCSCC23(plane), SPCSC_C1(-3330) | SPCSC_C0(0));
3262c9916cdSFrançois Tigeot 	I915_WRITE(SPCSCC45(plane), SPCSC_C1(-1605) | SPCSC_C0(4769));
3272c9916cdSFrançois Tigeot 	I915_WRITE(SPCSCC67(plane), SPCSC_C1(4769) | SPCSC_C0(0));
3282c9916cdSFrançois Tigeot 	I915_WRITE(SPCSCC8(plane), SPCSC_C0(8263));
3292c9916cdSFrançois Tigeot 
3302c9916cdSFrançois Tigeot 	I915_WRITE(SPCSCYGICLAMP(plane), SPCSC_IMAX(940) | SPCSC_IMIN(64));
3312c9916cdSFrançois Tigeot 	I915_WRITE(SPCSCCBICLAMP(plane), SPCSC_IMAX(448) | SPCSC_IMIN(-448));
3322c9916cdSFrançois Tigeot 	I915_WRITE(SPCSCCRICLAMP(plane), SPCSC_IMAX(448) | SPCSC_IMIN(-448));
3332c9916cdSFrançois Tigeot 
3342c9916cdSFrançois Tigeot 	I915_WRITE(SPCSCYGOCLAMP(plane), SPCSC_OMAX(1023) | SPCSC_OMIN(0));
3352c9916cdSFrançois Tigeot 	I915_WRITE(SPCSCCBOCLAMP(plane), SPCSC_OMAX(1023) | SPCSC_OMIN(0));
3362c9916cdSFrançois Tigeot 	I915_WRITE(SPCSCCROCLAMP(plane), SPCSC_OMAX(1023) | SPCSC_OMIN(0));
3372c9916cdSFrançois Tigeot }
3382c9916cdSFrançois Tigeot 
3392c9916cdSFrançois Tigeot static void
340c0e85e96SFrançois Tigeot vlv_update_plane(struct drm_plane *dplane,
341c0e85e96SFrançois Tigeot 		 const struct intel_crtc_state *crtc_state,
342c0e85e96SFrançois Tigeot 		 const struct intel_plane_state *plane_state)
3438e26cdf6SFrançois Tigeot {
3448e26cdf6SFrançois Tigeot 	struct drm_device *dev = dplane->dev;
3458e26cdf6SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
3468e26cdf6SFrançois Tigeot 	struct intel_plane *intel_plane = to_intel_plane(dplane);
347c0e85e96SFrançois Tigeot 	struct drm_framebuffer *fb = plane_state->base.fb;
348477eb7f9SFrançois Tigeot 	struct drm_i915_gem_object *obj = intel_fb_obj(fb);
3498e26cdf6SFrançois Tigeot 	int pipe = intel_plane->pipe;
3508e26cdf6SFrançois Tigeot 	int plane = intel_plane->plane;
3518e26cdf6SFrançois Tigeot 	u32 sprctl;
352c0e85e96SFrançois Tigeot 	u32 sprsurf_offset, linear_offset;
3538621f407SFrançois Tigeot 	unsigned int rotation = dplane->state->rotation;
354c0e85e96SFrançois Tigeot 	int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
355c0e85e96SFrançois Tigeot 	const struct drm_intel_sprite_colorkey *key = &plane_state->ckey;
356c0e85e96SFrançois Tigeot 	int crtc_x = plane_state->dst.x1;
357c0e85e96SFrançois Tigeot 	int crtc_y = plane_state->dst.y1;
358c0e85e96SFrançois Tigeot 	uint32_t crtc_w = drm_rect_width(&plane_state->dst);
359c0e85e96SFrançois Tigeot 	uint32_t crtc_h = drm_rect_height(&plane_state->dst);
360c0e85e96SFrançois Tigeot 	uint32_t x = plane_state->src.x1 >> 16;
361c0e85e96SFrançois Tigeot 	uint32_t y = plane_state->src.y1 >> 16;
362c0e85e96SFrançois Tigeot 	uint32_t src_w = drm_rect_width(&plane_state->src) >> 16;
363c0e85e96SFrançois Tigeot 	uint32_t src_h = drm_rect_height(&plane_state->src) >> 16;
3648e26cdf6SFrançois Tigeot 
365477eb7f9SFrançois Tigeot 	sprctl = SP_ENABLE;
3668e26cdf6SFrançois Tigeot 
3678e26cdf6SFrançois Tigeot 	switch (fb->pixel_format) {
3688e26cdf6SFrançois Tigeot 	case DRM_FORMAT_YUYV:
3698e26cdf6SFrançois Tigeot 		sprctl |= SP_FORMAT_YUV422 | SP_YUV_ORDER_YUYV;
3708e26cdf6SFrançois Tigeot 		break;
3718e26cdf6SFrançois Tigeot 	case DRM_FORMAT_YVYU:
3728e26cdf6SFrançois Tigeot 		sprctl |= SP_FORMAT_YUV422 | SP_YUV_ORDER_YVYU;
3738e26cdf6SFrançois Tigeot 		break;
3748e26cdf6SFrançois Tigeot 	case DRM_FORMAT_UYVY:
3758e26cdf6SFrançois Tigeot 		sprctl |= SP_FORMAT_YUV422 | SP_YUV_ORDER_UYVY;
3768e26cdf6SFrançois Tigeot 		break;
3778e26cdf6SFrançois Tigeot 	case DRM_FORMAT_VYUY:
3788e26cdf6SFrançois Tigeot 		sprctl |= SP_FORMAT_YUV422 | SP_YUV_ORDER_VYUY;
3798e26cdf6SFrançois Tigeot 		break;
3808e26cdf6SFrançois Tigeot 	case DRM_FORMAT_RGB565:
3818e26cdf6SFrançois Tigeot 		sprctl |= SP_FORMAT_BGR565;
3828e26cdf6SFrançois Tigeot 		break;
3838e26cdf6SFrançois Tigeot 	case DRM_FORMAT_XRGB8888:
3848e26cdf6SFrançois Tigeot 		sprctl |= SP_FORMAT_BGRX8888;
3858e26cdf6SFrançois Tigeot 		break;
3868e26cdf6SFrançois Tigeot 	case DRM_FORMAT_ARGB8888:
3878e26cdf6SFrançois Tigeot 		sprctl |= SP_FORMAT_BGRA8888;
3888e26cdf6SFrançois Tigeot 		break;
3898e26cdf6SFrançois Tigeot 	case DRM_FORMAT_XBGR2101010:
3908e26cdf6SFrançois Tigeot 		sprctl |= SP_FORMAT_RGBX1010102;
3918e26cdf6SFrançois Tigeot 		break;
3928e26cdf6SFrançois Tigeot 	case DRM_FORMAT_ABGR2101010:
3938e26cdf6SFrançois Tigeot 		sprctl |= SP_FORMAT_RGBA1010102;
3948e26cdf6SFrançois Tigeot 		break;
3958e26cdf6SFrançois Tigeot 	case DRM_FORMAT_XBGR8888:
3968e26cdf6SFrançois Tigeot 		sprctl |= SP_FORMAT_RGBX8888;
3978e26cdf6SFrançois Tigeot 		break;
3988e26cdf6SFrançois Tigeot 	case DRM_FORMAT_ABGR8888:
3998e26cdf6SFrançois Tigeot 		sprctl |= SP_FORMAT_RGBA8888;
4008e26cdf6SFrançois Tigeot 		break;
4018e26cdf6SFrançois Tigeot 	default:
4028e26cdf6SFrançois Tigeot 		/*
4038e26cdf6SFrançois Tigeot 		 * If we get here one of the upper layers failed to filter
4048e26cdf6SFrançois Tigeot 		 * out the unsupported plane formats
4058e26cdf6SFrançois Tigeot 		 */
4068e26cdf6SFrançois Tigeot 		BUG();
4078e26cdf6SFrançois Tigeot 		break;
4088e26cdf6SFrançois Tigeot 	}
4098e26cdf6SFrançois Tigeot 
4109edbd4a0SFrançois Tigeot 	/*
4119edbd4a0SFrançois Tigeot 	 * Enable gamma to match primary/cursor plane behaviour.
4129edbd4a0SFrançois Tigeot 	 * FIXME should be user controllable via propertiesa.
4139edbd4a0SFrançois Tigeot 	 */
4149edbd4a0SFrançois Tigeot 	sprctl |= SP_GAMMA_ENABLE;
4159edbd4a0SFrançois Tigeot 
4168e26cdf6SFrançois Tigeot 	if (obj->tiling_mode != I915_TILING_NONE)
4178e26cdf6SFrançois Tigeot 		sprctl |= SP_TILED;
4188e26cdf6SFrançois Tigeot 
4198e26cdf6SFrançois Tigeot 	/* Sizes are 0 based */
4208e26cdf6SFrançois Tigeot 	src_w--;
4218e26cdf6SFrançois Tigeot 	src_h--;
4228e26cdf6SFrançois Tigeot 	crtc_w--;
4238e26cdf6SFrançois Tigeot 	crtc_h--;
4248e26cdf6SFrançois Tigeot 
425c0e85e96SFrançois Tigeot 	linear_offset = y * fb->pitches[0] + x * cpp;
4268621f407SFrançois Tigeot 	sprsurf_offset = intel_compute_tile_offset(&x, &y, fb, 0,
4278621f407SFrançois Tigeot 						   fb->pitches[0], rotation);
4288e26cdf6SFrançois Tigeot 	linear_offset -= sprsurf_offset;
4298e26cdf6SFrançois Tigeot 
4308621f407SFrançois Tigeot 	if (rotation == BIT(DRM_ROTATE_180)) {
4311b13d190SFrançois Tigeot 		sprctl |= SP_ROTATE_180;
4321b13d190SFrançois Tigeot 
4331b13d190SFrançois Tigeot 		x += src_w;
4341b13d190SFrançois Tigeot 		y += src_h;
435c0e85e96SFrançois Tigeot 		linear_offset += src_h * fb->pitches[0] + src_w * cpp;
4361b13d190SFrançois Tigeot 	}
4371b13d190SFrançois Tigeot 
438477eb7f9SFrançois Tigeot 	if (key->flags) {
439477eb7f9SFrançois Tigeot 		I915_WRITE(SPKEYMINVAL(pipe, plane), key->min_value);
440477eb7f9SFrançois Tigeot 		I915_WRITE(SPKEYMAXVAL(pipe, plane), key->max_value);
441477eb7f9SFrançois Tigeot 		I915_WRITE(SPKEYMSK(pipe, plane), key->channel_mask);
442477eb7f9SFrançois Tigeot 	}
443477eb7f9SFrançois Tigeot 
444477eb7f9SFrançois Tigeot 	if (key->flags & I915_SET_COLORKEY_SOURCE)
445477eb7f9SFrançois Tigeot 		sprctl |= SP_SOURCE_KEY;
446477eb7f9SFrançois Tigeot 
4472c9916cdSFrançois Tigeot 	if (IS_CHERRYVIEW(dev) && pipe == PIPE_B)
4482c9916cdSFrançois Tigeot 		chv_update_csc(intel_plane, fb->pixel_format);
4492c9916cdSFrançois Tigeot 
450ba55f2f5SFrançois Tigeot 	I915_WRITE(SPSTRIDE(pipe, plane), fb->pitches[0]);
451ba55f2f5SFrançois Tigeot 	I915_WRITE(SPPOS(pipe, plane), (crtc_y << 16) | crtc_x);
452ba55f2f5SFrançois Tigeot 
4538e26cdf6SFrançois Tigeot 	if (obj->tiling_mode != I915_TILING_NONE)
4548e26cdf6SFrançois Tigeot 		I915_WRITE(SPTILEOFF(pipe, plane), (y << 16) | x);
4558e26cdf6SFrançois Tigeot 	else
4568e26cdf6SFrançois Tigeot 		I915_WRITE(SPLINOFF(pipe, plane), linear_offset);
4578e26cdf6SFrançois Tigeot 
4582c9916cdSFrançois Tigeot 	I915_WRITE(SPCONSTALPHA(pipe, plane), 0);
4592c9916cdSFrançois Tigeot 
4608e26cdf6SFrançois Tigeot 	I915_WRITE(SPSIZE(pipe, plane), (crtc_h << 16) | crtc_w);
4618e26cdf6SFrançois Tigeot 	I915_WRITE(SPCNTR(pipe, plane), sprctl);
4629edbd4a0SFrançois Tigeot 	I915_WRITE(SPSURF(pipe, plane), i915_gem_obj_ggtt_offset(obj) +
4638e26cdf6SFrançois Tigeot 		   sprsurf_offset);
46419c468b4SFrançois Tigeot 	POSTING_READ(SPSURF(pipe, plane));
4658e26cdf6SFrançois Tigeot }
4668e26cdf6SFrançois Tigeot 
4678e26cdf6SFrançois Tigeot static void
468a05eeebfSFrançois Tigeot vlv_disable_plane(struct drm_plane *dplane, struct drm_crtc *crtc)
4698e26cdf6SFrançois Tigeot {
4708e26cdf6SFrançois Tigeot 	struct drm_device *dev = dplane->dev;
4718e26cdf6SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
4728e26cdf6SFrançois Tigeot 	struct intel_plane *intel_plane = to_intel_plane(dplane);
4738e26cdf6SFrançois Tigeot 	int pipe = intel_plane->pipe;
4748e26cdf6SFrançois Tigeot 	int plane = intel_plane->plane;
475ba55f2f5SFrançois Tigeot 
476477eb7f9SFrançois Tigeot 	I915_WRITE(SPCNTR(pipe, plane), 0);
477477eb7f9SFrançois Tigeot 
4789edbd4a0SFrançois Tigeot 	I915_WRITE(SPSURF(pipe, plane), 0);
47919c468b4SFrançois Tigeot 	POSTING_READ(SPSURF(pipe, plane));
4808e26cdf6SFrançois Tigeot }
4818e26cdf6SFrançois Tigeot 
4828e26cdf6SFrançois Tigeot static void
483c0e85e96SFrançois Tigeot ivb_update_plane(struct drm_plane *plane,
484c0e85e96SFrançois Tigeot 		 const struct intel_crtc_state *crtc_state,
485c0e85e96SFrançois Tigeot 		 const struct intel_plane_state *plane_state)
486e3adcf8fSFrançois Tigeot {
487e3adcf8fSFrançois Tigeot 	struct drm_device *dev = plane->dev;
488e3adcf8fSFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
489e3adcf8fSFrançois Tigeot 	struct intel_plane *intel_plane = to_intel_plane(plane);
490c0e85e96SFrançois Tigeot 	struct drm_framebuffer *fb = plane_state->base.fb;
491477eb7f9SFrançois Tigeot 	struct drm_i915_gem_object *obj = intel_fb_obj(fb);
492477eb7f9SFrançois Tigeot 	enum i915_pipe pipe = intel_plane->pipe;
493e3adcf8fSFrançois Tigeot 	u32 sprctl, sprscale = 0;
494c0e85e96SFrançois Tigeot 	u32 sprsurf_offset, linear_offset;
4958621f407SFrançois Tigeot 	unsigned int rotation = plane_state->base.rotation;
496c0e85e96SFrançois Tigeot 	int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
497c0e85e96SFrançois Tigeot 	const struct drm_intel_sprite_colorkey *key = &plane_state->ckey;
498c0e85e96SFrançois Tigeot 	int crtc_x = plane_state->dst.x1;
499c0e85e96SFrançois Tigeot 	int crtc_y = plane_state->dst.y1;
500c0e85e96SFrançois Tigeot 	uint32_t crtc_w = drm_rect_width(&plane_state->dst);
501c0e85e96SFrançois Tigeot 	uint32_t crtc_h = drm_rect_height(&plane_state->dst);
502c0e85e96SFrançois Tigeot 	uint32_t x = plane_state->src.x1 >> 16;
503c0e85e96SFrançois Tigeot 	uint32_t y = plane_state->src.y1 >> 16;
504c0e85e96SFrançois Tigeot 	uint32_t src_w = drm_rect_width(&plane_state->src) >> 16;
505c0e85e96SFrançois Tigeot 	uint32_t src_h = drm_rect_height(&plane_state->src) >> 16;
506e3adcf8fSFrançois Tigeot 
507477eb7f9SFrançois Tigeot 	sprctl = SPRITE_ENABLE;
508e3adcf8fSFrançois Tigeot 
509e3adcf8fSFrançois Tigeot 	switch (fb->pixel_format) {
510e3adcf8fSFrançois Tigeot 	case DRM_FORMAT_XBGR8888:
5112c84b0b6SFrançois Tigeot 		sprctl |= SPRITE_FORMAT_RGBX888 | SPRITE_RGB_ORDER_RGBX;
512e3adcf8fSFrançois Tigeot 		break;
513e3adcf8fSFrançois Tigeot 	case DRM_FORMAT_XRGB8888:
5142c84b0b6SFrançois Tigeot 		sprctl |= SPRITE_FORMAT_RGBX888;
515e3adcf8fSFrançois Tigeot 		break;
516e3adcf8fSFrançois Tigeot 	case DRM_FORMAT_YUYV:
517e3adcf8fSFrançois Tigeot 		sprctl |= SPRITE_FORMAT_YUV422 | SPRITE_YUV_ORDER_YUYV;
518e3adcf8fSFrançois Tigeot 		break;
519e3adcf8fSFrançois Tigeot 	case DRM_FORMAT_YVYU:
520e3adcf8fSFrançois Tigeot 		sprctl |= SPRITE_FORMAT_YUV422 | SPRITE_YUV_ORDER_YVYU;
521e3adcf8fSFrançois Tigeot 		break;
522e3adcf8fSFrançois Tigeot 	case DRM_FORMAT_UYVY:
523e3adcf8fSFrançois Tigeot 		sprctl |= SPRITE_FORMAT_YUV422 | SPRITE_YUV_ORDER_UYVY;
524e3adcf8fSFrançois Tigeot 		break;
525e3adcf8fSFrançois Tigeot 	case DRM_FORMAT_VYUY:
526e3adcf8fSFrançois Tigeot 		sprctl |= SPRITE_FORMAT_YUV422 | SPRITE_YUV_ORDER_VYUY;
527e3adcf8fSFrançois Tigeot 		break;
528e3adcf8fSFrançois Tigeot 	default:
5292c84b0b6SFrançois Tigeot 		BUG();
530e3adcf8fSFrançois Tigeot 	}
531e3adcf8fSFrançois Tigeot 
5329edbd4a0SFrançois Tigeot 	/*
5339edbd4a0SFrançois Tigeot 	 * Enable gamma to match primary/cursor plane behaviour.
5349edbd4a0SFrançois Tigeot 	 * FIXME should be user controllable via propertiesa.
5359edbd4a0SFrançois Tigeot 	 */
5369edbd4a0SFrançois Tigeot 	sprctl |= SPRITE_GAMMA_ENABLE;
5379edbd4a0SFrançois Tigeot 
538e3adcf8fSFrançois Tigeot 	if (obj->tiling_mode != I915_TILING_NONE)
539e3adcf8fSFrançois Tigeot 		sprctl |= SPRITE_TILED;
540e3adcf8fSFrançois Tigeot 
5419edbd4a0SFrançois Tigeot 	if (IS_HASWELL(dev) || IS_BROADWELL(dev))
5429edbd4a0SFrançois Tigeot 		sprctl &= ~SPRITE_TRICKLE_FEED_DISABLE;
5439edbd4a0SFrançois Tigeot 	else
544e3adcf8fSFrançois Tigeot 		sprctl |= SPRITE_TRICKLE_FEED_DISABLE;
5459edbd4a0SFrançois Tigeot 
5469edbd4a0SFrançois Tigeot 	if (IS_HASWELL(dev) || IS_BROADWELL(dev))
547a2fdbec6SFrançois Tigeot 		sprctl |= SPRITE_PIPE_CSC_ENABLE;
548a2fdbec6SFrançois Tigeot 
549e3adcf8fSFrançois Tigeot 	/* Sizes are 0 based */
550e3adcf8fSFrançois Tigeot 	src_w--;
551e3adcf8fSFrançois Tigeot 	src_h--;
552e3adcf8fSFrançois Tigeot 	crtc_w--;
553e3adcf8fSFrançois Tigeot 	crtc_h--;
554e3adcf8fSFrançois Tigeot 
5559edbd4a0SFrançois Tigeot 	if (crtc_w != src_w || crtc_h != src_h)
556e3adcf8fSFrançois Tigeot 		sprscale = SPRITE_SCALE_ENABLE | (src_w << 16) | src_h;
557e3adcf8fSFrançois Tigeot 
558c0e85e96SFrançois Tigeot 	linear_offset = y * fb->pitches[0] + x * cpp;
5598621f407SFrançois Tigeot 	sprsurf_offset = intel_compute_tile_offset(&x, &y, fb, 0,
5608621f407SFrançois Tigeot 						   fb->pitches[0], rotation);
5612c84b0b6SFrançois Tigeot 	linear_offset -= sprsurf_offset;
5622c84b0b6SFrançois Tigeot 
5638621f407SFrançois Tigeot 	if (rotation == BIT(DRM_ROTATE_180)) {
5641b13d190SFrançois Tigeot 		sprctl |= SPRITE_ROTATE_180;
5651b13d190SFrançois Tigeot 
5661b13d190SFrançois Tigeot 		/* HSW and BDW does this automagically in hardware */
5671b13d190SFrançois Tigeot 		if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
5681b13d190SFrançois Tigeot 			x += src_w;
5691b13d190SFrançois Tigeot 			y += src_h;
570c0e85e96SFrançois Tigeot 			linear_offset += src_h * fb->pitches[0] + src_w * cpp;
5711b13d190SFrançois Tigeot 		}
5721b13d190SFrançois Tigeot 	}
5731b13d190SFrançois Tigeot 
574477eb7f9SFrançois Tigeot 	if (key->flags) {
575477eb7f9SFrançois Tigeot 		I915_WRITE(SPRKEYVAL(pipe), key->min_value);
576477eb7f9SFrançois Tigeot 		I915_WRITE(SPRKEYMAX(pipe), key->max_value);
577477eb7f9SFrançois Tigeot 		I915_WRITE(SPRKEYMSK(pipe), key->channel_mask);
578477eb7f9SFrançois Tigeot 	}
579477eb7f9SFrançois Tigeot 
580477eb7f9SFrançois Tigeot 	if (key->flags & I915_SET_COLORKEY_DESTINATION)
581477eb7f9SFrançois Tigeot 		sprctl |= SPRITE_DEST_KEY;
582477eb7f9SFrançois Tigeot 	else if (key->flags & I915_SET_COLORKEY_SOURCE)
583477eb7f9SFrançois Tigeot 		sprctl |= SPRITE_SOURCE_KEY;
584477eb7f9SFrançois Tigeot 
585ba55f2f5SFrançois Tigeot 	I915_WRITE(SPRSTRIDE(pipe), fb->pitches[0]);
586ba55f2f5SFrançois Tigeot 	I915_WRITE(SPRPOS(pipe), (crtc_y << 16) | crtc_x);
587ba55f2f5SFrançois Tigeot 
5882c84b0b6SFrançois Tigeot 	/* HSW consolidates SPRTILEOFF and SPRLINOFF into a single SPROFFSET
5892c84b0b6SFrançois Tigeot 	 * register */
5909edbd4a0SFrançois Tigeot 	if (IS_HASWELL(dev) || IS_BROADWELL(dev))
5912c84b0b6SFrançois Tigeot 		I915_WRITE(SPROFFSET(pipe), (y << 16) | x);
5922c84b0b6SFrançois Tigeot 	else if (obj->tiling_mode != I915_TILING_NONE)
5932c84b0b6SFrançois Tigeot 		I915_WRITE(SPRTILEOFF(pipe), (y << 16) | x);
5942c84b0b6SFrançois Tigeot 	else
5952c84b0b6SFrançois Tigeot 		I915_WRITE(SPRLINOFF(pipe), linear_offset);
5962c84b0b6SFrançois Tigeot 
597e3adcf8fSFrançois Tigeot 	I915_WRITE(SPRSIZE(pipe), (crtc_h << 16) | crtc_w);
5982c84b0b6SFrançois Tigeot 	if (intel_plane->can_scale)
599e3adcf8fSFrançois Tigeot 		I915_WRITE(SPRSCALE(pipe), sprscale);
600e3adcf8fSFrançois Tigeot 	I915_WRITE(SPRCTL(pipe), sprctl);
6019edbd4a0SFrançois Tigeot 	I915_WRITE(SPRSURF(pipe),
6029edbd4a0SFrançois Tigeot 		   i915_gem_obj_ggtt_offset(obj) + sprsurf_offset);
60319c468b4SFrançois Tigeot 	POSTING_READ(SPRSURF(pipe));
604e3adcf8fSFrançois Tigeot }
605e3adcf8fSFrançois Tigeot 
606e3adcf8fSFrançois Tigeot static void
607a05eeebfSFrançois Tigeot ivb_disable_plane(struct drm_plane *plane, struct drm_crtc *crtc)
608e3adcf8fSFrançois Tigeot {
609e3adcf8fSFrançois Tigeot 	struct drm_device *dev = plane->dev;
610e3adcf8fSFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
611e3adcf8fSFrançois Tigeot 	struct intel_plane *intel_plane = to_intel_plane(plane);
612e3adcf8fSFrançois Tigeot 	int pipe = intel_plane->pipe;
613ba55f2f5SFrançois Tigeot 
614352ff8bdSFrançois Tigeot 	I915_WRITE(SPRCTL(pipe), 0);
615e3adcf8fSFrançois Tigeot 	/* Can't leave the scaler enabled... */
6162c84b0b6SFrançois Tigeot 	if (intel_plane->can_scale)
617e3adcf8fSFrançois Tigeot 		I915_WRITE(SPRSCALE(pipe), 0);
618ba55f2f5SFrançois Tigeot 
61919c468b4SFrançois Tigeot 	I915_WRITE(SPRSURF(pipe), 0);
62019c468b4SFrançois Tigeot 	POSTING_READ(SPRSURF(pipe));
621e3adcf8fSFrançois Tigeot }
622e3adcf8fSFrançois Tigeot 
623e3adcf8fSFrançois Tigeot static void
624c0e85e96SFrançois Tigeot ilk_update_plane(struct drm_plane *plane,
625c0e85e96SFrançois Tigeot 		 const struct intel_crtc_state *crtc_state,
626c0e85e96SFrançois Tigeot 		 const struct intel_plane_state *plane_state)
627e3adcf8fSFrançois Tigeot {
628e3adcf8fSFrançois Tigeot 	struct drm_device *dev = plane->dev;
629e3adcf8fSFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
630e3adcf8fSFrançois Tigeot 	struct intel_plane *intel_plane = to_intel_plane(plane);
631c0e85e96SFrançois Tigeot 	struct drm_framebuffer *fb = plane_state->base.fb;
632477eb7f9SFrançois Tigeot 	struct drm_i915_gem_object *obj = intel_fb_obj(fb);
6332c84b0b6SFrançois Tigeot 	int pipe = intel_plane->pipe;
6342c84b0b6SFrançois Tigeot 	u32 dvscntr, dvsscale;
635c0e85e96SFrançois Tigeot 	u32 dvssurf_offset, linear_offset;
6368621f407SFrançois Tigeot 	unsigned int rotation = plane_state->base.rotation;
637c0e85e96SFrançois Tigeot 	int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
638c0e85e96SFrançois Tigeot 	const struct drm_intel_sprite_colorkey *key = &plane_state->ckey;
639c0e85e96SFrançois Tigeot 	int crtc_x = plane_state->dst.x1;
640c0e85e96SFrançois Tigeot 	int crtc_y = plane_state->dst.y1;
641c0e85e96SFrançois Tigeot 	uint32_t crtc_w = drm_rect_width(&plane_state->dst);
642c0e85e96SFrançois Tigeot 	uint32_t crtc_h = drm_rect_height(&plane_state->dst);
643c0e85e96SFrançois Tigeot 	uint32_t x = plane_state->src.x1 >> 16;
644c0e85e96SFrançois Tigeot 	uint32_t y = plane_state->src.y1 >> 16;
645c0e85e96SFrançois Tigeot 	uint32_t src_w = drm_rect_width(&plane_state->src) >> 16;
646c0e85e96SFrançois Tigeot 	uint32_t src_h = drm_rect_height(&plane_state->src) >> 16;
647e3adcf8fSFrançois Tigeot 
648477eb7f9SFrançois Tigeot 	dvscntr = DVS_ENABLE;
649e3adcf8fSFrançois Tigeot 
650e3adcf8fSFrançois Tigeot 	switch (fb->pixel_format) {
651e3adcf8fSFrançois Tigeot 	case DRM_FORMAT_XBGR8888:
652e3adcf8fSFrançois Tigeot 		dvscntr |= DVS_FORMAT_RGBX888 | DVS_RGB_ORDER_XBGR;
653e3adcf8fSFrançois Tigeot 		break;
654e3adcf8fSFrançois Tigeot 	case DRM_FORMAT_XRGB8888:
655e3adcf8fSFrançois Tigeot 		dvscntr |= DVS_FORMAT_RGBX888;
656e3adcf8fSFrançois Tigeot 		break;
657e3adcf8fSFrançois Tigeot 	case DRM_FORMAT_YUYV:
658e3adcf8fSFrançois Tigeot 		dvscntr |= DVS_FORMAT_YUV422 | DVS_YUV_ORDER_YUYV;
659e3adcf8fSFrançois Tigeot 		break;
660e3adcf8fSFrançois Tigeot 	case DRM_FORMAT_YVYU:
661e3adcf8fSFrançois Tigeot 		dvscntr |= DVS_FORMAT_YUV422 | DVS_YUV_ORDER_YVYU;
662e3adcf8fSFrançois Tigeot 		break;
663e3adcf8fSFrançois Tigeot 	case DRM_FORMAT_UYVY:
664e3adcf8fSFrançois Tigeot 		dvscntr |= DVS_FORMAT_YUV422 | DVS_YUV_ORDER_UYVY;
665e3adcf8fSFrançois Tigeot 		break;
666e3adcf8fSFrançois Tigeot 	case DRM_FORMAT_VYUY:
667e3adcf8fSFrançois Tigeot 		dvscntr |= DVS_FORMAT_YUV422 | DVS_YUV_ORDER_VYUY;
668e3adcf8fSFrançois Tigeot 		break;
669e3adcf8fSFrançois Tigeot 	default:
6702c84b0b6SFrançois Tigeot 		BUG();
671e3adcf8fSFrançois Tigeot 	}
672e3adcf8fSFrançois Tigeot 
6739edbd4a0SFrançois Tigeot 	/*
6749edbd4a0SFrançois Tigeot 	 * Enable gamma to match primary/cursor plane behaviour.
6759edbd4a0SFrançois Tigeot 	 * FIXME should be user controllable via propertiesa.
6769edbd4a0SFrançois Tigeot 	 */
6779edbd4a0SFrançois Tigeot 	dvscntr |= DVS_GAMMA_ENABLE;
6789edbd4a0SFrançois Tigeot 
679e3adcf8fSFrançois Tigeot 	if (obj->tiling_mode != I915_TILING_NONE)
680e3adcf8fSFrançois Tigeot 		dvscntr |= DVS_TILED;
681e3adcf8fSFrançois Tigeot 
6822c84b0b6SFrançois Tigeot 	if (IS_GEN6(dev))
6832c84b0b6SFrançois Tigeot 		dvscntr |= DVS_TRICKLE_FEED_DISABLE; /* must disable */
684e3adcf8fSFrançois Tigeot 
685e3adcf8fSFrançois Tigeot 	/* Sizes are 0 based */
686e3adcf8fSFrançois Tigeot 	src_w--;
687e3adcf8fSFrançois Tigeot 	src_h--;
688e3adcf8fSFrançois Tigeot 	crtc_w--;
689e3adcf8fSFrançois Tigeot 	crtc_h--;
690e3adcf8fSFrançois Tigeot 
6912c84b0b6SFrançois Tigeot 	dvsscale = 0;
6929edbd4a0SFrançois Tigeot 	if (crtc_w != src_w || crtc_h != src_h)
693e3adcf8fSFrançois Tigeot 		dvsscale = DVS_SCALE_ENABLE | (src_w << 16) | src_h;
694e3adcf8fSFrançois Tigeot 
695c0e85e96SFrançois Tigeot 	linear_offset = y * fb->pitches[0] + x * cpp;
6968621f407SFrançois Tigeot 	dvssurf_offset = intel_compute_tile_offset(&x, &y, fb, 0,
6978621f407SFrançois Tigeot 						   fb->pitches[0], rotation);
6982c84b0b6SFrançois Tigeot 	linear_offset -= dvssurf_offset;
6992c84b0b6SFrançois Tigeot 
7008621f407SFrançois Tigeot 	if (rotation == BIT(DRM_ROTATE_180)) {
7011b13d190SFrançois Tigeot 		dvscntr |= DVS_ROTATE_180;
7021b13d190SFrançois Tigeot 
7031b13d190SFrançois Tigeot 		x += src_w;
7041b13d190SFrançois Tigeot 		y += src_h;
705c0e85e96SFrançois Tigeot 		linear_offset += src_h * fb->pitches[0] + src_w * cpp;
7061b13d190SFrançois Tigeot 	}
7071b13d190SFrançois Tigeot 
708477eb7f9SFrançois Tigeot 	if (key->flags) {
709477eb7f9SFrançois Tigeot 		I915_WRITE(DVSKEYVAL(pipe), key->min_value);
710477eb7f9SFrançois Tigeot 		I915_WRITE(DVSKEYMAX(pipe), key->max_value);
711477eb7f9SFrançois Tigeot 		I915_WRITE(DVSKEYMSK(pipe), key->channel_mask);
712477eb7f9SFrançois Tigeot 	}
713477eb7f9SFrançois Tigeot 
714477eb7f9SFrançois Tigeot 	if (key->flags & I915_SET_COLORKEY_DESTINATION)
715477eb7f9SFrançois Tigeot 		dvscntr |= DVS_DEST_KEY;
716477eb7f9SFrançois Tigeot 	else if (key->flags & I915_SET_COLORKEY_SOURCE)
717477eb7f9SFrançois Tigeot 		dvscntr |= DVS_SOURCE_KEY;
718477eb7f9SFrançois Tigeot 
719ba55f2f5SFrançois Tigeot 	I915_WRITE(DVSSTRIDE(pipe), fb->pitches[0]);
720ba55f2f5SFrançois Tigeot 	I915_WRITE(DVSPOS(pipe), (crtc_y << 16) | crtc_x);
721ba55f2f5SFrançois Tigeot 
7222c84b0b6SFrançois Tigeot 	if (obj->tiling_mode != I915_TILING_NONE)
7232c84b0b6SFrançois Tigeot 		I915_WRITE(DVSTILEOFF(pipe), (y << 16) | x);
7242c84b0b6SFrançois Tigeot 	else
7252c84b0b6SFrançois Tigeot 		I915_WRITE(DVSLINOFF(pipe), linear_offset);
7262c84b0b6SFrançois Tigeot 
727e3adcf8fSFrançois Tigeot 	I915_WRITE(DVSSIZE(pipe), (crtc_h << 16) | crtc_w);
728e3adcf8fSFrançois Tigeot 	I915_WRITE(DVSSCALE(pipe), dvsscale);
729e3adcf8fSFrançois Tigeot 	I915_WRITE(DVSCNTR(pipe), dvscntr);
7309edbd4a0SFrançois Tigeot 	I915_WRITE(DVSSURF(pipe),
7319edbd4a0SFrançois Tigeot 		   i915_gem_obj_ggtt_offset(obj) + dvssurf_offset);
73219c468b4SFrançois Tigeot 	POSTING_READ(DVSSURF(pipe));
733e3adcf8fSFrançois Tigeot }
734e3adcf8fSFrançois Tigeot 
735e3adcf8fSFrançois Tigeot static void
736a05eeebfSFrançois Tigeot ilk_disable_plane(struct drm_plane *plane, struct drm_crtc *crtc)
737e3adcf8fSFrançois Tigeot {
738e3adcf8fSFrançois Tigeot 	struct drm_device *dev = plane->dev;
739e3adcf8fSFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
740e3adcf8fSFrançois Tigeot 	struct intel_plane *intel_plane = to_intel_plane(plane);
741e3adcf8fSFrançois Tigeot 	int pipe = intel_plane->pipe;
742ba55f2f5SFrançois Tigeot 
743477eb7f9SFrançois Tigeot 	I915_WRITE(DVSCNTR(pipe), 0);
744e3adcf8fSFrançois Tigeot 	/* Disable the scaler */
745e3adcf8fSFrançois Tigeot 	I915_WRITE(DVSSCALE(pipe), 0);
746477eb7f9SFrançois Tigeot 
7479edbd4a0SFrançois Tigeot 	I915_WRITE(DVSSURF(pipe), 0);
74819c468b4SFrançois Tigeot 	POSTING_READ(DVSSURF(pipe));
7499edbd4a0SFrançois Tigeot }
7509edbd4a0SFrançois Tigeot 
751e3adcf8fSFrançois Tigeot static int
7522c9916cdSFrançois Tigeot intel_check_sprite_plane(struct drm_plane *plane,
753a05eeebfSFrançois Tigeot 			 struct intel_crtc_state *crtc_state,
7542c9916cdSFrançois Tigeot 			 struct intel_plane_state *state)
755e3adcf8fSFrançois Tigeot {
75619c468b4SFrançois Tigeot 	struct drm_device *dev = plane->dev;
757a05eeebfSFrançois Tigeot 	struct drm_crtc *crtc = state->base.crtc;
758a05eeebfSFrançois Tigeot 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
759e3adcf8fSFrançois Tigeot 	struct intel_plane *intel_plane = to_intel_plane(plane);
7602c9916cdSFrançois Tigeot 	struct drm_framebuffer *fb = state->base.fb;
7619edbd4a0SFrançois Tigeot 	int crtc_x, crtc_y;
7629edbd4a0SFrançois Tigeot 	unsigned int crtc_w, crtc_h;
7639edbd4a0SFrançois Tigeot 	uint32_t src_x, src_y, src_w, src_h;
7642c9916cdSFrançois Tigeot 	struct drm_rect *src = &state->src;
7652c9916cdSFrançois Tigeot 	struct drm_rect *dst = &state->dst;
7662c9916cdSFrançois Tigeot 	const struct drm_rect *clip = &state->clip;
7672c9916cdSFrançois Tigeot 	int hscale, vscale;
7682c9916cdSFrançois Tigeot 	int max_scale, min_scale;
76919c468b4SFrançois Tigeot 	bool can_scale;
7702c9916cdSFrançois Tigeot 
7712c9916cdSFrançois Tigeot 	if (!fb) {
7722c9916cdSFrançois Tigeot 		state->visible = false;
773a05eeebfSFrançois Tigeot 		return 0;
7742c9916cdSFrançois Tigeot 	}
775e3adcf8fSFrançois Tigeot 
776e3adcf8fSFrançois Tigeot 	/* Don't modify another pipe's plane */
7775d0b1887SFrançois Tigeot 	if (intel_plane->pipe != intel_crtc->pipe) {
7785d0b1887SFrançois Tigeot 		DRM_DEBUG_KMS("Wrong plane <-> crtc mapping\n");
779e3adcf8fSFrançois Tigeot 		return -EINVAL;
7805d0b1887SFrançois Tigeot 	}
7815d0b1887SFrançois Tigeot 
7825d0b1887SFrançois Tigeot 	/* FIXME check all gen limits */
7835d0b1887SFrançois Tigeot 	if (fb->width < 3 || fb->height < 3 || fb->pitches[0] > 16384) {
7845d0b1887SFrançois Tigeot 		DRM_DEBUG_KMS("Unsuitable framebuffer for plane\n");
7855d0b1887SFrançois Tigeot 		return -EINVAL;
7865d0b1887SFrançois Tigeot 	}
787e3adcf8fSFrançois Tigeot 
78819c468b4SFrançois Tigeot 	/* setup can_scale, min_scale, max_scale */
78919c468b4SFrançois Tigeot 	if (INTEL_INFO(dev)->gen >= 9) {
79019c468b4SFrançois Tigeot 		/* use scaler when colorkey is not required */
791a05eeebfSFrançois Tigeot 		if (state->ckey.flags == I915_SET_COLORKEY_NONE) {
79219c468b4SFrançois Tigeot 			can_scale = 1;
79319c468b4SFrançois Tigeot 			min_scale = 1;
79419c468b4SFrançois Tigeot 			max_scale = skl_max_scale(intel_crtc, crtc_state);
79519c468b4SFrançois Tigeot 		} else {
79619c468b4SFrançois Tigeot 			can_scale = 0;
79719c468b4SFrançois Tigeot 			min_scale = DRM_PLANE_HELPER_NO_SCALING;
79819c468b4SFrançois Tigeot 			max_scale = DRM_PLANE_HELPER_NO_SCALING;
79919c468b4SFrançois Tigeot 		}
80019c468b4SFrançois Tigeot 	} else {
80119c468b4SFrançois Tigeot 		can_scale = intel_plane->can_scale;
80219c468b4SFrançois Tigeot 		max_scale = intel_plane->max_downscale << 16;
80319c468b4SFrançois Tigeot 		min_scale = intel_plane->can_scale ? 1 : (1 << 16);
80419c468b4SFrançois Tigeot 	}
80519c468b4SFrançois Tigeot 
806e3adcf8fSFrançois Tigeot 	/*
8075d0b1887SFrançois Tigeot 	 * FIXME the following code does a bunch of fuzzy adjustments to the
8085d0b1887SFrançois Tigeot 	 * coordinates and sizes. We probably need some way to decide whether
8095d0b1887SFrançois Tigeot 	 * more strict checking should be done instead.
810e3adcf8fSFrançois Tigeot 	 */
8112c9916cdSFrançois Tigeot 	drm_rect_rotate(src, fb->width << 16, fb->height << 16,
8122c9916cdSFrançois Tigeot 			state->base.rotation);
8131b13d190SFrançois Tigeot 
8142c9916cdSFrançois Tigeot 	hscale = drm_rect_calc_hscale_relaxed(src, dst, min_scale, max_scale);
8155d0b1887SFrançois Tigeot 	BUG_ON(hscale < 0);
816e3adcf8fSFrançois Tigeot 
8172c9916cdSFrançois Tigeot 	vscale = drm_rect_calc_vscale_relaxed(src, dst, min_scale, max_scale);
8185d0b1887SFrançois Tigeot 	BUG_ON(vscale < 0);
8195d0b1887SFrançois Tigeot 
8202c9916cdSFrançois Tigeot 	state->visible = drm_rect_clip_scaled(src, dst, clip, hscale, vscale);
8215d0b1887SFrançois Tigeot 
8222c9916cdSFrançois Tigeot 	crtc_x = dst->x1;
8232c9916cdSFrançois Tigeot 	crtc_y = dst->y1;
8242c9916cdSFrançois Tigeot 	crtc_w = drm_rect_width(dst);
8252c9916cdSFrançois Tigeot 	crtc_h = drm_rect_height(dst);
8265d0b1887SFrançois Tigeot 
8272c9916cdSFrançois Tigeot 	if (state->visible) {
8285d0b1887SFrançois Tigeot 		/* check again in case clipping clamped the results */
8292c9916cdSFrançois Tigeot 		hscale = drm_rect_calc_hscale(src, dst, min_scale, max_scale);
8305d0b1887SFrançois Tigeot 		if (hscale < 0) {
8315d0b1887SFrançois Tigeot 			DRM_DEBUG_KMS("Horizontal scaling factor out of limits\n");
832aee94f86SFrançois Tigeot 			drm_rect_debug_print("src: ", src, true);
833aee94f86SFrançois Tigeot 			drm_rect_debug_print("dst: ", dst, false);
8345d0b1887SFrançois Tigeot 
8355d0b1887SFrançois Tigeot 			return hscale;
8365d0b1887SFrançois Tigeot 		}
8375d0b1887SFrançois Tigeot 
8382c9916cdSFrançois Tigeot 		vscale = drm_rect_calc_vscale(src, dst, min_scale, max_scale);
8395d0b1887SFrançois Tigeot 		if (vscale < 0) {
8405d0b1887SFrançois Tigeot 			DRM_DEBUG_KMS("Vertical scaling factor out of limits\n");
841aee94f86SFrançois Tigeot 			drm_rect_debug_print("src: ", src, true);
842aee94f86SFrançois Tigeot 			drm_rect_debug_print("dst: ", dst, false);
8435d0b1887SFrançois Tigeot 
8445d0b1887SFrançois Tigeot 			return vscale;
8455d0b1887SFrançois Tigeot 		}
8465d0b1887SFrançois Tigeot 
8475d0b1887SFrançois Tigeot 		/* Make the source viewport size an exact multiple of the scaling factors. */
8482c9916cdSFrançois Tigeot 		drm_rect_adjust_size(src,
8492c9916cdSFrançois Tigeot 				     drm_rect_width(dst) * hscale - drm_rect_width(src),
8502c9916cdSFrançois Tigeot 				     drm_rect_height(dst) * vscale - drm_rect_height(src));
8515d0b1887SFrançois Tigeot 
8522c9916cdSFrançois Tigeot 		drm_rect_rotate_inv(src, fb->width << 16, fb->height << 16,
8532c9916cdSFrançois Tigeot 				    state->base.rotation);
8541b13d190SFrançois Tigeot 
8555d0b1887SFrançois Tigeot 		/* sanity check to make sure the src viewport wasn't enlarged */
8562c9916cdSFrançois Tigeot 		WARN_ON(src->x1 < (int) state->base.src_x ||
8572c9916cdSFrançois Tigeot 			src->y1 < (int) state->base.src_y ||
8582c9916cdSFrançois Tigeot 			src->x2 > (int) state->base.src_x + state->base.src_w ||
8592c9916cdSFrançois Tigeot 			src->y2 > (int) state->base.src_y + state->base.src_h);
860e3adcf8fSFrançois Tigeot 
861e3adcf8fSFrançois Tigeot 		/*
8625d0b1887SFrançois Tigeot 		 * Hardware doesn't handle subpixel coordinates.
8635d0b1887SFrançois Tigeot 		 * Adjust to (macro)pixel boundary, but be careful not to
8645d0b1887SFrançois Tigeot 		 * increase the source viewport size, because that could
8655d0b1887SFrançois Tigeot 		 * push the downscaling factor out of bounds.
8662c84b0b6SFrançois Tigeot 		 */
8672c9916cdSFrançois Tigeot 		src_x = src->x1 >> 16;
8682c9916cdSFrançois Tigeot 		src_w = drm_rect_width(src) >> 16;
8692c9916cdSFrançois Tigeot 		src_y = src->y1 >> 16;
8702c9916cdSFrançois Tigeot 		src_h = drm_rect_height(src) >> 16;
8715d0b1887SFrançois Tigeot 
8725d0b1887SFrançois Tigeot 		if (format_is_yuv(fb->pixel_format)) {
8735d0b1887SFrançois Tigeot 			src_x &= ~1;
8745d0b1887SFrançois Tigeot 			src_w &= ~1;
8752c84b0b6SFrançois Tigeot 
8762c84b0b6SFrançois Tigeot 			/*
8775d0b1887SFrançois Tigeot 			 * Must keep src and dst the
8785d0b1887SFrançois Tigeot 			 * same if we can't scale.
879e3adcf8fSFrançois Tigeot 			 */
88019c468b4SFrançois Tigeot 			if (!can_scale)
8815d0b1887SFrançois Tigeot 				crtc_w &= ~1;
8825d0b1887SFrançois Tigeot 
8835d0b1887SFrançois Tigeot 			if (crtc_w == 0)
8842c9916cdSFrançois Tigeot 				state->visible = false;
8855d0b1887SFrançois Tigeot 		}
8865d0b1887SFrançois Tigeot 	}
8875d0b1887SFrançois Tigeot 
8885d0b1887SFrançois Tigeot 	/* Check size restrictions when scaling */
8892c9916cdSFrançois Tigeot 	if (state->visible && (src_w != crtc_w || src_h != crtc_h)) {
8905d0b1887SFrançois Tigeot 		unsigned int width_bytes;
891c0e85e96SFrançois Tigeot 		int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
8925d0b1887SFrançois Tigeot 
89319c468b4SFrançois Tigeot 		WARN_ON(!can_scale);
8945d0b1887SFrançois Tigeot 
8955d0b1887SFrançois Tigeot 		/* FIXME interlacing min height is 6 */
8965d0b1887SFrançois Tigeot 
8975d0b1887SFrançois Tigeot 		if (crtc_w < 3 || crtc_h < 3)
8982c9916cdSFrançois Tigeot 			state->visible = false;
8995d0b1887SFrançois Tigeot 
9005d0b1887SFrançois Tigeot 		if (src_w < 3 || src_h < 3)
9012c9916cdSFrançois Tigeot 			state->visible = false;
9025d0b1887SFrançois Tigeot 
903c0e85e96SFrançois Tigeot 		width_bytes = ((src_x * cpp) & 63) + src_w * cpp;
9045d0b1887SFrançois Tigeot 
90519c468b4SFrançois Tigeot 		if (INTEL_INFO(dev)->gen < 9 && (src_w > 2048 || src_h > 2048 ||
90619c468b4SFrançois Tigeot 		    width_bytes > 4096 || fb->pitches[0] > 4096)) {
9075d0b1887SFrançois Tigeot 			DRM_DEBUG_KMS("Source dimensions exceed hardware limits\n");
908e3adcf8fSFrançois Tigeot 			return -EINVAL;
9095d0b1887SFrançois Tigeot 		}
9105d0b1887SFrançois Tigeot 	}
9115d0b1887SFrançois Tigeot 
9122c9916cdSFrançois Tigeot 	if (state->visible) {
91319c468b4SFrançois Tigeot 		src->x1 = src_x << 16;
91419c468b4SFrançois Tigeot 		src->x2 = (src_x + src_w) << 16;
91519c468b4SFrançois Tigeot 		src->y1 = src_y << 16;
91619c468b4SFrançois Tigeot 		src->y2 = (src_y + src_h) << 16;
9172c9916cdSFrançois Tigeot 	}
918e3adcf8fSFrançois Tigeot 
9192c9916cdSFrançois Tigeot 	dst->x1 = crtc_x;
9202c9916cdSFrançois Tigeot 	dst->x2 = crtc_x + crtc_w;
9212c9916cdSFrançois Tigeot 	dst->y1 = crtc_y;
9222c9916cdSFrançois Tigeot 	dst->y2 = crtc_y + crtc_h;
9232c9916cdSFrançois Tigeot 
9242c9916cdSFrançois Tigeot 	return 0;
9252c9916cdSFrançois Tigeot }
9262c9916cdSFrançois Tigeot 
927e3adcf8fSFrançois Tigeot int intel_sprite_set_colorkey(struct drm_device *dev, void *data,
928e3adcf8fSFrançois Tigeot 			      struct drm_file *file_priv)
929e3adcf8fSFrançois Tigeot {
930e3adcf8fSFrançois Tigeot 	struct drm_intel_sprite_colorkey *set = data;
931e3adcf8fSFrançois Tigeot 	struct drm_plane *plane;
932a05eeebfSFrançois Tigeot 	struct drm_plane_state *plane_state;
933a05eeebfSFrançois Tigeot 	struct drm_atomic_state *state;
934a05eeebfSFrançois Tigeot 	struct drm_modeset_acquire_ctx ctx;
935e3adcf8fSFrançois Tigeot 	int ret = 0;
936e3adcf8fSFrançois Tigeot 
937e3adcf8fSFrançois Tigeot 	/* Make sure we don't try to enable both src & dest simultaneously */
938e3adcf8fSFrançois Tigeot 	if ((set->flags & (I915_SET_COLORKEY_DESTINATION | I915_SET_COLORKEY_SOURCE)) == (I915_SET_COLORKEY_DESTINATION | I915_SET_COLORKEY_SOURCE))
939e3adcf8fSFrançois Tigeot 		return -EINVAL;
940e3adcf8fSFrançois Tigeot 
941aee94f86SFrançois Tigeot 	if ((IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) &&
942477eb7f9SFrançois Tigeot 	    set->flags & I915_SET_COLORKEY_DESTINATION)
943477eb7f9SFrançois Tigeot 		return -EINVAL;
944477eb7f9SFrançois Tigeot 
94524edb884SFrançois Tigeot 	plane = drm_plane_find(dev, set->plane_id);
946a05eeebfSFrançois Tigeot 	if (!plane || plane->type != DRM_PLANE_TYPE_OVERLAY)
947a05eeebfSFrançois Tigeot 		return -ENOENT;
948a05eeebfSFrançois Tigeot 
949a05eeebfSFrançois Tigeot 	drm_modeset_acquire_init(&ctx, 0);
950a05eeebfSFrançois Tigeot 
951a05eeebfSFrançois Tigeot 	state = drm_atomic_state_alloc(plane->dev);
952a05eeebfSFrançois Tigeot 	if (!state) {
953a05eeebfSFrançois Tigeot 		ret = -ENOMEM;
954a05eeebfSFrançois Tigeot 		goto out;
955a05eeebfSFrançois Tigeot 	}
956a05eeebfSFrançois Tigeot 	state->acquire_ctx = &ctx;
957a05eeebfSFrançois Tigeot 
958a05eeebfSFrançois Tigeot 	while (1) {
959a05eeebfSFrançois Tigeot 		plane_state = drm_atomic_get_plane_state(state, plane);
960a05eeebfSFrançois Tigeot 		ret = PTR_ERR_OR_ZERO(plane_state);
961a05eeebfSFrançois Tigeot 		if (!ret) {
962a05eeebfSFrançois Tigeot 			to_intel_plane_state(plane_state)->ckey = *set;
963a05eeebfSFrançois Tigeot 			ret = drm_atomic_commit(state);
964e3adcf8fSFrançois Tigeot 		}
965e3adcf8fSFrançois Tigeot 
966a05eeebfSFrançois Tigeot 		if (ret != -EDEADLK)
967a05eeebfSFrançois Tigeot 			break;
96819c468b4SFrançois Tigeot 
969a05eeebfSFrançois Tigeot 		drm_atomic_state_clear(state);
970a05eeebfSFrançois Tigeot 		drm_modeset_backoff(&ctx);
97119c468b4SFrançois Tigeot 	}
97219c468b4SFrançois Tigeot 
973a05eeebfSFrançois Tigeot 	if (ret)
974a05eeebfSFrançois Tigeot 		drm_atomic_state_free(state);
975e3adcf8fSFrançois Tigeot 
976a05eeebfSFrançois Tigeot out:
977a05eeebfSFrançois Tigeot 	drm_modeset_drop_locks(&ctx);
978a05eeebfSFrançois Tigeot 	drm_modeset_acquire_fini(&ctx);
979e3adcf8fSFrançois Tigeot 	return ret;
980e3adcf8fSFrançois Tigeot }
981e3adcf8fSFrançois Tigeot 
98219c468b4SFrançois Tigeot static const uint32_t ilk_plane_formats[] = {
9832c84b0b6SFrançois Tigeot 	DRM_FORMAT_XRGB8888,
9842c84b0b6SFrançois Tigeot 	DRM_FORMAT_YUYV,
9852c84b0b6SFrançois Tigeot 	DRM_FORMAT_YVYU,
9862c84b0b6SFrançois Tigeot 	DRM_FORMAT_UYVY,
9872c84b0b6SFrançois Tigeot 	DRM_FORMAT_VYUY,
9882c84b0b6SFrançois Tigeot };
9892c84b0b6SFrançois Tigeot 
99019c468b4SFrançois Tigeot static const uint32_t snb_plane_formats[] = {
991e3adcf8fSFrançois Tigeot 	DRM_FORMAT_XBGR8888,
992e3adcf8fSFrançois Tigeot 	DRM_FORMAT_XRGB8888,
993e3adcf8fSFrançois Tigeot 	DRM_FORMAT_YUYV,
994e3adcf8fSFrançois Tigeot 	DRM_FORMAT_YVYU,
995e3adcf8fSFrançois Tigeot 	DRM_FORMAT_UYVY,
996e3adcf8fSFrançois Tigeot 	DRM_FORMAT_VYUY,
997e3adcf8fSFrançois Tigeot };
998e3adcf8fSFrançois Tigeot 
99919c468b4SFrançois Tigeot static const uint32_t vlv_plane_formats[] = {
10008e26cdf6SFrançois Tigeot 	DRM_FORMAT_RGB565,
10018e26cdf6SFrançois Tigeot 	DRM_FORMAT_ABGR8888,
10028e26cdf6SFrançois Tigeot 	DRM_FORMAT_ARGB8888,
10038e26cdf6SFrançois Tigeot 	DRM_FORMAT_XBGR8888,
10048e26cdf6SFrançois Tigeot 	DRM_FORMAT_XRGB8888,
10058e26cdf6SFrançois Tigeot 	DRM_FORMAT_XBGR2101010,
10068e26cdf6SFrançois Tigeot 	DRM_FORMAT_ABGR2101010,
10078e26cdf6SFrançois Tigeot 	DRM_FORMAT_YUYV,
10088e26cdf6SFrançois Tigeot 	DRM_FORMAT_YVYU,
10098e26cdf6SFrançois Tigeot 	DRM_FORMAT_UYVY,
10108e26cdf6SFrançois Tigeot 	DRM_FORMAT_VYUY,
10118e26cdf6SFrançois Tigeot };
10128e26cdf6SFrançois Tigeot 
10132c9916cdSFrançois Tigeot static uint32_t skl_plane_formats[] = {
10142c9916cdSFrançois Tigeot 	DRM_FORMAT_RGB565,
10152c9916cdSFrançois Tigeot 	DRM_FORMAT_ABGR8888,
10162c9916cdSFrançois Tigeot 	DRM_FORMAT_ARGB8888,
10172c9916cdSFrançois Tigeot 	DRM_FORMAT_XBGR8888,
10182c9916cdSFrançois Tigeot 	DRM_FORMAT_XRGB8888,
10192c9916cdSFrançois Tigeot 	DRM_FORMAT_YUYV,
10202c9916cdSFrançois Tigeot 	DRM_FORMAT_YVYU,
10212c9916cdSFrançois Tigeot 	DRM_FORMAT_UYVY,
10222c9916cdSFrançois Tigeot 	DRM_FORMAT_VYUY,
10232c9916cdSFrançois Tigeot };
10242c9916cdSFrançois Tigeot 
1025e3adcf8fSFrançois Tigeot int
10268e26cdf6SFrançois Tigeot intel_plane_init(struct drm_device *dev, enum i915_pipe pipe, int plane)
1027e3adcf8fSFrançois Tigeot {
10288621f407SFrançois Tigeot 	struct intel_plane *intel_plane = NULL;
10298621f407SFrançois Tigeot 	struct intel_plane_state *state = NULL;
1030e3adcf8fSFrançois Tigeot 	unsigned long possible_crtcs;
10312c84b0b6SFrançois Tigeot 	const uint32_t *plane_formats;
10322c84b0b6SFrançois Tigeot 	int num_plane_formats;
1033e3adcf8fSFrançois Tigeot 	int ret;
1034e3adcf8fSFrançois Tigeot 
10352c84b0b6SFrançois Tigeot 	if (INTEL_INFO(dev)->gen < 5)
1036e3adcf8fSFrançois Tigeot 		return -ENODEV;
1037e3adcf8fSFrançois Tigeot 
10389edbd4a0SFrançois Tigeot 	intel_plane = kzalloc(sizeof(*intel_plane), GFP_KERNEL);
10398621f407SFrançois Tigeot 	if (!intel_plane) {
10408621f407SFrançois Tigeot 		ret = -ENOMEM;
10418621f407SFrançois Tigeot 		goto fail;
10428621f407SFrançois Tigeot 	}
10432c84b0b6SFrançois Tigeot 
10442c9916cdSFrançois Tigeot 	state = intel_create_plane_state(&intel_plane->base);
10452c9916cdSFrançois Tigeot 	if (!state) {
10468621f407SFrançois Tigeot 		ret = -ENOMEM;
10478621f407SFrançois Tigeot 		goto fail;
10482c9916cdSFrançois Tigeot 	}
10492c9916cdSFrançois Tigeot 	intel_plane->base.state = &state->base;
10502c9916cdSFrançois Tigeot 
10512c84b0b6SFrançois Tigeot 	switch (INTEL_INFO(dev)->gen) {
10522c84b0b6SFrançois Tigeot 	case 5:
10532c84b0b6SFrançois Tigeot 	case 6:
10542c84b0b6SFrançois Tigeot 		intel_plane->can_scale = true;
10552c84b0b6SFrançois Tigeot 		intel_plane->max_downscale = 16;
10562c84b0b6SFrançois Tigeot 		intel_plane->update_plane = ilk_update_plane;
10572c84b0b6SFrançois Tigeot 		intel_plane->disable_plane = ilk_disable_plane;
1058e3adcf8fSFrançois Tigeot 
1059e3adcf8fSFrançois Tigeot 		if (IS_GEN6(dev)) {
10602c84b0b6SFrançois Tigeot 			plane_formats = snb_plane_formats;
10612c84b0b6SFrançois Tigeot 			num_plane_formats = ARRAY_SIZE(snb_plane_formats);
10622c84b0b6SFrançois Tigeot 		} else {
10632c84b0b6SFrançois Tigeot 			plane_formats = ilk_plane_formats;
10642c84b0b6SFrançois Tigeot 			num_plane_formats = ARRAY_SIZE(ilk_plane_formats);
10652c84b0b6SFrançois Tigeot 		}
10662c84b0b6SFrançois Tigeot 		break;
10672c84b0b6SFrançois Tigeot 
10682c84b0b6SFrançois Tigeot 	case 7:
10699edbd4a0SFrançois Tigeot 	case 8:
10705d0b1887SFrançois Tigeot 		if (IS_IVYBRIDGE(dev)) {
10712c84b0b6SFrançois Tigeot 			intel_plane->can_scale = true;
10725d0b1887SFrançois Tigeot 			intel_plane->max_downscale = 2;
10735d0b1887SFrançois Tigeot 		} else {
10745d0b1887SFrançois Tigeot 			intel_plane->can_scale = false;
10755d0b1887SFrançois Tigeot 			intel_plane->max_downscale = 1;
10765d0b1887SFrançois Tigeot 		}
10778e26cdf6SFrançois Tigeot 
1078aee94f86SFrançois Tigeot 		if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
10798e26cdf6SFrançois Tigeot 			intel_plane->update_plane = vlv_update_plane;
10808e26cdf6SFrançois Tigeot 			intel_plane->disable_plane = vlv_disable_plane;
10818e26cdf6SFrançois Tigeot 
10828e26cdf6SFrançois Tigeot 			plane_formats = vlv_plane_formats;
10838e26cdf6SFrançois Tigeot 			num_plane_formats = ARRAY_SIZE(vlv_plane_formats);
10848e26cdf6SFrançois Tigeot 		} else {
1085e3adcf8fSFrançois Tigeot 			intel_plane->update_plane = ivb_update_plane;
1086e3adcf8fSFrançois Tigeot 			intel_plane->disable_plane = ivb_disable_plane;
10872c84b0b6SFrançois Tigeot 
10882c84b0b6SFrançois Tigeot 			plane_formats = snb_plane_formats;
10892c84b0b6SFrançois Tigeot 			num_plane_formats = ARRAY_SIZE(snb_plane_formats);
10908e26cdf6SFrançois Tigeot 		}
10912c84b0b6SFrançois Tigeot 		break;
10922c9916cdSFrançois Tigeot 	case 9:
109319c468b4SFrançois Tigeot 		intel_plane->can_scale = true;
10942c9916cdSFrançois Tigeot 		intel_plane->update_plane = skl_update_plane;
10952c9916cdSFrançois Tigeot 		intel_plane->disable_plane = skl_disable_plane;
109619c468b4SFrançois Tigeot 		state->scaler_id = -1;
10972c84b0b6SFrançois Tigeot 
10982c9916cdSFrançois Tigeot 		plane_formats = skl_plane_formats;
10992c9916cdSFrançois Tigeot 		num_plane_formats = ARRAY_SIZE(skl_plane_formats);
11002c9916cdSFrançois Tigeot 		break;
11012c84b0b6SFrançois Tigeot 	default:
11028621f407SFrançois Tigeot 		MISSING_CASE(INTEL_INFO(dev)->gen);
11038621f407SFrançois Tigeot 		ret = -ENODEV;
11048621f407SFrançois Tigeot 		goto fail;
1105e3adcf8fSFrançois Tigeot 	}
1106e3adcf8fSFrançois Tigeot 
1107e3adcf8fSFrançois Tigeot 	intel_plane->pipe = pipe;
11088e26cdf6SFrançois Tigeot 	intel_plane->plane = plane;
1109352ff8bdSFrançois Tigeot 	intel_plane->frontbuffer_bit = INTEL_FRONTBUFFER_SPRITE(pipe, plane);
11102c9916cdSFrançois Tigeot 	intel_plane->check_plane = intel_check_sprite_plane;
11118621f407SFrançois Tigeot 
1112e3adcf8fSFrançois Tigeot 	possible_crtcs = (1 << pipe);
11138621f407SFrançois Tigeot 
11141b13d190SFrançois Tigeot 	ret = drm_universal_plane_init(dev, &intel_plane->base, possible_crtcs,
11152c84b0b6SFrançois Tigeot 				       &intel_plane_funcs,
11162c84b0b6SFrançois Tigeot 				       plane_formats, num_plane_formats,
1117aee94f86SFrançois Tigeot 				       DRM_PLANE_TYPE_OVERLAY, NULL);
11188621f407SFrançois Tigeot 	if (ret)
11198621f407SFrançois Tigeot 		goto fail;
1120e3adcf8fSFrançois Tigeot 
112119c468b4SFrançois Tigeot 	intel_create_rotation_property(dev, intel_plane);
11222c9916cdSFrançois Tigeot 
11232c9916cdSFrançois Tigeot 	drm_plane_helper_add(&intel_plane->base, &intel_plane_helper_funcs);
11241b13d190SFrançois Tigeot 
11258621f407SFrançois Tigeot 	return 0;
11268621f407SFrançois Tigeot 
11278621f407SFrançois Tigeot fail:
11288621f407SFrançois Tigeot 	kfree(state);
11298621f407SFrançois Tigeot 	kfree(intel_plane);
11308621f407SFrançois Tigeot 
1131e3adcf8fSFrançois Tigeot 	return ret;
1132e3adcf8fSFrançois Tigeot }
1133