xref: /dflybsd-src/sys/dev/drm/i915/intel_sprite.c (revision aee94f86171368465eaa15d649743f13cea3363a)
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 
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
1812c9916cdSFrançois Tigeot skl_update_plane(struct drm_plane *drm_plane, struct drm_crtc *crtc,
1822c9916cdSFrançois Tigeot 		 struct drm_framebuffer *fb,
183477eb7f9SFrançois Tigeot 		 int crtc_x, int crtc_y,
1842c9916cdSFrançois Tigeot 		 unsigned int crtc_w, unsigned int crtc_h,
1852c9916cdSFrançois Tigeot 		 uint32_t x, uint32_t y,
1862c9916cdSFrançois Tigeot 		 uint32_t src_w, uint32_t src_h)
1872c9916cdSFrançois Tigeot {
1882c9916cdSFrançois Tigeot 	struct drm_device *dev = drm_plane->dev;
1892c9916cdSFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
1902c9916cdSFrançois Tigeot 	struct intel_plane *intel_plane = to_intel_plane(drm_plane);
191477eb7f9SFrançois Tigeot 	struct drm_i915_gem_object *obj = intel_fb_obj(fb);
1922c9916cdSFrançois Tigeot 	const int pipe = intel_plane->pipe;
1932c9916cdSFrançois Tigeot 	const int plane = intel_plane->plane + 1;
19419c468b4SFrançois Tigeot 	u32 plane_ctl, stride_div, stride;
195a05eeebfSFrançois Tigeot 	const struct drm_intel_sprite_colorkey *key =
196a05eeebfSFrançois Tigeot 		&to_intel_plane_state(drm_plane->state)->ckey;
197*aee94f86SFrançois Tigeot 	u32 surf_addr;
19819c468b4SFrançois Tigeot 	u32 tile_height, plane_offset, plane_size;
19919c468b4SFrançois Tigeot 	unsigned int rotation;
20019c468b4SFrançois Tigeot 	int x_offset, y_offset;
20119c468b4SFrançois Tigeot 	struct intel_crtc_state *crtc_state = to_intel_crtc(crtc)->config;
20219c468b4SFrançois Tigeot 	int scaler_id;
2032c9916cdSFrançois Tigeot 
204477eb7f9SFrançois Tigeot 	plane_ctl = PLANE_CTL_ENABLE |
205352ff8bdSFrançois Tigeot 		PLANE_CTL_PIPE_GAMMA_ENABLE |
206477eb7f9SFrançois Tigeot 		PLANE_CTL_PIPE_CSC_ENABLE;
2072c9916cdSFrançois Tigeot 
20819c468b4SFrançois Tigeot 	plane_ctl |= skl_plane_ctl_format(fb->pixel_format);
20919c468b4SFrançois Tigeot 	plane_ctl |= skl_plane_ctl_tiling(fb->modifier[0]);
2102c9916cdSFrançois Tigeot 
21119c468b4SFrançois Tigeot 	rotation = drm_plane->state->rotation;
21219c468b4SFrançois Tigeot 	plane_ctl |= skl_plane_ctl_rotation(rotation);
2132c9916cdSFrançois Tigeot 
214477eb7f9SFrançois Tigeot 	stride_div = intel_fb_stride_alignment(dev, fb->modifier[0],
215477eb7f9SFrançois Tigeot 					       fb->pixel_format);
216477eb7f9SFrançois Tigeot 
21719c468b4SFrançois Tigeot 	scaler_id = to_intel_plane_state(drm_plane->state)->scaler_id;
21819c468b4SFrançois Tigeot 
2192c9916cdSFrançois Tigeot 	/* Sizes are 0 based */
2202c9916cdSFrançois Tigeot 	src_w--;
2212c9916cdSFrançois Tigeot 	src_h--;
2222c9916cdSFrançois Tigeot 	crtc_w--;
2232c9916cdSFrançois Tigeot 	crtc_h--;
2242c9916cdSFrançois Tigeot 
225477eb7f9SFrançois Tigeot 	if (key->flags) {
226477eb7f9SFrançois Tigeot 		I915_WRITE(PLANE_KEYVAL(pipe, plane), key->min_value);
227477eb7f9SFrançois Tigeot 		I915_WRITE(PLANE_KEYMAX(pipe, plane), key->max_value);
228477eb7f9SFrançois Tigeot 		I915_WRITE(PLANE_KEYMSK(pipe, plane), key->channel_mask);
229477eb7f9SFrançois Tigeot 	}
230477eb7f9SFrançois Tigeot 
231477eb7f9SFrançois Tigeot 	if (key->flags & I915_SET_COLORKEY_DESTINATION)
232477eb7f9SFrançois Tigeot 		plane_ctl |= PLANE_CTL_KEY_ENABLE_DESTINATION;
233477eb7f9SFrançois Tigeot 	else if (key->flags & I915_SET_COLORKEY_SOURCE)
234477eb7f9SFrançois Tigeot 		plane_ctl |= PLANE_CTL_KEY_ENABLE_SOURCE;
235477eb7f9SFrançois Tigeot 
236352ff8bdSFrançois Tigeot 	surf_addr = intel_plane_obj_offset(intel_plane, obj, 0);
237477eb7f9SFrançois Tigeot 
23819c468b4SFrançois Tigeot 	if (intel_rotation_90_or_270(rotation)) {
23919c468b4SFrançois Tigeot 		/* stride: Surface height in tiles */
24019c468b4SFrançois Tigeot 		tile_height = intel_tile_height(dev, fb->pixel_format,
241352ff8bdSFrançois Tigeot 						fb->modifier[0], 0);
24219c468b4SFrançois Tigeot 		stride = DIV_ROUND_UP(fb->height, tile_height);
24319c468b4SFrançois Tigeot 		plane_size = (src_w << 16) | src_h;
24419c468b4SFrançois Tigeot 		x_offset = stride * tile_height - y - (src_h + 1);
24519c468b4SFrançois Tigeot 		y_offset = x;
24619c468b4SFrançois Tigeot 	} else {
24719c468b4SFrançois Tigeot 		stride = fb->pitches[0] / stride_div;
24819c468b4SFrançois Tigeot 		plane_size = (src_h << 16) | src_w;
24919c468b4SFrançois Tigeot 		x_offset = x;
25019c468b4SFrançois Tigeot 		y_offset = y;
25119c468b4SFrançois Tigeot 	}
25219c468b4SFrançois Tigeot 	plane_offset = y_offset << 16 | x_offset;
25319c468b4SFrançois Tigeot 
25419c468b4SFrançois Tigeot 	I915_WRITE(PLANE_OFFSET(pipe, plane), plane_offset);
25519c468b4SFrançois Tigeot 	I915_WRITE(PLANE_STRIDE(pipe, plane), stride);
25619c468b4SFrançois Tigeot 	I915_WRITE(PLANE_SIZE(pipe, plane), plane_size);
25719c468b4SFrançois Tigeot 
25819c468b4SFrançois Tigeot 	/* program plane scaler */
25919c468b4SFrançois Tigeot 	if (scaler_id >= 0) {
26019c468b4SFrançois Tigeot 		uint32_t ps_ctrl = 0;
26119c468b4SFrançois Tigeot 
26219c468b4SFrançois Tigeot 		DRM_DEBUG_KMS("plane = %d PS_PLANE_SEL(plane) = 0x%x\n", plane,
26319c468b4SFrançois Tigeot 			PS_PLANE_SEL(plane));
26419c468b4SFrançois Tigeot 		ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(plane) |
26519c468b4SFrançois Tigeot 			crtc_state->scaler_state.scalers[scaler_id].mode;
26619c468b4SFrançois Tigeot 		I915_WRITE(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
26719c468b4SFrançois Tigeot 		I915_WRITE(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
26819c468b4SFrançois Tigeot 		I915_WRITE(SKL_PS_WIN_POS(pipe, scaler_id), (crtc_x << 16) | crtc_y);
26919c468b4SFrançois Tigeot 		I915_WRITE(SKL_PS_WIN_SZ(pipe, scaler_id),
27019c468b4SFrançois Tigeot 			((crtc_w + 1) << 16)|(crtc_h + 1));
27119c468b4SFrançois Tigeot 
27219c468b4SFrançois Tigeot 		I915_WRITE(PLANE_POS(pipe, plane), 0);
27319c468b4SFrançois Tigeot 	} else {
2742c9916cdSFrançois Tigeot 		I915_WRITE(PLANE_POS(pipe, plane), (crtc_y << 16) | crtc_x);
27519c468b4SFrançois Tigeot 	}
27619c468b4SFrançois Tigeot 
2772c9916cdSFrançois Tigeot 	I915_WRITE(PLANE_CTL(pipe, plane), plane_ctl);
278477eb7f9SFrançois Tigeot 	I915_WRITE(PLANE_SURF(pipe, plane), surf_addr);
2792c9916cdSFrançois Tigeot 	POSTING_READ(PLANE_SURF(pipe, plane));
2802c9916cdSFrançois Tigeot }
2812c9916cdSFrançois Tigeot 
2822c9916cdSFrançois Tigeot static void
283a05eeebfSFrançois Tigeot skl_disable_plane(struct drm_plane *dplane, struct drm_crtc *crtc)
2842c9916cdSFrançois Tigeot {
28519c468b4SFrançois Tigeot 	struct drm_device *dev = dplane->dev;
2862c9916cdSFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
28719c468b4SFrançois Tigeot 	struct intel_plane *intel_plane = to_intel_plane(dplane);
2882c9916cdSFrançois Tigeot 	const int pipe = intel_plane->pipe;
2892c9916cdSFrançois Tigeot 	const int plane = intel_plane->plane + 1;
2902c9916cdSFrançois Tigeot 
291477eb7f9SFrançois Tigeot 	I915_WRITE(PLANE_CTL(pipe, plane), 0);
2922c9916cdSFrançois Tigeot 
293477eb7f9SFrançois Tigeot 	I915_WRITE(PLANE_SURF(pipe, plane), 0);
294477eb7f9SFrançois Tigeot 	POSTING_READ(PLANE_SURF(pipe, plane));
2952c9916cdSFrançois Tigeot }
2962c9916cdSFrançois Tigeot 
2972c9916cdSFrançois Tigeot static void
2982c9916cdSFrançois Tigeot chv_update_csc(struct intel_plane *intel_plane, uint32_t format)
2992c9916cdSFrançois Tigeot {
3002c9916cdSFrançois Tigeot 	struct drm_i915_private *dev_priv = intel_plane->base.dev->dev_private;
3012c9916cdSFrançois Tigeot 	int plane = intel_plane->plane;
3022c9916cdSFrançois Tigeot 
3032c9916cdSFrançois Tigeot 	/* Seems RGB data bypasses the CSC always */
3042c9916cdSFrançois Tigeot 	if (!format_is_yuv(format))
3052c9916cdSFrançois Tigeot 		return;
3062c9916cdSFrançois Tigeot 
3072c9916cdSFrançois Tigeot 	/*
3082c9916cdSFrançois Tigeot 	 * BT.601 limited range YCbCr -> full range RGB
3092c9916cdSFrançois Tigeot 	 *
3102c9916cdSFrançois Tigeot 	 * |r|   | 6537 4769     0|   |cr  |
3112c9916cdSFrançois Tigeot 	 * |g| = |-3330 4769 -1605| x |y-64|
3122c9916cdSFrançois Tigeot 	 * |b|   |    0 4769  8263|   |cb  |
3132c9916cdSFrançois Tigeot 	 *
3142c9916cdSFrançois Tigeot 	 * Cb and Cr apparently come in as signed already, so no
3152c9916cdSFrançois Tigeot 	 * need for any offset. For Y we need to remove the offset.
3162c9916cdSFrançois Tigeot 	 */
3172c9916cdSFrançois Tigeot 	I915_WRITE(SPCSCYGOFF(plane), SPCSC_OOFF(0) | SPCSC_IOFF(-64));
3182c9916cdSFrançois Tigeot 	I915_WRITE(SPCSCCBOFF(plane), SPCSC_OOFF(0) | SPCSC_IOFF(0));
3192c9916cdSFrançois Tigeot 	I915_WRITE(SPCSCCROFF(plane), SPCSC_OOFF(0) | SPCSC_IOFF(0));
3202c9916cdSFrançois Tigeot 
3212c9916cdSFrançois Tigeot 	I915_WRITE(SPCSCC01(plane), SPCSC_C1(4769) | SPCSC_C0(6537));
3222c9916cdSFrançois Tigeot 	I915_WRITE(SPCSCC23(plane), SPCSC_C1(-3330) | SPCSC_C0(0));
3232c9916cdSFrançois Tigeot 	I915_WRITE(SPCSCC45(plane), SPCSC_C1(-1605) | SPCSC_C0(4769));
3242c9916cdSFrançois Tigeot 	I915_WRITE(SPCSCC67(plane), SPCSC_C1(4769) | SPCSC_C0(0));
3252c9916cdSFrançois Tigeot 	I915_WRITE(SPCSCC8(plane), SPCSC_C0(8263));
3262c9916cdSFrançois Tigeot 
3272c9916cdSFrançois Tigeot 	I915_WRITE(SPCSCYGICLAMP(plane), SPCSC_IMAX(940) | SPCSC_IMIN(64));
3282c9916cdSFrançois Tigeot 	I915_WRITE(SPCSCCBICLAMP(plane), SPCSC_IMAX(448) | SPCSC_IMIN(-448));
3292c9916cdSFrançois Tigeot 	I915_WRITE(SPCSCCRICLAMP(plane), SPCSC_IMAX(448) | SPCSC_IMIN(-448));
3302c9916cdSFrançois Tigeot 
3312c9916cdSFrançois Tigeot 	I915_WRITE(SPCSCYGOCLAMP(plane), SPCSC_OMAX(1023) | SPCSC_OMIN(0));
3322c9916cdSFrançois Tigeot 	I915_WRITE(SPCSCCBOCLAMP(plane), SPCSC_OMAX(1023) | SPCSC_OMIN(0));
3332c9916cdSFrançois Tigeot 	I915_WRITE(SPCSCCROCLAMP(plane), SPCSC_OMAX(1023) | SPCSC_OMIN(0));
3342c9916cdSFrançois Tigeot }
3352c9916cdSFrançois Tigeot 
3362c9916cdSFrançois Tigeot static void
3379edbd4a0SFrançois Tigeot vlv_update_plane(struct drm_plane *dplane, struct drm_crtc *crtc,
3389edbd4a0SFrançois Tigeot 		 struct drm_framebuffer *fb,
339477eb7f9SFrançois Tigeot 		 int crtc_x, int crtc_y,
3408e26cdf6SFrançois Tigeot 		 unsigned int crtc_w, unsigned int crtc_h,
3418e26cdf6SFrançois Tigeot 		 uint32_t x, uint32_t y,
3428e26cdf6SFrançois Tigeot 		 uint32_t src_w, uint32_t src_h)
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);
347477eb7f9SFrançois Tigeot 	struct drm_i915_gem_object *obj = intel_fb_obj(fb);
3488e26cdf6SFrançois Tigeot 	int pipe = intel_plane->pipe;
3498e26cdf6SFrançois Tigeot 	int plane = intel_plane->plane;
3508e26cdf6SFrançois Tigeot 	u32 sprctl;
3518e26cdf6SFrançois Tigeot 	unsigned long sprsurf_offset, linear_offset;
3528e26cdf6SFrançois Tigeot 	int pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
353a05eeebfSFrançois Tigeot 	const struct drm_intel_sprite_colorkey *key =
354a05eeebfSFrançois Tigeot 		&to_intel_plane_state(dplane->state)->ckey;
3558e26cdf6SFrançois Tigeot 
356477eb7f9SFrançois Tigeot 	sprctl = SP_ENABLE;
3578e26cdf6SFrançois Tigeot 
3588e26cdf6SFrançois Tigeot 	switch (fb->pixel_format) {
3598e26cdf6SFrançois Tigeot 	case DRM_FORMAT_YUYV:
3608e26cdf6SFrançois Tigeot 		sprctl |= SP_FORMAT_YUV422 | SP_YUV_ORDER_YUYV;
3618e26cdf6SFrançois Tigeot 		break;
3628e26cdf6SFrançois Tigeot 	case DRM_FORMAT_YVYU:
3638e26cdf6SFrançois Tigeot 		sprctl |= SP_FORMAT_YUV422 | SP_YUV_ORDER_YVYU;
3648e26cdf6SFrançois Tigeot 		break;
3658e26cdf6SFrançois Tigeot 	case DRM_FORMAT_UYVY:
3668e26cdf6SFrançois Tigeot 		sprctl |= SP_FORMAT_YUV422 | SP_YUV_ORDER_UYVY;
3678e26cdf6SFrançois Tigeot 		break;
3688e26cdf6SFrançois Tigeot 	case DRM_FORMAT_VYUY:
3698e26cdf6SFrançois Tigeot 		sprctl |= SP_FORMAT_YUV422 | SP_YUV_ORDER_VYUY;
3708e26cdf6SFrançois Tigeot 		break;
3718e26cdf6SFrançois Tigeot 	case DRM_FORMAT_RGB565:
3728e26cdf6SFrançois Tigeot 		sprctl |= SP_FORMAT_BGR565;
3738e26cdf6SFrançois Tigeot 		break;
3748e26cdf6SFrançois Tigeot 	case DRM_FORMAT_XRGB8888:
3758e26cdf6SFrançois Tigeot 		sprctl |= SP_FORMAT_BGRX8888;
3768e26cdf6SFrançois Tigeot 		break;
3778e26cdf6SFrançois Tigeot 	case DRM_FORMAT_ARGB8888:
3788e26cdf6SFrançois Tigeot 		sprctl |= SP_FORMAT_BGRA8888;
3798e26cdf6SFrançois Tigeot 		break;
3808e26cdf6SFrançois Tigeot 	case DRM_FORMAT_XBGR2101010:
3818e26cdf6SFrançois Tigeot 		sprctl |= SP_FORMAT_RGBX1010102;
3828e26cdf6SFrançois Tigeot 		break;
3838e26cdf6SFrançois Tigeot 	case DRM_FORMAT_ABGR2101010:
3848e26cdf6SFrançois Tigeot 		sprctl |= SP_FORMAT_RGBA1010102;
3858e26cdf6SFrançois Tigeot 		break;
3868e26cdf6SFrançois Tigeot 	case DRM_FORMAT_XBGR8888:
3878e26cdf6SFrançois Tigeot 		sprctl |= SP_FORMAT_RGBX8888;
3888e26cdf6SFrançois Tigeot 		break;
3898e26cdf6SFrançois Tigeot 	case DRM_FORMAT_ABGR8888:
3908e26cdf6SFrançois Tigeot 		sprctl |= SP_FORMAT_RGBA8888;
3918e26cdf6SFrançois Tigeot 		break;
3928e26cdf6SFrançois Tigeot 	default:
3938e26cdf6SFrançois Tigeot 		/*
3948e26cdf6SFrançois Tigeot 		 * If we get here one of the upper layers failed to filter
3958e26cdf6SFrançois Tigeot 		 * out the unsupported plane formats
3968e26cdf6SFrançois Tigeot 		 */
3978e26cdf6SFrançois Tigeot 		BUG();
3988e26cdf6SFrançois Tigeot 		break;
3998e26cdf6SFrançois Tigeot 	}
4008e26cdf6SFrançois Tigeot 
4019edbd4a0SFrançois Tigeot 	/*
4029edbd4a0SFrançois Tigeot 	 * Enable gamma to match primary/cursor plane behaviour.
4039edbd4a0SFrançois Tigeot 	 * FIXME should be user controllable via propertiesa.
4049edbd4a0SFrançois Tigeot 	 */
4059edbd4a0SFrançois Tigeot 	sprctl |= SP_GAMMA_ENABLE;
4069edbd4a0SFrançois Tigeot 
4078e26cdf6SFrançois Tigeot 	if (obj->tiling_mode != I915_TILING_NONE)
4088e26cdf6SFrançois Tigeot 		sprctl |= SP_TILED;
4098e26cdf6SFrançois Tigeot 
4108e26cdf6SFrançois Tigeot 	/* Sizes are 0 based */
4118e26cdf6SFrançois Tigeot 	src_w--;
4128e26cdf6SFrançois Tigeot 	src_h--;
4138e26cdf6SFrançois Tigeot 	crtc_w--;
4148e26cdf6SFrançois Tigeot 	crtc_h--;
4158e26cdf6SFrançois Tigeot 
4168e26cdf6SFrançois Tigeot 	linear_offset = y * fb->pitches[0] + x * pixel_size;
417a05eeebfSFrançois Tigeot 	sprsurf_offset = intel_gen4_compute_page_offset(dev_priv,
418a05eeebfSFrançois Tigeot 							&x, &y,
4198e26cdf6SFrançois Tigeot 							obj->tiling_mode,
4208e26cdf6SFrançois Tigeot 							pixel_size,
4218e26cdf6SFrançois Tigeot 							fb->pitches[0]);
4228e26cdf6SFrançois Tigeot 	linear_offset -= sprsurf_offset;
4238e26cdf6SFrançois Tigeot 
4242c9916cdSFrançois Tigeot 	if (dplane->state->rotation == BIT(DRM_ROTATE_180)) {
4251b13d190SFrançois Tigeot 		sprctl |= SP_ROTATE_180;
4261b13d190SFrançois Tigeot 
4271b13d190SFrançois Tigeot 		x += src_w;
4281b13d190SFrançois Tigeot 		y += src_h;
4291b13d190SFrançois Tigeot 		linear_offset += src_h * fb->pitches[0] + src_w * pixel_size;
4301b13d190SFrançois Tigeot 	}
4311b13d190SFrançois Tigeot 
432477eb7f9SFrançois Tigeot 	if (key->flags) {
433477eb7f9SFrançois Tigeot 		I915_WRITE(SPKEYMINVAL(pipe, plane), key->min_value);
434477eb7f9SFrançois Tigeot 		I915_WRITE(SPKEYMAXVAL(pipe, plane), key->max_value);
435477eb7f9SFrançois Tigeot 		I915_WRITE(SPKEYMSK(pipe, plane), key->channel_mask);
436477eb7f9SFrançois Tigeot 	}
437477eb7f9SFrançois Tigeot 
438477eb7f9SFrançois Tigeot 	if (key->flags & I915_SET_COLORKEY_SOURCE)
439477eb7f9SFrançois Tigeot 		sprctl |= SP_SOURCE_KEY;
440477eb7f9SFrançois Tigeot 
4412c9916cdSFrançois Tigeot 	if (IS_CHERRYVIEW(dev) && pipe == PIPE_B)
4422c9916cdSFrançois Tigeot 		chv_update_csc(intel_plane, fb->pixel_format);
4432c9916cdSFrançois Tigeot 
444ba55f2f5SFrançois Tigeot 	I915_WRITE(SPSTRIDE(pipe, plane), fb->pitches[0]);
445ba55f2f5SFrançois Tigeot 	I915_WRITE(SPPOS(pipe, plane), (crtc_y << 16) | crtc_x);
446ba55f2f5SFrançois Tigeot 
4478e26cdf6SFrançois Tigeot 	if (obj->tiling_mode != I915_TILING_NONE)
4488e26cdf6SFrançois Tigeot 		I915_WRITE(SPTILEOFF(pipe, plane), (y << 16) | x);
4498e26cdf6SFrançois Tigeot 	else
4508e26cdf6SFrançois Tigeot 		I915_WRITE(SPLINOFF(pipe, plane), linear_offset);
4518e26cdf6SFrançois Tigeot 
4522c9916cdSFrançois Tigeot 	I915_WRITE(SPCONSTALPHA(pipe, plane), 0);
4532c9916cdSFrançois Tigeot 
4548e26cdf6SFrançois Tigeot 	I915_WRITE(SPSIZE(pipe, plane), (crtc_h << 16) | crtc_w);
4558e26cdf6SFrançois Tigeot 	I915_WRITE(SPCNTR(pipe, plane), sprctl);
4569edbd4a0SFrançois Tigeot 	I915_WRITE(SPSURF(pipe, plane), i915_gem_obj_ggtt_offset(obj) +
4578e26cdf6SFrançois Tigeot 		   sprsurf_offset);
45819c468b4SFrançois Tigeot 	POSTING_READ(SPSURF(pipe, plane));
4598e26cdf6SFrançois Tigeot }
4608e26cdf6SFrançois Tigeot 
4618e26cdf6SFrançois Tigeot static void
462a05eeebfSFrançois Tigeot vlv_disable_plane(struct drm_plane *dplane, struct drm_crtc *crtc)
4638e26cdf6SFrançois Tigeot {
4648e26cdf6SFrançois Tigeot 	struct drm_device *dev = dplane->dev;
4658e26cdf6SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
4668e26cdf6SFrançois Tigeot 	struct intel_plane *intel_plane = to_intel_plane(dplane);
4678e26cdf6SFrançois Tigeot 	int pipe = intel_plane->pipe;
4688e26cdf6SFrançois Tigeot 	int plane = intel_plane->plane;
469ba55f2f5SFrançois Tigeot 
470477eb7f9SFrançois Tigeot 	I915_WRITE(SPCNTR(pipe, plane), 0);
471477eb7f9SFrançois Tigeot 
4729edbd4a0SFrançois Tigeot 	I915_WRITE(SPSURF(pipe, plane), 0);
47319c468b4SFrançois Tigeot 	POSTING_READ(SPSURF(pipe, plane));
4748e26cdf6SFrançois Tigeot }
4758e26cdf6SFrançois Tigeot 
4768e26cdf6SFrançois Tigeot static void
4779edbd4a0SFrançois Tigeot ivb_update_plane(struct drm_plane *plane, struct drm_crtc *crtc,
4789edbd4a0SFrançois Tigeot 		 struct drm_framebuffer *fb,
479477eb7f9SFrançois Tigeot 		 int crtc_x, int crtc_y,
480e3adcf8fSFrançois Tigeot 		 unsigned int crtc_w, unsigned int crtc_h,
481e3adcf8fSFrançois Tigeot 		 uint32_t x, uint32_t y,
482e3adcf8fSFrançois Tigeot 		 uint32_t src_w, uint32_t src_h)
483e3adcf8fSFrançois Tigeot {
484e3adcf8fSFrançois Tigeot 	struct drm_device *dev = plane->dev;
485e3adcf8fSFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
486e3adcf8fSFrançois Tigeot 	struct intel_plane *intel_plane = to_intel_plane(plane);
487477eb7f9SFrançois Tigeot 	struct drm_i915_gem_object *obj = intel_fb_obj(fb);
488477eb7f9SFrançois Tigeot 	enum i915_pipe pipe = intel_plane->pipe;
489e3adcf8fSFrançois Tigeot 	u32 sprctl, sprscale = 0;
4902c84b0b6SFrançois Tigeot 	unsigned long sprsurf_offset, linear_offset;
4912c84b0b6SFrançois Tigeot 	int pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
492a05eeebfSFrançois Tigeot 	const struct drm_intel_sprite_colorkey *key =
493a05eeebfSFrançois Tigeot 		&to_intel_plane_state(plane->state)->ckey;
494e3adcf8fSFrançois Tigeot 
495477eb7f9SFrançois Tigeot 	sprctl = SPRITE_ENABLE;
496e3adcf8fSFrançois Tigeot 
497e3adcf8fSFrançois Tigeot 	switch (fb->pixel_format) {
498e3adcf8fSFrançois Tigeot 	case DRM_FORMAT_XBGR8888:
4992c84b0b6SFrançois Tigeot 		sprctl |= SPRITE_FORMAT_RGBX888 | SPRITE_RGB_ORDER_RGBX;
500e3adcf8fSFrançois Tigeot 		break;
501e3adcf8fSFrançois Tigeot 	case DRM_FORMAT_XRGB8888:
5022c84b0b6SFrançois Tigeot 		sprctl |= SPRITE_FORMAT_RGBX888;
503e3adcf8fSFrançois Tigeot 		break;
504e3adcf8fSFrançois Tigeot 	case DRM_FORMAT_YUYV:
505e3adcf8fSFrançois Tigeot 		sprctl |= SPRITE_FORMAT_YUV422 | SPRITE_YUV_ORDER_YUYV;
506e3adcf8fSFrançois Tigeot 		break;
507e3adcf8fSFrançois Tigeot 	case DRM_FORMAT_YVYU:
508e3adcf8fSFrançois Tigeot 		sprctl |= SPRITE_FORMAT_YUV422 | SPRITE_YUV_ORDER_YVYU;
509e3adcf8fSFrançois Tigeot 		break;
510e3adcf8fSFrançois Tigeot 	case DRM_FORMAT_UYVY:
511e3adcf8fSFrançois Tigeot 		sprctl |= SPRITE_FORMAT_YUV422 | SPRITE_YUV_ORDER_UYVY;
512e3adcf8fSFrançois Tigeot 		break;
513e3adcf8fSFrançois Tigeot 	case DRM_FORMAT_VYUY:
514e3adcf8fSFrançois Tigeot 		sprctl |= SPRITE_FORMAT_YUV422 | SPRITE_YUV_ORDER_VYUY;
515e3adcf8fSFrançois Tigeot 		break;
516e3adcf8fSFrançois Tigeot 	default:
5172c84b0b6SFrançois Tigeot 		BUG();
518e3adcf8fSFrançois Tigeot 	}
519e3adcf8fSFrançois Tigeot 
5209edbd4a0SFrançois Tigeot 	/*
5219edbd4a0SFrançois Tigeot 	 * Enable gamma to match primary/cursor plane behaviour.
5229edbd4a0SFrançois Tigeot 	 * FIXME should be user controllable via propertiesa.
5239edbd4a0SFrançois Tigeot 	 */
5249edbd4a0SFrançois Tigeot 	sprctl |= SPRITE_GAMMA_ENABLE;
5259edbd4a0SFrançois Tigeot 
526e3adcf8fSFrançois Tigeot 	if (obj->tiling_mode != I915_TILING_NONE)
527e3adcf8fSFrançois Tigeot 		sprctl |= SPRITE_TILED;
528e3adcf8fSFrançois Tigeot 
5299edbd4a0SFrançois Tigeot 	if (IS_HASWELL(dev) || IS_BROADWELL(dev))
5309edbd4a0SFrançois Tigeot 		sprctl &= ~SPRITE_TRICKLE_FEED_DISABLE;
5319edbd4a0SFrançois Tigeot 	else
532e3adcf8fSFrançois Tigeot 		sprctl |= SPRITE_TRICKLE_FEED_DISABLE;
5339edbd4a0SFrançois Tigeot 
5349edbd4a0SFrançois Tigeot 	if (IS_HASWELL(dev) || IS_BROADWELL(dev))
535a2fdbec6SFrançois Tigeot 		sprctl |= SPRITE_PIPE_CSC_ENABLE;
536a2fdbec6SFrançois Tigeot 
537e3adcf8fSFrançois Tigeot 	/* Sizes are 0 based */
538e3adcf8fSFrançois Tigeot 	src_w--;
539e3adcf8fSFrançois Tigeot 	src_h--;
540e3adcf8fSFrançois Tigeot 	crtc_w--;
541e3adcf8fSFrançois Tigeot 	crtc_h--;
542e3adcf8fSFrançois Tigeot 
5439edbd4a0SFrançois Tigeot 	if (crtc_w != src_w || crtc_h != src_h)
544e3adcf8fSFrançois Tigeot 		sprscale = SPRITE_SCALE_ENABLE | (src_w << 16) | src_h;
545e3adcf8fSFrançois Tigeot 
5462c84b0b6SFrançois Tigeot 	linear_offset = y * fb->pitches[0] + x * pixel_size;
5472c84b0b6SFrançois Tigeot 	sprsurf_offset =
548a05eeebfSFrançois Tigeot 		intel_gen4_compute_page_offset(dev_priv,
549a05eeebfSFrançois Tigeot 					       &x, &y, obj->tiling_mode,
5502c84b0b6SFrançois Tigeot 					       pixel_size, fb->pitches[0]);
5512c84b0b6SFrançois Tigeot 	linear_offset -= sprsurf_offset;
5522c84b0b6SFrançois Tigeot 
5532c9916cdSFrançois Tigeot 	if (plane->state->rotation == BIT(DRM_ROTATE_180)) {
5541b13d190SFrançois Tigeot 		sprctl |= SPRITE_ROTATE_180;
5551b13d190SFrançois Tigeot 
5561b13d190SFrançois Tigeot 		/* HSW and BDW does this automagically in hardware */
5571b13d190SFrançois Tigeot 		if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
5581b13d190SFrançois Tigeot 			x += src_w;
5591b13d190SFrançois Tigeot 			y += src_h;
5601b13d190SFrançois Tigeot 			linear_offset += src_h * fb->pitches[0] +
5611b13d190SFrançois Tigeot 				src_w * pixel_size;
5621b13d190SFrançois Tigeot 		}
5631b13d190SFrançois Tigeot 	}
5641b13d190SFrançois Tigeot 
565477eb7f9SFrançois Tigeot 	if (key->flags) {
566477eb7f9SFrançois Tigeot 		I915_WRITE(SPRKEYVAL(pipe), key->min_value);
567477eb7f9SFrançois Tigeot 		I915_WRITE(SPRKEYMAX(pipe), key->max_value);
568477eb7f9SFrançois Tigeot 		I915_WRITE(SPRKEYMSK(pipe), key->channel_mask);
569477eb7f9SFrançois Tigeot 	}
570477eb7f9SFrançois Tigeot 
571477eb7f9SFrançois Tigeot 	if (key->flags & I915_SET_COLORKEY_DESTINATION)
572477eb7f9SFrançois Tigeot 		sprctl |= SPRITE_DEST_KEY;
573477eb7f9SFrançois Tigeot 	else if (key->flags & I915_SET_COLORKEY_SOURCE)
574477eb7f9SFrançois Tigeot 		sprctl |= SPRITE_SOURCE_KEY;
575477eb7f9SFrançois Tigeot 
576ba55f2f5SFrançois Tigeot 	I915_WRITE(SPRSTRIDE(pipe), fb->pitches[0]);
577ba55f2f5SFrançois Tigeot 	I915_WRITE(SPRPOS(pipe), (crtc_y << 16) | crtc_x);
578ba55f2f5SFrançois Tigeot 
5792c84b0b6SFrançois Tigeot 	/* HSW consolidates SPRTILEOFF and SPRLINOFF into a single SPROFFSET
5802c84b0b6SFrançois Tigeot 	 * register */
5819edbd4a0SFrançois Tigeot 	if (IS_HASWELL(dev) || IS_BROADWELL(dev))
5822c84b0b6SFrançois Tigeot 		I915_WRITE(SPROFFSET(pipe), (y << 16) | x);
5832c84b0b6SFrançois Tigeot 	else if (obj->tiling_mode != I915_TILING_NONE)
5842c84b0b6SFrançois Tigeot 		I915_WRITE(SPRTILEOFF(pipe), (y << 16) | x);
5852c84b0b6SFrançois Tigeot 	else
5862c84b0b6SFrançois Tigeot 		I915_WRITE(SPRLINOFF(pipe), linear_offset);
5872c84b0b6SFrançois Tigeot 
588e3adcf8fSFrançois Tigeot 	I915_WRITE(SPRSIZE(pipe), (crtc_h << 16) | crtc_w);
5892c84b0b6SFrançois Tigeot 	if (intel_plane->can_scale)
590e3adcf8fSFrançois Tigeot 		I915_WRITE(SPRSCALE(pipe), sprscale);
591e3adcf8fSFrançois Tigeot 	I915_WRITE(SPRCTL(pipe), sprctl);
5929edbd4a0SFrançois Tigeot 	I915_WRITE(SPRSURF(pipe),
5939edbd4a0SFrançois Tigeot 		   i915_gem_obj_ggtt_offset(obj) + sprsurf_offset);
59419c468b4SFrançois Tigeot 	POSTING_READ(SPRSURF(pipe));
595e3adcf8fSFrançois Tigeot }
596e3adcf8fSFrançois Tigeot 
597e3adcf8fSFrançois Tigeot static void
598a05eeebfSFrançois Tigeot ivb_disable_plane(struct drm_plane *plane, struct drm_crtc *crtc)
599e3adcf8fSFrançois Tigeot {
600e3adcf8fSFrançois Tigeot 	struct drm_device *dev = plane->dev;
601e3adcf8fSFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
602e3adcf8fSFrançois Tigeot 	struct intel_plane *intel_plane = to_intel_plane(plane);
603e3adcf8fSFrançois Tigeot 	int pipe = intel_plane->pipe;
604ba55f2f5SFrançois Tigeot 
605352ff8bdSFrançois Tigeot 	I915_WRITE(SPRCTL(pipe), 0);
606e3adcf8fSFrançois Tigeot 	/* Can't leave the scaler enabled... */
6072c84b0b6SFrançois Tigeot 	if (intel_plane->can_scale)
608e3adcf8fSFrançois Tigeot 		I915_WRITE(SPRSCALE(pipe), 0);
609ba55f2f5SFrançois Tigeot 
61019c468b4SFrançois Tigeot 	I915_WRITE(SPRSURF(pipe), 0);
61119c468b4SFrançois Tigeot 	POSTING_READ(SPRSURF(pipe));
612e3adcf8fSFrançois Tigeot }
613e3adcf8fSFrançois Tigeot 
614e3adcf8fSFrançois Tigeot static void
6159edbd4a0SFrançois Tigeot ilk_update_plane(struct drm_plane *plane, struct drm_crtc *crtc,
6169edbd4a0SFrançois Tigeot 		 struct drm_framebuffer *fb,
617477eb7f9SFrançois Tigeot 		 int crtc_x, int crtc_y,
618e3adcf8fSFrançois Tigeot 		 unsigned int crtc_w, unsigned int crtc_h,
619e3adcf8fSFrançois Tigeot 		 uint32_t x, uint32_t y,
620e3adcf8fSFrançois Tigeot 		 uint32_t src_w, uint32_t src_h)
621e3adcf8fSFrançois Tigeot {
622e3adcf8fSFrançois Tigeot 	struct drm_device *dev = plane->dev;
623e3adcf8fSFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
624e3adcf8fSFrançois Tigeot 	struct intel_plane *intel_plane = to_intel_plane(plane);
625477eb7f9SFrançois Tigeot 	struct drm_i915_gem_object *obj = intel_fb_obj(fb);
6262c84b0b6SFrançois Tigeot 	int pipe = intel_plane->pipe;
6272c84b0b6SFrançois Tigeot 	unsigned long dvssurf_offset, linear_offset;
6282c84b0b6SFrançois Tigeot 	u32 dvscntr, dvsscale;
6292c84b0b6SFrançois Tigeot 	int pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
630a05eeebfSFrançois Tigeot 	const struct drm_intel_sprite_colorkey *key =
631a05eeebfSFrançois Tigeot 		&to_intel_plane_state(plane->state)->ckey;
632e3adcf8fSFrançois Tigeot 
633477eb7f9SFrançois Tigeot 	dvscntr = DVS_ENABLE;
634e3adcf8fSFrançois Tigeot 
635e3adcf8fSFrançois Tigeot 	switch (fb->pixel_format) {
636e3adcf8fSFrançois Tigeot 	case DRM_FORMAT_XBGR8888:
637e3adcf8fSFrançois Tigeot 		dvscntr |= DVS_FORMAT_RGBX888 | DVS_RGB_ORDER_XBGR;
638e3adcf8fSFrançois Tigeot 		break;
639e3adcf8fSFrançois Tigeot 	case DRM_FORMAT_XRGB8888:
640e3adcf8fSFrançois Tigeot 		dvscntr |= DVS_FORMAT_RGBX888;
641e3adcf8fSFrançois Tigeot 		break;
642e3adcf8fSFrançois Tigeot 	case DRM_FORMAT_YUYV:
643e3adcf8fSFrançois Tigeot 		dvscntr |= DVS_FORMAT_YUV422 | DVS_YUV_ORDER_YUYV;
644e3adcf8fSFrançois Tigeot 		break;
645e3adcf8fSFrançois Tigeot 	case DRM_FORMAT_YVYU:
646e3adcf8fSFrançois Tigeot 		dvscntr |= DVS_FORMAT_YUV422 | DVS_YUV_ORDER_YVYU;
647e3adcf8fSFrançois Tigeot 		break;
648e3adcf8fSFrançois Tigeot 	case DRM_FORMAT_UYVY:
649e3adcf8fSFrançois Tigeot 		dvscntr |= DVS_FORMAT_YUV422 | DVS_YUV_ORDER_UYVY;
650e3adcf8fSFrançois Tigeot 		break;
651e3adcf8fSFrançois Tigeot 	case DRM_FORMAT_VYUY:
652e3adcf8fSFrançois Tigeot 		dvscntr |= DVS_FORMAT_YUV422 | DVS_YUV_ORDER_VYUY;
653e3adcf8fSFrançois Tigeot 		break;
654e3adcf8fSFrançois Tigeot 	default:
6552c84b0b6SFrançois Tigeot 		BUG();
656e3adcf8fSFrançois Tigeot 	}
657e3adcf8fSFrançois Tigeot 
6589edbd4a0SFrançois Tigeot 	/*
6599edbd4a0SFrançois Tigeot 	 * Enable gamma to match primary/cursor plane behaviour.
6609edbd4a0SFrançois Tigeot 	 * FIXME should be user controllable via propertiesa.
6619edbd4a0SFrançois Tigeot 	 */
6629edbd4a0SFrançois Tigeot 	dvscntr |= DVS_GAMMA_ENABLE;
6639edbd4a0SFrançois Tigeot 
664e3adcf8fSFrançois Tigeot 	if (obj->tiling_mode != I915_TILING_NONE)
665e3adcf8fSFrançois Tigeot 		dvscntr |= DVS_TILED;
666e3adcf8fSFrançois Tigeot 
6672c84b0b6SFrançois Tigeot 	if (IS_GEN6(dev))
6682c84b0b6SFrançois Tigeot 		dvscntr |= DVS_TRICKLE_FEED_DISABLE; /* must disable */
669e3adcf8fSFranç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 =
682a05eeebfSFrançois Tigeot 		intel_gen4_compute_page_offset(dev_priv,
683a05eeebfSFranç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
723a05eeebfSFranç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,
740a05eeebfSFranç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;
744a05eeebfSFrançois Tigeot 	struct drm_crtc *crtc = state->base.crtc;
745a05eeebfSFranç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;
761a05eeebfSFranç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 */
779a05eeebfSFranç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");
820*aee94f86SFrançois Tigeot 			drm_rect_debug_print("src: ", src, true);
821*aee94f86SFrançois Tigeot 			drm_rect_debug_print("dst: ", 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");
829*aee94f86SFrançois Tigeot 			drm_rect_debug_print("src: ", src, true);
830*aee94f86SFrançois Tigeot 			drm_rect_debug_print("dst: ", 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 
9262c9916cdSFrançois Tigeot 	if (state->visible) {
927477eb7f9SFrançois Tigeot 		intel_plane->update_plane(plane, crtc, fb,
928a05eeebfSFrançois Tigeot 					  state->dst.x1, state->dst.y1,
929a05eeebfSFrançois Tigeot 					  drm_rect_width(&state->dst),
930a05eeebfSFrançois Tigeot 					  drm_rect_height(&state->dst),
931a05eeebfSFrançois Tigeot 					  state->src.x1 >> 16,
932a05eeebfSFrançois Tigeot 					  state->src.y1 >> 16,
933a05eeebfSFrançois Tigeot 					  drm_rect_width(&state->src) >> 16,
934a05eeebfSFrançois Tigeot 					  drm_rect_height(&state->src) >> 16);
9352c9916cdSFrançois Tigeot 	} else {
936a05eeebfSFrançois Tigeot 		intel_plane->disable_plane(plane, crtc);
937e3adcf8fSFrançois Tigeot 	}
938e3adcf8fSFrançois Tigeot }
939e3adcf8fSFrançois Tigeot 
940e3adcf8fSFrançois Tigeot int intel_sprite_set_colorkey(struct drm_device *dev, void *data,
941e3adcf8fSFrançois Tigeot 			      struct drm_file *file_priv)
942e3adcf8fSFrançois Tigeot {
943e3adcf8fSFrançois Tigeot 	struct drm_intel_sprite_colorkey *set = data;
944e3adcf8fSFrançois Tigeot 	struct drm_plane *plane;
945a05eeebfSFrançois Tigeot 	struct drm_plane_state *plane_state;
946a05eeebfSFrançois Tigeot 	struct drm_atomic_state *state;
947a05eeebfSFrançois Tigeot 	struct drm_modeset_acquire_ctx ctx;
948e3adcf8fSFrançois Tigeot 	int ret = 0;
949e3adcf8fSFrançois Tigeot 
950e3adcf8fSFrançois Tigeot 	/* Make sure we don't try to enable both src & dest simultaneously */
951e3adcf8fSFrançois Tigeot 	if ((set->flags & (I915_SET_COLORKEY_DESTINATION | I915_SET_COLORKEY_SOURCE)) == (I915_SET_COLORKEY_DESTINATION | I915_SET_COLORKEY_SOURCE))
952e3adcf8fSFrançois Tigeot 		return -EINVAL;
953e3adcf8fSFrançois Tigeot 
954*aee94f86SFrançois Tigeot 	if ((IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) &&
955477eb7f9SFrançois Tigeot 	    set->flags & I915_SET_COLORKEY_DESTINATION)
956477eb7f9SFrançois Tigeot 		return -EINVAL;
957477eb7f9SFrançois Tigeot 
95824edb884SFrançois Tigeot 	plane = drm_plane_find(dev, set->plane_id);
959a05eeebfSFrançois Tigeot 	if (!plane || plane->type != DRM_PLANE_TYPE_OVERLAY)
960a05eeebfSFrançois Tigeot 		return -ENOENT;
961a05eeebfSFrançois Tigeot 
962a05eeebfSFrançois Tigeot 	drm_modeset_acquire_init(&ctx, 0);
963a05eeebfSFrançois Tigeot 
964a05eeebfSFrançois Tigeot 	state = drm_atomic_state_alloc(plane->dev);
965a05eeebfSFrançois Tigeot 	if (!state) {
966a05eeebfSFrançois Tigeot 		ret = -ENOMEM;
967a05eeebfSFrançois Tigeot 		goto out;
968a05eeebfSFrançois Tigeot 	}
969a05eeebfSFrançois Tigeot 	state->acquire_ctx = &ctx;
970a05eeebfSFrançois Tigeot 
971a05eeebfSFrançois Tigeot 	while (1) {
972a05eeebfSFrançois Tigeot 		plane_state = drm_atomic_get_plane_state(state, plane);
973a05eeebfSFrançois Tigeot 		ret = PTR_ERR_OR_ZERO(plane_state);
974a05eeebfSFrançois Tigeot 		if (!ret) {
975a05eeebfSFrançois Tigeot 			to_intel_plane_state(plane_state)->ckey = *set;
976a05eeebfSFrançois Tigeot 			ret = drm_atomic_commit(state);
977e3adcf8fSFrançois Tigeot 		}
978e3adcf8fSFrançois Tigeot 
979a05eeebfSFrançois Tigeot 		if (ret != -EDEADLK)
980a05eeebfSFrançois Tigeot 			break;
98119c468b4SFrançois Tigeot 
982a05eeebfSFrançois Tigeot 		drm_atomic_state_clear(state);
983a05eeebfSFrançois Tigeot 		drm_modeset_backoff(&ctx);
98419c468b4SFrançois Tigeot 	}
98519c468b4SFrançois Tigeot 
986a05eeebfSFrançois Tigeot 	if (ret)
987a05eeebfSFrançois Tigeot 		drm_atomic_state_free(state);
988e3adcf8fSFrançois Tigeot 
989a05eeebfSFrançois Tigeot out:
990a05eeebfSFrançois Tigeot 	drm_modeset_drop_locks(&ctx);
991a05eeebfSFrançois Tigeot 	drm_modeset_acquire_fini(&ctx);
992e3adcf8fSFrançois Tigeot 	return ret;
993e3adcf8fSFrançois Tigeot }
994e3adcf8fSFrançois Tigeot 
99519c468b4SFrançois Tigeot static const uint32_t ilk_plane_formats[] = {
9962c84b0b6SFrançois Tigeot 	DRM_FORMAT_XRGB8888,
9972c84b0b6SFrançois Tigeot 	DRM_FORMAT_YUYV,
9982c84b0b6SFrançois Tigeot 	DRM_FORMAT_YVYU,
9992c84b0b6SFrançois Tigeot 	DRM_FORMAT_UYVY,
10002c84b0b6SFrançois Tigeot 	DRM_FORMAT_VYUY,
10012c84b0b6SFrançois Tigeot };
10022c84b0b6SFrançois Tigeot 
100319c468b4SFrançois Tigeot static const uint32_t snb_plane_formats[] = {
1004e3adcf8fSFrançois Tigeot 	DRM_FORMAT_XBGR8888,
1005e3adcf8fSFrançois Tigeot 	DRM_FORMAT_XRGB8888,
1006e3adcf8fSFrançois Tigeot 	DRM_FORMAT_YUYV,
1007e3adcf8fSFrançois Tigeot 	DRM_FORMAT_YVYU,
1008e3adcf8fSFrançois Tigeot 	DRM_FORMAT_UYVY,
1009e3adcf8fSFrançois Tigeot 	DRM_FORMAT_VYUY,
1010e3adcf8fSFrançois Tigeot };
1011e3adcf8fSFrançois Tigeot 
101219c468b4SFrançois Tigeot static const uint32_t vlv_plane_formats[] = {
10138e26cdf6SFrançois Tigeot 	DRM_FORMAT_RGB565,
10148e26cdf6SFrançois Tigeot 	DRM_FORMAT_ABGR8888,
10158e26cdf6SFrançois Tigeot 	DRM_FORMAT_ARGB8888,
10168e26cdf6SFrançois Tigeot 	DRM_FORMAT_XBGR8888,
10178e26cdf6SFrançois Tigeot 	DRM_FORMAT_XRGB8888,
10188e26cdf6SFrançois Tigeot 	DRM_FORMAT_XBGR2101010,
10198e26cdf6SFrançois Tigeot 	DRM_FORMAT_ABGR2101010,
10208e26cdf6SFrançois Tigeot 	DRM_FORMAT_YUYV,
10218e26cdf6SFrançois Tigeot 	DRM_FORMAT_YVYU,
10228e26cdf6SFrançois Tigeot 	DRM_FORMAT_UYVY,
10238e26cdf6SFrançois Tigeot 	DRM_FORMAT_VYUY,
10248e26cdf6SFrançois Tigeot };
10258e26cdf6SFrançois Tigeot 
10262c9916cdSFrançois Tigeot static uint32_t skl_plane_formats[] = {
10272c9916cdSFrançois Tigeot 	DRM_FORMAT_RGB565,
10282c9916cdSFrançois Tigeot 	DRM_FORMAT_ABGR8888,
10292c9916cdSFrançois Tigeot 	DRM_FORMAT_ARGB8888,
10302c9916cdSFrançois Tigeot 	DRM_FORMAT_XBGR8888,
10312c9916cdSFrançois Tigeot 	DRM_FORMAT_XRGB8888,
10322c9916cdSFrançois Tigeot 	DRM_FORMAT_YUYV,
10332c9916cdSFrançois Tigeot 	DRM_FORMAT_YVYU,
10342c9916cdSFrançois Tigeot 	DRM_FORMAT_UYVY,
10352c9916cdSFrançois Tigeot 	DRM_FORMAT_VYUY,
10362c9916cdSFrançois Tigeot };
10372c9916cdSFrançois Tigeot 
1038e3adcf8fSFrançois Tigeot int
10398e26cdf6SFrançois Tigeot intel_plane_init(struct drm_device *dev, enum i915_pipe pipe, int plane)
1040e3adcf8fSFrançois Tigeot {
1041e3adcf8fSFrançois Tigeot 	struct intel_plane *intel_plane;
10422c9916cdSFrançois Tigeot 	struct intel_plane_state *state;
1043e3adcf8fSFrançois Tigeot 	unsigned long possible_crtcs;
10442c84b0b6SFrançois Tigeot 	const uint32_t *plane_formats;
10452c84b0b6SFrançois Tigeot 	int num_plane_formats;
1046e3adcf8fSFrançois Tigeot 	int ret;
1047e3adcf8fSFrançois Tigeot 
10482c84b0b6SFrançois Tigeot 	if (INTEL_INFO(dev)->gen < 5)
1049e3adcf8fSFrançois Tigeot 		return -ENODEV;
1050e3adcf8fSFrançois Tigeot 
10519edbd4a0SFrançois Tigeot 	intel_plane = kzalloc(sizeof(*intel_plane), GFP_KERNEL);
10522c84b0b6SFrançois Tigeot 	if (!intel_plane)
10532c84b0b6SFrançois Tigeot 		return -ENOMEM;
10542c84b0b6SFrançois Tigeot 
10552c9916cdSFrançois Tigeot 	state = intel_create_plane_state(&intel_plane->base);
10562c9916cdSFrançois Tigeot 	if (!state) {
10572c9916cdSFrançois Tigeot 		kfree(intel_plane);
10582c9916cdSFrançois Tigeot 		return -ENOMEM;
10592c9916cdSFrançois Tigeot 	}
10602c9916cdSFrançois Tigeot 	intel_plane->base.state = &state->base;
10612c9916cdSFrançois Tigeot 
10622c84b0b6SFrançois Tigeot 	switch (INTEL_INFO(dev)->gen) {
10632c84b0b6SFrançois Tigeot 	case 5:
10642c84b0b6SFrançois Tigeot 	case 6:
10652c84b0b6SFrançois Tigeot 		intel_plane->can_scale = true;
10662c84b0b6SFrançois Tigeot 		intel_plane->max_downscale = 16;
10672c84b0b6SFrançois Tigeot 		intel_plane->update_plane = ilk_update_plane;
10682c84b0b6SFrançois Tigeot 		intel_plane->disable_plane = ilk_disable_plane;
1069e3adcf8fSFrançois Tigeot 
1070e3adcf8fSFrançois Tigeot 		if (IS_GEN6(dev)) {
10712c84b0b6SFrançois Tigeot 			plane_formats = snb_plane_formats;
10722c84b0b6SFrançois Tigeot 			num_plane_formats = ARRAY_SIZE(snb_plane_formats);
10732c84b0b6SFrançois Tigeot 		} else {
10742c84b0b6SFrançois Tigeot 			plane_formats = ilk_plane_formats;
10752c84b0b6SFrançois Tigeot 			num_plane_formats = ARRAY_SIZE(ilk_plane_formats);
10762c84b0b6SFrançois Tigeot 		}
10772c84b0b6SFrançois Tigeot 		break;
10782c84b0b6SFrançois Tigeot 
10792c84b0b6SFrançois Tigeot 	case 7:
10809edbd4a0SFrançois Tigeot 	case 8:
10815d0b1887SFrançois Tigeot 		if (IS_IVYBRIDGE(dev)) {
10822c84b0b6SFrançois Tigeot 			intel_plane->can_scale = true;
10835d0b1887SFrançois Tigeot 			intel_plane->max_downscale = 2;
10845d0b1887SFrançois Tigeot 		} else {
10855d0b1887SFrançois Tigeot 			intel_plane->can_scale = false;
10865d0b1887SFrançois Tigeot 			intel_plane->max_downscale = 1;
10875d0b1887SFrançois Tigeot 		}
10888e26cdf6SFrançois Tigeot 
1089*aee94f86SFrançois Tigeot 		if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
10908e26cdf6SFrançois Tigeot 			intel_plane->update_plane = vlv_update_plane;
10918e26cdf6SFrançois Tigeot 			intel_plane->disable_plane = vlv_disable_plane;
10928e26cdf6SFrançois Tigeot 
10938e26cdf6SFrançois Tigeot 			plane_formats = vlv_plane_formats;
10948e26cdf6SFrançois Tigeot 			num_plane_formats = ARRAY_SIZE(vlv_plane_formats);
10958e26cdf6SFrançois Tigeot 		} else {
1096e3adcf8fSFrançois Tigeot 			intel_plane->update_plane = ivb_update_plane;
1097e3adcf8fSFrançois Tigeot 			intel_plane->disable_plane = ivb_disable_plane;
10982c84b0b6SFrançois Tigeot 
10992c84b0b6SFrançois Tigeot 			plane_formats = snb_plane_formats;
11002c84b0b6SFrançois Tigeot 			num_plane_formats = ARRAY_SIZE(snb_plane_formats);
11018e26cdf6SFrançois Tigeot 		}
11022c84b0b6SFrançois Tigeot 		break;
11032c9916cdSFrançois Tigeot 	case 9:
110419c468b4SFrançois Tigeot 		intel_plane->can_scale = true;
11052c9916cdSFrançois Tigeot 		intel_plane->update_plane = skl_update_plane;
11062c9916cdSFrançois Tigeot 		intel_plane->disable_plane = skl_disable_plane;
110719c468b4SFrançois Tigeot 		state->scaler_id = -1;
11082c84b0b6SFrançois Tigeot 
11092c9916cdSFrançois Tigeot 		plane_formats = skl_plane_formats;
11102c9916cdSFrançois Tigeot 		num_plane_formats = ARRAY_SIZE(skl_plane_formats);
11112c9916cdSFrançois Tigeot 		break;
11122c84b0b6SFrançois Tigeot 	default:
1113158486a6SFrançois Tigeot 		kfree(intel_plane);
11142c84b0b6SFrançois Tigeot 		return -ENODEV;
1115e3adcf8fSFrançois Tigeot 	}
1116e3adcf8fSFrançois Tigeot 
1117e3adcf8fSFrançois Tigeot 	intel_plane->pipe = pipe;
11188e26cdf6SFrançois Tigeot 	intel_plane->plane = plane;
1119352ff8bdSFrançois Tigeot 	intel_plane->frontbuffer_bit = INTEL_FRONTBUFFER_SPRITE(pipe, plane);
11202c9916cdSFrançois Tigeot 	intel_plane->check_plane = intel_check_sprite_plane;
11212c9916cdSFrançois Tigeot 	intel_plane->commit_plane = intel_commit_sprite_plane;
1122e3adcf8fSFrançois Tigeot 	possible_crtcs = (1 << pipe);
11231b13d190SFrançois Tigeot 	ret = drm_universal_plane_init(dev, &intel_plane->base, possible_crtcs,
11242c84b0b6SFrançois Tigeot 				       &intel_plane_funcs,
11252c84b0b6SFrançois Tigeot 				       plane_formats, num_plane_formats,
1126*aee94f86SFrançois Tigeot 				       DRM_PLANE_TYPE_OVERLAY, NULL);
11271b13d190SFrançois Tigeot 	if (ret) {
1128158486a6SFrançois Tigeot 		kfree(intel_plane);
11291b13d190SFrançois Tigeot 		goto out;
11301b13d190SFrançois Tigeot 	}
1131e3adcf8fSFrançois Tigeot 
113219c468b4SFrançois Tigeot 	intel_create_rotation_property(dev, intel_plane);
11332c9916cdSFrançois Tigeot 
11342c9916cdSFrançois Tigeot 	drm_plane_helper_add(&intel_plane->base, &intel_plane_helper_funcs);
11351b13d190SFrançois Tigeot 
11361b13d190SFrançois Tigeot out:
1137e3adcf8fSFrançois Tigeot 	return ret;
1138e3adcf8fSFrançois Tigeot }
1139