xref: /dflybsd-src/sys/dev/drm/i915/intel_sprite.c (revision a05eeebfe8ec06e5625c15c535aed68dbf568bd6)
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>
3418e26a6dSFrançois Tigeot #include <uapi_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 
56ba55f2f5SFrançois Tigeot static int usecs_to_scanlines(const struct drm_display_mode *mode, int usecs)
57ba55f2f5SFrançois Tigeot {
58ba55f2f5SFrançois Tigeot 	/* paranoia */
59ba55f2f5SFrançois Tigeot 	if (!mode->crtc_htotal)
60ba55f2f5SFrançois Tigeot 		return 1;
61ba55f2f5SFrançois Tigeot 
62ba55f2f5SFrançois Tigeot 	return DIV_ROUND_UP(usecs * mode->crtc_clock, 1000 * mode->crtc_htotal);
63ba55f2f5SFrançois Tigeot }
64ba55f2f5SFrançois Tigeot 
652c9916cdSFrançois Tigeot /**
662c9916cdSFrançois Tigeot  * intel_pipe_update_start() - start update of a set of display registers
672c9916cdSFrançois Tigeot  * @crtc: the crtc of which the registers are going to be updated
682c9916cdSFrançois Tigeot  * @start_vbl_count: vblank counter return pointer used for error checking
692c9916cdSFrançois Tigeot  *
702c9916cdSFrançois Tigeot  * Mark the start of an update to pipe registers that should be updated
712c9916cdSFrançois Tigeot  * atomically regarding vblank. If the next vblank will happens within
722c9916cdSFrançois Tigeot  * the next 100 us, this function waits until the vblank passes.
732c9916cdSFrançois Tigeot  *
742c9916cdSFrançois Tigeot  * After a successful call to this function, interrupts will be disabled
752c9916cdSFrançois Tigeot  * until a subsequent call to intel_pipe_update_end(). That is done to
762c9916cdSFrançois Tigeot  * avoid random delays. The value written to @start_vbl_count should be
772c9916cdSFrançois Tigeot  * supplied to intel_pipe_update_end() for error checking.
782c9916cdSFrançois Tigeot  */
79*a05eeebfSFrançois Tigeot void intel_pipe_update_start(struct intel_crtc *crtc, uint32_t *start_vbl_count)
80ba55f2f5SFrançois Tigeot {
81ba55f2f5SFrançois Tigeot 	struct drm_device *dev = crtc->base.dev;
822c9916cdSFrançois Tigeot 	const struct drm_display_mode *mode = &crtc->config->base.adjusted_mode;
83ba55f2f5SFrançois Tigeot 	enum i915_pipe pipe = crtc->pipe;
84ba55f2f5SFrançois Tigeot 	long timeout = msecs_to_jiffies_timeout(1);
85ba55f2f5SFrançois Tigeot 	int scanline, min, max, vblank_start;
861b13d190SFrançois Tigeot 	wait_queue_head_t *wq = drm_crtc_vblank_waitqueue(&crtc->base);
87ba55f2f5SFrançois Tigeot 	DEFINE_WAIT(wait);
88ba55f2f5SFrançois Tigeot 
89ba55f2f5SFrançois Tigeot 	vblank_start = mode->crtc_vblank_start;
90ba55f2f5SFrançois Tigeot 	if (mode->flags & DRM_MODE_FLAG_INTERLACE)
91ba55f2f5SFrançois Tigeot 		vblank_start = DIV_ROUND_UP(vblank_start, 2);
92ba55f2f5SFrançois Tigeot 
93ba55f2f5SFrançois Tigeot 	/* FIXME needs to be calibrated sensibly */
94ba55f2f5SFrançois Tigeot 	min = vblank_start - usecs_to_scanlines(mode, 100);
95ba55f2f5SFrançois Tigeot 	max = vblank_start - 1;
96ba55f2f5SFrançois Tigeot 
97*a05eeebfSFrançois Tigeot 	local_irq_disable();
98*a05eeebfSFrançois Tigeot 	*start_vbl_count = 0;
99*a05eeebfSFrançois Tigeot 
100ba55f2f5SFrançois Tigeot 	if (min <= 0 || max <= 0)
101*a05eeebfSFrançois Tigeot 		return;
102ba55f2f5SFrançois Tigeot 
103477eb7f9SFrançois Tigeot 	if (WARN_ON(drm_crtc_vblank_get(&crtc->base)))
104*a05eeebfSFrançois Tigeot 		return;
105ba55f2f5SFrançois Tigeot 
106ba55f2f5SFrançois Tigeot 	trace_i915_pipe_update_start(crtc, min, max);
107ba55f2f5SFrançois Tigeot 
108ba55f2f5SFrançois Tigeot 	for (;;) {
109ba55f2f5SFrançois Tigeot 		/*
110ba55f2f5SFrançois Tigeot 		 * prepare_to_wait() has a memory barrier, which guarantees
111ba55f2f5SFrançois Tigeot 		 * other CPUs can see the task state update by the time we
112ba55f2f5SFrançois Tigeot 		 * read the scanline.
113ba55f2f5SFrançois Tigeot 		 */
1141b13d190SFrançois Tigeot 		prepare_to_wait(wq, &wait, TASK_UNINTERRUPTIBLE);
115ba55f2f5SFrançois Tigeot 
116ba55f2f5SFrançois Tigeot 		scanline = intel_get_crtc_scanline(crtc);
117ba55f2f5SFrançois Tigeot 		if (scanline < min || scanline > max)
118ba55f2f5SFrançois Tigeot 			break;
119ba55f2f5SFrançois Tigeot 
120ba55f2f5SFrançois Tigeot 		if (timeout <= 0) {
121ba55f2f5SFrançois Tigeot 			DRM_ERROR("Potential atomic update failure on pipe %c\n",
122ba55f2f5SFrançois Tigeot 				  pipe_name(crtc->pipe));
123ba55f2f5SFrançois Tigeot 			break;
124ba55f2f5SFrançois Tigeot 		}
125ba55f2f5SFrançois Tigeot 
126ba55f2f5SFrançois Tigeot 		local_irq_enable();
127ba55f2f5SFrançois Tigeot 
128ba55f2f5SFrançois Tigeot 		timeout = schedule_timeout(timeout);
129ba55f2f5SFrançois Tigeot 
130ba55f2f5SFrançois Tigeot 		local_irq_disable();
131ba55f2f5SFrançois Tigeot 	}
132ba55f2f5SFrançois Tigeot 
1331b13d190SFrançois Tigeot 	finish_wait(wq, &wait);
134ba55f2f5SFrançois Tigeot 
135477eb7f9SFrançois Tigeot 	drm_crtc_vblank_put(&crtc->base);
136ba55f2f5SFrançois Tigeot 
137ba55f2f5SFrançois Tigeot 	*start_vbl_count = dev->driver->get_vblank_counter(dev, pipe);
138ba55f2f5SFrançois Tigeot 
139ba55f2f5SFrançois Tigeot 	trace_i915_pipe_update_vblank_evaded(crtc, min, max, *start_vbl_count);
140ba55f2f5SFrançois Tigeot }
141ba55f2f5SFrançois Tigeot 
1422c9916cdSFrançois Tigeot /**
1432c9916cdSFrançois Tigeot  * intel_pipe_update_end() - end update of a set of display registers
1442c9916cdSFrançois Tigeot  * @crtc: the crtc of which the registers were updated
1452c9916cdSFrançois Tigeot  * @start_vbl_count: start vblank counter (used for error checking)
1462c9916cdSFrançois Tigeot  *
1472c9916cdSFrançois Tigeot  * Mark the end of an update started with intel_pipe_update_start(). This
1482c9916cdSFrançois Tigeot  * re-enables interrupts and verifies the update was actually completed
1492c9916cdSFrançois Tigeot  * before a vblank using the value of @start_vbl_count.
1502c9916cdSFrançois Tigeot  */
1512c9916cdSFrançois Tigeot void intel_pipe_update_end(struct intel_crtc *crtc, u32 start_vbl_count)
152ba55f2f5SFrançois Tigeot {
153ba55f2f5SFrançois Tigeot 	struct drm_device *dev = crtc->base.dev;
154ba55f2f5SFrançois Tigeot 	enum i915_pipe pipe = crtc->pipe;
155ba55f2f5SFrançois Tigeot 	u32 end_vbl_count = dev->driver->get_vblank_counter(dev, pipe);
156ba55f2f5SFrançois Tigeot 
157ba55f2f5SFrançois Tigeot 	trace_i915_pipe_update_end(crtc, end_vbl_count);
158ba55f2f5SFrançois Tigeot 
159ba55f2f5SFrançois Tigeot 	local_irq_enable();
160ba55f2f5SFrançois Tigeot 
161*a05eeebfSFrançois Tigeot 	if (start_vbl_count && start_vbl_count != end_vbl_count)
162ba55f2f5SFrançois Tigeot 		DRM_ERROR("Atomic update failure on pipe %c (start=%u end=%u)\n",
163ba55f2f5SFrançois Tigeot 			  pipe_name(pipe), start_vbl_count, end_vbl_count);
164ba55f2f5SFrançois Tigeot }
165ba55f2f5SFrançois Tigeot 
166e3adcf8fSFrançois Tigeot static void
1672c9916cdSFrançois Tigeot skl_update_plane(struct drm_plane *drm_plane, struct drm_crtc *crtc,
1682c9916cdSFrançois Tigeot 		 struct drm_framebuffer *fb,
169477eb7f9SFrançois Tigeot 		 int crtc_x, int crtc_y,
1702c9916cdSFrançois Tigeot 		 unsigned int crtc_w, unsigned int crtc_h,
1712c9916cdSFrançois Tigeot 		 uint32_t x, uint32_t y,
1722c9916cdSFrançois Tigeot 		 uint32_t src_w, uint32_t src_h)
1732c9916cdSFrançois Tigeot {
1742c9916cdSFrançois Tigeot 	struct drm_device *dev = drm_plane->dev;
1752c9916cdSFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
1762c9916cdSFrançois Tigeot 	struct intel_plane *intel_plane = to_intel_plane(drm_plane);
177477eb7f9SFrançois Tigeot 	struct drm_i915_gem_object *obj = intel_fb_obj(fb);
1782c9916cdSFrançois Tigeot 	const int pipe = intel_plane->pipe;
1792c9916cdSFrançois Tigeot 	const int plane = intel_plane->plane + 1;
18019c468b4SFrançois Tigeot 	u32 plane_ctl, stride_div, stride;
1812c9916cdSFrançois Tigeot 	int pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
182*a05eeebfSFrançois Tigeot 	const struct drm_intel_sprite_colorkey *key =
183*a05eeebfSFrançois Tigeot 		&to_intel_plane_state(drm_plane->state)->ckey;
184477eb7f9SFrançois Tigeot 	unsigned long surf_addr;
18519c468b4SFrançois Tigeot 	u32 tile_height, plane_offset, plane_size;
18619c468b4SFrançois Tigeot 	unsigned int rotation;
18719c468b4SFrançois Tigeot 	int x_offset, y_offset;
18819c468b4SFrançois Tigeot 	struct intel_crtc_state *crtc_state = to_intel_crtc(crtc)->config;
18919c468b4SFrançois Tigeot 	int scaler_id;
1902c9916cdSFrançois Tigeot 
191477eb7f9SFrançois Tigeot 	plane_ctl = PLANE_CTL_ENABLE |
192477eb7f9SFrançois Tigeot 		PLANE_CTL_PIPE_CSC_ENABLE;
1932c9916cdSFrançois Tigeot 
19419c468b4SFrançois Tigeot 	plane_ctl |= skl_plane_ctl_format(fb->pixel_format);
19519c468b4SFrançois Tigeot 	plane_ctl |= skl_plane_ctl_tiling(fb->modifier[0]);
1962c9916cdSFrançois Tigeot 
19719c468b4SFrançois Tigeot 	rotation = drm_plane->state->rotation;
19819c468b4SFrançois Tigeot 	plane_ctl |= skl_plane_ctl_rotation(rotation);
1992c9916cdSFrançois Tigeot 
2002c9916cdSFrançois Tigeot 	intel_update_sprite_watermarks(drm_plane, crtc, src_w, src_h,
2012c9916cdSFrançois Tigeot 				       pixel_size, true,
2022c9916cdSFrançois Tigeot 				       src_w != crtc_w || src_h != crtc_h);
2032c9916cdSFrançois Tigeot 
204477eb7f9SFrançois Tigeot 	stride_div = intel_fb_stride_alignment(dev, fb->modifier[0],
205477eb7f9SFrançois Tigeot 					       fb->pixel_format);
206477eb7f9SFrançois Tigeot 
20719c468b4SFrançois Tigeot 	scaler_id = to_intel_plane_state(drm_plane->state)->scaler_id;
20819c468b4SFrançois Tigeot 
2092c9916cdSFrançois Tigeot 	/* Sizes are 0 based */
2102c9916cdSFrançois Tigeot 	src_w--;
2112c9916cdSFrançois Tigeot 	src_h--;
2122c9916cdSFrançois Tigeot 	crtc_w--;
2132c9916cdSFrançois Tigeot 	crtc_h--;
2142c9916cdSFrançois Tigeot 
215477eb7f9SFrançois Tigeot 	if (key->flags) {
216477eb7f9SFrançois Tigeot 		I915_WRITE(PLANE_KEYVAL(pipe, plane), key->min_value);
217477eb7f9SFrançois Tigeot 		I915_WRITE(PLANE_KEYMAX(pipe, plane), key->max_value);
218477eb7f9SFrançois Tigeot 		I915_WRITE(PLANE_KEYMSK(pipe, plane), key->channel_mask);
219477eb7f9SFrançois Tigeot 	}
220477eb7f9SFrançois Tigeot 
221477eb7f9SFrançois Tigeot 	if (key->flags & I915_SET_COLORKEY_DESTINATION)
222477eb7f9SFrançois Tigeot 		plane_ctl |= PLANE_CTL_KEY_ENABLE_DESTINATION;
223477eb7f9SFrançois Tigeot 	else if (key->flags & I915_SET_COLORKEY_SOURCE)
224477eb7f9SFrançois Tigeot 		plane_ctl |= PLANE_CTL_KEY_ENABLE_SOURCE;
225477eb7f9SFrançois Tigeot 
226477eb7f9SFrançois Tigeot 	surf_addr = intel_plane_obj_offset(intel_plane, obj);
227477eb7f9SFrançois Tigeot 
22819c468b4SFrançois Tigeot 	if (intel_rotation_90_or_270(rotation)) {
22919c468b4SFrançois Tigeot 		/* stride: Surface height in tiles */
23019c468b4SFrançois Tigeot 		tile_height = intel_tile_height(dev, fb->pixel_format,
23119c468b4SFrançois Tigeot 						fb->modifier[0]);
23219c468b4SFrançois Tigeot 		stride = DIV_ROUND_UP(fb->height, tile_height);
23319c468b4SFrançois Tigeot 		plane_size = (src_w << 16) | src_h;
23419c468b4SFrançois Tigeot 		x_offset = stride * tile_height - y - (src_h + 1);
23519c468b4SFrançois Tigeot 		y_offset = x;
23619c468b4SFrançois Tigeot 	} else {
23719c468b4SFrançois Tigeot 		stride = fb->pitches[0] / stride_div;
23819c468b4SFrançois Tigeot 		plane_size = (src_h << 16) | src_w;
23919c468b4SFrançois Tigeot 		x_offset = x;
24019c468b4SFrançois Tigeot 		y_offset = y;
24119c468b4SFrançois Tigeot 	}
24219c468b4SFrançois Tigeot 	plane_offset = y_offset << 16 | x_offset;
24319c468b4SFrançois Tigeot 
24419c468b4SFrançois Tigeot 	I915_WRITE(PLANE_OFFSET(pipe, plane), plane_offset);
24519c468b4SFrançois Tigeot 	I915_WRITE(PLANE_STRIDE(pipe, plane), stride);
24619c468b4SFrançois Tigeot 	I915_WRITE(PLANE_SIZE(pipe, plane), plane_size);
24719c468b4SFrançois Tigeot 
24819c468b4SFrançois Tigeot 	/* program plane scaler */
24919c468b4SFrançois Tigeot 	if (scaler_id >= 0) {
25019c468b4SFrançois Tigeot 		uint32_t ps_ctrl = 0;
25119c468b4SFrançois Tigeot 
25219c468b4SFrançois Tigeot 		DRM_DEBUG_KMS("plane = %d PS_PLANE_SEL(plane) = 0x%x\n", plane,
25319c468b4SFrançois Tigeot 			PS_PLANE_SEL(plane));
25419c468b4SFrançois Tigeot 		ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(plane) |
25519c468b4SFrançois Tigeot 			crtc_state->scaler_state.scalers[scaler_id].mode;
25619c468b4SFrançois Tigeot 		I915_WRITE(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
25719c468b4SFrançois Tigeot 		I915_WRITE(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
25819c468b4SFrançois Tigeot 		I915_WRITE(SKL_PS_WIN_POS(pipe, scaler_id), (crtc_x << 16) | crtc_y);
25919c468b4SFrançois Tigeot 		I915_WRITE(SKL_PS_WIN_SZ(pipe, scaler_id),
26019c468b4SFrançois Tigeot 			((crtc_w + 1) << 16)|(crtc_h + 1));
26119c468b4SFrançois Tigeot 
26219c468b4SFrançois Tigeot 		I915_WRITE(PLANE_POS(pipe, plane), 0);
26319c468b4SFrançois Tigeot 	} else {
2642c9916cdSFrançois Tigeot 		I915_WRITE(PLANE_POS(pipe, plane), (crtc_y << 16) | crtc_x);
26519c468b4SFrançois Tigeot 	}
26619c468b4SFrançois Tigeot 
2672c9916cdSFrançois Tigeot 	I915_WRITE(PLANE_CTL(pipe, plane), plane_ctl);
268477eb7f9SFrançois Tigeot 	I915_WRITE(PLANE_SURF(pipe, plane), surf_addr);
2692c9916cdSFrançois Tigeot 	POSTING_READ(PLANE_SURF(pipe, plane));
2702c9916cdSFrançois Tigeot }
2712c9916cdSFrançois Tigeot 
2722c9916cdSFrançois Tigeot static void
273*a05eeebfSFrançois Tigeot skl_disable_plane(struct drm_plane *dplane, struct drm_crtc *crtc)
2742c9916cdSFrançois Tigeot {
27519c468b4SFrançois Tigeot 	struct drm_device *dev = dplane->dev;
2762c9916cdSFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
27719c468b4SFrançois Tigeot 	struct intel_plane *intel_plane = to_intel_plane(dplane);
2782c9916cdSFrançois Tigeot 	const int pipe = intel_plane->pipe;
2792c9916cdSFrançois Tigeot 	const int plane = intel_plane->plane + 1;
2802c9916cdSFrançois Tigeot 
281477eb7f9SFrançois Tigeot 	I915_WRITE(PLANE_CTL(pipe, plane), 0);
2822c9916cdSFrançois Tigeot 
283477eb7f9SFrançois Tigeot 	I915_WRITE(PLANE_SURF(pipe, plane), 0);
284477eb7f9SFrançois Tigeot 	POSTING_READ(PLANE_SURF(pipe, plane));
2852c9916cdSFrançois Tigeot 
28619c468b4SFrançois Tigeot 	intel_update_sprite_watermarks(dplane, crtc, 0, 0, 0, false, false);
2872c9916cdSFrançois Tigeot }
2882c9916cdSFrançois Tigeot 
2892c9916cdSFrançois Tigeot static void
2902c9916cdSFrançois Tigeot chv_update_csc(struct intel_plane *intel_plane, uint32_t format)
2912c9916cdSFrançois Tigeot {
2922c9916cdSFrançois Tigeot 	struct drm_i915_private *dev_priv = intel_plane->base.dev->dev_private;
2932c9916cdSFrançois Tigeot 	int plane = intel_plane->plane;
2942c9916cdSFrançois Tigeot 
2952c9916cdSFrançois Tigeot 	/* Seems RGB data bypasses the CSC always */
2962c9916cdSFrançois Tigeot 	if (!format_is_yuv(format))
2972c9916cdSFrançois Tigeot 		return;
2982c9916cdSFrançois Tigeot 
2992c9916cdSFrançois Tigeot 	/*
3002c9916cdSFrançois Tigeot 	 * BT.601 limited range YCbCr -> full range RGB
3012c9916cdSFrançois Tigeot 	 *
3022c9916cdSFrançois Tigeot 	 * |r|   | 6537 4769     0|   |cr  |
3032c9916cdSFrançois Tigeot 	 * |g| = |-3330 4769 -1605| x |y-64|
3042c9916cdSFrançois Tigeot 	 * |b|   |    0 4769  8263|   |cb  |
3052c9916cdSFrançois Tigeot 	 *
3062c9916cdSFrançois Tigeot 	 * Cb and Cr apparently come in as signed already, so no
3072c9916cdSFrançois Tigeot 	 * need for any offset. For Y we need to remove the offset.
3082c9916cdSFrançois Tigeot 	 */
3092c9916cdSFrançois Tigeot 	I915_WRITE(SPCSCYGOFF(plane), SPCSC_OOFF(0) | SPCSC_IOFF(-64));
3102c9916cdSFrançois Tigeot 	I915_WRITE(SPCSCCBOFF(plane), SPCSC_OOFF(0) | SPCSC_IOFF(0));
3112c9916cdSFrançois Tigeot 	I915_WRITE(SPCSCCROFF(plane), SPCSC_OOFF(0) | SPCSC_IOFF(0));
3122c9916cdSFrançois Tigeot 
3132c9916cdSFrançois Tigeot 	I915_WRITE(SPCSCC01(plane), SPCSC_C1(4769) | SPCSC_C0(6537));
3142c9916cdSFrançois Tigeot 	I915_WRITE(SPCSCC23(plane), SPCSC_C1(-3330) | SPCSC_C0(0));
3152c9916cdSFrançois Tigeot 	I915_WRITE(SPCSCC45(plane), SPCSC_C1(-1605) | SPCSC_C0(4769));
3162c9916cdSFrançois Tigeot 	I915_WRITE(SPCSCC67(plane), SPCSC_C1(4769) | SPCSC_C0(0));
3172c9916cdSFrançois Tigeot 	I915_WRITE(SPCSCC8(plane), SPCSC_C0(8263));
3182c9916cdSFrançois Tigeot 
3192c9916cdSFrançois Tigeot 	I915_WRITE(SPCSCYGICLAMP(plane), SPCSC_IMAX(940) | SPCSC_IMIN(64));
3202c9916cdSFrançois Tigeot 	I915_WRITE(SPCSCCBICLAMP(plane), SPCSC_IMAX(448) | SPCSC_IMIN(-448));
3212c9916cdSFrançois Tigeot 	I915_WRITE(SPCSCCRICLAMP(plane), SPCSC_IMAX(448) | SPCSC_IMIN(-448));
3222c9916cdSFrançois Tigeot 
3232c9916cdSFrançois Tigeot 	I915_WRITE(SPCSCYGOCLAMP(plane), SPCSC_OMAX(1023) | SPCSC_OMIN(0));
3242c9916cdSFrançois Tigeot 	I915_WRITE(SPCSCCBOCLAMP(plane), SPCSC_OMAX(1023) | SPCSC_OMIN(0));
3252c9916cdSFrançois Tigeot 	I915_WRITE(SPCSCCROCLAMP(plane), SPCSC_OMAX(1023) | SPCSC_OMIN(0));
3262c9916cdSFrançois Tigeot }
3272c9916cdSFrançois Tigeot 
3282c9916cdSFrançois Tigeot static void
3299edbd4a0SFrançois Tigeot vlv_update_plane(struct drm_plane *dplane, struct drm_crtc *crtc,
3309edbd4a0SFrançois Tigeot 		 struct drm_framebuffer *fb,
331477eb7f9SFrançois Tigeot 		 int crtc_x, int crtc_y,
3328e26cdf6SFrançois Tigeot 		 unsigned int crtc_w, unsigned int crtc_h,
3338e26cdf6SFrançois Tigeot 		 uint32_t x, uint32_t y,
3348e26cdf6SFrançois Tigeot 		 uint32_t src_w, uint32_t src_h)
3358e26cdf6SFrançois Tigeot {
3368e26cdf6SFrançois Tigeot 	struct drm_device *dev = dplane->dev;
3378e26cdf6SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
3388e26cdf6SFrançois Tigeot 	struct intel_plane *intel_plane = to_intel_plane(dplane);
339477eb7f9SFrançois Tigeot 	struct drm_i915_gem_object *obj = intel_fb_obj(fb);
3408e26cdf6SFrançois Tigeot 	int pipe = intel_plane->pipe;
3418e26cdf6SFrançois Tigeot 	int plane = intel_plane->plane;
3428e26cdf6SFrançois Tigeot 	u32 sprctl;
3438e26cdf6SFrançois Tigeot 	unsigned long sprsurf_offset, linear_offset;
3448e26cdf6SFrançois Tigeot 	int pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
345*a05eeebfSFrançois Tigeot 	const struct drm_intel_sprite_colorkey *key =
346*a05eeebfSFrançois Tigeot 		&to_intel_plane_state(dplane->state)->ckey;
3478e26cdf6SFrançois Tigeot 
348477eb7f9SFrançois Tigeot 	sprctl = SP_ENABLE;
3498e26cdf6SFrançois Tigeot 
3508e26cdf6SFrançois Tigeot 	switch (fb->pixel_format) {
3518e26cdf6SFrançois Tigeot 	case DRM_FORMAT_YUYV:
3528e26cdf6SFrançois Tigeot 		sprctl |= SP_FORMAT_YUV422 | SP_YUV_ORDER_YUYV;
3538e26cdf6SFrançois Tigeot 		break;
3548e26cdf6SFrançois Tigeot 	case DRM_FORMAT_YVYU:
3558e26cdf6SFrançois Tigeot 		sprctl |= SP_FORMAT_YUV422 | SP_YUV_ORDER_YVYU;
3568e26cdf6SFrançois Tigeot 		break;
3578e26cdf6SFrançois Tigeot 	case DRM_FORMAT_UYVY:
3588e26cdf6SFrançois Tigeot 		sprctl |= SP_FORMAT_YUV422 | SP_YUV_ORDER_UYVY;
3598e26cdf6SFrançois Tigeot 		break;
3608e26cdf6SFrançois Tigeot 	case DRM_FORMAT_VYUY:
3618e26cdf6SFrançois Tigeot 		sprctl |= SP_FORMAT_YUV422 | SP_YUV_ORDER_VYUY;
3628e26cdf6SFrançois Tigeot 		break;
3638e26cdf6SFrançois Tigeot 	case DRM_FORMAT_RGB565:
3648e26cdf6SFrançois Tigeot 		sprctl |= SP_FORMAT_BGR565;
3658e26cdf6SFrançois Tigeot 		break;
3668e26cdf6SFrançois Tigeot 	case DRM_FORMAT_XRGB8888:
3678e26cdf6SFrançois Tigeot 		sprctl |= SP_FORMAT_BGRX8888;
3688e26cdf6SFrançois Tigeot 		break;
3698e26cdf6SFrançois Tigeot 	case DRM_FORMAT_ARGB8888:
3708e26cdf6SFrançois Tigeot 		sprctl |= SP_FORMAT_BGRA8888;
3718e26cdf6SFrançois Tigeot 		break;
3728e26cdf6SFrançois Tigeot 	case DRM_FORMAT_XBGR2101010:
3738e26cdf6SFrançois Tigeot 		sprctl |= SP_FORMAT_RGBX1010102;
3748e26cdf6SFrançois Tigeot 		break;
3758e26cdf6SFrançois Tigeot 	case DRM_FORMAT_ABGR2101010:
3768e26cdf6SFrançois Tigeot 		sprctl |= SP_FORMAT_RGBA1010102;
3778e26cdf6SFrançois Tigeot 		break;
3788e26cdf6SFrançois Tigeot 	case DRM_FORMAT_XBGR8888:
3798e26cdf6SFrançois Tigeot 		sprctl |= SP_FORMAT_RGBX8888;
3808e26cdf6SFrançois Tigeot 		break;
3818e26cdf6SFrançois Tigeot 	case DRM_FORMAT_ABGR8888:
3828e26cdf6SFrançois Tigeot 		sprctl |= SP_FORMAT_RGBA8888;
3838e26cdf6SFrançois Tigeot 		break;
3848e26cdf6SFrançois Tigeot 	default:
3858e26cdf6SFrançois Tigeot 		/*
3868e26cdf6SFrançois Tigeot 		 * If we get here one of the upper layers failed to filter
3878e26cdf6SFrançois Tigeot 		 * out the unsupported plane formats
3888e26cdf6SFrançois Tigeot 		 */
3898e26cdf6SFrançois Tigeot 		BUG();
3908e26cdf6SFrançois Tigeot 		break;
3918e26cdf6SFrançois Tigeot 	}
3928e26cdf6SFrançois Tigeot 
3939edbd4a0SFrançois Tigeot 	/*
3949edbd4a0SFrançois Tigeot 	 * Enable gamma to match primary/cursor plane behaviour.
3959edbd4a0SFrançois Tigeot 	 * FIXME should be user controllable via propertiesa.
3969edbd4a0SFrançois Tigeot 	 */
3979edbd4a0SFrançois Tigeot 	sprctl |= SP_GAMMA_ENABLE;
3989edbd4a0SFrançois Tigeot 
3998e26cdf6SFrançois Tigeot 	if (obj->tiling_mode != I915_TILING_NONE)
4008e26cdf6SFrançois Tigeot 		sprctl |= SP_TILED;
4018e26cdf6SFrançois Tigeot 
4028e26cdf6SFrançois Tigeot 	/* Sizes are 0 based */
4038e26cdf6SFrançois Tigeot 	src_w--;
4048e26cdf6SFrançois Tigeot 	src_h--;
4058e26cdf6SFrançois Tigeot 	crtc_w--;
4068e26cdf6SFrançois Tigeot 	crtc_h--;
4078e26cdf6SFrançois Tigeot 
4088e26cdf6SFrançois Tigeot 	linear_offset = y * fb->pitches[0] + x * pixel_size;
409*a05eeebfSFrançois Tigeot 	sprsurf_offset = intel_gen4_compute_page_offset(dev_priv,
410*a05eeebfSFrançois Tigeot 							&x, &y,
4118e26cdf6SFrançois Tigeot 							obj->tiling_mode,
4128e26cdf6SFrançois Tigeot 							pixel_size,
4138e26cdf6SFrançois Tigeot 							fb->pitches[0]);
4148e26cdf6SFrançois Tigeot 	linear_offset -= sprsurf_offset;
4158e26cdf6SFrançois Tigeot 
4162c9916cdSFrançois Tigeot 	if (dplane->state->rotation == BIT(DRM_ROTATE_180)) {
4171b13d190SFrançois Tigeot 		sprctl |= SP_ROTATE_180;
4181b13d190SFrançois Tigeot 
4191b13d190SFrançois Tigeot 		x += src_w;
4201b13d190SFrançois Tigeot 		y += src_h;
4211b13d190SFrançois Tigeot 		linear_offset += src_h * fb->pitches[0] + src_w * pixel_size;
4221b13d190SFrançois Tigeot 	}
4231b13d190SFrançois Tigeot 
424477eb7f9SFrançois Tigeot 	if (key->flags) {
425477eb7f9SFrançois Tigeot 		I915_WRITE(SPKEYMINVAL(pipe, plane), key->min_value);
426477eb7f9SFrançois Tigeot 		I915_WRITE(SPKEYMAXVAL(pipe, plane), key->max_value);
427477eb7f9SFrançois Tigeot 		I915_WRITE(SPKEYMSK(pipe, plane), key->channel_mask);
428477eb7f9SFrançois Tigeot 	}
429477eb7f9SFrançois Tigeot 
430477eb7f9SFrançois Tigeot 	if (key->flags & I915_SET_COLORKEY_SOURCE)
431477eb7f9SFrançois Tigeot 		sprctl |= SP_SOURCE_KEY;
432477eb7f9SFrançois Tigeot 
4332c9916cdSFrançois Tigeot 	if (IS_CHERRYVIEW(dev) && pipe == PIPE_B)
4342c9916cdSFrançois Tigeot 		chv_update_csc(intel_plane, fb->pixel_format);
4352c9916cdSFrançois Tigeot 
436ba55f2f5SFrançois Tigeot 	I915_WRITE(SPSTRIDE(pipe, plane), fb->pitches[0]);
437ba55f2f5SFrançois Tigeot 	I915_WRITE(SPPOS(pipe, plane), (crtc_y << 16) | crtc_x);
438ba55f2f5SFrançois Tigeot 
4398e26cdf6SFrançois Tigeot 	if (obj->tiling_mode != I915_TILING_NONE)
4408e26cdf6SFrançois Tigeot 		I915_WRITE(SPTILEOFF(pipe, plane), (y << 16) | x);
4418e26cdf6SFrançois Tigeot 	else
4428e26cdf6SFrançois Tigeot 		I915_WRITE(SPLINOFF(pipe, plane), linear_offset);
4438e26cdf6SFrançois Tigeot 
4442c9916cdSFrançois Tigeot 	I915_WRITE(SPCONSTALPHA(pipe, plane), 0);
4452c9916cdSFrançois Tigeot 
4468e26cdf6SFrançois Tigeot 	I915_WRITE(SPSIZE(pipe, plane), (crtc_h << 16) | crtc_w);
4478e26cdf6SFrançois Tigeot 	I915_WRITE(SPCNTR(pipe, plane), sprctl);
4489edbd4a0SFrançois Tigeot 	I915_WRITE(SPSURF(pipe, plane), i915_gem_obj_ggtt_offset(obj) +
4498e26cdf6SFrançois Tigeot 		   sprsurf_offset);
45019c468b4SFrançois Tigeot 	POSTING_READ(SPSURF(pipe, plane));
4518e26cdf6SFrançois Tigeot }
4528e26cdf6SFrançois Tigeot 
4538e26cdf6SFrançois Tigeot static void
454*a05eeebfSFrançois Tigeot vlv_disable_plane(struct drm_plane *dplane, struct drm_crtc *crtc)
4558e26cdf6SFrançois Tigeot {
4568e26cdf6SFrançois Tigeot 	struct drm_device *dev = dplane->dev;
4578e26cdf6SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
4588e26cdf6SFrançois Tigeot 	struct intel_plane *intel_plane = to_intel_plane(dplane);
4598e26cdf6SFrançois Tigeot 	int pipe = intel_plane->pipe;
4608e26cdf6SFrançois Tigeot 	int plane = intel_plane->plane;
461ba55f2f5SFrançois Tigeot 
462477eb7f9SFrançois Tigeot 	I915_WRITE(SPCNTR(pipe, plane), 0);
463477eb7f9SFrançois Tigeot 
4649edbd4a0SFrançois Tigeot 	I915_WRITE(SPSURF(pipe, plane), 0);
46519c468b4SFrançois Tigeot 	POSTING_READ(SPSURF(pipe, plane));
4668e26cdf6SFrançois Tigeot }
4678e26cdf6SFrançois Tigeot 
4688e26cdf6SFrançois Tigeot static void
4699edbd4a0SFrançois Tigeot ivb_update_plane(struct drm_plane *plane, struct drm_crtc *crtc,
4709edbd4a0SFrançois Tigeot 		 struct drm_framebuffer *fb,
471477eb7f9SFrançois Tigeot 		 int crtc_x, int crtc_y,
472e3adcf8fSFrançois Tigeot 		 unsigned int crtc_w, unsigned int crtc_h,
473e3adcf8fSFrançois Tigeot 		 uint32_t x, uint32_t y,
474e3adcf8fSFrançois Tigeot 		 uint32_t src_w, uint32_t src_h)
475e3adcf8fSFrançois Tigeot {
476e3adcf8fSFrançois Tigeot 	struct drm_device *dev = plane->dev;
477e3adcf8fSFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
478e3adcf8fSFrançois Tigeot 	struct intel_plane *intel_plane = to_intel_plane(plane);
479477eb7f9SFrançois Tigeot 	struct drm_i915_gem_object *obj = intel_fb_obj(fb);
480477eb7f9SFrançois Tigeot 	enum i915_pipe pipe = intel_plane->pipe;
481e3adcf8fSFrançois Tigeot 	u32 sprctl, sprscale = 0;
4822c84b0b6SFrançois Tigeot 	unsigned long sprsurf_offset, linear_offset;
4832c84b0b6SFrançois Tigeot 	int pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
484*a05eeebfSFrançois Tigeot 	const struct drm_intel_sprite_colorkey *key =
485*a05eeebfSFrançois Tigeot 		&to_intel_plane_state(plane->state)->ckey;
486e3adcf8fSFrançois Tigeot 
487477eb7f9SFrançois Tigeot 	sprctl = SPRITE_ENABLE;
488e3adcf8fSFrançois Tigeot 
489e3adcf8fSFrançois Tigeot 	switch (fb->pixel_format) {
490e3adcf8fSFrançois Tigeot 	case DRM_FORMAT_XBGR8888:
4912c84b0b6SFrançois Tigeot 		sprctl |= SPRITE_FORMAT_RGBX888 | SPRITE_RGB_ORDER_RGBX;
492e3adcf8fSFrançois Tigeot 		break;
493e3adcf8fSFrançois Tigeot 	case DRM_FORMAT_XRGB8888:
4942c84b0b6SFrançois Tigeot 		sprctl |= SPRITE_FORMAT_RGBX888;
495e3adcf8fSFrançois Tigeot 		break;
496e3adcf8fSFrançois Tigeot 	case DRM_FORMAT_YUYV:
497e3adcf8fSFrançois Tigeot 		sprctl |= SPRITE_FORMAT_YUV422 | SPRITE_YUV_ORDER_YUYV;
498e3adcf8fSFrançois Tigeot 		break;
499e3adcf8fSFrançois Tigeot 	case DRM_FORMAT_YVYU:
500e3adcf8fSFrançois Tigeot 		sprctl |= SPRITE_FORMAT_YUV422 | SPRITE_YUV_ORDER_YVYU;
501e3adcf8fSFrançois Tigeot 		break;
502e3adcf8fSFrançois Tigeot 	case DRM_FORMAT_UYVY:
503e3adcf8fSFrançois Tigeot 		sprctl |= SPRITE_FORMAT_YUV422 | SPRITE_YUV_ORDER_UYVY;
504e3adcf8fSFrançois Tigeot 		break;
505e3adcf8fSFrançois Tigeot 	case DRM_FORMAT_VYUY:
506e3adcf8fSFrançois Tigeot 		sprctl |= SPRITE_FORMAT_YUV422 | SPRITE_YUV_ORDER_VYUY;
507e3adcf8fSFrançois Tigeot 		break;
508e3adcf8fSFrançois Tigeot 	default:
5092c84b0b6SFrançois Tigeot 		BUG();
510e3adcf8fSFrançois Tigeot 	}
511e3adcf8fSFrançois Tigeot 
5129edbd4a0SFrançois Tigeot 	/*
5139edbd4a0SFrançois Tigeot 	 * Enable gamma to match primary/cursor plane behaviour.
5149edbd4a0SFrançois Tigeot 	 * FIXME should be user controllable via propertiesa.
5159edbd4a0SFrançois Tigeot 	 */
5169edbd4a0SFrançois Tigeot 	sprctl |= SPRITE_GAMMA_ENABLE;
5179edbd4a0SFrançois Tigeot 
518e3adcf8fSFrançois Tigeot 	if (obj->tiling_mode != I915_TILING_NONE)
519e3adcf8fSFrançois Tigeot 		sprctl |= SPRITE_TILED;
520e3adcf8fSFrançois Tigeot 
5219edbd4a0SFrançois Tigeot 	if (IS_HASWELL(dev) || IS_BROADWELL(dev))
5229edbd4a0SFrançois Tigeot 		sprctl &= ~SPRITE_TRICKLE_FEED_DISABLE;
5239edbd4a0SFrançois Tigeot 	else
524e3adcf8fSFrançois Tigeot 		sprctl |= SPRITE_TRICKLE_FEED_DISABLE;
5259edbd4a0SFrançois Tigeot 
5269edbd4a0SFrançois Tigeot 	if (IS_HASWELL(dev) || IS_BROADWELL(dev))
527a2fdbec6SFrançois Tigeot 		sprctl |= SPRITE_PIPE_CSC_ENABLE;
528a2fdbec6SFrançois Tigeot 
52924edb884SFrançois Tigeot 	intel_update_sprite_watermarks(plane, crtc, src_w, src_h, pixel_size,
53024edb884SFrançois Tigeot 				       true,
5319edbd4a0SFrançois Tigeot 				       src_w != crtc_w || src_h != crtc_h);
5329edbd4a0SFrançois Tigeot 
533e3adcf8fSFrançois Tigeot 	/* Sizes are 0 based */
534e3adcf8fSFrançois Tigeot 	src_w--;
535e3adcf8fSFrançois Tigeot 	src_h--;
536e3adcf8fSFrançois Tigeot 	crtc_w--;
537e3adcf8fSFrançois Tigeot 	crtc_h--;
538e3adcf8fSFrançois Tigeot 
5399edbd4a0SFrançois Tigeot 	if (crtc_w != src_w || crtc_h != src_h)
540e3adcf8fSFrançois Tigeot 		sprscale = SPRITE_SCALE_ENABLE | (src_w << 16) | src_h;
541e3adcf8fSFrançois Tigeot 
5422c84b0b6SFrançois Tigeot 	linear_offset = y * fb->pitches[0] + x * pixel_size;
5432c84b0b6SFrançois Tigeot 	sprsurf_offset =
544*a05eeebfSFrançois Tigeot 		intel_gen4_compute_page_offset(dev_priv,
545*a05eeebfSFrançois Tigeot 					       &x, &y, obj->tiling_mode,
5462c84b0b6SFrançois Tigeot 					       pixel_size, fb->pitches[0]);
5472c84b0b6SFrançois Tigeot 	linear_offset -= sprsurf_offset;
5482c84b0b6SFrançois Tigeot 
5492c9916cdSFrançois Tigeot 	if (plane->state->rotation == BIT(DRM_ROTATE_180)) {
5501b13d190SFrançois Tigeot 		sprctl |= SPRITE_ROTATE_180;
5511b13d190SFrançois Tigeot 
5521b13d190SFrançois Tigeot 		/* HSW and BDW does this automagically in hardware */
5531b13d190SFrançois Tigeot 		if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
5541b13d190SFrançois Tigeot 			x += src_w;
5551b13d190SFrançois Tigeot 			y += src_h;
5561b13d190SFrançois Tigeot 			linear_offset += src_h * fb->pitches[0] +
5571b13d190SFrançois Tigeot 				src_w * pixel_size;
5581b13d190SFrançois Tigeot 		}
5591b13d190SFrançois Tigeot 	}
5601b13d190SFrançois Tigeot 
561477eb7f9SFrançois Tigeot 	if (key->flags) {
562477eb7f9SFrançois Tigeot 		I915_WRITE(SPRKEYVAL(pipe), key->min_value);
563477eb7f9SFrançois Tigeot 		I915_WRITE(SPRKEYMAX(pipe), key->max_value);
564477eb7f9SFrançois Tigeot 		I915_WRITE(SPRKEYMSK(pipe), key->channel_mask);
565477eb7f9SFrançois Tigeot 	}
566477eb7f9SFrançois Tigeot 
567477eb7f9SFrançois Tigeot 	if (key->flags & I915_SET_COLORKEY_DESTINATION)
568477eb7f9SFrançois Tigeot 		sprctl |= SPRITE_DEST_KEY;
569477eb7f9SFrançois Tigeot 	else if (key->flags & I915_SET_COLORKEY_SOURCE)
570477eb7f9SFrançois Tigeot 		sprctl |= SPRITE_SOURCE_KEY;
571477eb7f9SFrançois Tigeot 
572ba55f2f5SFrançois Tigeot 	I915_WRITE(SPRSTRIDE(pipe), fb->pitches[0]);
573ba55f2f5SFrançois Tigeot 	I915_WRITE(SPRPOS(pipe), (crtc_y << 16) | crtc_x);
574ba55f2f5SFrançois Tigeot 
5752c84b0b6SFrançois Tigeot 	/* HSW consolidates SPRTILEOFF and SPRLINOFF into a single SPROFFSET
5762c84b0b6SFrançois Tigeot 	 * register */
5779edbd4a0SFrançois Tigeot 	if (IS_HASWELL(dev) || IS_BROADWELL(dev))
5782c84b0b6SFrançois Tigeot 		I915_WRITE(SPROFFSET(pipe), (y << 16) | x);
5792c84b0b6SFrançois Tigeot 	else if (obj->tiling_mode != I915_TILING_NONE)
5802c84b0b6SFrançois Tigeot 		I915_WRITE(SPRTILEOFF(pipe), (y << 16) | x);
5812c84b0b6SFrançois Tigeot 	else
5822c84b0b6SFrançois Tigeot 		I915_WRITE(SPRLINOFF(pipe), linear_offset);
5832c84b0b6SFrançois Tigeot 
584e3adcf8fSFrançois Tigeot 	I915_WRITE(SPRSIZE(pipe), (crtc_h << 16) | crtc_w);
5852c84b0b6SFrançois Tigeot 	if (intel_plane->can_scale)
586e3adcf8fSFrançois Tigeot 		I915_WRITE(SPRSCALE(pipe), sprscale);
587e3adcf8fSFrançois Tigeot 	I915_WRITE(SPRCTL(pipe), sprctl);
5889edbd4a0SFrançois Tigeot 	I915_WRITE(SPRSURF(pipe),
5899edbd4a0SFrançois Tigeot 		   i915_gem_obj_ggtt_offset(obj) + sprsurf_offset);
59019c468b4SFrançois Tigeot 	POSTING_READ(SPRSURF(pipe));
591e3adcf8fSFrançois Tigeot }
592e3adcf8fSFrançois Tigeot 
593e3adcf8fSFrançois Tigeot static void
594*a05eeebfSFrançois Tigeot ivb_disable_plane(struct drm_plane *plane, struct drm_crtc *crtc)
595e3adcf8fSFrançois Tigeot {
596e3adcf8fSFrançois Tigeot 	struct drm_device *dev = plane->dev;
597e3adcf8fSFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
598e3adcf8fSFrançois Tigeot 	struct intel_plane *intel_plane = to_intel_plane(plane);
599e3adcf8fSFrançois Tigeot 	int pipe = intel_plane->pipe;
600ba55f2f5SFrançois Tigeot 
601e3adcf8fSFrançois Tigeot 	I915_WRITE(SPRCTL(pipe), I915_READ(SPRCTL(pipe)) & ~SPRITE_ENABLE);
602e3adcf8fSFrançois Tigeot 	/* Can't leave the scaler enabled... */
6032c84b0b6SFrançois Tigeot 	if (intel_plane->can_scale)
604e3adcf8fSFrançois Tigeot 		I915_WRITE(SPRSCALE(pipe), 0);
605ba55f2f5SFrançois Tigeot 
60619c468b4SFrançois Tigeot 	I915_WRITE(SPRSURF(pipe), 0);
60719c468b4SFrançois Tigeot 	POSTING_READ(SPRSURF(pipe));
608e3adcf8fSFrançois Tigeot }
609e3adcf8fSFrançois Tigeot 
610e3adcf8fSFrançois Tigeot static void
6119edbd4a0SFrançois Tigeot ilk_update_plane(struct drm_plane *plane, struct drm_crtc *crtc,
6129edbd4a0SFrançois Tigeot 		 struct drm_framebuffer *fb,
613477eb7f9SFrançois Tigeot 		 int crtc_x, int crtc_y,
614e3adcf8fSFrançois Tigeot 		 unsigned int crtc_w, unsigned int crtc_h,
615e3adcf8fSFrançois Tigeot 		 uint32_t x, uint32_t y,
616e3adcf8fSFrançois Tigeot 		 uint32_t src_w, uint32_t src_h)
617e3adcf8fSFrançois Tigeot {
618e3adcf8fSFrançois Tigeot 	struct drm_device *dev = plane->dev;
619e3adcf8fSFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
620e3adcf8fSFrançois Tigeot 	struct intel_plane *intel_plane = to_intel_plane(plane);
621477eb7f9SFrançois Tigeot 	struct drm_i915_gem_object *obj = intel_fb_obj(fb);
6222c84b0b6SFrançois Tigeot 	int pipe = intel_plane->pipe;
6232c84b0b6SFrançois Tigeot 	unsigned long dvssurf_offset, linear_offset;
6242c84b0b6SFrançois Tigeot 	u32 dvscntr, dvsscale;
6252c84b0b6SFrançois Tigeot 	int pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
626*a05eeebfSFrançois Tigeot 	const struct drm_intel_sprite_colorkey *key =
627*a05eeebfSFrançois Tigeot 		&to_intel_plane_state(plane->state)->ckey;
628e3adcf8fSFrançois Tigeot 
629477eb7f9SFrançois Tigeot 	dvscntr = DVS_ENABLE;
630e3adcf8fSFrançois Tigeot 
631e3adcf8fSFrançois Tigeot 	switch (fb->pixel_format) {
632e3adcf8fSFrançois Tigeot 	case DRM_FORMAT_XBGR8888:
633e3adcf8fSFrançois Tigeot 		dvscntr |= DVS_FORMAT_RGBX888 | DVS_RGB_ORDER_XBGR;
634e3adcf8fSFrançois Tigeot 		break;
635e3adcf8fSFrançois Tigeot 	case DRM_FORMAT_XRGB8888:
636e3adcf8fSFrançois Tigeot 		dvscntr |= DVS_FORMAT_RGBX888;
637e3adcf8fSFrançois Tigeot 		break;
638e3adcf8fSFrançois Tigeot 	case DRM_FORMAT_YUYV:
639e3adcf8fSFrançois Tigeot 		dvscntr |= DVS_FORMAT_YUV422 | DVS_YUV_ORDER_YUYV;
640e3adcf8fSFrançois Tigeot 		break;
641e3adcf8fSFrançois Tigeot 	case DRM_FORMAT_YVYU:
642e3adcf8fSFrançois Tigeot 		dvscntr |= DVS_FORMAT_YUV422 | DVS_YUV_ORDER_YVYU;
643e3adcf8fSFrançois Tigeot 		break;
644e3adcf8fSFrançois Tigeot 	case DRM_FORMAT_UYVY:
645e3adcf8fSFrançois Tigeot 		dvscntr |= DVS_FORMAT_YUV422 | DVS_YUV_ORDER_UYVY;
646e3adcf8fSFrançois Tigeot 		break;
647e3adcf8fSFrançois Tigeot 	case DRM_FORMAT_VYUY:
648e3adcf8fSFrançois Tigeot 		dvscntr |= DVS_FORMAT_YUV422 | DVS_YUV_ORDER_VYUY;
649e3adcf8fSFrançois Tigeot 		break;
650e3adcf8fSFrançois Tigeot 	default:
6512c84b0b6SFrançois Tigeot 		BUG();
652e3adcf8fSFrançois Tigeot 	}
653e3adcf8fSFrançois Tigeot 
6549edbd4a0SFrançois Tigeot 	/*
6559edbd4a0SFrançois Tigeot 	 * Enable gamma to match primary/cursor plane behaviour.
6569edbd4a0SFrançois Tigeot 	 * FIXME should be user controllable via propertiesa.
6579edbd4a0SFrançois Tigeot 	 */
6589edbd4a0SFrançois Tigeot 	dvscntr |= DVS_GAMMA_ENABLE;
6599edbd4a0SFrançois Tigeot 
660e3adcf8fSFrançois Tigeot 	if (obj->tiling_mode != I915_TILING_NONE)
661e3adcf8fSFrançois Tigeot 		dvscntr |= DVS_TILED;
662e3adcf8fSFrançois Tigeot 
6632c84b0b6SFrançois Tigeot 	if (IS_GEN6(dev))
6642c84b0b6SFrançois Tigeot 		dvscntr |= DVS_TRICKLE_FEED_DISABLE; /* must disable */
665e3adcf8fSFrançois Tigeot 
66624edb884SFrançois Tigeot 	intel_update_sprite_watermarks(plane, crtc, src_w, src_h,
66724edb884SFrançois Tigeot 				       pixel_size, true,
6689edbd4a0SFrançois Tigeot 				       src_w != crtc_w || src_h != crtc_h);
6699edbd4a0SFrançois Tigeot 
670e3adcf8fSFrançois Tigeot 	/* Sizes are 0 based */
671e3adcf8fSFrançois Tigeot 	src_w--;
672e3adcf8fSFrançois Tigeot 	src_h--;
673e3adcf8fSFrançois Tigeot 	crtc_w--;
674e3adcf8fSFrançois Tigeot 	crtc_h--;
675e3adcf8fSFrançois Tigeot 
6762c84b0b6SFrançois Tigeot 	dvsscale = 0;
6779edbd4a0SFrançois Tigeot 	if (crtc_w != src_w || crtc_h != src_h)
678e3adcf8fSFrançois Tigeot 		dvsscale = DVS_SCALE_ENABLE | (src_w << 16) | src_h;
679e3adcf8fSFrançois Tigeot 
6802c84b0b6SFrançois Tigeot 	linear_offset = y * fb->pitches[0] + x * pixel_size;
6812c84b0b6SFrançois Tigeot 	dvssurf_offset =
682*a05eeebfSFrançois Tigeot 		intel_gen4_compute_page_offset(dev_priv,
683*a05eeebfSFrançois Tigeot 					       &x, &y, obj->tiling_mode,
6842c84b0b6SFrançois Tigeot 					       pixel_size, fb->pitches[0]);
6852c84b0b6SFrançois Tigeot 	linear_offset -= dvssurf_offset;
6862c84b0b6SFrançois Tigeot 
6872c9916cdSFrançois Tigeot 	if (plane->state->rotation == BIT(DRM_ROTATE_180)) {
6881b13d190SFrançois Tigeot 		dvscntr |= DVS_ROTATE_180;
6891b13d190SFrançois Tigeot 
6901b13d190SFrançois Tigeot 		x += src_w;
6911b13d190SFrançois Tigeot 		y += src_h;
6921b13d190SFrançois Tigeot 		linear_offset += src_h * fb->pitches[0] + src_w * pixel_size;
6931b13d190SFrançois Tigeot 	}
6941b13d190SFrançois Tigeot 
695477eb7f9SFrançois Tigeot 	if (key->flags) {
696477eb7f9SFrançois Tigeot 		I915_WRITE(DVSKEYVAL(pipe), key->min_value);
697477eb7f9SFrançois Tigeot 		I915_WRITE(DVSKEYMAX(pipe), key->max_value);
698477eb7f9SFrançois Tigeot 		I915_WRITE(DVSKEYMSK(pipe), key->channel_mask);
699477eb7f9SFrançois Tigeot 	}
700477eb7f9SFrançois Tigeot 
701477eb7f9SFrançois Tigeot 	if (key->flags & I915_SET_COLORKEY_DESTINATION)
702477eb7f9SFrançois Tigeot 		dvscntr |= DVS_DEST_KEY;
703477eb7f9SFrançois Tigeot 	else if (key->flags & I915_SET_COLORKEY_SOURCE)
704477eb7f9SFrançois Tigeot 		dvscntr |= DVS_SOURCE_KEY;
705477eb7f9SFrançois Tigeot 
706ba55f2f5SFrançois Tigeot 	I915_WRITE(DVSSTRIDE(pipe), fb->pitches[0]);
707ba55f2f5SFrançois Tigeot 	I915_WRITE(DVSPOS(pipe), (crtc_y << 16) | crtc_x);
708ba55f2f5SFrançois Tigeot 
7092c84b0b6SFrançois Tigeot 	if (obj->tiling_mode != I915_TILING_NONE)
7102c84b0b6SFrançois Tigeot 		I915_WRITE(DVSTILEOFF(pipe), (y << 16) | x);
7112c84b0b6SFrançois Tigeot 	else
7122c84b0b6SFrançois Tigeot 		I915_WRITE(DVSLINOFF(pipe), linear_offset);
7132c84b0b6SFrançois Tigeot 
714e3adcf8fSFrançois Tigeot 	I915_WRITE(DVSSIZE(pipe), (crtc_h << 16) | crtc_w);
715e3adcf8fSFrançois Tigeot 	I915_WRITE(DVSSCALE(pipe), dvsscale);
716e3adcf8fSFrançois Tigeot 	I915_WRITE(DVSCNTR(pipe), dvscntr);
7179edbd4a0SFrançois Tigeot 	I915_WRITE(DVSSURF(pipe),
7189edbd4a0SFrançois Tigeot 		   i915_gem_obj_ggtt_offset(obj) + dvssurf_offset);
71919c468b4SFrançois Tigeot 	POSTING_READ(DVSSURF(pipe));
720e3adcf8fSFrançois Tigeot }
721e3adcf8fSFrançois Tigeot 
722e3adcf8fSFrançois Tigeot static void
723*a05eeebfSFrançois Tigeot ilk_disable_plane(struct drm_plane *plane, struct drm_crtc *crtc)
724e3adcf8fSFrançois Tigeot {
725e3adcf8fSFrançois Tigeot 	struct drm_device *dev = plane->dev;
726e3adcf8fSFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
727e3adcf8fSFrançois Tigeot 	struct intel_plane *intel_plane = to_intel_plane(plane);
728e3adcf8fSFrançois Tigeot 	int pipe = intel_plane->pipe;
729ba55f2f5SFrançois Tigeot 
730477eb7f9SFrançois Tigeot 	I915_WRITE(DVSCNTR(pipe), 0);
731e3adcf8fSFrançois Tigeot 	/* Disable the scaler */
732e3adcf8fSFrançois Tigeot 	I915_WRITE(DVSSCALE(pipe), 0);
733477eb7f9SFrançois Tigeot 
7349edbd4a0SFrançois Tigeot 	I915_WRITE(DVSSURF(pipe), 0);
73519c468b4SFrançois Tigeot 	POSTING_READ(DVSSURF(pipe));
7369edbd4a0SFrançois Tigeot }
7379edbd4a0SFrançois Tigeot 
738e3adcf8fSFrançois Tigeot static int
7392c9916cdSFrançois Tigeot intel_check_sprite_plane(struct drm_plane *plane,
740*a05eeebfSFrançois Tigeot 			 struct intel_crtc_state *crtc_state,
7412c9916cdSFrançois Tigeot 			 struct intel_plane_state *state)
742e3adcf8fSFrançois Tigeot {
74319c468b4SFrançois Tigeot 	struct drm_device *dev = plane->dev;
744*a05eeebfSFrançois Tigeot 	struct drm_crtc *crtc = state->base.crtc;
745*a05eeebfSFrançois Tigeot 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
746e3adcf8fSFrançois Tigeot 	struct intel_plane *intel_plane = to_intel_plane(plane);
7472c9916cdSFrançois Tigeot 	struct drm_framebuffer *fb = state->base.fb;
7489edbd4a0SFrançois Tigeot 	int crtc_x, crtc_y;
7499edbd4a0SFrançois Tigeot 	unsigned int crtc_w, crtc_h;
7509edbd4a0SFrançois Tigeot 	uint32_t src_x, src_y, src_w, src_h;
7512c9916cdSFrançois Tigeot 	struct drm_rect *src = &state->src;
7522c9916cdSFrançois Tigeot 	struct drm_rect *dst = &state->dst;
7532c9916cdSFrançois Tigeot 	const struct drm_rect *clip = &state->clip;
7542c9916cdSFrançois Tigeot 	int hscale, vscale;
7552c9916cdSFrançois Tigeot 	int max_scale, min_scale;
75619c468b4SFrançois Tigeot 	bool can_scale;
7572c9916cdSFrançois Tigeot 	int pixel_size;
7582c9916cdSFrançois Tigeot 
7592c9916cdSFrançois Tigeot 	if (!fb) {
7602c9916cdSFrançois Tigeot 		state->visible = false;
761*a05eeebfSFrançois Tigeot 		return 0;
7622c9916cdSFrançois Tigeot 	}
763e3adcf8fSFrançois Tigeot 
764e3adcf8fSFrançois Tigeot 	/* Don't modify another pipe's plane */
7655d0b1887SFrançois Tigeot 	if (intel_plane->pipe != intel_crtc->pipe) {
7665d0b1887SFrançois Tigeot 		DRM_DEBUG_KMS("Wrong plane <-> crtc mapping\n");
767e3adcf8fSFrançois Tigeot 		return -EINVAL;
7685d0b1887SFrançois Tigeot 	}
7695d0b1887SFrançois Tigeot 
7705d0b1887SFrançois Tigeot 	/* FIXME check all gen limits */
7715d0b1887SFrançois Tigeot 	if (fb->width < 3 || fb->height < 3 || fb->pitches[0] > 16384) {
7725d0b1887SFrançois Tigeot 		DRM_DEBUG_KMS("Unsuitable framebuffer for plane\n");
7735d0b1887SFrançois Tigeot 		return -EINVAL;
7745d0b1887SFrançois Tigeot 	}
775e3adcf8fSFrançois Tigeot 
77619c468b4SFrançois Tigeot 	/* setup can_scale, min_scale, max_scale */
77719c468b4SFrançois Tigeot 	if (INTEL_INFO(dev)->gen >= 9) {
77819c468b4SFrançois Tigeot 		/* use scaler when colorkey is not required */
779*a05eeebfSFrançois Tigeot 		if (state->ckey.flags == I915_SET_COLORKEY_NONE) {
78019c468b4SFrançois Tigeot 			can_scale = 1;
78119c468b4SFrançois Tigeot 			min_scale = 1;
78219c468b4SFrançois Tigeot 			max_scale = skl_max_scale(intel_crtc, crtc_state);
78319c468b4SFrançois Tigeot 		} else {
78419c468b4SFrançois Tigeot 			can_scale = 0;
78519c468b4SFrançois Tigeot 			min_scale = DRM_PLANE_HELPER_NO_SCALING;
78619c468b4SFrançois Tigeot 			max_scale = DRM_PLANE_HELPER_NO_SCALING;
78719c468b4SFrançois Tigeot 		}
78819c468b4SFrançois Tigeot 	} else {
78919c468b4SFrançois Tigeot 		can_scale = intel_plane->can_scale;
79019c468b4SFrançois Tigeot 		max_scale = intel_plane->max_downscale << 16;
79119c468b4SFrançois Tigeot 		min_scale = intel_plane->can_scale ? 1 : (1 << 16);
79219c468b4SFrançois Tigeot 	}
79319c468b4SFrançois Tigeot 
794e3adcf8fSFrançois Tigeot 	/*
7955d0b1887SFrançois Tigeot 	 * FIXME the following code does a bunch of fuzzy adjustments to the
7965d0b1887SFrançois Tigeot 	 * coordinates and sizes. We probably need some way to decide whether
7975d0b1887SFrançois Tigeot 	 * more strict checking should be done instead.
798e3adcf8fSFrançois Tigeot 	 */
7992c9916cdSFrançois Tigeot 	drm_rect_rotate(src, fb->width << 16, fb->height << 16,
8002c9916cdSFrançois Tigeot 			state->base.rotation);
8011b13d190SFrançois Tigeot 
8022c9916cdSFrançois Tigeot 	hscale = drm_rect_calc_hscale_relaxed(src, dst, min_scale, max_scale);
8035d0b1887SFrançois Tigeot 	BUG_ON(hscale < 0);
804e3adcf8fSFrançois Tigeot 
8052c9916cdSFrançois Tigeot 	vscale = drm_rect_calc_vscale_relaxed(src, dst, min_scale, max_scale);
8065d0b1887SFrançois Tigeot 	BUG_ON(vscale < 0);
8075d0b1887SFrançois Tigeot 
8082c9916cdSFrançois Tigeot 	state->visible = drm_rect_clip_scaled(src, dst, clip, hscale, vscale);
8095d0b1887SFrançois Tigeot 
8102c9916cdSFrançois Tigeot 	crtc_x = dst->x1;
8112c9916cdSFrançois Tigeot 	crtc_y = dst->y1;
8122c9916cdSFrançois Tigeot 	crtc_w = drm_rect_width(dst);
8132c9916cdSFrançois Tigeot 	crtc_h = drm_rect_height(dst);
8145d0b1887SFrançois Tigeot 
8152c9916cdSFrançois Tigeot 	if (state->visible) {
8165d0b1887SFrançois Tigeot 		/* check again in case clipping clamped the results */
8172c9916cdSFrançois Tigeot 		hscale = drm_rect_calc_hscale(src, dst, min_scale, max_scale);
8185d0b1887SFrançois Tigeot 		if (hscale < 0) {
8195d0b1887SFrançois Tigeot 			DRM_DEBUG_KMS("Horizontal scaling factor out of limits\n");
8202c9916cdSFrançois Tigeot 			drm_rect_debug_print(src, true);
8212c9916cdSFrançois Tigeot 			drm_rect_debug_print(dst, false);
8225d0b1887SFrançois Tigeot 
8235d0b1887SFrançois Tigeot 			return hscale;
8245d0b1887SFrançois Tigeot 		}
8255d0b1887SFrançois Tigeot 
8262c9916cdSFrançois Tigeot 		vscale = drm_rect_calc_vscale(src, dst, min_scale, max_scale);
8275d0b1887SFrançois Tigeot 		if (vscale < 0) {
8285d0b1887SFrançois Tigeot 			DRM_DEBUG_KMS("Vertical scaling factor out of limits\n");
8292c9916cdSFrançois Tigeot 			drm_rect_debug_print(src, true);
8302c9916cdSFrançois Tigeot 			drm_rect_debug_print(dst, false);
8315d0b1887SFrançois Tigeot 
8325d0b1887SFrançois Tigeot 			return vscale;
8335d0b1887SFrançois Tigeot 		}
8345d0b1887SFrançois Tigeot 
8355d0b1887SFrançois Tigeot 		/* Make the source viewport size an exact multiple of the scaling factors. */
8362c9916cdSFrançois Tigeot 		drm_rect_adjust_size(src,
8372c9916cdSFrançois Tigeot 				     drm_rect_width(dst) * hscale - drm_rect_width(src),
8382c9916cdSFrançois Tigeot 				     drm_rect_height(dst) * vscale - drm_rect_height(src));
8395d0b1887SFrançois Tigeot 
8402c9916cdSFrançois Tigeot 		drm_rect_rotate_inv(src, fb->width << 16, fb->height << 16,
8412c9916cdSFrançois Tigeot 				    state->base.rotation);
8421b13d190SFrançois Tigeot 
8435d0b1887SFrançois Tigeot 		/* sanity check to make sure the src viewport wasn't enlarged */
8442c9916cdSFrançois Tigeot 		WARN_ON(src->x1 < (int) state->base.src_x ||
8452c9916cdSFrançois Tigeot 			src->y1 < (int) state->base.src_y ||
8462c9916cdSFrançois Tigeot 			src->x2 > (int) state->base.src_x + state->base.src_w ||
8472c9916cdSFrançois Tigeot 			src->y2 > (int) state->base.src_y + state->base.src_h);
848e3adcf8fSFrançois Tigeot 
849e3adcf8fSFrançois Tigeot 		/*
8505d0b1887SFrançois Tigeot 		 * Hardware doesn't handle subpixel coordinates.
8515d0b1887SFrançois Tigeot 		 * Adjust to (macro)pixel boundary, but be careful not to
8525d0b1887SFrançois Tigeot 		 * increase the source viewport size, because that could
8535d0b1887SFrançois Tigeot 		 * push the downscaling factor out of bounds.
8542c84b0b6SFrançois Tigeot 		 */
8552c9916cdSFrançois Tigeot 		src_x = src->x1 >> 16;
8562c9916cdSFrançois Tigeot 		src_w = drm_rect_width(src) >> 16;
8572c9916cdSFrançois Tigeot 		src_y = src->y1 >> 16;
8582c9916cdSFrançois Tigeot 		src_h = drm_rect_height(src) >> 16;
8595d0b1887SFrançois Tigeot 
8605d0b1887SFrançois Tigeot 		if (format_is_yuv(fb->pixel_format)) {
8615d0b1887SFrançois Tigeot 			src_x &= ~1;
8625d0b1887SFrançois Tigeot 			src_w &= ~1;
8632c84b0b6SFrançois Tigeot 
8642c84b0b6SFrançois Tigeot 			/*
8655d0b1887SFrançois Tigeot 			 * Must keep src and dst the
8665d0b1887SFrançois Tigeot 			 * same if we can't scale.
867e3adcf8fSFrançois Tigeot 			 */
86819c468b4SFrançois Tigeot 			if (!can_scale)
8695d0b1887SFrançois Tigeot 				crtc_w &= ~1;
8705d0b1887SFrançois Tigeot 
8715d0b1887SFrançois Tigeot 			if (crtc_w == 0)
8722c9916cdSFrançois Tigeot 				state->visible = false;
8735d0b1887SFrançois Tigeot 		}
8745d0b1887SFrançois Tigeot 	}
8755d0b1887SFrançois Tigeot 
8765d0b1887SFrançois Tigeot 	/* Check size restrictions when scaling */
8772c9916cdSFrançois Tigeot 	if (state->visible && (src_w != crtc_w || src_h != crtc_h)) {
8785d0b1887SFrançois Tigeot 		unsigned int width_bytes;
8795d0b1887SFrançois Tigeot 
88019c468b4SFrançois Tigeot 		WARN_ON(!can_scale);
8815d0b1887SFrançois Tigeot 
8825d0b1887SFrançois Tigeot 		/* FIXME interlacing min height is 6 */
8835d0b1887SFrançois Tigeot 
8845d0b1887SFrançois Tigeot 		if (crtc_w < 3 || crtc_h < 3)
8852c9916cdSFrançois Tigeot 			state->visible = false;
8865d0b1887SFrançois Tigeot 
8875d0b1887SFrançois Tigeot 		if (src_w < 3 || src_h < 3)
8882c9916cdSFrançois Tigeot 			state->visible = false;
8895d0b1887SFrançois Tigeot 
8902c9916cdSFrançois Tigeot 		pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
8912c9916cdSFrançois Tigeot 		width_bytes = ((src_x * pixel_size) & 63) +
8922c9916cdSFrançois Tigeot 					src_w * pixel_size;
8935d0b1887SFrançois Tigeot 
89419c468b4SFrançois Tigeot 		if (INTEL_INFO(dev)->gen < 9 && (src_w > 2048 || src_h > 2048 ||
89519c468b4SFrançois Tigeot 		    width_bytes > 4096 || fb->pitches[0] > 4096)) {
8965d0b1887SFrançois Tigeot 			DRM_DEBUG_KMS("Source dimensions exceed hardware limits\n");
897e3adcf8fSFrançois Tigeot 			return -EINVAL;
8985d0b1887SFrançois Tigeot 		}
8995d0b1887SFrançois Tigeot 	}
9005d0b1887SFrançois Tigeot 
9012c9916cdSFrançois Tigeot 	if (state->visible) {
90219c468b4SFrançois Tigeot 		src->x1 = src_x << 16;
90319c468b4SFrançois Tigeot 		src->x2 = (src_x + src_w) << 16;
90419c468b4SFrançois Tigeot 		src->y1 = src_y << 16;
90519c468b4SFrançois Tigeot 		src->y2 = (src_y + src_h) << 16;
9062c9916cdSFrançois Tigeot 	}
907e3adcf8fSFrançois Tigeot 
9082c9916cdSFrançois Tigeot 	dst->x1 = crtc_x;
9092c9916cdSFrançois Tigeot 	dst->x2 = crtc_x + crtc_w;
9102c9916cdSFrançois Tigeot 	dst->y1 = crtc_y;
9112c9916cdSFrançois Tigeot 	dst->y2 = crtc_y + crtc_h;
9122c9916cdSFrançois Tigeot 
9132c9916cdSFrançois Tigeot 	return 0;
9142c9916cdSFrançois Tigeot }
9152c9916cdSFrançois Tigeot 
9162c9916cdSFrançois Tigeot static void
9172c9916cdSFrançois Tigeot intel_commit_sprite_plane(struct drm_plane *plane,
9182c9916cdSFrançois Tigeot 			  struct intel_plane_state *state)
9192c9916cdSFrançois Tigeot {
9202c9916cdSFrançois Tigeot 	struct drm_crtc *crtc = state->base.crtc;
9212c9916cdSFrançois Tigeot 	struct intel_plane *intel_plane = to_intel_plane(plane);
9222c9916cdSFrançois Tigeot 	struct drm_framebuffer *fb = state->base.fb;
9232c9916cdSFrançois Tigeot 
9242c9916cdSFrançois Tigeot 	crtc = crtc ? crtc : plane->crtc;
9252c9916cdSFrançois Tigeot 
926477eb7f9SFrançois Tigeot 	plane->fb = fb;
927e3adcf8fSFrançois Tigeot 
928*a05eeebfSFrançois Tigeot 	if (!crtc->state->active)
929*a05eeebfSFrançois Tigeot 		return;
930*a05eeebfSFrançois Tigeot 
9312c9916cdSFrançois Tigeot 	if (state->visible) {
932477eb7f9SFrançois Tigeot 		intel_plane->update_plane(plane, crtc, fb,
933*a05eeebfSFrançois Tigeot 					  state->dst.x1, state->dst.y1,
934*a05eeebfSFrançois Tigeot 					  drm_rect_width(&state->dst),
935*a05eeebfSFrançois Tigeot 					  drm_rect_height(&state->dst),
936*a05eeebfSFrançois Tigeot 					  state->src.x1 >> 16,
937*a05eeebfSFrançois Tigeot 					  state->src.y1 >> 16,
938*a05eeebfSFrançois Tigeot 					  drm_rect_width(&state->src) >> 16,
939*a05eeebfSFrançois Tigeot 					  drm_rect_height(&state->src) >> 16);
9402c9916cdSFrançois Tigeot 	} else {
941*a05eeebfSFrançois Tigeot 		intel_plane->disable_plane(plane, crtc);
942e3adcf8fSFrançois Tigeot 	}
943e3adcf8fSFrançois Tigeot }
944e3adcf8fSFrançois Tigeot 
945e3adcf8fSFrançois Tigeot int intel_sprite_set_colorkey(struct drm_device *dev, void *data,
946e3adcf8fSFrançois Tigeot 			      struct drm_file *file_priv)
947e3adcf8fSFrançois Tigeot {
948e3adcf8fSFrançois Tigeot 	struct drm_intel_sprite_colorkey *set = data;
949e3adcf8fSFrançois Tigeot 	struct drm_plane *plane;
950*a05eeebfSFrançois Tigeot 	struct drm_plane_state *plane_state;
951*a05eeebfSFrançois Tigeot 	struct drm_atomic_state *state;
952*a05eeebfSFrançois Tigeot 	struct drm_modeset_acquire_ctx ctx;
953e3adcf8fSFrançois Tigeot 	int ret = 0;
954e3adcf8fSFrançois Tigeot 
955e3adcf8fSFrançois Tigeot 	/* Make sure we don't try to enable both src & dest simultaneously */
956e3adcf8fSFrançois Tigeot 	if ((set->flags & (I915_SET_COLORKEY_DESTINATION | I915_SET_COLORKEY_SOURCE)) == (I915_SET_COLORKEY_DESTINATION | I915_SET_COLORKEY_SOURCE))
957e3adcf8fSFrançois Tigeot 		return -EINVAL;
958e3adcf8fSFrançois Tigeot 
959477eb7f9SFrançois Tigeot 	if (IS_VALLEYVIEW(dev) &&
960477eb7f9SFrançois Tigeot 	    set->flags & I915_SET_COLORKEY_DESTINATION)
961477eb7f9SFrançois Tigeot 		return -EINVAL;
962477eb7f9SFrançois Tigeot 
96324edb884SFrançois Tigeot 	plane = drm_plane_find(dev, set->plane_id);
964*a05eeebfSFrançois Tigeot 	if (!plane || plane->type != DRM_PLANE_TYPE_OVERLAY)
965*a05eeebfSFrançois Tigeot 		return -ENOENT;
966*a05eeebfSFrançois Tigeot 
967*a05eeebfSFrançois Tigeot 	drm_modeset_acquire_init(&ctx, 0);
968*a05eeebfSFrançois Tigeot 
969*a05eeebfSFrançois Tigeot 	state = drm_atomic_state_alloc(plane->dev);
970*a05eeebfSFrançois Tigeot 	if (!state) {
971*a05eeebfSFrançois Tigeot 		ret = -ENOMEM;
972*a05eeebfSFrançois Tigeot 		goto out;
973*a05eeebfSFrançois Tigeot 	}
974*a05eeebfSFrançois Tigeot 	state->acquire_ctx = &ctx;
975*a05eeebfSFrançois Tigeot 
976*a05eeebfSFrançois Tigeot 	while (1) {
977*a05eeebfSFrançois Tigeot 		plane_state = drm_atomic_get_plane_state(state, plane);
978*a05eeebfSFrançois Tigeot 		ret = PTR_ERR_OR_ZERO(plane_state);
979*a05eeebfSFrançois Tigeot 		if (!ret) {
980*a05eeebfSFrançois Tigeot 			to_intel_plane_state(plane_state)->ckey = *set;
981*a05eeebfSFrançois Tigeot 			ret = drm_atomic_commit(state);
982e3adcf8fSFrançois Tigeot 		}
983e3adcf8fSFrançois Tigeot 
984*a05eeebfSFrançois Tigeot 		if (ret != -EDEADLK)
985*a05eeebfSFrançois Tigeot 			break;
98619c468b4SFrançois Tigeot 
987*a05eeebfSFrançois Tigeot 		drm_atomic_state_clear(state);
988*a05eeebfSFrançois Tigeot 		drm_modeset_backoff(&ctx);
98919c468b4SFrançois Tigeot 	}
99019c468b4SFrançois Tigeot 
991*a05eeebfSFrançois Tigeot 	if (ret)
992*a05eeebfSFrançois Tigeot 		drm_atomic_state_free(state);
993e3adcf8fSFrançois Tigeot 
994*a05eeebfSFrançois Tigeot out:
995*a05eeebfSFrançois Tigeot 	drm_modeset_drop_locks(&ctx);
996*a05eeebfSFrançois Tigeot 	drm_modeset_acquire_fini(&ctx);
997e3adcf8fSFrançois Tigeot 	return ret;
998e3adcf8fSFrançois Tigeot }
999e3adcf8fSFrançois Tigeot 
100019c468b4SFrançois Tigeot static const uint32_t ilk_plane_formats[] = {
10012c84b0b6SFrançois Tigeot 	DRM_FORMAT_XRGB8888,
10022c84b0b6SFrançois Tigeot 	DRM_FORMAT_YUYV,
10032c84b0b6SFrançois Tigeot 	DRM_FORMAT_YVYU,
10042c84b0b6SFrançois Tigeot 	DRM_FORMAT_UYVY,
10052c84b0b6SFrançois Tigeot 	DRM_FORMAT_VYUY,
10062c84b0b6SFrançois Tigeot };
10072c84b0b6SFrançois Tigeot 
100819c468b4SFrançois Tigeot static const uint32_t snb_plane_formats[] = {
1009e3adcf8fSFrançois Tigeot 	DRM_FORMAT_XBGR8888,
1010e3adcf8fSFrançois Tigeot 	DRM_FORMAT_XRGB8888,
1011e3adcf8fSFrançois Tigeot 	DRM_FORMAT_YUYV,
1012e3adcf8fSFrançois Tigeot 	DRM_FORMAT_YVYU,
1013e3adcf8fSFrançois Tigeot 	DRM_FORMAT_UYVY,
1014e3adcf8fSFrançois Tigeot 	DRM_FORMAT_VYUY,
1015e3adcf8fSFrançois Tigeot };
1016e3adcf8fSFrançois Tigeot 
101719c468b4SFrançois Tigeot static const uint32_t vlv_plane_formats[] = {
10188e26cdf6SFrançois Tigeot 	DRM_FORMAT_RGB565,
10198e26cdf6SFrançois Tigeot 	DRM_FORMAT_ABGR8888,
10208e26cdf6SFrançois Tigeot 	DRM_FORMAT_ARGB8888,
10218e26cdf6SFrançois Tigeot 	DRM_FORMAT_XBGR8888,
10228e26cdf6SFrançois Tigeot 	DRM_FORMAT_XRGB8888,
10238e26cdf6SFrançois Tigeot 	DRM_FORMAT_XBGR2101010,
10248e26cdf6SFrançois Tigeot 	DRM_FORMAT_ABGR2101010,
10258e26cdf6SFrançois Tigeot 	DRM_FORMAT_YUYV,
10268e26cdf6SFrançois Tigeot 	DRM_FORMAT_YVYU,
10278e26cdf6SFrançois Tigeot 	DRM_FORMAT_UYVY,
10288e26cdf6SFrançois Tigeot 	DRM_FORMAT_VYUY,
10298e26cdf6SFrançois Tigeot };
10308e26cdf6SFrançois Tigeot 
10312c9916cdSFrançois Tigeot static uint32_t skl_plane_formats[] = {
10322c9916cdSFrançois Tigeot 	DRM_FORMAT_RGB565,
10332c9916cdSFrançois Tigeot 	DRM_FORMAT_ABGR8888,
10342c9916cdSFrançois Tigeot 	DRM_FORMAT_ARGB8888,
10352c9916cdSFrançois Tigeot 	DRM_FORMAT_XBGR8888,
10362c9916cdSFrançois Tigeot 	DRM_FORMAT_XRGB8888,
10372c9916cdSFrançois Tigeot 	DRM_FORMAT_YUYV,
10382c9916cdSFrançois Tigeot 	DRM_FORMAT_YVYU,
10392c9916cdSFrançois Tigeot 	DRM_FORMAT_UYVY,
10402c9916cdSFrançois Tigeot 	DRM_FORMAT_VYUY,
10412c9916cdSFrançois Tigeot };
10422c9916cdSFrançois Tigeot 
1043e3adcf8fSFrançois Tigeot int
10448e26cdf6SFrançois Tigeot intel_plane_init(struct drm_device *dev, enum i915_pipe pipe, int plane)
1045e3adcf8fSFrançois Tigeot {
1046e3adcf8fSFrançois Tigeot 	struct intel_plane *intel_plane;
10472c9916cdSFrançois Tigeot 	struct intel_plane_state *state;
1048e3adcf8fSFrançois Tigeot 	unsigned long possible_crtcs;
10492c84b0b6SFrançois Tigeot 	const uint32_t *plane_formats;
10502c84b0b6SFrançois Tigeot 	int num_plane_formats;
1051e3adcf8fSFrançois Tigeot 	int ret;
1052e3adcf8fSFrançois Tigeot 
10532c84b0b6SFrançois Tigeot 	if (INTEL_INFO(dev)->gen < 5)
1054e3adcf8fSFrançois Tigeot 		return -ENODEV;
1055e3adcf8fSFrançois Tigeot 
10569edbd4a0SFrançois Tigeot 	intel_plane = kzalloc(sizeof(*intel_plane), GFP_KERNEL);
10572c84b0b6SFrançois Tigeot 	if (!intel_plane)
10582c84b0b6SFrançois Tigeot 		return -ENOMEM;
10592c84b0b6SFrançois Tigeot 
10602c9916cdSFrançois Tigeot 	state = intel_create_plane_state(&intel_plane->base);
10612c9916cdSFrançois Tigeot 	if (!state) {
10622c9916cdSFrançois Tigeot 		kfree(intel_plane);
10632c9916cdSFrançois Tigeot 		return -ENOMEM;
10642c9916cdSFrançois Tigeot 	}
10652c9916cdSFrançois Tigeot 	intel_plane->base.state = &state->base;
10662c9916cdSFrançois Tigeot 
10672c84b0b6SFrançois Tigeot 	switch (INTEL_INFO(dev)->gen) {
10682c84b0b6SFrançois Tigeot 	case 5:
10692c84b0b6SFrançois Tigeot 	case 6:
10702c84b0b6SFrançois Tigeot 		intel_plane->can_scale = true;
10712c84b0b6SFrançois Tigeot 		intel_plane->max_downscale = 16;
10722c84b0b6SFrançois Tigeot 		intel_plane->update_plane = ilk_update_plane;
10732c84b0b6SFrançois Tigeot 		intel_plane->disable_plane = ilk_disable_plane;
1074e3adcf8fSFrançois Tigeot 
1075e3adcf8fSFrançois Tigeot 		if (IS_GEN6(dev)) {
10762c84b0b6SFrançois Tigeot 			plane_formats = snb_plane_formats;
10772c84b0b6SFrançois Tigeot 			num_plane_formats = ARRAY_SIZE(snb_plane_formats);
10782c84b0b6SFrançois Tigeot 		} else {
10792c84b0b6SFrançois Tigeot 			plane_formats = ilk_plane_formats;
10802c84b0b6SFrançois Tigeot 			num_plane_formats = ARRAY_SIZE(ilk_plane_formats);
10812c84b0b6SFrançois Tigeot 		}
10822c84b0b6SFrançois Tigeot 		break;
10832c84b0b6SFrançois Tigeot 
10842c84b0b6SFrançois Tigeot 	case 7:
10859edbd4a0SFrançois Tigeot 	case 8:
10865d0b1887SFrançois Tigeot 		if (IS_IVYBRIDGE(dev)) {
10872c84b0b6SFrançois Tigeot 			intel_plane->can_scale = true;
10885d0b1887SFrançois Tigeot 			intel_plane->max_downscale = 2;
10895d0b1887SFrançois Tigeot 		} else {
10905d0b1887SFrançois Tigeot 			intel_plane->can_scale = false;
10915d0b1887SFrançois Tigeot 			intel_plane->max_downscale = 1;
10925d0b1887SFrançois Tigeot 		}
10938e26cdf6SFrançois Tigeot 
10948e26cdf6SFrançois Tigeot 		if (IS_VALLEYVIEW(dev)) {
10958e26cdf6SFrançois Tigeot 			intel_plane->update_plane = vlv_update_plane;
10968e26cdf6SFrançois Tigeot 			intel_plane->disable_plane = vlv_disable_plane;
10978e26cdf6SFrançois Tigeot 
10988e26cdf6SFrançois Tigeot 			plane_formats = vlv_plane_formats;
10998e26cdf6SFrançois Tigeot 			num_plane_formats = ARRAY_SIZE(vlv_plane_formats);
11008e26cdf6SFrançois Tigeot 		} else {
1101e3adcf8fSFrançois Tigeot 			intel_plane->update_plane = ivb_update_plane;
1102e3adcf8fSFrançois Tigeot 			intel_plane->disable_plane = ivb_disable_plane;
11032c84b0b6SFrançois Tigeot 
11042c84b0b6SFrançois Tigeot 			plane_formats = snb_plane_formats;
11052c84b0b6SFrançois Tigeot 			num_plane_formats = ARRAY_SIZE(snb_plane_formats);
11068e26cdf6SFrançois Tigeot 		}
11072c84b0b6SFrançois Tigeot 		break;
11082c9916cdSFrançois Tigeot 	case 9:
110919c468b4SFrançois Tigeot 		intel_plane->can_scale = true;
11102c9916cdSFrançois Tigeot 		intel_plane->update_plane = skl_update_plane;
11112c9916cdSFrançois Tigeot 		intel_plane->disable_plane = skl_disable_plane;
111219c468b4SFrançois Tigeot 		state->scaler_id = -1;
11132c84b0b6SFrançois Tigeot 
11142c9916cdSFrançois Tigeot 		plane_formats = skl_plane_formats;
11152c9916cdSFrançois Tigeot 		num_plane_formats = ARRAY_SIZE(skl_plane_formats);
11162c9916cdSFrançois Tigeot 		break;
11172c84b0b6SFrançois Tigeot 	default:
1118158486a6SFrançois Tigeot 		kfree(intel_plane);
11192c84b0b6SFrançois Tigeot 		return -ENODEV;
1120e3adcf8fSFrançois Tigeot 	}
1121e3adcf8fSFrançois Tigeot 
1122e3adcf8fSFrançois Tigeot 	intel_plane->pipe = pipe;
11238e26cdf6SFrançois Tigeot 	intel_plane->plane = plane;
1124*a05eeebfSFrançois Tigeot 	intel_plane->frontbuffer_bit = INTEL_FRONTBUFFER_SPRITE(pipe);
11252c9916cdSFrançois Tigeot 	intel_plane->check_plane = intel_check_sprite_plane;
11262c9916cdSFrançois Tigeot 	intel_plane->commit_plane = intel_commit_sprite_plane;
1127e3adcf8fSFrançois Tigeot 	possible_crtcs = (1 << pipe);
11281b13d190SFrançois Tigeot 	ret = drm_universal_plane_init(dev, &intel_plane->base, possible_crtcs,
11292c84b0b6SFrançois Tigeot 				       &intel_plane_funcs,
11302c84b0b6SFrançois Tigeot 				       plane_formats, num_plane_formats,
11311b13d190SFrançois Tigeot 				       DRM_PLANE_TYPE_OVERLAY);
11321b13d190SFrançois Tigeot 	if (ret) {
1133158486a6SFrançois Tigeot 		kfree(intel_plane);
11341b13d190SFrançois Tigeot 		goto out;
11351b13d190SFrançois Tigeot 	}
1136e3adcf8fSFrançois Tigeot 
113719c468b4SFrançois Tigeot 	intel_create_rotation_property(dev, intel_plane);
11382c9916cdSFrançois Tigeot 
11392c9916cdSFrançois Tigeot 	drm_plane_helper_add(&intel_plane->base, &intel_plane_helper_funcs);
11401b13d190SFrançois Tigeot 
11411b13d190SFrançois Tigeot out:
1142e3adcf8fSFrançois Tigeot 	return ret;
1143e3adcf8fSFrançois Tigeot }
1144