drm/i915: Fix kerneldocs for intel_audio.c
[linux-block.git] / drivers / gpu / drm / i915 / intel_sprite.c
CommitLineData
b840d907
JB
1/*
2 * Copyright © 2011 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 *
23 * Authors:
24 * Jesse Barnes <jbarnes@virtuousgeek.org>
25 *
26 * New plane/sprite handling.
27 *
28 * The older chips had a separate interface for programming plane related
29 * registers; newer ones are much simpler and we can use the new DRM plane
30 * support.
31 */
760285e7 32#include <drm/drmP.h>
714244e2 33#include <drm/drm_atomic_helper.h>
760285e7
DH
34#include <drm/drm_crtc.h>
35#include <drm/drm_fourcc.h>
1731693a 36#include <drm/drm_rect.h>
c331879c 37#include <drm/drm_atomic.h>
ea2c67bb 38#include <drm/drm_plane_helper.h>
b840d907 39#include "intel_drv.h"
5d723d7a 40#include "intel_frontbuffer.h"
760285e7 41#include <drm/i915_drm.h>
b840d907
JB
42#include "i915_drv.h"
43
6ca2aeb2
VS
44static bool
45format_is_yuv(uint32_t format)
46{
47 switch (format) {
48 case DRM_FORMAT_YUYV:
49 case DRM_FORMAT_UYVY:
50 case DRM_FORMAT_VYUY:
51 case DRM_FORMAT_YVYU:
52 return true;
53 default:
54 return false;
55 }
56}
57
dfd2e9ab
VS
58int intel_usecs_to_scanlines(const struct drm_display_mode *adjusted_mode,
59 int usecs)
8d7849db
VS
60{
61 /* paranoia */
5e7234c9 62 if (!adjusted_mode->crtc_htotal)
8d7849db
VS
63 return 1;
64
5e7234c9
VS
65 return DIV_ROUND_UP(usecs * adjusted_mode->crtc_clock,
66 1000 * adjusted_mode->crtc_htotal);
8d7849db
VS
67}
68
69208c9e
DV
69/* FIXME: We should instead only take spinlocks once for the entire update
70 * instead of once per mmio. */
71#if IS_ENABLED(CONFIG_PROVE_LOCKING)
72#define VBLANK_EVASION_TIME_US 250
73#else
e1edbd44 74#define VBLANK_EVASION_TIME_US 100
69208c9e 75#endif
e1edbd44 76
26ff2762
ACO
77/**
78 * intel_pipe_update_start() - start update of a set of display registers
d3a8fb32 79 * @new_crtc_state: the new crtc state
26ff2762
ACO
80 *
81 * Mark the start of an update to pipe registers that should be updated
82 * atomically regarding vblank. If the next vblank will happens within
83 * the next 100 us, this function waits until the vblank passes.
84 *
85 * After a successful call to this function, interrupts will be disabled
86 * until a subsequent call to intel_pipe_update_end(). That is done to
d3a8fb32 87 * avoid random delays.
26ff2762 88 */
d3a8fb32 89void intel_pipe_update_start(const struct intel_crtc_state *new_crtc_state)
8d7849db 90{
d3a8fb32 91 struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->base.crtc);
ec1b4ee2 92 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
d3a8fb32 93 const struct drm_display_mode *adjusted_mode = &new_crtc_state->base.adjusted_mode;
8d7849db
VS
94 long timeout = msecs_to_jiffies_timeout(1);
95 int scanline, min, max, vblank_start;
210871b6 96 wait_queue_head_t *wq = drm_crtc_vblank_waitqueue(&crtc->base);
ec1b4ee2 97 bool need_vlv_dsi_wa = (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
d3a8fb32 98 intel_crtc_has_type(new_crtc_state, INTEL_OUTPUT_DSI);
8d7849db
VS
99 DEFINE_WAIT(wait);
100
124abe07
VS
101 vblank_start = adjusted_mode->crtc_vblank_start;
102 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
8d7849db
VS
103 vblank_start = DIV_ROUND_UP(vblank_start, 2);
104
105 /* FIXME needs to be calibrated sensibly */
e1edbd44
ML
106 min = vblank_start - intel_usecs_to_scanlines(adjusted_mode,
107 VBLANK_EVASION_TIME_US);
8d7849db
VS
108 max = vblank_start - 1;
109
8f539a83 110 local_irq_disable();
8f539a83 111
8d7849db 112 if (min <= 0 || max <= 0)
8f539a83 113 return;
8d7849db 114
1e3feefd 115 if (WARN_ON(drm_crtc_vblank_get(&crtc->base)))
8f539a83 116 return;
8d7849db 117
d637ce3f
JB
118 crtc->debug.min_vbl = min;
119 crtc->debug.max_vbl = max;
120 trace_i915_pipe_update_start(crtc);
25ef284a 121
8d7849db
VS
122 for (;;) {
123 /*
124 * prepare_to_wait() has a memory barrier, which guarantees
125 * other CPUs can see the task state update by the time we
126 * read the scanline.
127 */
210871b6 128 prepare_to_wait(wq, &wait, TASK_UNINTERRUPTIBLE);
8d7849db
VS
129
130 scanline = intel_get_crtc_scanline(crtc);
131 if (scanline < min || scanline > max)
132 break;
133
134 if (timeout <= 0) {
135 DRM_ERROR("Potential atomic update failure on pipe %c\n",
136 pipe_name(crtc->pipe));
137 break;
138 }
139
140 local_irq_enable();
141
142 timeout = schedule_timeout(timeout);
143
144 local_irq_disable();
145 }
146
210871b6 147 finish_wait(wq, &wait);
8d7849db 148
1e3feefd 149 drm_crtc_vblank_put(&crtc->base);
8d7849db 150
ec1b4ee2
VS
151 /*
152 * On VLV/CHV DSI the scanline counter would appear to
153 * increment approx. 1/3 of a scanline before start of vblank.
154 * The registers still get latched at start of vblank however.
155 * This means we must not write any registers on the first
156 * line of vblank (since not the whole line is actually in
157 * vblank). And unfortunately we can't use the interrupt to
158 * wait here since it will fire too soon. We could use the
159 * frame start interrupt instead since it will fire after the
160 * critical scanline, but that would require more changes
161 * in the interrupt code. So for now we'll just do the nasty
162 * thing and poll for the bad scanline to pass us by.
163 *
164 * FIXME figure out if BXT+ DSI suffers from this as well
165 */
166 while (need_vlv_dsi_wa && scanline == vblank_start)
167 scanline = intel_get_crtc_scanline(crtc);
168
eb120ef6
JB
169 crtc->debug.scanline_start = scanline;
170 crtc->debug.start_vbl_time = ktime_get();
a2991414 171 crtc->debug.start_vbl_count = intel_crtc_get_vblank_counter(crtc);
8d7849db 172
d637ce3f 173 trace_i915_pipe_update_vblank_evaded(crtc);
8d7849db
VS
174}
175
26ff2762
ACO
176/**
177 * intel_pipe_update_end() - end update of a set of display registers
d3a8fb32 178 * @new_crtc_state: the new crtc state
26ff2762
ACO
179 *
180 * Mark the end of an update started with intel_pipe_update_start(). This
181 * re-enables interrupts and verifies the update was actually completed
d3a8fb32 182 * before a vblank.
26ff2762 183 */
d3a8fb32 184void intel_pipe_update_end(struct intel_crtc_state *new_crtc_state)
8d7849db 185{
d3a8fb32 186 struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->base.crtc);
8d7849db 187 enum pipe pipe = crtc->pipe;
eb120ef6 188 int scanline_end = intel_get_crtc_scanline(crtc);
a2991414 189 u32 end_vbl_count = intel_crtc_get_vblank_counter(crtc);
85a62bf9 190 ktime_t end_vbl_time = ktime_get();
a94f2b92 191 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
8d7849db 192
d637ce3f 193 trace_i915_pipe_update_end(crtc, end_vbl_count, scanline_end);
25ef284a 194
1f7528c4
DV
195 /* We're still in the vblank-evade critical section, this can't race.
196 * Would be slightly nice to just grab the vblank count and arm the
197 * event outside of the critical section - the spinlock might spin for a
198 * while ... */
d3a8fb32 199 if (new_crtc_state->base.event) {
1f7528c4
DV
200 WARN_ON(drm_crtc_vblank_get(&crtc->base) != 0);
201
202 spin_lock(&crtc->base.dev->event_lock);
d3a8fb32 203 drm_crtc_arm_vblank_event(&crtc->base, new_crtc_state->base.event);
1f7528c4
DV
204 spin_unlock(&crtc->base.dev->event_lock);
205
d3a8fb32 206 new_crtc_state->base.event = NULL;
1f7528c4
DV
207 }
208
8d7849db
VS
209 local_irq_enable();
210
a94f2b92
BN
211 if (intel_vgpu_active(dev_priv))
212 return;
213
eb120ef6
JB
214 if (crtc->debug.start_vbl_count &&
215 crtc->debug.start_vbl_count != end_vbl_count) {
216 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",
217 pipe_name(pipe), crtc->debug.start_vbl_count,
218 end_vbl_count,
219 ktime_us_delta(end_vbl_time, crtc->debug.start_vbl_time),
220 crtc->debug.min_vbl, crtc->debug.max_vbl,
221 crtc->debug.scanline_start, scanline_end);
7b8cd336
VS
222 }
223#ifdef CONFIG_DRM_I915_DEBUG_VBLANK_EVADE
224 else if (ktime_us_delta(end_vbl_time, crtc->debug.start_vbl_time) >
225 VBLANK_EVASION_TIME_US)
e1edbd44
ML
226 DRM_WARN("Atomic update on pipe (%c) took %lld us, max time under evasion is %u us\n",
227 pipe_name(pipe),
228 ktime_us_delta(end_vbl_time, crtc->debug.start_vbl_time),
229 VBLANK_EVASION_TIME_US);
7b8cd336 230#endif
8d7849db
VS
231}
232
9a8cc576 233void
282dbf9b 234skl_update_plane(struct intel_plane *plane,
2fde1391
ML
235 const struct intel_crtc_state *crtc_state,
236 const struct intel_plane_state *plane_state)
dc2a41b4 237{
282dbf9b
VS
238 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
239 const struct drm_framebuffer *fb = plane_state->base.fb;
240 enum plane_id plane_id = plane->id;
241 enum pipe pipe = plane->pipe;
a0864d59 242 u32 plane_ctl = plane_state->ctl;
2fde1391 243 const struct drm_intel_sprite_colorkey *key = &plane_state->ckey;
b63a16f6 244 u32 surf_addr = plane_state->main.offset;
8d0deca8 245 unsigned int rotation = plane_state->base.rotation;
d2196774 246 u32 stride = skl_plane_stride(fb, 0, rotation);
2e2adb05 247 u32 aux_stride = skl_plane_stride(fb, 1, rotation);
936e71e3
VS
248 int crtc_x = plane_state->base.dst.x1;
249 int crtc_y = plane_state->base.dst.y1;
250 uint32_t crtc_w = drm_rect_width(&plane_state->base.dst);
251 uint32_t crtc_h = drm_rect_height(&plane_state->base.dst);
b63a16f6
VS
252 uint32_t x = plane_state->main.x;
253 uint32_t y = plane_state->main.y;
936e71e3
VS
254 uint32_t src_w = drm_rect_width(&plane_state->base.src) >> 16;
255 uint32_t src_h = drm_rect_height(&plane_state->base.src) >> 16;
dd584fc0 256 unsigned long irqflags;
dc2a41b4 257
6687c906
VS
258 /* Sizes are 0 based */
259 src_w--;
260 src_h--;
261 crtc_w--;
262 crtc_h--;
263
dd584fc0
VS
264 spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
265
4036c78c 266 if (INTEL_GEN(dev_priv) >= 10 || IS_GEMINILAKE(dev_priv))
dd584fc0 267 I915_WRITE_FW(PLANE_COLOR_CTL(pipe, plane_id),
4036c78c 268 plane_state->color_ctl);
78587de2 269 if (key->flags) {
dd584fc0
VS
270 I915_WRITE_FW(PLANE_KEYVAL(pipe, plane_id), key->min_value);
271 I915_WRITE_FW(PLANE_KEYMAX(pipe, plane_id), key->max_value);
272 I915_WRITE_FW(PLANE_KEYMSK(pipe, plane_id), key->channel_mask);
78587de2
VS
273 }
274
dd584fc0
VS
275 I915_WRITE_FW(PLANE_OFFSET(pipe, plane_id), (y << 16) | x);
276 I915_WRITE_FW(PLANE_STRIDE(pipe, plane_id), stride);
277 I915_WRITE_FW(PLANE_SIZE(pipe, plane_id), (src_h << 16) | src_w);
2e2adb05
VS
278 I915_WRITE_FW(PLANE_AUX_DIST(pipe, plane_id),
279 (plane_state->aux.offset - surf_addr) | aux_stride);
280 I915_WRITE_FW(PLANE_AUX_OFFSET(pipe, plane_id),
281 (plane_state->aux.y << 16) | plane_state->aux.x);
c331879c
CK
282
283 /* program plane scaler */
2fde1391 284 if (plane_state->scaler_id >= 0) {
2fde1391 285 int scaler_id = plane_state->scaler_id;
7494bcdc 286 const struct intel_scaler *scaler;
c331879c 287
7494bcdc
ID
288 scaler = &crtc_state->scaler_state.scalers[scaler_id];
289
dd584fc0
VS
290 I915_WRITE_FW(SKL_PS_CTRL(pipe, scaler_id),
291 PS_SCALER_EN | PS_PLANE_SEL(plane_id) | scaler->mode);
292 I915_WRITE_FW(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
293 I915_WRITE_FW(SKL_PS_WIN_POS(pipe, scaler_id), (crtc_x << 16) | crtc_y);
294 I915_WRITE_FW(SKL_PS_WIN_SZ(pipe, scaler_id),
295 ((crtc_w + 1) << 16)|(crtc_h + 1));
c331879c 296
dd584fc0 297 I915_WRITE_FW(PLANE_POS(pipe, plane_id), 0);
c331879c 298 } else {
dd584fc0 299 I915_WRITE_FW(PLANE_POS(pipe, plane_id), (crtc_y << 16) | crtc_x);
c331879c
CK
300 }
301
dd584fc0
VS
302 I915_WRITE_FW(PLANE_CTL(pipe, plane_id), plane_ctl);
303 I915_WRITE_FW(PLANE_SURF(pipe, plane_id),
304 intel_plane_ggtt_offset(plane_state) + surf_addr);
305 POSTING_READ_FW(PLANE_SURF(pipe, plane_id));
306
307 spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
dc2a41b4
DL
308}
309
779d4d8f 310void
282dbf9b 311skl_disable_plane(struct intel_plane *plane, struct intel_crtc *crtc)
dc2a41b4 312{
282dbf9b
VS
313 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
314 enum plane_id plane_id = plane->id;
315 enum pipe pipe = plane->pipe;
dd584fc0
VS
316 unsigned long irqflags;
317
318 spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
dc2a41b4 319
dd584fc0 320 I915_WRITE_FW(PLANE_CTL(pipe, plane_id), 0);
dc2a41b4 321
dd584fc0
VS
322 I915_WRITE_FW(PLANE_SURF(pipe, plane_id), 0);
323 POSTING_READ_FW(PLANE_SURF(pipe, plane_id));
324
325 spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
dc2a41b4
DL
326}
327
6ca2aeb2 328static void
282dbf9b 329chv_update_csc(struct intel_plane *plane, uint32_t format)
6ca2aeb2 330{
282dbf9b
VS
331 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
332 enum plane_id plane_id = plane->id;
6ca2aeb2
VS
333
334 /* Seems RGB data bypasses the CSC always */
335 if (!format_is_yuv(format))
336 return;
337
338 /*
339 * BT.601 limited range YCbCr -> full range RGB
340 *
341 * |r| | 6537 4769 0| |cr |
342 * |g| = |-3330 4769 -1605| x |y-64|
343 * |b| | 0 4769 8263| |cb |
344 *
345 * Cb and Cr apparently come in as signed already, so no
346 * need for any offset. For Y we need to remove the offset.
347 */
dd584fc0
VS
348 I915_WRITE_FW(SPCSCYGOFF(plane_id), SPCSC_OOFF(0) | SPCSC_IOFF(-64));
349 I915_WRITE_FW(SPCSCCBOFF(plane_id), SPCSC_OOFF(0) | SPCSC_IOFF(0));
350 I915_WRITE_FW(SPCSCCROFF(plane_id), SPCSC_OOFF(0) | SPCSC_IOFF(0));
351
352 I915_WRITE_FW(SPCSCC01(plane_id), SPCSC_C1(4769) | SPCSC_C0(6537));
353 I915_WRITE_FW(SPCSCC23(plane_id), SPCSC_C1(-3330) | SPCSC_C0(0));
354 I915_WRITE_FW(SPCSCC45(plane_id), SPCSC_C1(-1605) | SPCSC_C0(4769));
355 I915_WRITE_FW(SPCSCC67(plane_id), SPCSC_C1(4769) | SPCSC_C0(0));
356 I915_WRITE_FW(SPCSCC8(plane_id), SPCSC_C0(8263));
357
358 I915_WRITE_FW(SPCSCYGICLAMP(plane_id), SPCSC_IMAX(940) | SPCSC_IMIN(64));
359 I915_WRITE_FW(SPCSCCBICLAMP(plane_id), SPCSC_IMAX(448) | SPCSC_IMIN(-448));
360 I915_WRITE_FW(SPCSCCRICLAMP(plane_id), SPCSC_IMAX(448) | SPCSC_IMIN(-448));
361
362 I915_WRITE_FW(SPCSCYGOCLAMP(plane_id), SPCSC_OMAX(1023) | SPCSC_OMIN(0));
363 I915_WRITE_FW(SPCSCCBOCLAMP(plane_id), SPCSC_OMAX(1023) | SPCSC_OMIN(0));
364 I915_WRITE_FW(SPCSCCROCLAMP(plane_id), SPCSC_OMAX(1023) | SPCSC_OMIN(0));
6ca2aeb2
VS
365}
366
96ef6854
VS
367static u32 vlv_sprite_ctl(const struct intel_crtc_state *crtc_state,
368 const struct intel_plane_state *plane_state)
7f1f3851 369{
96ef6854 370 const struct drm_framebuffer *fb = plane_state->base.fb;
11df4d95 371 unsigned int rotation = plane_state->base.rotation;
2fde1391 372 const struct drm_intel_sprite_colorkey *key = &plane_state->ckey;
96ef6854 373 u32 sprctl;
7f1f3851 374
96ef6854 375 sprctl = SP_ENABLE | SP_GAMMA_ENABLE;
7f1f3851 376
438b74a5 377 switch (fb->format->format) {
7f1f3851
JB
378 case DRM_FORMAT_YUYV:
379 sprctl |= SP_FORMAT_YUV422 | SP_YUV_ORDER_YUYV;
380 break;
381 case DRM_FORMAT_YVYU:
382 sprctl |= SP_FORMAT_YUV422 | SP_YUV_ORDER_YVYU;
383 break;
384 case DRM_FORMAT_UYVY:
385 sprctl |= SP_FORMAT_YUV422 | SP_YUV_ORDER_UYVY;
386 break;
387 case DRM_FORMAT_VYUY:
388 sprctl |= SP_FORMAT_YUV422 | SP_YUV_ORDER_VYUY;
389 break;
390 case DRM_FORMAT_RGB565:
391 sprctl |= SP_FORMAT_BGR565;
392 break;
393 case DRM_FORMAT_XRGB8888:
394 sprctl |= SP_FORMAT_BGRX8888;
395 break;
396 case DRM_FORMAT_ARGB8888:
397 sprctl |= SP_FORMAT_BGRA8888;
398 break;
399 case DRM_FORMAT_XBGR2101010:
400 sprctl |= SP_FORMAT_RGBX1010102;
401 break;
402 case DRM_FORMAT_ABGR2101010:
403 sprctl |= SP_FORMAT_RGBA1010102;
404 break;
405 case DRM_FORMAT_XBGR8888:
406 sprctl |= SP_FORMAT_RGBX8888;
407 break;
408 case DRM_FORMAT_ABGR8888:
409 sprctl |= SP_FORMAT_RGBA8888;
410 break;
411 default:
96ef6854
VS
412 MISSING_CASE(fb->format->format);
413 return 0;
7f1f3851
JB
414 }
415
bae781b2 416 if (fb->modifier == I915_FORMAT_MOD_X_TILED)
7f1f3851
JB
417 sprctl |= SP_TILED;
418
c2c446ad 419 if (rotation & DRM_MODE_ROTATE_180)
df0cd455
VS
420 sprctl |= SP_ROTATE_180;
421
c2c446ad 422 if (rotation & DRM_MODE_REFLECT_X)
4ea7be2b
VS
423 sprctl |= SP_MIRROR;
424
78587de2
VS
425 if (key->flags & I915_SET_COLORKEY_SOURCE)
426 sprctl |= SP_SOURCE_KEY;
427
96ef6854
VS
428 return sprctl;
429}
430
431static void
282dbf9b 432vlv_update_plane(struct intel_plane *plane,
96ef6854
VS
433 const struct intel_crtc_state *crtc_state,
434 const struct intel_plane_state *plane_state)
435{
282dbf9b
VS
436 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
437 const struct drm_framebuffer *fb = plane_state->base.fb;
438 enum pipe pipe = plane->pipe;
439 enum plane_id plane_id = plane->id;
a0864d59 440 u32 sprctl = plane_state->ctl;
f9407ae1
VS
441 u32 sprsurf_offset = plane_state->main.offset;
442 u32 linear_offset;
96ef6854
VS
443 const struct drm_intel_sprite_colorkey *key = &plane_state->ckey;
444 int crtc_x = plane_state->base.dst.x1;
445 int crtc_y = plane_state->base.dst.y1;
446 uint32_t crtc_w = drm_rect_width(&plane_state->base.dst);
447 uint32_t crtc_h = drm_rect_height(&plane_state->base.dst);
f9407ae1
VS
448 uint32_t x = plane_state->main.x;
449 uint32_t y = plane_state->main.y;
96ef6854
VS
450 unsigned long irqflags;
451
7f1f3851 452 /* Sizes are 0 based */
7f1f3851
JB
453 crtc_w--;
454 crtc_h--;
455
2949056c 456 linear_offset = intel_fb_xy_to_linear(x, y, plane_state, 0);
6687c906 457
dd584fc0
VS
458 spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
459
78587de2 460 if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_B)
282dbf9b 461 chv_update_csc(plane, fb->format->format);
78587de2 462
47ecbb20 463 if (key->flags) {
dd584fc0
VS
464 I915_WRITE_FW(SPKEYMINVAL(pipe, plane_id), key->min_value);
465 I915_WRITE_FW(SPKEYMAXVAL(pipe, plane_id), key->max_value);
466 I915_WRITE_FW(SPKEYMSK(pipe, plane_id), key->channel_mask);
47ecbb20 467 }
dd584fc0
VS
468 I915_WRITE_FW(SPSTRIDE(pipe, plane_id), fb->pitches[0]);
469 I915_WRITE_FW(SPPOS(pipe, plane_id), (crtc_y << 16) | crtc_x);
ca6ad025 470
bae781b2 471 if (fb->modifier == I915_FORMAT_MOD_X_TILED)
dd584fc0 472 I915_WRITE_FW(SPTILEOFF(pipe, plane_id), (y << 16) | x);
7f1f3851 473 else
dd584fc0 474 I915_WRITE_FW(SPLINOFF(pipe, plane_id), linear_offset);
7f1f3851 475
dd584fc0 476 I915_WRITE_FW(SPCONSTALPHA(pipe, plane_id), 0);
c14b0485 477
dd584fc0
VS
478 I915_WRITE_FW(SPSIZE(pipe, plane_id), (crtc_h << 16) | crtc_w);
479 I915_WRITE_FW(SPCNTR(pipe, plane_id), sprctl);
480 I915_WRITE_FW(SPSURF(pipe, plane_id),
481 intel_plane_ggtt_offset(plane_state) + sprsurf_offset);
482 POSTING_READ_FW(SPSURF(pipe, plane_id));
483
484 spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
7f1f3851
JB
485}
486
487static void
282dbf9b 488vlv_disable_plane(struct intel_plane *plane, struct intel_crtc *crtc)
7f1f3851 489{
282dbf9b
VS
490 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
491 enum pipe pipe = plane->pipe;
492 enum plane_id plane_id = plane->id;
dd584fc0
VS
493 unsigned long irqflags;
494
495 spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
7f1f3851 496
dd584fc0 497 I915_WRITE_FW(SPCNTR(pipe, plane_id), 0);
48fe4691 498
dd584fc0
VS
499 I915_WRITE_FW(SPSURF(pipe, plane_id), 0);
500 POSTING_READ_FW(SPSURF(pipe, plane_id));
501
502 spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
7f1f3851
JB
503}
504
45dea7b0
VS
505static u32 ivb_sprite_ctl(const struct intel_crtc_state *crtc_state,
506 const struct intel_plane_state *plane_state)
b840d907 507{
45dea7b0
VS
508 struct drm_i915_private *dev_priv =
509 to_i915(plane_state->base.plane->dev);
510 const struct drm_framebuffer *fb = plane_state->base.fb;
8d0deca8 511 unsigned int rotation = plane_state->base.rotation;
2fde1391 512 const struct drm_intel_sprite_colorkey *key = &plane_state->ckey;
45dea7b0
VS
513 u32 sprctl;
514
515 sprctl = SPRITE_ENABLE | SPRITE_GAMMA_ENABLE;
b840d907 516
45dea7b0
VS
517 if (IS_IVYBRIDGE(dev_priv))
518 sprctl |= SPRITE_TRICKLE_FEED_DISABLE;
519
520 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
521 sprctl |= SPRITE_PIPE_CSC_ENABLE;
b840d907 522
438b74a5 523 switch (fb->format->format) {
b840d907 524 case DRM_FORMAT_XBGR8888:
5ee36913 525 sprctl |= SPRITE_FORMAT_RGBX888 | SPRITE_RGB_ORDER_RGBX;
b840d907
JB
526 break;
527 case DRM_FORMAT_XRGB8888:
5ee36913 528 sprctl |= SPRITE_FORMAT_RGBX888;
b840d907
JB
529 break;
530 case DRM_FORMAT_YUYV:
531 sprctl |= SPRITE_FORMAT_YUV422 | SPRITE_YUV_ORDER_YUYV;
b840d907
JB
532 break;
533 case DRM_FORMAT_YVYU:
534 sprctl |= SPRITE_FORMAT_YUV422 | SPRITE_YUV_ORDER_YVYU;
b840d907
JB
535 break;
536 case DRM_FORMAT_UYVY:
537 sprctl |= SPRITE_FORMAT_YUV422 | SPRITE_YUV_ORDER_UYVY;
b840d907
JB
538 break;
539 case DRM_FORMAT_VYUY:
540 sprctl |= SPRITE_FORMAT_YUV422 | SPRITE_YUV_ORDER_VYUY;
b840d907
JB
541 break;
542 default:
45dea7b0
VS
543 MISSING_CASE(fb->format->format);
544 return 0;
b840d907
JB
545 }
546
bae781b2 547 if (fb->modifier == I915_FORMAT_MOD_X_TILED)
b840d907
JB
548 sprctl |= SPRITE_TILED;
549
c2c446ad 550 if (rotation & DRM_MODE_ROTATE_180)
df0cd455
VS
551 sprctl |= SPRITE_ROTATE_180;
552
78587de2
VS
553 if (key->flags & I915_SET_COLORKEY_DESTINATION)
554 sprctl |= SPRITE_DEST_KEY;
555 else if (key->flags & I915_SET_COLORKEY_SOURCE)
556 sprctl |= SPRITE_SOURCE_KEY;
557
45dea7b0
VS
558 return sprctl;
559}
560
561static void
282dbf9b 562ivb_update_plane(struct intel_plane *plane,
45dea7b0
VS
563 const struct intel_crtc_state *crtc_state,
564 const struct intel_plane_state *plane_state)
565{
282dbf9b
VS
566 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
567 const struct drm_framebuffer *fb = plane_state->base.fb;
568 enum pipe pipe = plane->pipe;
a0864d59 569 u32 sprctl = plane_state->ctl, sprscale = 0;
f9407ae1
VS
570 u32 sprsurf_offset = plane_state->main.offset;
571 u32 linear_offset;
45dea7b0
VS
572 const struct drm_intel_sprite_colorkey *key = &plane_state->ckey;
573 int crtc_x = plane_state->base.dst.x1;
574 int crtc_y = plane_state->base.dst.y1;
575 uint32_t crtc_w = drm_rect_width(&plane_state->base.dst);
576 uint32_t crtc_h = drm_rect_height(&plane_state->base.dst);
f9407ae1
VS
577 uint32_t x = plane_state->main.x;
578 uint32_t y = plane_state->main.y;
45dea7b0
VS
579 uint32_t src_w = drm_rect_width(&plane_state->base.src) >> 16;
580 uint32_t src_h = drm_rect_height(&plane_state->base.src) >> 16;
581 unsigned long irqflags;
582
b840d907
JB
583 /* Sizes are 0 based */
584 src_w--;
585 src_h--;
586 crtc_w--;
587 crtc_h--;
588
8553c18e 589 if (crtc_w != src_w || crtc_h != src_h)
b840d907 590 sprscale = SPRITE_SCALE_ENABLE | (src_w << 16) | src_h;
b840d907 591
2949056c 592 linear_offset = intel_fb_xy_to_linear(x, y, plane_state, 0);
6687c906 593
dd584fc0
VS
594 spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
595
47ecbb20 596 if (key->flags) {
dd584fc0
VS
597 I915_WRITE_FW(SPRKEYVAL(pipe), key->min_value);
598 I915_WRITE_FW(SPRKEYMAX(pipe), key->max_value);
599 I915_WRITE_FW(SPRKEYMSK(pipe), key->channel_mask);
47ecbb20
VS
600 }
601
dd584fc0
VS
602 I915_WRITE_FW(SPRSTRIDE(pipe), fb->pitches[0]);
603 I915_WRITE_FW(SPRPOS(pipe), (crtc_y << 16) | crtc_x);
ca6ad025 604
5a35e99e
DL
605 /* HSW consolidates SPRTILEOFF and SPRLINOFF into a single SPROFFSET
606 * register */
8652744b 607 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
dd584fc0 608 I915_WRITE_FW(SPROFFSET(pipe), (y << 16) | x);
bae781b2 609 else if (fb->modifier == I915_FORMAT_MOD_X_TILED)
dd584fc0 610 I915_WRITE_FW(SPRTILEOFF(pipe), (y << 16) | x);
5a35e99e 611 else
dd584fc0 612 I915_WRITE_FW(SPRLINOFF(pipe), linear_offset);
c54173a8 613
dd584fc0 614 I915_WRITE_FW(SPRSIZE(pipe), (crtc_h << 16) | crtc_w);
282dbf9b 615 if (plane->can_scale)
dd584fc0
VS
616 I915_WRITE_FW(SPRSCALE(pipe), sprscale);
617 I915_WRITE_FW(SPRCTL(pipe), sprctl);
618 I915_WRITE_FW(SPRSURF(pipe),
619 intel_plane_ggtt_offset(plane_state) + sprsurf_offset);
620 POSTING_READ_FW(SPRSURF(pipe));
621
622 spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
b840d907
JB
623}
624
625static void
282dbf9b 626ivb_disable_plane(struct intel_plane *plane, struct intel_crtc *crtc)
b840d907 627{
282dbf9b
VS
628 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
629 enum pipe pipe = plane->pipe;
dd584fc0
VS
630 unsigned long irqflags;
631
632 spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
b840d907 633
dd584fc0 634 I915_WRITE_FW(SPRCTL(pipe), 0);
b840d907 635 /* Can't leave the scaler enabled... */
282dbf9b 636 if (plane->can_scale)
dd584fc0 637 I915_WRITE_FW(SPRSCALE(pipe), 0);
5b633d6b 638
dd584fc0
VS
639 I915_WRITE_FW(SPRSURF(pipe), 0);
640 POSTING_READ_FW(SPRSURF(pipe));
641
642 spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
b840d907
JB
643}
644
ab33081a 645static u32 g4x_sprite_ctl(const struct intel_crtc_state *crtc_state,
0a375147 646 const struct intel_plane_state *plane_state)
b840d907 647{
0a375147
VS
648 struct drm_i915_private *dev_priv =
649 to_i915(plane_state->base.plane->dev);
650 const struct drm_framebuffer *fb = plane_state->base.fb;
8d0deca8 651 unsigned int rotation = plane_state->base.rotation;
2fde1391 652 const struct drm_intel_sprite_colorkey *key = &plane_state->ckey;
0a375147
VS
653 u32 dvscntr;
654
655 dvscntr = DVS_ENABLE | DVS_GAMMA_ENABLE;
b840d907 656
0a375147
VS
657 if (IS_GEN6(dev_priv))
658 dvscntr |= DVS_TRICKLE_FEED_DISABLE;
b840d907 659
438b74a5 660 switch (fb->format->format) {
b840d907 661 case DRM_FORMAT_XBGR8888:
ab2f9df1 662 dvscntr |= DVS_FORMAT_RGBX888 | DVS_RGB_ORDER_XBGR;
b840d907
JB
663 break;
664 case DRM_FORMAT_XRGB8888:
ab2f9df1 665 dvscntr |= DVS_FORMAT_RGBX888;
b840d907
JB
666 break;
667 case DRM_FORMAT_YUYV:
668 dvscntr |= DVS_FORMAT_YUV422 | DVS_YUV_ORDER_YUYV;
b840d907
JB
669 break;
670 case DRM_FORMAT_YVYU:
671 dvscntr |= DVS_FORMAT_YUV422 | DVS_YUV_ORDER_YVYU;
b840d907
JB
672 break;
673 case DRM_FORMAT_UYVY:
674 dvscntr |= DVS_FORMAT_YUV422 | DVS_YUV_ORDER_UYVY;
b840d907
JB
675 break;
676 case DRM_FORMAT_VYUY:
677 dvscntr |= DVS_FORMAT_YUV422 | DVS_YUV_ORDER_VYUY;
b840d907
JB
678 break;
679 default:
0a375147
VS
680 MISSING_CASE(fb->format->format);
681 return 0;
b840d907
JB
682 }
683
bae781b2 684 if (fb->modifier == I915_FORMAT_MOD_X_TILED)
b840d907
JB
685 dvscntr |= DVS_TILED;
686
c2c446ad 687 if (rotation & DRM_MODE_ROTATE_180)
df0cd455
VS
688 dvscntr |= DVS_ROTATE_180;
689
78587de2
VS
690 if (key->flags & I915_SET_COLORKEY_DESTINATION)
691 dvscntr |= DVS_DEST_KEY;
692 else if (key->flags & I915_SET_COLORKEY_SOURCE)
693 dvscntr |= DVS_SOURCE_KEY;
694
0a375147
VS
695 return dvscntr;
696}
697
698static void
282dbf9b 699g4x_update_plane(struct intel_plane *plane,
0a375147
VS
700 const struct intel_crtc_state *crtc_state,
701 const struct intel_plane_state *plane_state)
702{
282dbf9b
VS
703 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
704 const struct drm_framebuffer *fb = plane_state->base.fb;
705 enum pipe pipe = plane->pipe;
f9407ae1
VS
706 u32 dvscntr = plane_state->ctl, dvsscale = 0;
707 u32 dvssurf_offset = plane_state->main.offset;
708 u32 linear_offset;
0a375147
VS
709 const struct drm_intel_sprite_colorkey *key = &plane_state->ckey;
710 int crtc_x = plane_state->base.dst.x1;
711 int crtc_y = plane_state->base.dst.y1;
712 uint32_t crtc_w = drm_rect_width(&plane_state->base.dst);
713 uint32_t crtc_h = drm_rect_height(&plane_state->base.dst);
f9407ae1
VS
714 uint32_t x = plane_state->main.x;
715 uint32_t y = plane_state->main.y;
0a375147
VS
716 uint32_t src_w = drm_rect_width(&plane_state->base.src) >> 16;
717 uint32_t src_h = drm_rect_height(&plane_state->base.src) >> 16;
718 unsigned long irqflags;
719
b840d907
JB
720 /* Sizes are 0 based */
721 src_w--;
722 src_h--;
723 crtc_w--;
724 crtc_h--;
725
8368f014 726 if (crtc_w != src_w || crtc_h != src_h)
b840d907
JB
727 dvsscale = DVS_SCALE_ENABLE | (src_w << 16) | src_h;
728
2949056c 729 linear_offset = intel_fb_xy_to_linear(x, y, plane_state, 0);
6687c906 730
dd584fc0
VS
731 spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
732
47ecbb20 733 if (key->flags) {
dd584fc0
VS
734 I915_WRITE_FW(DVSKEYVAL(pipe), key->min_value);
735 I915_WRITE_FW(DVSKEYMAX(pipe), key->max_value);
736 I915_WRITE_FW(DVSKEYMSK(pipe), key->channel_mask);
47ecbb20
VS
737 }
738
dd584fc0
VS
739 I915_WRITE_FW(DVSSTRIDE(pipe), fb->pitches[0]);
740 I915_WRITE_FW(DVSPOS(pipe), (crtc_y << 16) | crtc_x);
ca6ad025 741
bae781b2 742 if (fb->modifier == I915_FORMAT_MOD_X_TILED)
dd584fc0 743 I915_WRITE_FW(DVSTILEOFF(pipe), (y << 16) | x);
5a35e99e 744 else
dd584fc0
VS
745 I915_WRITE_FW(DVSLINOFF(pipe), linear_offset);
746
747 I915_WRITE_FW(DVSSIZE(pipe), (crtc_h << 16) | crtc_w);
748 I915_WRITE_FW(DVSSCALE(pipe), dvsscale);
749 I915_WRITE_FW(DVSCNTR(pipe), dvscntr);
750 I915_WRITE_FW(DVSSURF(pipe),
751 intel_plane_ggtt_offset(plane_state) + dvssurf_offset);
752 POSTING_READ_FW(DVSSURF(pipe));
753
754 spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
b840d907
JB
755}
756
757static void
282dbf9b 758g4x_disable_plane(struct intel_plane *plane, struct intel_crtc *crtc)
b840d907 759{
282dbf9b
VS
760 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
761 enum pipe pipe = plane->pipe;
dd584fc0 762 unsigned long irqflags;
b840d907 763
dd584fc0
VS
764 spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
765
766 I915_WRITE_FW(DVSCNTR(pipe), 0);
b840d907 767 /* Disable the scaler */
dd584fc0
VS
768 I915_WRITE_FW(DVSSCALE(pipe), 0);
769
770 I915_WRITE_FW(DVSSURF(pipe), 0);
771 POSTING_READ_FW(DVSSURF(pipe));
48fe4691 772
dd584fc0 773 spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
b840d907
JB
774}
775
776static int
282dbf9b 777intel_check_sprite_plane(struct intel_plane *plane,
061e4b8d 778 struct intel_crtc_state *crtc_state,
96d61a7f 779 struct intel_plane_state *state)
b840d907 780{
282dbf9b
VS
781 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
782 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
2b875c22 783 struct drm_framebuffer *fb = state->base.fb;
96d61a7f
GP
784 int crtc_x, crtc_y;
785 unsigned int crtc_w, crtc_h;
786 uint32_t src_x, src_y, src_w, src_h;
936e71e3
VS
787 struct drm_rect *src = &state->base.src;
788 struct drm_rect *dst = &state->base.dst;
96d61a7f 789 const struct drm_rect *clip = &state->clip;
1731693a
VS
790 int hscale, vscale;
791 int max_scale, min_scale;
225c228a 792 bool can_scale;
b63a16f6 793 int ret;
cf4c7c12 794
1638d30c
RC
795 *src = drm_plane_state_src(&state->base);
796 *dst = drm_plane_state_dest(&state->base);
f8856a44 797
cf4c7c12 798 if (!fb) {
936e71e3 799 state->base.visible = false;
da20eabd 800 return 0;
cf4c7c12 801 }
5e1bac2f 802
1731693a 803 /* Don't modify another pipe's plane */
282dbf9b 804 if (plane->pipe != crtc->pipe) {
1731693a 805 DRM_DEBUG_KMS("Wrong plane <-> crtc mapping\n");
b840d907 806 return -EINVAL;
1731693a 807 }
b840d907 808
1731693a
VS
809 /* FIXME check all gen limits */
810 if (fb->width < 3 || fb->height < 3 || fb->pitches[0] > 16384) {
811 DRM_DEBUG_KMS("Unsuitable framebuffer for plane\n");
b840d907 812 return -EINVAL;
1731693a 813 }
b840d907 814
225c228a 815 /* setup can_scale, min_scale, max_scale */
55b8f2a7 816 if (INTEL_GEN(dev_priv) >= 9) {
225c228a 817 /* use scaler when colorkey is not required */
818ed961 818 if (state->ckey.flags == I915_SET_COLORKEY_NONE) {
225c228a
CK
819 can_scale = 1;
820 min_scale = 1;
282dbf9b 821 max_scale = skl_max_scale(crtc, crtc_state);
225c228a
CK
822 } else {
823 can_scale = 0;
824 min_scale = DRM_PLANE_HELPER_NO_SCALING;
825 max_scale = DRM_PLANE_HELPER_NO_SCALING;
826 }
827 } else {
282dbf9b
VS
828 can_scale = plane->can_scale;
829 max_scale = plane->max_downscale << 16;
830 min_scale = plane->can_scale ? 1 : (1 << 16);
225c228a
CK
831 }
832
3c3686cd
VS
833 /*
834 * FIXME the following code does a bunch of fuzzy adjustments to the
835 * coordinates and sizes. We probably need some way to decide whether
836 * more strict checking should be done instead.
837 */
96d61a7f 838 drm_rect_rotate(src, fb->width << 16, fb->height << 16,
8e7d688b 839 state->base.rotation);
76eebda7 840
96d61a7f 841 hscale = drm_rect_calc_hscale_relaxed(src, dst, min_scale, max_scale);
3c3686cd 842 BUG_ON(hscale < 0);
1731693a 843
96d61a7f 844 vscale = drm_rect_calc_vscale_relaxed(src, dst, min_scale, max_scale);
3c3686cd 845 BUG_ON(vscale < 0);
b840d907 846
936e71e3 847 state->base.visible = drm_rect_clip_scaled(src, dst, clip, hscale, vscale);
b840d907 848
96d61a7f
GP
849 crtc_x = dst->x1;
850 crtc_y = dst->y1;
851 crtc_w = drm_rect_width(dst);
852 crtc_h = drm_rect_height(dst);
2d354c34 853
936e71e3 854 if (state->base.visible) {
3c3686cd 855 /* check again in case clipping clamped the results */
96d61a7f 856 hscale = drm_rect_calc_hscale(src, dst, min_scale, max_scale);
3c3686cd
VS
857 if (hscale < 0) {
858 DRM_DEBUG_KMS("Horizontal scaling factor out of limits\n");
c70f577a
VS
859 drm_rect_debug_print("src: ", src, true);
860 drm_rect_debug_print("dst: ", dst, false);
3c3686cd
VS
861
862 return hscale;
863 }
864
96d61a7f 865 vscale = drm_rect_calc_vscale(src, dst, min_scale, max_scale);
3c3686cd
VS
866 if (vscale < 0) {
867 DRM_DEBUG_KMS("Vertical scaling factor out of limits\n");
c70f577a
VS
868 drm_rect_debug_print("src: ", src, true);
869 drm_rect_debug_print("dst: ", dst, false);
3c3686cd
VS
870
871 return vscale;
872 }
873
1731693a 874 /* Make the source viewport size an exact multiple of the scaling factors. */
96d61a7f
GP
875 drm_rect_adjust_size(src,
876 drm_rect_width(dst) * hscale - drm_rect_width(src),
877 drm_rect_height(dst) * vscale - drm_rect_height(src));
1731693a 878
96d61a7f 879 drm_rect_rotate_inv(src, fb->width << 16, fb->height << 16,
8e7d688b 880 state->base.rotation);
76eebda7 881
1731693a 882 /* sanity check to make sure the src viewport wasn't enlarged */
ea2c67bb
MR
883 WARN_ON(src->x1 < (int) state->base.src_x ||
884 src->y1 < (int) state->base.src_y ||
885 src->x2 > (int) state->base.src_x + state->base.src_w ||
886 src->y2 > (int) state->base.src_y + state->base.src_h);
1731693a
VS
887
888 /*
889 * Hardware doesn't handle subpixel coordinates.
890 * Adjust to (macro)pixel boundary, but be careful not to
891 * increase the source viewport size, because that could
892 * push the downscaling factor out of bounds.
1731693a 893 */
96d61a7f
GP
894 src_x = src->x1 >> 16;
895 src_w = drm_rect_width(src) >> 16;
896 src_y = src->y1 >> 16;
897 src_h = drm_rect_height(src) >> 16;
1731693a 898
438b74a5 899 if (format_is_yuv(fb->format->format)) {
1731693a
VS
900 src_x &= ~1;
901 src_w &= ~1;
902
903 /*
904 * Must keep src and dst the
905 * same if we can't scale.
906 */
225c228a 907 if (!can_scale)
1731693a
VS
908 crtc_w &= ~1;
909
910 if (crtc_w == 0)
936e71e3 911 state->base.visible = false;
1731693a
VS
912 }
913 }
914
915 /* Check size restrictions when scaling */
936e71e3 916 if (state->base.visible && (src_w != crtc_w || src_h != crtc_h)) {
1731693a 917 unsigned int width_bytes;
353c8598 918 int cpp = fb->format->cpp[0];
1731693a 919
225c228a 920 WARN_ON(!can_scale);
1731693a
VS
921
922 /* FIXME interlacing min height is 6 */
923
924 if (crtc_w < 3 || crtc_h < 3)
936e71e3 925 state->base.visible = false;
1731693a
VS
926
927 if (src_w < 3 || src_h < 3)
936e71e3 928 state->base.visible = false;
1731693a 929
ac484963 930 width_bytes = ((src_x * cpp) & 63) + src_w * cpp;
1731693a 931
55b8f2a7 932 if (INTEL_GEN(dev_priv) < 9 && (src_w > 2048 || src_h > 2048 ||
c331879c 933 width_bytes > 4096 || fb->pitches[0] > 4096)) {
1731693a
VS
934 DRM_DEBUG_KMS("Source dimensions exceed hardware limits\n");
935 return -EINVAL;
936 }
937 }
938
936e71e3 939 if (state->base.visible) {
0a5ae1b0
CK
940 src->x1 = src_x << 16;
941 src->x2 = (src_x + src_w) << 16;
942 src->y1 = src_y << 16;
943 src->y2 = (src_y + src_h) << 16;
96d61a7f
GP
944 }
945
946 dst->x1 = crtc_x;
947 dst->x2 = crtc_x + crtc_w;
948 dst->y1 = crtc_y;
949 dst->y2 = crtc_y + crtc_h;
950
55b8f2a7 951 if (INTEL_GEN(dev_priv) >= 9) {
b63a16f6
VS
952 ret = skl_check_plane_surface(state);
953 if (ret)
954 return ret;
a0864d59
VS
955
956 state->ctl = skl_plane_ctl(crtc_state, state);
957 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
f9407ae1
VS
958 ret = i9xx_check_plane_surface(state);
959 if (ret)
960 return ret;
961
a0864d59
VS
962 state->ctl = vlv_sprite_ctl(crtc_state, state);
963 } else if (INTEL_GEN(dev_priv) >= 7) {
f9407ae1
VS
964 ret = i9xx_check_plane_surface(state);
965 if (ret)
966 return ret;
967
a0864d59
VS
968 state->ctl = ivb_sprite_ctl(crtc_state, state);
969 } else {
f9407ae1
VS
970 ret = i9xx_check_plane_surface(state);
971 if (ret)
972 return ret;
973
ab33081a 974 state->ctl = g4x_sprite_ctl(crtc_state, state);
b63a16f6
VS
975 }
976
4036c78c
JA
977 if (INTEL_GEN(dev_priv) >= 10 || IS_GEMINILAKE(dev_priv))
978 state->color_ctl = glk_plane_color_ctl(crtc_state, state);
979
96d61a7f
GP
980 return 0;
981}
982
8ea30864
JB
983int intel_sprite_set_colorkey(struct drm_device *dev, void *data,
984 struct drm_file *file_priv)
985{
920a14b2 986 struct drm_i915_private *dev_priv = to_i915(dev);
8ea30864 987 struct drm_intel_sprite_colorkey *set = data;
8ea30864 988 struct drm_plane *plane;
818ed961
ML
989 struct drm_plane_state *plane_state;
990 struct drm_atomic_state *state;
991 struct drm_modeset_acquire_ctx ctx;
8ea30864
JB
992 int ret = 0;
993
8ea30864
JB
994 /* Make sure we don't try to enable both src & dest simultaneously */
995 if ((set->flags & (I915_SET_COLORKEY_DESTINATION | I915_SET_COLORKEY_SOURCE)) == (I915_SET_COLORKEY_DESTINATION | I915_SET_COLORKEY_SOURCE))
996 return -EINVAL;
997
920a14b2 998 if ((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
47ecbb20
VS
999 set->flags & I915_SET_COLORKEY_DESTINATION)
1000 return -EINVAL;
1001
418da172 1002 plane = drm_plane_find(dev, file_priv, set->plane_id);
818ed961
ML
1003 if (!plane || plane->type != DRM_PLANE_TYPE_OVERLAY)
1004 return -ENOENT;
8ea30864 1005
818ed961 1006 drm_modeset_acquire_init(&ctx, 0);
6156a456 1007
818ed961
ML
1008 state = drm_atomic_state_alloc(plane->dev);
1009 if (!state) {
1010 ret = -ENOMEM;
1011 goto out;
6156a456 1012 }
818ed961
ML
1013 state->acquire_ctx = &ctx;
1014
1015 while (1) {
1016 plane_state = drm_atomic_get_plane_state(state, plane);
1017 ret = PTR_ERR_OR_ZERO(plane_state);
1018 if (!ret) {
1019 to_intel_plane_state(plane_state)->ckey = *set;
1020 ret = drm_atomic_commit(state);
1021 }
6156a456 1022
818ed961
ML
1023 if (ret != -EDEADLK)
1024 break;
8ea30864 1025
818ed961
ML
1026 drm_atomic_state_clear(state);
1027 drm_modeset_backoff(&ctx);
1028 }
8ea30864 1029
0853695c 1030 drm_atomic_state_put(state);
818ed961
ML
1031out:
1032 drm_modeset_drop_locks(&ctx);
1033 drm_modeset_acquire_fini(&ctx);
1034 return ret;
5e1bac2f
JB
1035}
1036
ab33081a 1037static const uint32_t g4x_plane_formats[] = {
d1686ae3
CW
1038 DRM_FORMAT_XRGB8888,
1039 DRM_FORMAT_YUYV,
1040 DRM_FORMAT_YVYU,
1041 DRM_FORMAT_UYVY,
1042 DRM_FORMAT_VYUY,
1043};
1044
714244e2
BW
1045static const uint64_t i9xx_plane_format_modifiers[] = {
1046 I915_FORMAT_MOD_X_TILED,
1047 DRM_FORMAT_MOD_LINEAR,
1048 DRM_FORMAT_MOD_INVALID
1049};
1050
dada2d53 1051static const uint32_t snb_plane_formats[] = {
b840d907
JB
1052 DRM_FORMAT_XBGR8888,
1053 DRM_FORMAT_XRGB8888,
1054 DRM_FORMAT_YUYV,
1055 DRM_FORMAT_YVYU,
1056 DRM_FORMAT_UYVY,
1057 DRM_FORMAT_VYUY,
1058};
1059
dada2d53 1060static const uint32_t vlv_plane_formats[] = {
7f1f3851
JB
1061 DRM_FORMAT_RGB565,
1062 DRM_FORMAT_ABGR8888,
1063 DRM_FORMAT_ARGB8888,
1064 DRM_FORMAT_XBGR8888,
1065 DRM_FORMAT_XRGB8888,
1066 DRM_FORMAT_XBGR2101010,
1067 DRM_FORMAT_ABGR2101010,
1068 DRM_FORMAT_YUYV,
1069 DRM_FORMAT_YVYU,
1070 DRM_FORMAT_UYVY,
1071 DRM_FORMAT_VYUY,
1072};
1073
dc2a41b4
DL
1074static uint32_t skl_plane_formats[] = {
1075 DRM_FORMAT_RGB565,
1076 DRM_FORMAT_ABGR8888,
1077 DRM_FORMAT_ARGB8888,
1078 DRM_FORMAT_XBGR8888,
1079 DRM_FORMAT_XRGB8888,
1080 DRM_FORMAT_YUYV,
1081 DRM_FORMAT_YVYU,
1082 DRM_FORMAT_UYVY,
1083 DRM_FORMAT_VYUY,
1084};
1085
714244e2
BW
1086static const uint64_t skl_plane_format_modifiers[] = {
1087 I915_FORMAT_MOD_X_TILED,
1088 DRM_FORMAT_MOD_LINEAR,
1089 DRM_FORMAT_MOD_INVALID
1090};
1091
1092static bool g4x_sprite_plane_format_mod_supported(struct drm_plane *plane,
1093 uint32_t format,
1094 uint64_t modifier)
1095{
1096 switch (format) {
1097 case DRM_FORMAT_XBGR8888:
1098 case DRM_FORMAT_XRGB8888:
1099 case DRM_FORMAT_YUYV:
1100 case DRM_FORMAT_YVYU:
1101 case DRM_FORMAT_UYVY:
1102 case DRM_FORMAT_VYUY:
1103 if (modifier == DRM_FORMAT_MOD_LINEAR ||
1104 modifier == I915_FORMAT_MOD_X_TILED)
1105 return true;
1106 /* fall through */
1107 default:
1108 return false;
1109 }
1110}
1111
1112static bool vlv_sprite_plane_format_mod_supported(struct drm_plane *plane,
1113 uint32_t format,
1114 uint64_t modifier)
1115{
1116 switch (format) {
1117 case DRM_FORMAT_YUYV:
1118 case DRM_FORMAT_YVYU:
1119 case DRM_FORMAT_UYVY:
1120 case DRM_FORMAT_VYUY:
1121 case DRM_FORMAT_RGB565:
1122 case DRM_FORMAT_XRGB8888:
1123 case DRM_FORMAT_ARGB8888:
1124 case DRM_FORMAT_XBGR2101010:
1125 case DRM_FORMAT_ABGR2101010:
1126 case DRM_FORMAT_XBGR8888:
1127 case DRM_FORMAT_ABGR8888:
1128 if (modifier == DRM_FORMAT_MOD_LINEAR ||
1129 modifier == I915_FORMAT_MOD_X_TILED)
1130 return true;
1131 /* fall through */
1132 default:
1133 return false;
1134 }
1135}
1136
1137static bool skl_sprite_plane_format_mod_supported(struct drm_plane *plane,
1138 uint32_t format,
1139 uint64_t modifier)
1140{
1141 /* This is the same as primary plane since SKL has universal planes */
1142 switch (format) {
1143 case DRM_FORMAT_XRGB8888:
1144 case DRM_FORMAT_XBGR8888:
1145 case DRM_FORMAT_ARGB8888:
1146 case DRM_FORMAT_ABGR8888:
1147 case DRM_FORMAT_RGB565:
1148 case DRM_FORMAT_XRGB2101010:
1149 case DRM_FORMAT_XBGR2101010:
1150 case DRM_FORMAT_YUYV:
1151 case DRM_FORMAT_YVYU:
1152 case DRM_FORMAT_UYVY:
1153 case DRM_FORMAT_VYUY:
1154 if (modifier == I915_FORMAT_MOD_Yf_TILED)
1155 return true;
1156 /* fall through */
1157 case DRM_FORMAT_C8:
1158 if (modifier == DRM_FORMAT_MOD_LINEAR ||
1159 modifier == I915_FORMAT_MOD_X_TILED ||
1160 modifier == I915_FORMAT_MOD_Y_TILED)
1161 return true;
1162 /* fall through */
1163 default:
1164 return false;
1165 }
1166}
1167
1168static bool intel_sprite_plane_format_mod_supported(struct drm_plane *plane,
1169 uint32_t format,
1170 uint64_t modifier)
1171{
1172 struct drm_i915_private *dev_priv = to_i915(plane->dev);
1173
1174 if (WARN_ON(modifier == DRM_FORMAT_MOD_INVALID))
1175 return false;
1176
1177 if ((modifier >> 56) != DRM_FORMAT_MOD_VENDOR_INTEL &&
1178 modifier != DRM_FORMAT_MOD_LINEAR)
1179 return false;
1180
1181 if (INTEL_GEN(dev_priv) >= 9)
1182 return skl_sprite_plane_format_mod_supported(plane, format, modifier);
1183 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
1184 return vlv_sprite_plane_format_mod_supported(plane, format, modifier);
1185 else
1186 return g4x_sprite_plane_format_mod_supported(plane, format, modifier);
1187
1188 unreachable();
1189}
1190
2d567585 1191static const struct drm_plane_funcs intel_sprite_plane_funcs = {
714244e2
BW
1192 .update_plane = drm_atomic_helper_update_plane,
1193 .disable_plane = drm_atomic_helper_disable_plane,
1194 .destroy = intel_plane_destroy,
1195 .atomic_get_property = intel_plane_atomic_get_property,
1196 .atomic_set_property = intel_plane_atomic_set_property,
1197 .atomic_duplicate_state = intel_plane_duplicate_state,
1198 .atomic_destroy_state = intel_plane_destroy_state,
1199 .format_mod_supported = intel_sprite_plane_format_mod_supported,
1200};
1201
b079bd17 1202struct intel_plane *
580503c7
VS
1203intel_sprite_plane_create(struct drm_i915_private *dev_priv,
1204 enum pipe pipe, int plane)
b840d907 1205{
fca0ce2a
VS
1206 struct intel_plane *intel_plane = NULL;
1207 struct intel_plane_state *state = NULL;
b840d907 1208 unsigned long possible_crtcs;
d1686ae3 1209 const uint32_t *plane_formats;
714244e2 1210 const uint64_t *modifiers;
93ca7e00 1211 unsigned int supported_rotations;
d1686ae3 1212 int num_plane_formats;
b840d907
JB
1213 int ret;
1214
b14c5679 1215 intel_plane = kzalloc(sizeof(*intel_plane), GFP_KERNEL);
fca0ce2a
VS
1216 if (!intel_plane) {
1217 ret = -ENOMEM;
1218 goto fail;
1219 }
b840d907 1220
8e7d688b
MR
1221 state = intel_create_plane_state(&intel_plane->base);
1222 if (!state) {
fca0ce2a
VS
1223 ret = -ENOMEM;
1224 goto fail;
ea2c67bb 1225 }
8e7d688b 1226 intel_plane->base.state = &state->base;
ea2c67bb 1227
714244e2
BW
1228 if (INTEL_GEN(dev_priv) >= 10) {
1229 intel_plane->can_scale = true;
1230 state->scaler_id = -1;
1231
1232 intel_plane->update_plane = skl_update_plane;
1233 intel_plane->disable_plane = skl_disable_plane;
1234
1235 plane_formats = skl_plane_formats;
1236 num_plane_formats = ARRAY_SIZE(skl_plane_formats);
1237 modifiers = skl_plane_format_modifiers;
1238 } else if (INTEL_GEN(dev_priv) >= 9) {
2d354c34 1239 intel_plane->can_scale = true;
1890ae64 1240 state->scaler_id = -1;
d1686ae3 1241
1890ae64
VS
1242 intel_plane->update_plane = skl_update_plane;
1243 intel_plane->disable_plane = skl_disable_plane;
1244
1245 plane_formats = skl_plane_formats;
1246 num_plane_formats = ARRAY_SIZE(skl_plane_formats);
714244e2 1247 modifiers = skl_plane_format_modifiers;
1890ae64
VS
1248 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
1249 intel_plane->can_scale = false;
1250 intel_plane->max_downscale = 1;
1251
1252 intel_plane->update_plane = vlv_update_plane;
1253 intel_plane->disable_plane = vlv_disable_plane;
d1686ae3 1254
1890ae64
VS
1255 plane_formats = vlv_plane_formats;
1256 num_plane_formats = ARRAY_SIZE(vlv_plane_formats);
714244e2 1257 modifiers = i9xx_plane_format_modifiers;
1890ae64 1258 } else if (INTEL_GEN(dev_priv) >= 7) {
920a14b2 1259 if (IS_IVYBRIDGE(dev_priv)) {
2d354c34 1260 intel_plane->can_scale = true;
d49f7091
DL
1261 intel_plane->max_downscale = 2;
1262 } else {
1263 intel_plane->can_scale = false;
1264 intel_plane->max_downscale = 1;
1265 }
7f1f3851 1266
1890ae64
VS
1267 intel_plane->update_plane = ivb_update_plane;
1268 intel_plane->disable_plane = ivb_disable_plane;
7f1f3851 1269
1890ae64
VS
1270 plane_formats = snb_plane_formats;
1271 num_plane_formats = ARRAY_SIZE(snb_plane_formats);
714244e2 1272 modifiers = i9xx_plane_format_modifiers;
1890ae64
VS
1273 } else {
1274 intel_plane->can_scale = true;
1275 intel_plane->max_downscale = 16;
1276
ab33081a
VS
1277 intel_plane->update_plane = g4x_update_plane;
1278 intel_plane->disable_plane = g4x_disable_plane;
7f1f3851 1279
714244e2 1280 modifiers = i9xx_plane_format_modifiers;
1890ae64 1281 if (IS_GEN6(dev_priv)) {
7f1f3851
JB
1282 plane_formats = snb_plane_formats;
1283 num_plane_formats = ARRAY_SIZE(snb_plane_formats);
1890ae64 1284 } else {
ab33081a
VS
1285 plane_formats = g4x_plane_formats;
1286 num_plane_formats = ARRAY_SIZE(g4x_plane_formats);
7f1f3851 1287 }
b840d907
JB
1288 }
1289
5481e27f 1290 if (INTEL_GEN(dev_priv) >= 9) {
93ca7e00 1291 supported_rotations =
c2c446ad
RF
1292 DRM_MODE_ROTATE_0 | DRM_MODE_ROTATE_90 |
1293 DRM_MODE_ROTATE_180 | DRM_MODE_ROTATE_270;
4ea7be2b
VS
1294 } else if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_B) {
1295 supported_rotations =
c2c446ad
RF
1296 DRM_MODE_ROTATE_0 | DRM_MODE_ROTATE_180 |
1297 DRM_MODE_REFLECT_X;
93ca7e00
VS
1298 } else {
1299 supported_rotations =
c2c446ad 1300 DRM_MODE_ROTATE_0 | DRM_MODE_ROTATE_180;
93ca7e00
VS
1301 }
1302
b840d907 1303 intel_plane->pipe = pipe;
7f1f3851 1304 intel_plane->plane = plane;
b14e5848 1305 intel_plane->id = PLANE_SPRITE0 + plane;
d1b9d039 1306 intel_plane->frontbuffer_bit = INTEL_FRONTBUFFER_SPRITE(pipe, plane);
c59cb179 1307 intel_plane->check_plane = intel_check_sprite_plane;
fca0ce2a 1308
b840d907 1309 possible_crtcs = (1 << pipe);
fca0ce2a 1310
1890ae64 1311 if (INTEL_GEN(dev_priv) >= 9)
580503c7 1312 ret = drm_universal_plane_init(&dev_priv->drm, &intel_plane->base,
714244e2 1313 possible_crtcs, &intel_sprite_plane_funcs,
38573dc1 1314 plane_formats, num_plane_formats,
714244e2
BW
1315 modifiers,
1316 DRM_PLANE_TYPE_OVERLAY,
38573dc1
VS
1317 "plane %d%c", plane + 2, pipe_name(pipe));
1318 else
580503c7 1319 ret = drm_universal_plane_init(&dev_priv->drm, &intel_plane->base,
714244e2 1320 possible_crtcs, &intel_sprite_plane_funcs,
38573dc1 1321 plane_formats, num_plane_formats,
714244e2
BW
1322 modifiers,
1323 DRM_PLANE_TYPE_OVERLAY,
38573dc1 1324 "sprite %c", sprite_name(pipe, plane));
fca0ce2a
VS
1325 if (ret)
1326 goto fail;
7ed6eeee 1327
93ca7e00 1328 drm_plane_create_rotation_property(&intel_plane->base,
c2c446ad 1329 DRM_MODE_ROTATE_0,
93ca7e00 1330 supported_rotations);
b840d907 1331
ea2c67bb
MR
1332 drm_plane_helper_add(&intel_plane->base, &intel_plane_helper_funcs);
1333
b079bd17 1334 return intel_plane;
fca0ce2a
VS
1335
1336fail:
1337 kfree(state);
1338 kfree(intel_plane);
1339
b079bd17 1340 return ERR_PTR(ret);
b840d907 1341}