blob: 750b634d45eced1387e7a9a8b950bb7f5fe881ed [file] [log] [blame]
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001/*
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 */
David Howells760285e2012-10-02 18:01:07 +010032#include <drm/drmP.h>
33#include <drm/drm_crtc.h>
34#include <drm/drm_fourcc.h>
Ville Syrjälä17316932013-04-24 18:52:38 +030035#include <drm/drm_rect.h>
Jesse Barnesb840d907f2011-12-13 13:19:38 -080036#include "intel_drv.h"
David Howells760285e2012-10-02 18:01:07 +010037#include <drm/i915_drm.h>
Jesse Barnesb840d907f2011-12-13 13:19:38 -080038#include "i915_drv.h"
39
Ville Syrjälä8d7849d2014-04-29 13:35:46 +030040static int usecs_to_scanlines(const struct drm_display_mode *mode, int usecs)
41{
42 /* paranoia */
43 if (!mode->crtc_htotal)
44 return 1;
45
46 return DIV_ROUND_UP(usecs * mode->crtc_clock, 1000 * mode->crtc_htotal);
47}
48
49static bool intel_pipe_update_start(struct intel_crtc *crtc, uint32_t *start_vbl_count)
50{
51 struct drm_device *dev = crtc->base.dev;
52 const struct drm_display_mode *mode = &crtc->config.adjusted_mode;
53 enum pipe pipe = crtc->pipe;
54 long timeout = msecs_to_jiffies_timeout(1);
55 int scanline, min, max, vblank_start;
Ville Syrjälä210871b2014-05-22 19:00:50 +030056 wait_queue_head_t *wq = drm_crtc_vblank_waitqueue(&crtc->base);
Ville Syrjälä8d7849d2014-04-29 13:35:46 +030057 DEFINE_WAIT(wait);
58
Rob Clark51fd3712013-11-19 12:10:12 -050059 WARN_ON(!drm_modeset_is_locked(&crtc->base.mutex));
Ville Syrjälä8d7849d2014-04-29 13:35:46 +030060
61 vblank_start = mode->crtc_vblank_start;
62 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
63 vblank_start = DIV_ROUND_UP(vblank_start, 2);
64
65 /* FIXME needs to be calibrated sensibly */
66 min = vblank_start - usecs_to_scanlines(mode, 100);
67 max = vblank_start - 1;
68
69 if (min <= 0 || max <= 0)
70 return false;
71
72 if (WARN_ON(drm_vblank_get(dev, pipe)))
73 return false;
74
75 local_irq_disable();
76
Ville Syrjälä25ef2842014-04-29 13:35:48 +030077 trace_i915_pipe_update_start(crtc, min, max);
78
Ville Syrjälä8d7849d2014-04-29 13:35:46 +030079 for (;;) {
80 /*
81 * prepare_to_wait() has a memory barrier, which guarantees
82 * other CPUs can see the task state update by the time we
83 * read the scanline.
84 */
Ville Syrjälä210871b2014-05-22 19:00:50 +030085 prepare_to_wait(wq, &wait, TASK_UNINTERRUPTIBLE);
Ville Syrjälä8d7849d2014-04-29 13:35:46 +030086
87 scanline = intel_get_crtc_scanline(crtc);
88 if (scanline < min || scanline > max)
89 break;
90
91 if (timeout <= 0) {
92 DRM_ERROR("Potential atomic update failure on pipe %c\n",
93 pipe_name(crtc->pipe));
94 break;
95 }
96
97 local_irq_enable();
98
99 timeout = schedule_timeout(timeout);
100
101 local_irq_disable();
102 }
103
Ville Syrjälä210871b2014-05-22 19:00:50 +0300104 finish_wait(wq, &wait);
Ville Syrjälä8d7849d2014-04-29 13:35:46 +0300105
106 drm_vblank_put(dev, pipe);
107
108 *start_vbl_count = dev->driver->get_vblank_counter(dev, pipe);
109
Ville Syrjälä25ef2842014-04-29 13:35:48 +0300110 trace_i915_pipe_update_vblank_evaded(crtc, min, max, *start_vbl_count);
111
Ville Syrjälä8d7849d2014-04-29 13:35:46 +0300112 return true;
113}
114
115static void intel_pipe_update_end(struct intel_crtc *crtc, u32 start_vbl_count)
116{
117 struct drm_device *dev = crtc->base.dev;
118 enum pipe pipe = crtc->pipe;
119 u32 end_vbl_count = dev->driver->get_vblank_counter(dev, pipe);
120
Ville Syrjälä25ef2842014-04-29 13:35:48 +0300121 trace_i915_pipe_update_end(crtc, end_vbl_count);
122
Ville Syrjälä8d7849d2014-04-29 13:35:46 +0300123 local_irq_enable();
124
125 if (start_vbl_count != end_vbl_count)
126 DRM_ERROR("Atomic update failure on pipe %c (start=%u end=%u)\n",
127 pipe_name(pipe), start_vbl_count, end_vbl_count);
128}
129
Ville Syrjälä5b633d62014-04-29 13:35:47 +0300130static void intel_update_primary_plane(struct intel_crtc *crtc)
131{
132 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
133 int reg = DSPCNTR(crtc->plane);
134
135 if (crtc->primary_enabled)
136 I915_WRITE(reg, I915_READ(reg) | DISPLAY_PLANE_ENABLE);
137 else
138 I915_WRITE(reg, I915_READ(reg) & ~DISPLAY_PLANE_ENABLE);
139}
140
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800141static void
Damien Lespiaudc2a41b2013-12-04 00:49:41 +0000142skl_update_plane(struct drm_plane *drm_plane, struct drm_crtc *crtc,
143 struct drm_framebuffer *fb,
144 struct drm_i915_gem_object *obj, int crtc_x, int crtc_y,
145 unsigned int crtc_w, unsigned int crtc_h,
146 uint32_t x, uint32_t y,
147 uint32_t src_w, uint32_t src_h)
148{
149 struct drm_device *dev = drm_plane->dev;
150 struct drm_i915_private *dev_priv = dev->dev_private;
151 struct intel_plane *intel_plane = to_intel_plane(drm_plane);
152 const int pipe = intel_plane->pipe;
153 const int plane = intel_plane->plane + 1;
154 u32 plane_ctl, stride;
155 int pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
156
157 plane_ctl = I915_READ(PLANE_CTL(pipe, plane));
158
159 /* Mask out pixel format bits in case we change it */
160 plane_ctl &= ~PLANE_CTL_FORMAT_MASK;
161 plane_ctl &= ~PLANE_CTL_ORDER_RGBX;
162 plane_ctl &= ~PLANE_CTL_YUV422_ORDER_MASK;
163 plane_ctl &= ~PLANE_CTL_TILED_MASK;
164 plane_ctl &= ~PLANE_CTL_ALPHA_MASK;
165
166 /* Trickle feed has to be enabled */
167 plane_ctl &= ~PLANE_CTL_TRICKLE_FEED_DISABLE;
168
169 switch (fb->pixel_format) {
170 case DRM_FORMAT_RGB565:
171 plane_ctl |= PLANE_CTL_FORMAT_RGB_565;
172 break;
173 case DRM_FORMAT_XBGR8888:
174 plane_ctl |= PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
175 break;
176 case DRM_FORMAT_XRGB8888:
177 plane_ctl |= PLANE_CTL_FORMAT_XRGB_8888;
178 break;
179 /*
180 * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
181 * to be already pre-multiplied. We need to add a knob (or a different
182 * DRM_FORMAT) for user-space to configure that.
183 */
184 case DRM_FORMAT_ABGR8888:
185 plane_ctl |= PLANE_CTL_FORMAT_XRGB_8888 |
186 PLANE_CTL_ORDER_RGBX |
187 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
188 break;
189 case DRM_FORMAT_ARGB8888:
190 plane_ctl |= PLANE_CTL_FORMAT_XRGB_8888 |
191 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
192 break;
193 case DRM_FORMAT_YUYV:
194 plane_ctl |= PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
195 break;
196 case DRM_FORMAT_YVYU:
197 plane_ctl |= PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
198 break;
199 case DRM_FORMAT_UYVY:
200 plane_ctl |= PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
201 break;
202 case DRM_FORMAT_VYUY:
203 plane_ctl |= PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
204 break;
205 default:
206 BUG();
207 }
208
209 switch (obj->tiling_mode) {
210 case I915_TILING_NONE:
211 stride = fb->pitches[0] >> 6;
212 break;
213 case I915_TILING_X:
214 plane_ctl |= PLANE_CTL_TILED_X;
215 stride = fb->pitches[0] >> 9;
216 break;
217 default:
218 BUG();
219 }
220
221 plane_ctl |= PLANE_CTL_ENABLE;
222 plane_ctl |= PLANE_CTL_PIPE_CSC_ENABLE;
223
224 intel_update_sprite_watermarks(drm_plane, crtc, src_w, src_h,
225 pixel_size, true,
226 src_w != crtc_w || src_h != crtc_h);
227
228 /* Sizes are 0 based */
229 src_w--;
230 src_h--;
231 crtc_w--;
232 crtc_h--;
233
234 I915_WRITE(PLANE_OFFSET(pipe, plane), (y << 16) | x);
235 I915_WRITE(PLANE_STRIDE(pipe, plane), stride);
236 I915_WRITE(PLANE_POS(pipe, plane), (crtc_y << 16) | crtc_x);
237 I915_WRITE(PLANE_SIZE(pipe, plane), (crtc_h << 16) | crtc_w);
238 I915_WRITE(PLANE_CTL(pipe, plane), plane_ctl);
239 I915_WRITE(PLANE_SURF(pipe, plane), i915_gem_obj_ggtt_offset(obj));
240 POSTING_READ(PLANE_SURF(pipe, plane));
241}
242
243static void
244skl_disable_plane(struct drm_plane *drm_plane, struct drm_crtc *crtc)
245{
246 struct drm_device *dev = drm_plane->dev;
247 struct drm_i915_private *dev_priv = dev->dev_private;
248 struct intel_plane *intel_plane = to_intel_plane(drm_plane);
249 const int pipe = intel_plane->pipe;
250 const int plane = intel_plane->plane + 1;
251
252 I915_WRITE(PLANE_CTL(pipe, plane),
253 I915_READ(PLANE_CTL(pipe, plane)) & ~PLANE_CTL_ENABLE);
254
255 /* Activate double buffered register update */
256 I915_WRITE(PLANE_CTL(pipe, plane), 0);
257 POSTING_READ(PLANE_CTL(pipe, plane));
258
259 intel_update_sprite_watermarks(drm_plane, crtc, 0, 0, 0, false, false);
260}
261
262static int
263skl_update_colorkey(struct drm_plane *drm_plane,
264 struct drm_intel_sprite_colorkey *key)
265{
266 struct drm_device *dev = drm_plane->dev;
267 struct drm_i915_private *dev_priv = dev->dev_private;
268 struct intel_plane *intel_plane = to_intel_plane(drm_plane);
269 const int pipe = intel_plane->pipe;
270 const int plane = intel_plane->plane;
271 u32 plane_ctl;
272
273 I915_WRITE(PLANE_KEYVAL(pipe, plane), key->min_value);
274 I915_WRITE(PLANE_KEYMAX(pipe, plane), key->max_value);
275 I915_WRITE(PLANE_KEYMSK(pipe, plane), key->channel_mask);
276
277 plane_ctl = I915_READ(PLANE_CTL(pipe, plane));
278 plane_ctl &= ~PLANE_CTL_KEY_ENABLE_MASK;
279 if (key->flags & I915_SET_COLORKEY_DESTINATION)
280 plane_ctl |= PLANE_CTL_KEY_ENABLE_DESTINATION;
281 else if (key->flags & I915_SET_COLORKEY_SOURCE)
282 plane_ctl |= PLANE_CTL_KEY_ENABLE_SOURCE;
283 I915_WRITE(PLANE_CTL(pipe, plane), plane_ctl);
284
285 POSTING_READ(PLANE_CTL(pipe, plane));
286
287 return 0;
288}
289
290static void
291skl_get_colorkey(struct drm_plane *drm_plane,
292 struct drm_intel_sprite_colorkey *key)
293{
294 struct drm_device *dev = drm_plane->dev;
295 struct drm_i915_private *dev_priv = dev->dev_private;
296 struct intel_plane *intel_plane = to_intel_plane(drm_plane);
297 const int pipe = intel_plane->pipe;
298 const int plane = intel_plane->plane;
299 u32 plane_ctl;
300
301 key->min_value = I915_READ(PLANE_KEYVAL(pipe, plane));
302 key->max_value = I915_READ(PLANE_KEYMAX(pipe, plane));
303 key->channel_mask = I915_READ(PLANE_KEYMSK(pipe, plane));
304
305 plane_ctl = I915_READ(PLANE_CTL(pipe, plane));
306
307 switch (plane_ctl & PLANE_CTL_KEY_ENABLE_MASK) {
308 case PLANE_CTL_KEY_ENABLE_DESTINATION:
309 key->flags = I915_SET_COLORKEY_DESTINATION;
310 break;
311 case PLANE_CTL_KEY_ENABLE_SOURCE:
312 key->flags = I915_SET_COLORKEY_SOURCE;
313 break;
314 default:
315 key->flags = I915_SET_COLORKEY_NONE;
316 }
317}
318
319static void
Ville Syrjäläb39d53f2013-08-06 22:24:09 +0300320vlv_update_plane(struct drm_plane *dplane, struct drm_crtc *crtc,
321 struct drm_framebuffer *fb,
Jesse Barnes7f1f3852013-04-02 11:22:20 -0700322 struct drm_i915_gem_object *obj, int crtc_x, int crtc_y,
323 unsigned int crtc_w, unsigned int crtc_h,
324 uint32_t x, uint32_t y,
325 uint32_t src_w, uint32_t src_h)
326{
327 struct drm_device *dev = dplane->dev;
328 struct drm_i915_private *dev_priv = dev->dev_private;
329 struct intel_plane *intel_plane = to_intel_plane(dplane);
Ville Syrjälä8d7849d2014-04-29 13:35:46 +0300330 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes7f1f3852013-04-02 11:22:20 -0700331 int pipe = intel_plane->pipe;
332 int plane = intel_plane->plane;
333 u32 sprctl;
334 unsigned long sprsurf_offset, linear_offset;
335 int pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
Ville Syrjälä8d7849d2014-04-29 13:35:46 +0300336 u32 start_vbl_count;
337 bool atomic_update;
Jesse Barnes7f1f3852013-04-02 11:22:20 -0700338
339 sprctl = I915_READ(SPCNTR(pipe, plane));
340
341 /* Mask out pixel format bits in case we change it */
342 sprctl &= ~SP_PIXFORMAT_MASK;
343 sprctl &= ~SP_YUV_BYTE_ORDER_MASK;
344 sprctl &= ~SP_TILED;
Ville Syrjälä76eebda2014-08-05 11:26:52 +0530345 sprctl &= ~SP_ROTATE_180;
Jesse Barnes7f1f3852013-04-02 11:22:20 -0700346
347 switch (fb->pixel_format) {
348 case DRM_FORMAT_YUYV:
349 sprctl |= SP_FORMAT_YUV422 | SP_YUV_ORDER_YUYV;
350 break;
351 case DRM_FORMAT_YVYU:
352 sprctl |= SP_FORMAT_YUV422 | SP_YUV_ORDER_YVYU;
353 break;
354 case DRM_FORMAT_UYVY:
355 sprctl |= SP_FORMAT_YUV422 | SP_YUV_ORDER_UYVY;
356 break;
357 case DRM_FORMAT_VYUY:
358 sprctl |= SP_FORMAT_YUV422 | SP_YUV_ORDER_VYUY;
359 break;
360 case DRM_FORMAT_RGB565:
361 sprctl |= SP_FORMAT_BGR565;
362 break;
363 case DRM_FORMAT_XRGB8888:
364 sprctl |= SP_FORMAT_BGRX8888;
365 break;
366 case DRM_FORMAT_ARGB8888:
367 sprctl |= SP_FORMAT_BGRA8888;
368 break;
369 case DRM_FORMAT_XBGR2101010:
370 sprctl |= SP_FORMAT_RGBX1010102;
371 break;
372 case DRM_FORMAT_ABGR2101010:
373 sprctl |= SP_FORMAT_RGBA1010102;
374 break;
375 case DRM_FORMAT_XBGR8888:
376 sprctl |= SP_FORMAT_RGBX8888;
377 break;
378 case DRM_FORMAT_ABGR8888:
379 sprctl |= SP_FORMAT_RGBA8888;
380 break;
381 default:
382 /*
383 * If we get here one of the upper layers failed to filter
384 * out the unsupported plane formats
385 */
386 BUG();
387 break;
388 }
389
Ville Syrjälä4ea67bc2013-11-18 18:32:38 -0800390 /*
391 * Enable gamma to match primary/cursor plane behaviour.
392 * FIXME should be user controllable via propertiesa.
393 */
394 sprctl |= SP_GAMMA_ENABLE;
395
Jesse Barnes7f1f3852013-04-02 11:22:20 -0700396 if (obj->tiling_mode != I915_TILING_NONE)
397 sprctl |= SP_TILED;
398
399 sprctl |= SP_ENABLE;
400
Damien Lespiaued57cb82014-07-15 09:21:24 +0200401 intel_update_sprite_watermarks(dplane, crtc, src_w, src_h,
402 pixel_size, true,
Ville Syrjälä67ca28f2013-07-05 11:57:14 +0300403 src_w != crtc_w || src_h != crtc_h);
404
Jesse Barnes7f1f3852013-04-02 11:22:20 -0700405 /* Sizes are 0 based */
406 src_w--;
407 src_h--;
408 crtc_w--;
409 crtc_h--;
410
Jesse Barnes7f1f3852013-04-02 11:22:20 -0700411 linear_offset = y * fb->pitches[0] + x * pixel_size;
412 sprsurf_offset = intel_gen4_compute_page_offset(&x, &y,
413 obj->tiling_mode,
414 pixel_size,
415 fb->pitches[0]);
416 linear_offset -= sprsurf_offset;
417
Ville Syrjälä76eebda2014-08-05 11:26:52 +0530418 if (intel_plane->rotation == BIT(DRM_ROTATE_180)) {
419 sprctl |= SP_ROTATE_180;
420
421 x += src_w;
422 y += src_h;
423 linear_offset += src_h * fb->pitches[0] + src_w * pixel_size;
424 }
425
Ville Syrjälä8d7849d2014-04-29 13:35:46 +0300426 atomic_update = intel_pipe_update_start(intel_crtc, &start_vbl_count);
427
Ville Syrjälä5b633d62014-04-29 13:35:47 +0300428 intel_update_primary_plane(intel_crtc);
429
Ville Syrjäläca6ad022014-01-17 20:09:03 +0200430 I915_WRITE(SPSTRIDE(pipe, plane), fb->pitches[0]);
431 I915_WRITE(SPPOS(pipe, plane), (crtc_y << 16) | crtc_x);
432
Jesse Barnes7f1f3852013-04-02 11:22:20 -0700433 if (obj->tiling_mode != I915_TILING_NONE)
434 I915_WRITE(SPTILEOFF(pipe, plane), (y << 16) | x);
435 else
436 I915_WRITE(SPLINOFF(pipe, plane), linear_offset);
437
438 I915_WRITE(SPSIZE(pipe, plane), (crtc_h << 16) | crtc_w);
439 I915_WRITE(SPCNTR(pipe, plane), sprctl);
Daniel Vetter85ba7b72014-01-24 10:31:44 +0100440 I915_WRITE(SPSURF(pipe, plane), i915_gem_obj_ggtt_offset(obj) +
441 sprsurf_offset);
Ville Syrjälä5b633d62014-04-29 13:35:47 +0300442
443 intel_flush_primary_plane(dev_priv, intel_crtc->plane);
Ville Syrjälä8d7849d2014-04-29 13:35:46 +0300444
445 if (atomic_update)
446 intel_pipe_update_end(intel_crtc, start_vbl_count);
Jesse Barnes7f1f3852013-04-02 11:22:20 -0700447}
448
449static void
Ville Syrjäläb39d53f2013-08-06 22:24:09 +0300450vlv_disable_plane(struct drm_plane *dplane, struct drm_crtc *crtc)
Jesse Barnes7f1f3852013-04-02 11:22:20 -0700451{
452 struct drm_device *dev = dplane->dev;
453 struct drm_i915_private *dev_priv = dev->dev_private;
454 struct intel_plane *intel_plane = to_intel_plane(dplane);
Ville Syrjälä8d7849d2014-04-29 13:35:46 +0300455 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes7f1f3852013-04-02 11:22:20 -0700456 int pipe = intel_plane->pipe;
457 int plane = intel_plane->plane;
Ville Syrjälä8d7849d2014-04-29 13:35:46 +0300458 u32 start_vbl_count;
459 bool atomic_update;
460
461 atomic_update = intel_pipe_update_start(intel_crtc, &start_vbl_count);
Jesse Barnes7f1f3852013-04-02 11:22:20 -0700462
Ville Syrjälä5b633d62014-04-29 13:35:47 +0300463 intel_update_primary_plane(intel_crtc);
464
Jesse Barnes7f1f3852013-04-02 11:22:20 -0700465 I915_WRITE(SPCNTR(pipe, plane), I915_READ(SPCNTR(pipe, plane)) &
466 ~SP_ENABLE);
467 /* Activate double buffered register update */
Daniel Vetter85ba7b72014-01-24 10:31:44 +0100468 I915_WRITE(SPSURF(pipe, plane), 0);
Ville Syrjälä5b633d62014-04-29 13:35:47 +0300469
470 intel_flush_primary_plane(dev_priv, intel_crtc->plane);
Ville Syrjäläa95fd8c2013-08-06 22:24:12 +0300471
Ville Syrjälä8d7849d2014-04-29 13:35:46 +0300472 if (atomic_update)
473 intel_pipe_update_end(intel_crtc, start_vbl_count);
474
Damien Lespiaued57cb82014-07-15 09:21:24 +0200475 intel_update_sprite_watermarks(dplane, crtc, 0, 0, 0, false, false);
Jesse Barnes7f1f3852013-04-02 11:22:20 -0700476}
477
478static int
479vlv_update_colorkey(struct drm_plane *dplane,
480 struct drm_intel_sprite_colorkey *key)
481{
482 struct drm_device *dev = dplane->dev;
483 struct drm_i915_private *dev_priv = dev->dev_private;
484 struct intel_plane *intel_plane = to_intel_plane(dplane);
485 int pipe = intel_plane->pipe;
486 int plane = intel_plane->plane;
487 u32 sprctl;
488
489 if (key->flags & I915_SET_COLORKEY_DESTINATION)
490 return -EINVAL;
491
492 I915_WRITE(SPKEYMINVAL(pipe, plane), key->min_value);
493 I915_WRITE(SPKEYMAXVAL(pipe, plane), key->max_value);
494 I915_WRITE(SPKEYMSK(pipe, plane), key->channel_mask);
495
496 sprctl = I915_READ(SPCNTR(pipe, plane));
497 sprctl &= ~SP_SOURCE_KEY;
498 if (key->flags & I915_SET_COLORKEY_SOURCE)
499 sprctl |= SP_SOURCE_KEY;
500 I915_WRITE(SPCNTR(pipe, plane), sprctl);
501
502 POSTING_READ(SPKEYMSK(pipe, plane));
503
504 return 0;
505}
506
507static void
508vlv_get_colorkey(struct drm_plane *dplane,
509 struct drm_intel_sprite_colorkey *key)
510{
511 struct drm_device *dev = dplane->dev;
512 struct drm_i915_private *dev_priv = dev->dev_private;
513 struct intel_plane *intel_plane = to_intel_plane(dplane);
514 int pipe = intel_plane->pipe;
515 int plane = intel_plane->plane;
516 u32 sprctl;
517
518 key->min_value = I915_READ(SPKEYMINVAL(pipe, plane));
519 key->max_value = I915_READ(SPKEYMAXVAL(pipe, plane));
520 key->channel_mask = I915_READ(SPKEYMSK(pipe, plane));
521
522 sprctl = I915_READ(SPCNTR(pipe, plane));
523 if (sprctl & SP_SOURCE_KEY)
524 key->flags = I915_SET_COLORKEY_SOURCE;
525 else
526 key->flags = I915_SET_COLORKEY_NONE;
527}
528
529static void
Ville Syrjäläb39d53f2013-08-06 22:24:09 +0300530ivb_update_plane(struct drm_plane *plane, struct drm_crtc *crtc,
531 struct drm_framebuffer *fb,
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800532 struct drm_i915_gem_object *obj, int crtc_x, int crtc_y,
533 unsigned int crtc_w, unsigned int crtc_h,
534 uint32_t x, uint32_t y,
535 uint32_t src_w, uint32_t src_h)
536{
537 struct drm_device *dev = plane->dev;
538 struct drm_i915_private *dev_priv = dev->dev_private;
539 struct intel_plane *intel_plane = to_intel_plane(plane);
Ville Syrjälä8d7849d2014-04-29 13:35:46 +0300540 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800541 int pipe = intel_plane->pipe;
542 u32 sprctl, sprscale = 0;
Damien Lespiau5a35e992012-10-26 18:20:12 +0100543 unsigned long sprsurf_offset, linear_offset;
Ville Syrjälä2bd3c3c2012-10-31 17:50:20 +0200544 int pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
Ville Syrjälä8d7849d2014-04-29 13:35:46 +0300545 u32 start_vbl_count;
546 bool atomic_update;
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800547
548 sprctl = I915_READ(SPRCTL(pipe));
549
550 /* Mask out pixel format bits in case we change it */
551 sprctl &= ~SPRITE_PIXFORMAT_MASK;
552 sprctl &= ~SPRITE_RGB_ORDER_RGBX;
553 sprctl &= ~SPRITE_YUV_BYTE_ORDER_MASK;
Jesse Barnese86fe0d2012-06-26 13:10:11 -0700554 sprctl &= ~SPRITE_TILED;
Ville Syrjälä76eebda2014-08-05 11:26:52 +0530555 sprctl &= ~SPRITE_ROTATE_180;
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800556
557 switch (fb->pixel_format) {
558 case DRM_FORMAT_XBGR8888:
Vijay Purushothaman5ee36912012-08-23 12:08:57 +0530559 sprctl |= SPRITE_FORMAT_RGBX888 | SPRITE_RGB_ORDER_RGBX;
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800560 break;
561 case DRM_FORMAT_XRGB8888:
Vijay Purushothaman5ee36912012-08-23 12:08:57 +0530562 sprctl |= SPRITE_FORMAT_RGBX888;
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800563 break;
564 case DRM_FORMAT_YUYV:
565 sprctl |= SPRITE_FORMAT_YUV422 | SPRITE_YUV_ORDER_YUYV;
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800566 break;
567 case DRM_FORMAT_YVYU:
568 sprctl |= SPRITE_FORMAT_YUV422 | SPRITE_YUV_ORDER_YVYU;
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800569 break;
570 case DRM_FORMAT_UYVY:
571 sprctl |= SPRITE_FORMAT_YUV422 | SPRITE_YUV_ORDER_UYVY;
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800572 break;
573 case DRM_FORMAT_VYUY:
574 sprctl |= SPRITE_FORMAT_YUV422 | SPRITE_YUV_ORDER_VYUY;
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800575 break;
576 default:
Ville Syrjälä28d491d2012-10-31 17:50:21 +0200577 BUG();
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800578 }
579
Ville Syrjälä4ea67bc2013-11-18 18:32:38 -0800580 /*
581 * Enable gamma to match primary/cursor plane behaviour.
582 * FIXME should be user controllable via propertiesa.
583 */
584 sprctl |= SPRITE_GAMMA_ENABLE;
585
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800586 if (obj->tiling_mode != I915_TILING_NONE)
587 sprctl |= SPRITE_TILED;
588
Ville Syrjäläb42c6002013-11-03 13:47:27 +0200589 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Paulo Zanoni1f5d76d2013-08-23 19:51:28 -0300590 sprctl &= ~SPRITE_TRICKLE_FEED_DISABLE;
591 else
592 sprctl |= SPRITE_TRICKLE_FEED_DISABLE;
593
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800594 sprctl |= SPRITE_ENABLE;
595
Ville Syrjälä6bbfa1c2013-11-02 21:07:39 -0700596 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Ville Syrjälä86d3efc2013-01-18 19:11:38 +0200597 sprctl |= SPRITE_PIPE_CSC_ENABLE;
598
Damien Lespiaued57cb82014-07-15 09:21:24 +0200599 intel_update_sprite_watermarks(plane, crtc, src_w, src_h, pixel_size,
600 true,
Ville Syrjälä67ca28f2013-07-05 11:57:14 +0300601 src_w != crtc_w || src_h != crtc_h);
602
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800603 /* Sizes are 0 based */
604 src_w--;
605 src_h--;
606 crtc_w--;
607 crtc_h--;
608
Ville Syrjälä8553c182013-12-05 15:51:39 +0200609 if (crtc_w != src_w || crtc_h != src_h)
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800610 sprscale = SPRITE_SCALE_ENABLE | (src_w << 16) | src_h;
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800611
Chris Wilsonca320ac2012-12-19 12:14:22 +0000612 linear_offset = y * fb->pitches[0] + x * pixel_size;
Damien Lespiau5a35e992012-10-26 18:20:12 +0100613 sprsurf_offset =
Chris Wilsonbc752862013-02-21 20:04:31 +0000614 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
615 pixel_size, fb->pitches[0]);
Damien Lespiau5a35e992012-10-26 18:20:12 +0100616 linear_offset -= sprsurf_offset;
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800617
Ville Syrjälä76eebda2014-08-05 11:26:52 +0530618 if (intel_plane->rotation == BIT(DRM_ROTATE_180)) {
619 sprctl |= SPRITE_ROTATE_180;
620
621 /* HSW and BDW does this automagically in hardware */
622 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
623 x += src_w;
624 y += src_h;
625 linear_offset += src_h * fb->pitches[0] +
626 src_w * pixel_size;
627 }
628 }
629
Ville Syrjälä8d7849d2014-04-29 13:35:46 +0300630 atomic_update = intel_pipe_update_start(intel_crtc, &start_vbl_count);
631
Ville Syrjälä5b633d62014-04-29 13:35:47 +0300632 intel_update_primary_plane(intel_crtc);
633
Ville Syrjäläca6ad022014-01-17 20:09:03 +0200634 I915_WRITE(SPRSTRIDE(pipe), fb->pitches[0]);
635 I915_WRITE(SPRPOS(pipe), (crtc_y << 16) | crtc_x);
636
Damien Lespiau5a35e992012-10-26 18:20:12 +0100637 /* HSW consolidates SPRTILEOFF and SPRLINOFF into a single SPROFFSET
638 * register */
Paulo Zanonib3dc6852013-11-02 21:07:33 -0700639 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Damien Lespiau5a35e992012-10-26 18:20:12 +0100640 I915_WRITE(SPROFFSET(pipe), (y << 16) | x);
641 else if (obj->tiling_mode != I915_TILING_NONE)
642 I915_WRITE(SPRTILEOFF(pipe), (y << 16) | x);
643 else
644 I915_WRITE(SPRLINOFF(pipe), linear_offset);
Damien Lespiauc54173a2012-10-26 18:20:11 +0100645
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800646 I915_WRITE(SPRSIZE(pipe), (crtc_h << 16) | crtc_w);
Damien Lespiau2d354c32012-10-22 18:19:27 +0100647 if (intel_plane->can_scale)
648 I915_WRITE(SPRSCALE(pipe), sprscale);
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800649 I915_WRITE(SPRCTL(pipe), sprctl);
Daniel Vetter85ba7b72014-01-24 10:31:44 +0100650 I915_WRITE(SPRSURF(pipe),
651 i915_gem_obj_ggtt_offset(obj) + sprsurf_offset);
Ville Syrjälä5b633d62014-04-29 13:35:47 +0300652
653 intel_flush_primary_plane(dev_priv, intel_crtc->plane);
Ville Syrjälä8d7849d2014-04-29 13:35:46 +0300654
655 if (atomic_update)
656 intel_pipe_update_end(intel_crtc, start_vbl_count);
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800657}
658
659static void
Ville Syrjäläb39d53f2013-08-06 22:24:09 +0300660ivb_disable_plane(struct drm_plane *plane, struct drm_crtc *crtc)
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800661{
662 struct drm_device *dev = plane->dev;
663 struct drm_i915_private *dev_priv = dev->dev_private;
664 struct intel_plane *intel_plane = to_intel_plane(plane);
Ville Syrjälä8d7849d2014-04-29 13:35:46 +0300665 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800666 int pipe = intel_plane->pipe;
Ville Syrjälä8d7849d2014-04-29 13:35:46 +0300667 u32 start_vbl_count;
668 bool atomic_update;
669
670 atomic_update = intel_pipe_update_start(intel_crtc, &start_vbl_count);
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800671
Ville Syrjälä5b633d62014-04-29 13:35:47 +0300672 intel_update_primary_plane(intel_crtc);
673
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800674 I915_WRITE(SPRCTL(pipe), I915_READ(SPRCTL(pipe)) & ~SPRITE_ENABLE);
675 /* Can't leave the scaler enabled... */
Damien Lespiau2d354c32012-10-22 18:19:27 +0100676 if (intel_plane->can_scale)
677 I915_WRITE(SPRSCALE(pipe), 0);
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800678 /* Activate double buffered register update */
Daniel Vetter85ba7b72014-01-24 10:31:44 +0100679 I915_WRITE(SPRSURF(pipe), 0);
Ville Syrjälä5b633d62014-04-29 13:35:47 +0300680
681 intel_flush_primary_plane(dev_priv, intel_crtc->plane);
Chris Wilson828ed3e2012-04-18 17:12:26 +0100682
Ville Syrjälä8d7849d2014-04-29 13:35:46 +0300683 if (atomic_update)
684 intel_pipe_update_end(intel_crtc, start_vbl_count);
685
Ville Syrjälä1bd09ec2013-12-05 15:51:41 +0200686 /*
687 * Avoid underruns when disabling the sprite.
688 * FIXME remove once watermark updates are done properly.
689 */
690 intel_wait_for_vblank(dev, pipe);
691
Damien Lespiaued57cb82014-07-15 09:21:24 +0200692 intel_update_sprite_watermarks(plane, crtc, 0, 0, 0, false, false);
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800693}
694
Jesse Barnes8ea30862012-01-03 08:05:39 -0800695static int
696ivb_update_colorkey(struct drm_plane *plane,
697 struct drm_intel_sprite_colorkey *key)
698{
699 struct drm_device *dev = plane->dev;
700 struct drm_i915_private *dev_priv = dev->dev_private;
701 struct intel_plane *intel_plane;
702 u32 sprctl;
703 int ret = 0;
704
705 intel_plane = to_intel_plane(plane);
706
707 I915_WRITE(SPRKEYVAL(intel_plane->pipe), key->min_value);
708 I915_WRITE(SPRKEYMAX(intel_plane->pipe), key->max_value);
709 I915_WRITE(SPRKEYMSK(intel_plane->pipe), key->channel_mask);
710
711 sprctl = I915_READ(SPRCTL(intel_plane->pipe));
712 sprctl &= ~(SPRITE_SOURCE_KEY | SPRITE_DEST_KEY);
713 if (key->flags & I915_SET_COLORKEY_DESTINATION)
714 sprctl |= SPRITE_DEST_KEY;
715 else if (key->flags & I915_SET_COLORKEY_SOURCE)
716 sprctl |= SPRITE_SOURCE_KEY;
717 I915_WRITE(SPRCTL(intel_plane->pipe), sprctl);
718
719 POSTING_READ(SPRKEYMSK(intel_plane->pipe));
720
721 return ret;
722}
723
724static void
725ivb_get_colorkey(struct drm_plane *plane, struct drm_intel_sprite_colorkey *key)
726{
727 struct drm_device *dev = plane->dev;
728 struct drm_i915_private *dev_priv = dev->dev_private;
729 struct intel_plane *intel_plane;
730 u32 sprctl;
731
732 intel_plane = to_intel_plane(plane);
733
734 key->min_value = I915_READ(SPRKEYVAL(intel_plane->pipe));
735 key->max_value = I915_READ(SPRKEYMAX(intel_plane->pipe));
736 key->channel_mask = I915_READ(SPRKEYMSK(intel_plane->pipe));
737 key->flags = 0;
738
739 sprctl = I915_READ(SPRCTL(intel_plane->pipe));
740
741 if (sprctl & SPRITE_DEST_KEY)
742 key->flags = I915_SET_COLORKEY_DESTINATION;
743 else if (sprctl & SPRITE_SOURCE_KEY)
744 key->flags = I915_SET_COLORKEY_SOURCE;
745 else
746 key->flags = I915_SET_COLORKEY_NONE;
747}
748
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800749static void
Ville Syrjäläb39d53f2013-08-06 22:24:09 +0300750ilk_update_plane(struct drm_plane *plane, struct drm_crtc *crtc,
751 struct drm_framebuffer *fb,
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800752 struct drm_i915_gem_object *obj, int crtc_x, int crtc_y,
753 unsigned int crtc_w, unsigned int crtc_h,
754 uint32_t x, uint32_t y,
755 uint32_t src_w, uint32_t src_h)
756{
757 struct drm_device *dev = plane->dev;
758 struct drm_i915_private *dev_priv = dev->dev_private;
759 struct intel_plane *intel_plane = to_intel_plane(plane);
Ville Syrjälä8d7849d2014-04-29 13:35:46 +0300760 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjälä2bd3c3c2012-10-31 17:50:20 +0200761 int pipe = intel_plane->pipe;
Damien Lespiau5a35e992012-10-26 18:20:12 +0100762 unsigned long dvssurf_offset, linear_offset;
Chris Wilson8aaa81a2012-04-14 22:14:26 +0100763 u32 dvscntr, dvsscale;
Ville Syrjälä2bd3c3c2012-10-31 17:50:20 +0200764 int pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
Ville Syrjälä8d7849d2014-04-29 13:35:46 +0300765 u32 start_vbl_count;
766 bool atomic_update;
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800767
768 dvscntr = I915_READ(DVSCNTR(pipe));
769
770 /* Mask out pixel format bits in case we change it */
771 dvscntr &= ~DVS_PIXFORMAT_MASK;
Jesse Barnesab2f9df2012-02-27 12:40:10 -0800772 dvscntr &= ~DVS_RGB_ORDER_XBGR;
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800773 dvscntr &= ~DVS_YUV_BYTE_ORDER_MASK;
Ander Conselvan de Oliveira79626522012-07-13 15:50:33 +0300774 dvscntr &= ~DVS_TILED;
Ville Syrjälä76eebda2014-08-05 11:26:52 +0530775 dvscntr &= ~DVS_ROTATE_180;
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800776
777 switch (fb->pixel_format) {
778 case DRM_FORMAT_XBGR8888:
Jesse Barnesab2f9df2012-02-27 12:40:10 -0800779 dvscntr |= DVS_FORMAT_RGBX888 | DVS_RGB_ORDER_XBGR;
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800780 break;
781 case DRM_FORMAT_XRGB8888:
Jesse Barnesab2f9df2012-02-27 12:40:10 -0800782 dvscntr |= DVS_FORMAT_RGBX888;
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800783 break;
784 case DRM_FORMAT_YUYV:
785 dvscntr |= DVS_FORMAT_YUV422 | DVS_YUV_ORDER_YUYV;
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800786 break;
787 case DRM_FORMAT_YVYU:
788 dvscntr |= DVS_FORMAT_YUV422 | DVS_YUV_ORDER_YVYU;
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800789 break;
790 case DRM_FORMAT_UYVY:
791 dvscntr |= DVS_FORMAT_YUV422 | DVS_YUV_ORDER_UYVY;
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800792 break;
793 case DRM_FORMAT_VYUY:
794 dvscntr |= DVS_FORMAT_YUV422 | DVS_YUV_ORDER_VYUY;
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800795 break;
796 default:
Ville Syrjälä28d491d2012-10-31 17:50:21 +0200797 BUG();
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800798 }
799
Ville Syrjälä4ea67bc2013-11-18 18:32:38 -0800800 /*
801 * Enable gamma to match primary/cursor plane behaviour.
802 * FIXME should be user controllable via propertiesa.
803 */
804 dvscntr |= DVS_GAMMA_ENABLE;
805
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800806 if (obj->tiling_mode != I915_TILING_NONE)
807 dvscntr |= DVS_TILED;
808
Chris Wilsond1686ae2012-04-10 11:41:49 +0100809 if (IS_GEN6(dev))
810 dvscntr |= DVS_TRICKLE_FEED_DISABLE; /* must disable */
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800811 dvscntr |= DVS_ENABLE;
812
Damien Lespiaued57cb82014-07-15 09:21:24 +0200813 intel_update_sprite_watermarks(plane, crtc, src_w, src_h,
814 pixel_size, true,
Ville Syrjälä67ca28f2013-07-05 11:57:14 +0300815 src_w != crtc_w || src_h != crtc_h);
816
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800817 /* Sizes are 0 based */
818 src_w--;
819 src_h--;
820 crtc_w--;
821 crtc_h--;
822
Chris Wilson8aaa81a2012-04-14 22:14:26 +0100823 dvsscale = 0;
Ville Syrjälä8368f012013-12-05 15:51:31 +0200824 if (crtc_w != src_w || crtc_h != src_h)
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800825 dvsscale = DVS_SCALE_ENABLE | (src_w << 16) | src_h;
826
Chris Wilsonca320ac2012-12-19 12:14:22 +0000827 linear_offset = y * fb->pitches[0] + x * pixel_size;
Damien Lespiau5a35e992012-10-26 18:20:12 +0100828 dvssurf_offset =
Chris Wilsonbc752862013-02-21 20:04:31 +0000829 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
830 pixel_size, fb->pitches[0]);
Damien Lespiau5a35e992012-10-26 18:20:12 +0100831 linear_offset -= dvssurf_offset;
832
Ville Syrjälä76eebda2014-08-05 11:26:52 +0530833 if (intel_plane->rotation == BIT(DRM_ROTATE_180)) {
834 dvscntr |= DVS_ROTATE_180;
835
836 x += src_w;
837 y += src_h;
838 linear_offset += src_h * fb->pitches[0] + src_w * pixel_size;
839 }
840
Ville Syrjälä8d7849d2014-04-29 13:35:46 +0300841 atomic_update = intel_pipe_update_start(intel_crtc, &start_vbl_count);
842
Ville Syrjälä5b633d62014-04-29 13:35:47 +0300843 intel_update_primary_plane(intel_crtc);
844
Ville Syrjäläca6ad022014-01-17 20:09:03 +0200845 I915_WRITE(DVSSTRIDE(pipe), fb->pitches[0]);
846 I915_WRITE(DVSPOS(pipe), (crtc_y << 16) | crtc_x);
847
Damien Lespiau5a35e992012-10-26 18:20:12 +0100848 if (obj->tiling_mode != I915_TILING_NONE)
849 I915_WRITE(DVSTILEOFF(pipe), (y << 16) | x);
850 else
851 I915_WRITE(DVSLINOFF(pipe), linear_offset);
852
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800853 I915_WRITE(DVSSIZE(pipe), (crtc_h << 16) | crtc_w);
854 I915_WRITE(DVSSCALE(pipe), dvsscale);
855 I915_WRITE(DVSCNTR(pipe), dvscntr);
Daniel Vetter85ba7b72014-01-24 10:31:44 +0100856 I915_WRITE(DVSSURF(pipe),
857 i915_gem_obj_ggtt_offset(obj) + dvssurf_offset);
Ville Syrjälä5b633d62014-04-29 13:35:47 +0300858
859 intel_flush_primary_plane(dev_priv, intel_crtc->plane);
Ville Syrjälä8d7849d2014-04-29 13:35:46 +0300860
861 if (atomic_update)
862 intel_pipe_update_end(intel_crtc, start_vbl_count);
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800863}
864
865static void
Ville Syrjäläb39d53f2013-08-06 22:24:09 +0300866ilk_disable_plane(struct drm_plane *plane, struct drm_crtc *crtc)
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800867{
868 struct drm_device *dev = plane->dev;
869 struct drm_i915_private *dev_priv = dev->dev_private;
870 struct intel_plane *intel_plane = to_intel_plane(plane);
Ville Syrjälä8d7849d2014-04-29 13:35:46 +0300871 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800872 int pipe = intel_plane->pipe;
Ville Syrjälä8d7849d2014-04-29 13:35:46 +0300873 u32 start_vbl_count;
874 bool atomic_update;
875
876 atomic_update = intel_pipe_update_start(intel_crtc, &start_vbl_count);
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800877
Ville Syrjälä5b633d62014-04-29 13:35:47 +0300878 intel_update_primary_plane(intel_crtc);
879
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800880 I915_WRITE(DVSCNTR(pipe), I915_READ(DVSCNTR(pipe)) & ~DVS_ENABLE);
881 /* Disable the scaler */
882 I915_WRITE(DVSSCALE(pipe), 0);
883 /* Flush double buffered register updates */
Daniel Vetter85ba7b72014-01-24 10:31:44 +0100884 I915_WRITE(DVSSURF(pipe), 0);
Ville Syrjälä5b633d62014-04-29 13:35:47 +0300885
886 intel_flush_primary_plane(dev_priv, intel_crtc->plane);
Ville Syrjäläa95fd8c2013-08-06 22:24:12 +0300887
Ville Syrjälä8d7849d2014-04-29 13:35:46 +0300888 if (atomic_update)
889 intel_pipe_update_end(intel_crtc, start_vbl_count);
890
Ville Syrjälä1bd09ec2013-12-05 15:51:41 +0200891 /*
892 * Avoid underruns when disabling the sprite.
893 * FIXME remove once watermark updates are done properly.
894 */
895 intel_wait_for_vblank(dev, pipe);
896
Damien Lespiaued57cb82014-07-15 09:21:24 +0200897 intel_update_sprite_watermarks(plane, crtc, 0, 0, 0, false, false);
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800898}
899
Jesse Barnes175bd422011-12-13 13:19:39 -0800900static void
Ville Syrjälä5b633d62014-04-29 13:35:47 +0300901intel_post_enable_primary(struct drm_crtc *crtc)
Jesse Barnes175bd422011-12-13 13:19:39 -0800902{
903 struct drm_device *dev = crtc->dev;
Jesse Barnes175bd422011-12-13 13:19:39 -0800904 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjäläabae50e2013-10-01 18:02:16 +0300905
Ville Syrjälä20bc86732013-10-01 18:02:17 +0300906 /*
Ville Syrjälä33c3b0d2014-06-24 13:59:28 +0300907 * BDW signals flip done immediately if the plane
908 * is disabled, even if the plane enable is already
909 * armed to occur at the next vblank :(
910 */
911 if (IS_BROADWELL(dev))
912 intel_wait_for_vblank(dev, intel_crtc->pipe);
913
914 /*
Ville Syrjälä20bc86732013-10-01 18:02:17 +0300915 * FIXME IPS should be fine as long as one plane is
916 * enabled, but in practice it seems to have problems
917 * when going from primary only to sprite only and vice
918 * versa.
919 */
Ville Syrjäläcea165c2014-04-15 21:41:35 +0300920 hsw_enable_ips(intel_crtc);
Ville Syrjälä20bc86732013-10-01 18:02:17 +0300921
Ville Syrjälä82284b62013-10-01 18:02:12 +0300922 mutex_lock(&dev->struct_mutex);
Chris Wilson93314b52012-06-13 17:36:55 +0100923 intel_update_fbc(dev);
Ville Syrjälä82284b62013-10-01 18:02:12 +0300924 mutex_unlock(&dev->struct_mutex);
Jesse Barnes175bd422011-12-13 13:19:39 -0800925}
926
927static void
Ville Syrjälä5b633d62014-04-29 13:35:47 +0300928intel_pre_disable_primary(struct drm_crtc *crtc)
Jesse Barnes175bd422011-12-13 13:19:39 -0800929{
930 struct drm_device *dev = crtc->dev;
931 struct drm_i915_private *dev_priv = dev->dev_private;
932 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjälä82284b62013-10-01 18:02:12 +0300933
934 mutex_lock(&dev->struct_mutex);
Ville Syrjäläabae50e2013-10-01 18:02:16 +0300935 if (dev_priv->fbc.plane == intel_crtc->plane)
936 intel_disable_fbc(dev);
Ville Syrjälä82284b62013-10-01 18:02:12 +0300937 mutex_unlock(&dev->struct_mutex);
Ville Syrjäläabae50e2013-10-01 18:02:16 +0300938
Ville Syrjälä20bc86732013-10-01 18:02:17 +0300939 /*
940 * FIXME IPS should be fine as long as one plane is
941 * enabled, but in practice it seems to have problems
942 * when going from primary only to sprite only and vice
943 * versa.
944 */
945 hsw_disable_ips(intel_crtc);
Jesse Barnes175bd422011-12-13 13:19:39 -0800946}
947
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800948static int
Chris Wilsond1686ae2012-04-10 11:41:49 +0100949ilk_update_colorkey(struct drm_plane *plane,
Jesse Barnes8ea30862012-01-03 08:05:39 -0800950 struct drm_intel_sprite_colorkey *key)
951{
952 struct drm_device *dev = plane->dev;
953 struct drm_i915_private *dev_priv = dev->dev_private;
954 struct intel_plane *intel_plane;
955 u32 dvscntr;
956 int ret = 0;
957
958 intel_plane = to_intel_plane(plane);
959
960 I915_WRITE(DVSKEYVAL(intel_plane->pipe), key->min_value);
961 I915_WRITE(DVSKEYMAX(intel_plane->pipe), key->max_value);
962 I915_WRITE(DVSKEYMSK(intel_plane->pipe), key->channel_mask);
963
964 dvscntr = I915_READ(DVSCNTR(intel_plane->pipe));
965 dvscntr &= ~(DVS_SOURCE_KEY | DVS_DEST_KEY);
966 if (key->flags & I915_SET_COLORKEY_DESTINATION)
967 dvscntr |= DVS_DEST_KEY;
968 else if (key->flags & I915_SET_COLORKEY_SOURCE)
969 dvscntr |= DVS_SOURCE_KEY;
970 I915_WRITE(DVSCNTR(intel_plane->pipe), dvscntr);
971
972 POSTING_READ(DVSKEYMSK(intel_plane->pipe));
973
974 return ret;
975}
976
977static void
Chris Wilsond1686ae2012-04-10 11:41:49 +0100978ilk_get_colorkey(struct drm_plane *plane, struct drm_intel_sprite_colorkey *key)
Jesse Barnes8ea30862012-01-03 08:05:39 -0800979{
980 struct drm_device *dev = plane->dev;
981 struct drm_i915_private *dev_priv = dev->dev_private;
982 struct intel_plane *intel_plane;
983 u32 dvscntr;
984
985 intel_plane = to_intel_plane(plane);
986
987 key->min_value = I915_READ(DVSKEYVAL(intel_plane->pipe));
988 key->max_value = I915_READ(DVSKEYMAX(intel_plane->pipe));
989 key->channel_mask = I915_READ(DVSKEYMSK(intel_plane->pipe));
990 key->flags = 0;
991
992 dvscntr = I915_READ(DVSCNTR(intel_plane->pipe));
993
994 if (dvscntr & DVS_DEST_KEY)
995 key->flags = I915_SET_COLORKEY_DESTINATION;
996 else if (dvscntr & DVS_SOURCE_KEY)
997 key->flags = I915_SET_COLORKEY_SOURCE;
998 else
999 key->flags = I915_SET_COLORKEY_NONE;
1000}
1001
Ville Syrjälä17316932013-04-24 18:52:38 +03001002static bool
1003format_is_yuv(uint32_t format)
1004{
1005 switch (format) {
1006 case DRM_FORMAT_YUYV:
1007 case DRM_FORMAT_UYVY:
1008 case DRM_FORMAT_VYUY:
1009 case DRM_FORMAT_YVYU:
1010 return true;
1011 default:
1012 return false;
1013 }
1014}
1015
Ville Syrjäläefb31d12013-12-05 15:51:40 +02001016static bool colorkey_enabled(struct intel_plane *intel_plane)
1017{
1018 struct drm_intel_sprite_colorkey key;
1019
1020 intel_plane->get_colorkey(&intel_plane->base, &key);
1021
1022 return key.flags != I915_SET_COLORKEY_NONE;
1023}
1024
Jesse Barnes8ea30862012-01-03 08:05:39 -08001025static int
Gustavo Padovan96d61a72014-09-05 17:04:47 -03001026intel_check_sprite_plane(struct drm_plane *plane,
1027 struct intel_plane_state *state)
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001028{
Gustavo Padovan96d61a72014-09-05 17:04:47 -03001029 struct intel_crtc *intel_crtc = to_intel_crtc(state->crtc);
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001030 struct intel_plane *intel_plane = to_intel_plane(plane);
Gustavo Padovan96d61a72014-09-05 17:04:47 -03001031 struct drm_framebuffer *fb = state->fb;
Ville Syrjälä2afd9ef2013-10-01 18:02:14 +03001032 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
1033 struct drm_i915_gem_object *obj = intel_fb->obj;
Gustavo Padovan96d61a72014-09-05 17:04:47 -03001034 int crtc_x, crtc_y;
1035 unsigned int crtc_w, crtc_h;
1036 uint32_t src_x, src_y, src_w, src_h;
1037 struct drm_rect *src = &state->src;
1038 struct drm_rect *dst = &state->dst;
1039 struct drm_rect *orig_src = &state->orig_src;
1040 const struct drm_rect *clip = &state->clip;
Ville Syrjälä17316932013-04-24 18:52:38 +03001041 int hscale, vscale;
1042 int max_scale, min_scale;
1043 int pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
Jesse Barnes5e1bac22013-03-26 09:25:43 -07001044
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001045 /* Don't modify another pipe's plane */
Ville Syrjälä17316932013-04-24 18:52:38 +03001046 if (intel_plane->pipe != intel_crtc->pipe) {
1047 DRM_DEBUG_KMS("Wrong plane <-> crtc mapping\n");
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001048 return -EINVAL;
Ville Syrjälä17316932013-04-24 18:52:38 +03001049 }
1050
1051 /* FIXME check all gen limits */
1052 if (fb->width < 3 || fb->height < 3 || fb->pitches[0] > 16384) {
1053 DRM_DEBUG_KMS("Unsuitable framebuffer for plane\n");
1054 return -EINVAL;
1055 }
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001056
Damien Lespiau94c64192012-10-29 15:14:51 +00001057 /* Sprite planes can be linear or x-tiled surfaces */
1058 switch (obj->tiling_mode) {
1059 case I915_TILING_NONE:
1060 case I915_TILING_X:
1061 break;
1062 default:
Ville Syrjälä17316932013-04-24 18:52:38 +03001063 DRM_DEBUG_KMS("Unsupported tiling mode\n");
Damien Lespiau94c64192012-10-29 15:14:51 +00001064 return -EINVAL;
1065 }
1066
Ville Syrjälä3c3686c2013-04-24 18:52:39 +03001067 /*
1068 * FIXME the following code does a bunch of fuzzy adjustments to the
1069 * coordinates and sizes. We probably need some way to decide whether
1070 * more strict checking should be done instead.
1071 */
Ville Syrjälä17316932013-04-24 18:52:38 +03001072 max_scale = intel_plane->max_downscale << 16;
1073 min_scale = intel_plane->can_scale ? 1 : (1 << 16);
1074
Gustavo Padovan96d61a72014-09-05 17:04:47 -03001075 drm_rect_rotate(src, fb->width << 16, fb->height << 16,
Ville Syrjälä76eebda2014-08-05 11:26:52 +05301076 intel_plane->rotation);
1077
Gustavo Padovan96d61a72014-09-05 17:04:47 -03001078 hscale = drm_rect_calc_hscale_relaxed(src, dst, min_scale, max_scale);
Ville Syrjälä3c3686c2013-04-24 18:52:39 +03001079 BUG_ON(hscale < 0);
Ville Syrjälä17316932013-04-24 18:52:38 +03001080
Gustavo Padovan96d61a72014-09-05 17:04:47 -03001081 vscale = drm_rect_calc_vscale_relaxed(src, dst, min_scale, max_scale);
Ville Syrjälä3c3686c2013-04-24 18:52:39 +03001082 BUG_ON(vscale < 0);
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001083
Gustavo Padovan96d61a72014-09-05 17:04:47 -03001084 state->visible = drm_rect_clip_scaled(src, dst, clip, hscale, vscale);
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001085
Gustavo Padovan96d61a72014-09-05 17:04:47 -03001086 crtc_x = dst->x1;
1087 crtc_y = dst->y1;
1088 crtc_w = drm_rect_width(dst);
1089 crtc_h = drm_rect_height(dst);
Damien Lespiau2d354c32012-10-22 18:19:27 +01001090
Gustavo Padovan96d61a72014-09-05 17:04:47 -03001091 if (state->visible) {
Ville Syrjälä3c3686c2013-04-24 18:52:39 +03001092 /* check again in case clipping clamped the results */
Gustavo Padovan96d61a72014-09-05 17:04:47 -03001093 hscale = drm_rect_calc_hscale(src, dst, min_scale, max_scale);
Ville Syrjälä3c3686c2013-04-24 18:52:39 +03001094 if (hscale < 0) {
1095 DRM_DEBUG_KMS("Horizontal scaling factor out of limits\n");
Gustavo Padovan96d61a72014-09-05 17:04:47 -03001096 drm_rect_debug_print(src, true);
1097 drm_rect_debug_print(dst, false);
Ville Syrjälä3c3686c2013-04-24 18:52:39 +03001098
1099 return hscale;
1100 }
1101
Gustavo Padovan96d61a72014-09-05 17:04:47 -03001102 vscale = drm_rect_calc_vscale(src, dst, min_scale, max_scale);
Ville Syrjälä3c3686c2013-04-24 18:52:39 +03001103 if (vscale < 0) {
1104 DRM_DEBUG_KMS("Vertical scaling factor out of limits\n");
Gustavo Padovan96d61a72014-09-05 17:04:47 -03001105 drm_rect_debug_print(src, true);
1106 drm_rect_debug_print(dst, false);
Ville Syrjälä3c3686c2013-04-24 18:52:39 +03001107
1108 return vscale;
1109 }
1110
Ville Syrjälä17316932013-04-24 18:52:38 +03001111 /* Make the source viewport size an exact multiple of the scaling factors. */
Gustavo Padovan96d61a72014-09-05 17:04:47 -03001112 drm_rect_adjust_size(src,
1113 drm_rect_width(dst) * hscale - drm_rect_width(src),
1114 drm_rect_height(dst) * vscale - drm_rect_height(src));
Ville Syrjälä17316932013-04-24 18:52:38 +03001115
Gustavo Padovan96d61a72014-09-05 17:04:47 -03001116 drm_rect_rotate_inv(src, fb->width << 16, fb->height << 16,
Ville Syrjälä76eebda2014-08-05 11:26:52 +05301117 intel_plane->rotation);
1118
Ville Syrjälä17316932013-04-24 18:52:38 +03001119 /* sanity check to make sure the src viewport wasn't enlarged */
Gustavo Padovan96d61a72014-09-05 17:04:47 -03001120 WARN_ON(src->x1 < (int) orig_src->x1 ||
1121 src->y1 < (int) orig_src->y1 ||
1122 src->x2 > (int) orig_src->x2 ||
1123 src->y2 > (int) orig_src->y2);
Ville Syrjälä17316932013-04-24 18:52:38 +03001124
1125 /*
1126 * Hardware doesn't handle subpixel coordinates.
1127 * Adjust to (macro)pixel boundary, but be careful not to
1128 * increase the source viewport size, because that could
1129 * push the downscaling factor out of bounds.
Ville Syrjälä17316932013-04-24 18:52:38 +03001130 */
Gustavo Padovan96d61a72014-09-05 17:04:47 -03001131 src_x = src->x1 >> 16;
1132 src_w = drm_rect_width(src) >> 16;
1133 src_y = src->y1 >> 16;
1134 src_h = drm_rect_height(src) >> 16;
Ville Syrjälä17316932013-04-24 18:52:38 +03001135
1136 if (format_is_yuv(fb->pixel_format)) {
1137 src_x &= ~1;
1138 src_w &= ~1;
1139
1140 /*
1141 * Must keep src and dst the
1142 * same if we can't scale.
1143 */
1144 if (!intel_plane->can_scale)
1145 crtc_w &= ~1;
1146
1147 if (crtc_w == 0)
Gustavo Padovan96d61a72014-09-05 17:04:47 -03001148 state->visible = false;
Ville Syrjälä17316932013-04-24 18:52:38 +03001149 }
1150 }
1151
1152 /* Check size restrictions when scaling */
Gustavo Padovan96d61a72014-09-05 17:04:47 -03001153 if (state->visible && (src_w != crtc_w || src_h != crtc_h)) {
Ville Syrjälä17316932013-04-24 18:52:38 +03001154 unsigned int width_bytes;
1155
1156 WARN_ON(!intel_plane->can_scale);
1157
1158 /* FIXME interlacing min height is 6 */
1159
1160 if (crtc_w < 3 || crtc_h < 3)
Gustavo Padovan96d61a72014-09-05 17:04:47 -03001161 state->visible = false;
Ville Syrjälä17316932013-04-24 18:52:38 +03001162
1163 if (src_w < 3 || src_h < 3)
Gustavo Padovan96d61a72014-09-05 17:04:47 -03001164 state->visible = false;
Ville Syrjälä17316932013-04-24 18:52:38 +03001165
Gustavo Padovan96d61a72014-09-05 17:04:47 -03001166 width_bytes = ((src_x * pixel_size) & 63) +
1167 src_w * pixel_size;
Ville Syrjälä17316932013-04-24 18:52:38 +03001168
1169 if (src_w > 2048 || src_h > 2048 ||
1170 width_bytes > 4096 || fb->pitches[0] > 4096) {
1171 DRM_DEBUG_KMS("Source dimensions exceed hardware limits\n");
1172 return -EINVAL;
1173 }
1174 }
1175
Gustavo Padovan96d61a72014-09-05 17:04:47 -03001176 if (state->visible) {
1177 src->x1 = src_x;
1178 src->x2 = src_x + src_w;
1179 src->y1 = src_y;
1180 src->y2 = src_y + src_h;
1181 }
1182
1183 dst->x1 = crtc_x;
1184 dst->x2 = crtc_x + crtc_w;
1185 dst->y1 = crtc_y;
1186 dst->y2 = crtc_y + crtc_h;
1187
1188 return 0;
1189}
1190
1191static int
1192intel_commit_sprite_plane(struct drm_plane *plane,
1193 struct intel_plane_state *state)
1194{
1195 struct drm_device *dev = plane->dev;
1196 struct drm_crtc *crtc = state->crtc;
1197 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1198 struct intel_plane *intel_plane = to_intel_plane(plane);
1199 enum pipe pipe = intel_crtc->pipe;
1200 struct drm_framebuffer *fb = state->fb;
1201 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
1202 struct drm_i915_gem_object *obj = intel_fb->obj;
1203 struct drm_i915_gem_object *old_obj = intel_plane->obj;
1204 int crtc_x, crtc_y;
1205 unsigned int crtc_w, crtc_h;
1206 uint32_t src_x, src_y, src_w, src_h;
1207 struct drm_rect *dst = &state->dst;
1208 const struct drm_rect *clip = &state->clip;
1209 bool primary_enabled;
1210 int ret;
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001211
1212 /*
1213 * If the sprite is completely covering the primary plane,
1214 * we can disable the primary and save power.
1215 */
Gustavo Padovan96d61a72014-09-05 17:04:47 -03001216 primary_enabled = !drm_rect_equals(dst, clip) || colorkey_enabled(intel_plane);
1217 WARN_ON(!primary_enabled && !state->visible && intel_crtc->active);
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001218
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001219
Gustavo Padovan25067bf2014-09-10 12:03:17 -03001220 if (old_obj != obj) {
1221 mutex_lock(&dev->struct_mutex);
Ville Syrjälä82284b62013-10-01 18:02:12 +03001222
Gustavo Padovan25067bf2014-09-10 12:03:17 -03001223 /* Note that this will apply the VT-d workaround for scanouts,
1224 * which is more restrictive than required for sprites. (The
1225 * primary plane requires 256KiB alignment with 64 PTE padding,
1226 * the sprite planes only require 128KiB alignment and 32 PTE
1227 * padding.
1228 */
1229 ret = intel_pin_and_fence_fb_obj(dev, obj, NULL);
1230 if (ret == 0)
1231 i915_gem_track_fb(old_obj, obj,
1232 INTEL_FRONTBUFFER_SPRITE(pipe));
1233 mutex_unlock(&dev->struct_mutex);
1234 if (ret)
1235 return ret;
1236 }
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001237
Gustavo Padovan96d61a72014-09-05 17:04:47 -03001238 intel_plane->crtc_x = state->orig_dst.x1;
1239 intel_plane->crtc_y = state->orig_dst.y1;
1240 intel_plane->crtc_w = drm_rect_width(&state->orig_dst);
1241 intel_plane->crtc_h = drm_rect_height(&state->orig_dst);
1242 intel_plane->src_x = state->orig_src.x1;
1243 intel_plane->src_y = state->orig_src.y1;
1244 intel_plane->src_w = drm_rect_width(&state->orig_src);
1245 intel_plane->src_h = drm_rect_height(&state->orig_src);
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001246 intel_plane->obj = obj;
1247
Ville Syrjälä03c5b252013-10-01 18:02:11 +03001248 if (intel_crtc->active) {
Ville Syrjälä5b633d62014-04-29 13:35:47 +03001249 bool primary_was_enabled = intel_crtc->primary_enabled;
1250
1251 intel_crtc->primary_enabled = primary_enabled;
1252
Ville Syrjälä46a55d32014-05-21 14:04:46 +03001253 if (primary_was_enabled != primary_enabled)
1254 intel_crtc_wait_for_pending_flips(crtc);
1255
Ville Syrjälä5b633d62014-04-29 13:35:47 +03001256 if (primary_was_enabled && !primary_enabled)
1257 intel_pre_disable_primary(crtc);
Jesse Barnes175bd422011-12-13 13:19:39 -08001258
Gustavo Padovan96d61a72014-09-05 17:04:47 -03001259 if (state->visible) {
1260 crtc_x = state->dst.x1;
Gustavo Padovane259f172014-09-11 17:42:15 -03001261 crtc_y = state->dst.y1;
Gustavo Padovan96d61a72014-09-05 17:04:47 -03001262 crtc_w = drm_rect_width(&state->dst);
1263 crtc_h = drm_rect_height(&state->dst);
1264 src_x = state->src.x1;
1265 src_y = state->src.y1;
1266 src_w = drm_rect_width(&state->src);
1267 src_h = drm_rect_height(&state->src);
Ville Syrjälä03c5b252013-10-01 18:02:11 +03001268 intel_plane->update_plane(plane, crtc, fb, obj,
1269 crtc_x, crtc_y, crtc_w, crtc_h,
1270 src_x, src_y, src_w, src_h);
Gustavo Padovan96d61a72014-09-05 17:04:47 -03001271 } else {
Ville Syrjälä03c5b252013-10-01 18:02:11 +03001272 intel_plane->disable_plane(plane, crtc);
Gustavo Padovan96d61a72014-09-05 17:04:47 -03001273 }
1274
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001275
Daniel Vetterf99d7062014-06-19 16:01:59 +02001276 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_SPRITE(pipe));
1277
Ville Syrjälä5b633d62014-04-29 13:35:47 +03001278 if (!primary_was_enabled && primary_enabled)
1279 intel_post_enable_primary(crtc);
Ville Syrjälä03c5b252013-10-01 18:02:11 +03001280 }
Jesse Barnes175bd422011-12-13 13:19:39 -08001281
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001282 /* Unpin old obj after new one is active to avoid ugliness */
Gustavo Padovan25067bf2014-09-10 12:03:17 -03001283 if (old_obj && old_obj != obj) {
1284
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001285 /*
1286 * It's fairly common to simply update the position of
1287 * an existing object. In that case, we don't need to
1288 * wait for vblank to avoid ugliness, we only need to
1289 * do the pin & ref bookkeeping.
1290 */
Gustavo Padovan25067bf2014-09-10 12:03:17 -03001291 if (intel_crtc->active)
Ville Syrjälä2afd9ef2013-10-01 18:02:14 +03001292 intel_wait_for_vblank(dev, intel_crtc->pipe);
Ville Syrjälä82284b62013-10-01 18:02:12 +03001293
1294 mutex_lock(&dev->struct_mutex);
Chris Wilson1690e1e2011-12-14 13:57:08 +01001295 intel_unpin_fb_obj(old_obj);
Ville Syrjälä82284b62013-10-01 18:02:12 +03001296 mutex_unlock(&dev->struct_mutex);
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001297 }
1298
Ville Syrjälä82284b62013-10-01 18:02:12 +03001299 return 0;
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001300}
1301
1302static int
Gustavo Padovan96d61a72014-09-05 17:04:47 -03001303intel_update_plane(struct drm_plane *plane, struct drm_crtc *crtc,
1304 struct drm_framebuffer *fb, int crtc_x, int crtc_y,
1305 unsigned int crtc_w, unsigned int crtc_h,
1306 uint32_t src_x, uint32_t src_y,
1307 uint32_t src_w, uint32_t src_h)
1308{
1309 struct intel_plane_state state;
1310 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1311 int ret;
1312
1313 state.crtc = crtc;
1314 state.fb = fb;
1315
1316 /* sample coordinates in 16.16 fixed point */
1317 state.src.x1 = src_x;
1318 state.src.x2 = src_x + src_w;
1319 state.src.y1 = src_y;
1320 state.src.y2 = src_y + src_h;
1321
1322 /* integer pixels */
1323 state.dst.x1 = crtc_x;
1324 state.dst.x2 = crtc_x + crtc_w;
1325 state.dst.y1 = crtc_y;
1326 state.dst.y2 = crtc_y + crtc_h;
1327
1328 state.clip.x1 = 0;
1329 state.clip.y1 = 0;
1330 state.clip.x2 = intel_crtc->active ? intel_crtc->config.pipe_src_w : 0;
1331 state.clip.y2 = intel_crtc->active ? intel_crtc->config.pipe_src_h : 0;
1332 state.orig_src = state.src;
1333 state.orig_dst = state.dst;
1334
1335 ret = intel_check_sprite_plane(plane, &state);
1336 if (ret)
1337 return ret;
1338
1339 return intel_commit_sprite_plane(plane, &state);
1340}
1341
1342static int
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001343intel_disable_plane(struct drm_plane *plane)
1344{
1345 struct drm_device *dev = plane->dev;
1346 struct intel_plane *intel_plane = to_intel_plane(plane);
Ville Syrjälä03c5b252013-10-01 18:02:11 +03001347 struct intel_crtc *intel_crtc;
Daniel Vettera071fa02014-06-18 23:28:09 +02001348 enum pipe pipe;
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001349
Ville Syrjälä88a94a52013-08-07 13:30:23 +03001350 if (!plane->fb)
1351 return 0;
1352
1353 if (WARN_ON(!plane->crtc))
1354 return -EINVAL;
1355
Ville Syrjälä03c5b252013-10-01 18:02:11 +03001356 intel_crtc = to_intel_crtc(plane->crtc);
Daniel Vettera071fa02014-06-18 23:28:09 +02001357 pipe = intel_crtc->pipe;
Ville Syrjälä03c5b252013-10-01 18:02:11 +03001358
1359 if (intel_crtc->active) {
Ville Syrjälä5b633d62014-04-29 13:35:47 +03001360 bool primary_was_enabled = intel_crtc->primary_enabled;
1361
1362 intel_crtc->primary_enabled = true;
1363
Ville Syrjälä03c5b252013-10-01 18:02:11 +03001364 intel_plane->disable_plane(plane, plane->crtc);
Ville Syrjälä5b633d62014-04-29 13:35:47 +03001365
1366 if (!primary_was_enabled && intel_crtc->primary_enabled)
1367 intel_post_enable_primary(plane->crtc);
Ville Syrjälä03c5b252013-10-01 18:02:11 +03001368 }
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001369
Ville Syrjälä5f3fb462013-10-01 18:02:13 +03001370 if (intel_plane->obj) {
1371 if (intel_crtc->active)
1372 intel_wait_for_vblank(dev, intel_plane->pipe);
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001373
Ville Syrjälä5f3fb462013-10-01 18:02:13 +03001374 mutex_lock(&dev->struct_mutex);
1375 intel_unpin_fb_obj(intel_plane->obj);
Daniel Vettera071fa02014-06-18 23:28:09 +02001376 i915_gem_track_fb(intel_plane->obj, NULL,
1377 INTEL_FRONTBUFFER_SPRITE(pipe));
Ville Syrjälä5f3fb462013-10-01 18:02:13 +03001378 mutex_unlock(&dev->struct_mutex);
Ville Syrjäläc626d312013-03-27 17:49:13 +02001379
Ville Syrjälä5f3fb462013-10-01 18:02:13 +03001380 intel_plane->obj = NULL;
1381 }
Ville Syrjälä82284b62013-10-01 18:02:12 +03001382
Ville Syrjälä5f3fb462013-10-01 18:02:13 +03001383 return 0;
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001384}
1385
1386static void intel_destroy_plane(struct drm_plane *plane)
1387{
1388 struct intel_plane *intel_plane = to_intel_plane(plane);
1389 intel_disable_plane(plane);
1390 drm_plane_cleanup(plane);
1391 kfree(intel_plane);
1392}
1393
Jesse Barnes8ea30862012-01-03 08:05:39 -08001394int intel_sprite_set_colorkey(struct drm_device *dev, void *data,
1395 struct drm_file *file_priv)
1396{
1397 struct drm_intel_sprite_colorkey *set = data;
Jesse Barnes8ea30862012-01-03 08:05:39 -08001398 struct drm_plane *plane;
1399 struct intel_plane *intel_plane;
1400 int ret = 0;
1401
Daniel Vetter1cff8f62012-04-24 09:55:08 +02001402 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1403 return -ENODEV;
Jesse Barnes8ea30862012-01-03 08:05:39 -08001404
1405 /* Make sure we don't try to enable both src & dest simultaneously */
1406 if ((set->flags & (I915_SET_COLORKEY_DESTINATION | I915_SET_COLORKEY_SOURCE)) == (I915_SET_COLORKEY_DESTINATION | I915_SET_COLORKEY_SOURCE))
1407 return -EINVAL;
1408
Daniel Vettera0e99e62012-12-02 01:05:46 +01001409 drm_modeset_lock_all(dev);
Jesse Barnes8ea30862012-01-03 08:05:39 -08001410
Rob Clark7707e652014-07-17 23:30:04 -04001411 plane = drm_plane_find(dev, set->plane_id);
1412 if (!plane) {
Ville Syrjälä3f2c2052013-10-17 13:35:03 +03001413 ret = -ENOENT;
Jesse Barnes8ea30862012-01-03 08:05:39 -08001414 goto out_unlock;
1415 }
1416
Jesse Barnes8ea30862012-01-03 08:05:39 -08001417 intel_plane = to_intel_plane(plane);
1418 ret = intel_plane->update_colorkey(plane, set);
1419
1420out_unlock:
Daniel Vettera0e99e62012-12-02 01:05:46 +01001421 drm_modeset_unlock_all(dev);
Jesse Barnes8ea30862012-01-03 08:05:39 -08001422 return ret;
1423}
1424
1425int intel_sprite_get_colorkey(struct drm_device *dev, void *data,
1426 struct drm_file *file_priv)
1427{
1428 struct drm_intel_sprite_colorkey *get = data;
Jesse Barnes8ea30862012-01-03 08:05:39 -08001429 struct drm_plane *plane;
1430 struct intel_plane *intel_plane;
1431 int ret = 0;
1432
Daniel Vetter1cff8f62012-04-24 09:55:08 +02001433 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1434 return -ENODEV;
Jesse Barnes8ea30862012-01-03 08:05:39 -08001435
Daniel Vettera0e99e62012-12-02 01:05:46 +01001436 drm_modeset_lock_all(dev);
Jesse Barnes8ea30862012-01-03 08:05:39 -08001437
Rob Clark7707e652014-07-17 23:30:04 -04001438 plane = drm_plane_find(dev, get->plane_id);
1439 if (!plane) {
Ville Syrjälä3f2c2052013-10-17 13:35:03 +03001440 ret = -ENOENT;
Jesse Barnes8ea30862012-01-03 08:05:39 -08001441 goto out_unlock;
1442 }
1443
Jesse Barnes8ea30862012-01-03 08:05:39 -08001444 intel_plane = to_intel_plane(plane);
1445 intel_plane->get_colorkey(plane, get);
1446
1447out_unlock:
Daniel Vettera0e99e62012-12-02 01:05:46 +01001448 drm_modeset_unlock_all(dev);
Jesse Barnes8ea30862012-01-03 08:05:39 -08001449 return ret;
1450}
1451
Sonika Jindal48404c12014-08-22 14:06:04 +05301452int intel_plane_set_property(struct drm_plane *plane,
1453 struct drm_property *prop,
1454 uint64_t val)
Ville Syrjälä7ed6eee2014-08-05 11:26:55 +05301455{
1456 struct drm_device *dev = plane->dev;
1457 struct intel_plane *intel_plane = to_intel_plane(plane);
1458 uint64_t old_val;
1459 int ret = -ENOENT;
1460
1461 if (prop == dev->mode_config.rotation_property) {
1462 /* exactly one rotation angle please */
1463 if (hweight32(val & 0xf) != 1)
1464 return -EINVAL;
1465
Ville Syrjälä09dba002014-09-01 18:08:25 +03001466 if (intel_plane->rotation == val)
1467 return 0;
1468
Ville Syrjälä7ed6eee2014-08-05 11:26:55 +05301469 old_val = intel_plane->rotation;
1470 intel_plane->rotation = val;
1471 ret = intel_plane_restore(plane);
1472 if (ret)
1473 intel_plane->rotation = old_val;
1474 }
1475
1476 return ret;
1477}
1478
Ville Syrjäläe57465f2014-08-05 11:26:53 +05301479int intel_plane_restore(struct drm_plane *plane)
Jesse Barnes5e1bac22013-03-26 09:25:43 -07001480{
1481 struct intel_plane *intel_plane = to_intel_plane(plane);
1482
1483 if (!plane->crtc || !plane->fb)
Ville Syrjäläe57465f2014-08-05 11:26:53 +05301484 return 0;
Jesse Barnes5e1bac22013-03-26 09:25:43 -07001485
Sonika Jindal48404c12014-08-22 14:06:04 +05301486 return plane->funcs->update_plane(plane, plane->crtc, plane->fb,
Ville Syrjäläe57465f2014-08-05 11:26:53 +05301487 intel_plane->crtc_x, intel_plane->crtc_y,
1488 intel_plane->crtc_w, intel_plane->crtc_h,
1489 intel_plane->src_x, intel_plane->src_y,
1490 intel_plane->src_w, intel_plane->src_h);
Jesse Barnes5e1bac22013-03-26 09:25:43 -07001491}
1492
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03001493void intel_plane_disable(struct drm_plane *plane)
1494{
1495 if (!plane->crtc || !plane->fb)
1496 return;
1497
1498 intel_disable_plane(plane);
1499}
1500
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001501static const struct drm_plane_funcs intel_plane_funcs = {
1502 .update_plane = intel_update_plane,
1503 .disable_plane = intel_disable_plane,
1504 .destroy = intel_destroy_plane,
Ville Syrjälä7ed6eee2014-08-05 11:26:55 +05301505 .set_property = intel_plane_set_property,
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001506};
1507
Chris Wilsond1686ae2012-04-10 11:41:49 +01001508static uint32_t ilk_plane_formats[] = {
1509 DRM_FORMAT_XRGB8888,
1510 DRM_FORMAT_YUYV,
1511 DRM_FORMAT_YVYU,
1512 DRM_FORMAT_UYVY,
1513 DRM_FORMAT_VYUY,
1514};
1515
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001516static uint32_t snb_plane_formats[] = {
1517 DRM_FORMAT_XBGR8888,
1518 DRM_FORMAT_XRGB8888,
1519 DRM_FORMAT_YUYV,
1520 DRM_FORMAT_YVYU,
1521 DRM_FORMAT_UYVY,
1522 DRM_FORMAT_VYUY,
1523};
1524
Jesse Barnes7f1f3852013-04-02 11:22:20 -07001525static uint32_t vlv_plane_formats[] = {
1526 DRM_FORMAT_RGB565,
1527 DRM_FORMAT_ABGR8888,
1528 DRM_FORMAT_ARGB8888,
1529 DRM_FORMAT_XBGR8888,
1530 DRM_FORMAT_XRGB8888,
1531 DRM_FORMAT_XBGR2101010,
1532 DRM_FORMAT_ABGR2101010,
1533 DRM_FORMAT_YUYV,
1534 DRM_FORMAT_YVYU,
1535 DRM_FORMAT_UYVY,
1536 DRM_FORMAT_VYUY,
1537};
1538
Damien Lespiaudc2a41b2013-12-04 00:49:41 +00001539static uint32_t skl_plane_formats[] = {
1540 DRM_FORMAT_RGB565,
1541 DRM_FORMAT_ABGR8888,
1542 DRM_FORMAT_ARGB8888,
1543 DRM_FORMAT_XBGR8888,
1544 DRM_FORMAT_XRGB8888,
1545 DRM_FORMAT_YUYV,
1546 DRM_FORMAT_YVYU,
1547 DRM_FORMAT_UYVY,
1548 DRM_FORMAT_VYUY,
1549};
1550
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001551int
Jesse Barnes7f1f3852013-04-02 11:22:20 -07001552intel_plane_init(struct drm_device *dev, enum pipe pipe, int plane)
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001553{
1554 struct intel_plane *intel_plane;
1555 unsigned long possible_crtcs;
Chris Wilsond1686ae2012-04-10 11:41:49 +01001556 const uint32_t *plane_formats;
1557 int num_plane_formats;
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001558 int ret;
1559
Chris Wilsond1686ae2012-04-10 11:41:49 +01001560 if (INTEL_INFO(dev)->gen < 5)
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001561 return -ENODEV;
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001562
Daniel Vetterb14c5672013-09-19 12:18:32 +02001563 intel_plane = kzalloc(sizeof(*intel_plane), GFP_KERNEL);
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001564 if (!intel_plane)
1565 return -ENOMEM;
1566
Chris Wilsond1686ae2012-04-10 11:41:49 +01001567 switch (INTEL_INFO(dev)->gen) {
1568 case 5:
1569 case 6:
Damien Lespiau2d354c32012-10-22 18:19:27 +01001570 intel_plane->can_scale = true;
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001571 intel_plane->max_downscale = 16;
Chris Wilsond1686ae2012-04-10 11:41:49 +01001572 intel_plane->update_plane = ilk_update_plane;
1573 intel_plane->disable_plane = ilk_disable_plane;
1574 intel_plane->update_colorkey = ilk_update_colorkey;
1575 intel_plane->get_colorkey = ilk_get_colorkey;
1576
1577 if (IS_GEN6(dev)) {
1578 plane_formats = snb_plane_formats;
1579 num_plane_formats = ARRAY_SIZE(snb_plane_formats);
1580 } else {
1581 plane_formats = ilk_plane_formats;
1582 num_plane_formats = ARRAY_SIZE(ilk_plane_formats);
1583 }
1584 break;
1585
1586 case 7:
Ben Widawsky4e0bbc32013-11-02 21:07:07 -07001587 case 8:
Damien Lespiaud49f7092013-04-25 15:15:00 +01001588 if (IS_IVYBRIDGE(dev)) {
Damien Lespiau2d354c32012-10-22 18:19:27 +01001589 intel_plane->can_scale = true;
Damien Lespiaud49f7092013-04-25 15:15:00 +01001590 intel_plane->max_downscale = 2;
1591 } else {
1592 intel_plane->can_scale = false;
1593 intel_plane->max_downscale = 1;
1594 }
Chris Wilsond1686ae2012-04-10 11:41:49 +01001595
Jesse Barnes7f1f3852013-04-02 11:22:20 -07001596 if (IS_VALLEYVIEW(dev)) {
Jesse Barnes7f1f3852013-04-02 11:22:20 -07001597 intel_plane->update_plane = vlv_update_plane;
1598 intel_plane->disable_plane = vlv_disable_plane;
1599 intel_plane->update_colorkey = vlv_update_colorkey;
1600 intel_plane->get_colorkey = vlv_get_colorkey;
1601
1602 plane_formats = vlv_plane_formats;
1603 num_plane_formats = ARRAY_SIZE(vlv_plane_formats);
1604 } else {
Jesse Barnes7f1f3852013-04-02 11:22:20 -07001605 intel_plane->update_plane = ivb_update_plane;
1606 intel_plane->disable_plane = ivb_disable_plane;
1607 intel_plane->update_colorkey = ivb_update_colorkey;
1608 intel_plane->get_colorkey = ivb_get_colorkey;
1609
1610 plane_formats = snb_plane_formats;
1611 num_plane_formats = ARRAY_SIZE(snb_plane_formats);
1612 }
Chris Wilsond1686ae2012-04-10 11:41:49 +01001613 break;
Damien Lespiaudc2a41b2013-12-04 00:49:41 +00001614 case 9:
1615 /*
1616 * FIXME: Skylake planes can be scaled (with some restrictions),
1617 * but this is for another time.
1618 */
1619 intel_plane->can_scale = false;
1620 intel_plane->max_downscale = 1;
1621 intel_plane->update_plane = skl_update_plane;
1622 intel_plane->disable_plane = skl_disable_plane;
1623 intel_plane->update_colorkey = skl_update_colorkey;
1624 intel_plane->get_colorkey = skl_get_colorkey;
Chris Wilsond1686ae2012-04-10 11:41:49 +01001625
Damien Lespiaudc2a41b2013-12-04 00:49:41 +00001626 plane_formats = skl_plane_formats;
1627 num_plane_formats = ARRAY_SIZE(skl_plane_formats);
1628 break;
Chris Wilsond1686ae2012-04-10 11:41:49 +01001629 default:
Jesper Juhla8b0bba2012-06-27 00:55:37 +02001630 kfree(intel_plane);
Chris Wilsond1686ae2012-04-10 11:41:49 +01001631 return -ENODEV;
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001632 }
1633
1634 intel_plane->pipe = pipe;
Jesse Barnes7f1f3852013-04-02 11:22:20 -07001635 intel_plane->plane = plane;
Ville Syrjälä76eebda2014-08-05 11:26:52 +05301636 intel_plane->rotation = BIT(DRM_ROTATE_0);
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001637 possible_crtcs = (1 << pipe);
Derek Foreman8fe8a3f2014-09-03 10:38:20 -03001638 ret = drm_universal_plane_init(dev, &intel_plane->base, possible_crtcs,
1639 &intel_plane_funcs,
1640 plane_formats, num_plane_formats,
1641 DRM_PLANE_TYPE_OVERLAY);
Ville Syrjälä7ed6eee2014-08-05 11:26:55 +05301642 if (ret) {
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001643 kfree(intel_plane);
Ville Syrjälä7ed6eee2014-08-05 11:26:55 +05301644 goto out;
1645 }
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001646
Ville Syrjälä7ed6eee2014-08-05 11:26:55 +05301647 if (!dev->mode_config.rotation_property)
1648 dev->mode_config.rotation_property =
1649 drm_mode_create_rotation_property(dev,
1650 BIT(DRM_ROTATE_0) |
1651 BIT(DRM_ROTATE_180));
1652
1653 if (dev->mode_config.rotation_property)
1654 drm_object_attach_property(&intel_plane->base.base,
1655 dev->mode_config.rotation_property,
1656 intel_plane->rotation);
1657
1658 out:
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001659 return ret;
1660}