blob: a4306cf94f56e5ab1bbb13ec36682ce486585ea3 [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ä210871b62014-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ä210871b62014-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ä210871b62014-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
Ville Syrjäläb39d53f2013-08-06 22:24:09 +0300142vlv_update_plane(struct drm_plane *dplane, struct drm_crtc *crtc,
143 struct drm_framebuffer *fb,
Jesse Barnes7f1f3852013-04-02 11:22:20 -0700144 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 = dplane->dev;
150 struct drm_i915_private *dev_priv = dev->dev_private;
151 struct intel_plane *intel_plane = to_intel_plane(dplane);
Ville Syrjälä8d7849d2014-04-29 13:35:46 +0300152 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes7f1f3852013-04-02 11:22:20 -0700153 int pipe = intel_plane->pipe;
154 int plane = intel_plane->plane;
155 u32 sprctl;
156 unsigned long sprsurf_offset, linear_offset;
157 int pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
Ville Syrjälä8d7849d2014-04-29 13:35:46 +0300158 u32 start_vbl_count;
159 bool atomic_update;
Jesse Barnes7f1f3852013-04-02 11:22:20 -0700160
161 sprctl = I915_READ(SPCNTR(pipe, plane));
162
163 /* Mask out pixel format bits in case we change it */
164 sprctl &= ~SP_PIXFORMAT_MASK;
165 sprctl &= ~SP_YUV_BYTE_ORDER_MASK;
166 sprctl &= ~SP_TILED;
Ville Syrjälä76eebda2014-08-05 11:26:52 +0530167 sprctl &= ~SP_ROTATE_180;
Jesse Barnes7f1f3852013-04-02 11:22:20 -0700168
169 switch (fb->pixel_format) {
170 case DRM_FORMAT_YUYV:
171 sprctl |= SP_FORMAT_YUV422 | SP_YUV_ORDER_YUYV;
172 break;
173 case DRM_FORMAT_YVYU:
174 sprctl |= SP_FORMAT_YUV422 | SP_YUV_ORDER_YVYU;
175 break;
176 case DRM_FORMAT_UYVY:
177 sprctl |= SP_FORMAT_YUV422 | SP_YUV_ORDER_UYVY;
178 break;
179 case DRM_FORMAT_VYUY:
180 sprctl |= SP_FORMAT_YUV422 | SP_YUV_ORDER_VYUY;
181 break;
182 case DRM_FORMAT_RGB565:
183 sprctl |= SP_FORMAT_BGR565;
184 break;
185 case DRM_FORMAT_XRGB8888:
186 sprctl |= SP_FORMAT_BGRX8888;
187 break;
188 case DRM_FORMAT_ARGB8888:
189 sprctl |= SP_FORMAT_BGRA8888;
190 break;
191 case DRM_FORMAT_XBGR2101010:
192 sprctl |= SP_FORMAT_RGBX1010102;
193 break;
194 case DRM_FORMAT_ABGR2101010:
195 sprctl |= SP_FORMAT_RGBA1010102;
196 break;
197 case DRM_FORMAT_XBGR8888:
198 sprctl |= SP_FORMAT_RGBX8888;
199 break;
200 case DRM_FORMAT_ABGR8888:
201 sprctl |= SP_FORMAT_RGBA8888;
202 break;
203 default:
204 /*
205 * If we get here one of the upper layers failed to filter
206 * out the unsupported plane formats
207 */
208 BUG();
209 break;
210 }
211
Ville Syrjälä4ea67bc2013-11-18 18:32:38 -0800212 /*
213 * Enable gamma to match primary/cursor plane behaviour.
214 * FIXME should be user controllable via propertiesa.
215 */
216 sprctl |= SP_GAMMA_ENABLE;
217
Jesse Barnes7f1f3852013-04-02 11:22:20 -0700218 if (obj->tiling_mode != I915_TILING_NONE)
219 sprctl |= SP_TILED;
220
221 sprctl |= SP_ENABLE;
222
Damien Lespiaued57cb82014-07-15 09:21:24 +0200223 intel_update_sprite_watermarks(dplane, crtc, src_w, src_h,
224 pixel_size, true,
Ville Syrjälä67ca28f2013-07-05 11:57:14 +0300225 src_w != crtc_w || src_h != crtc_h);
226
Jesse Barnes7f1f3852013-04-02 11:22:20 -0700227 /* Sizes are 0 based */
228 src_w--;
229 src_h--;
230 crtc_w--;
231 crtc_h--;
232
Jesse Barnes7f1f3852013-04-02 11:22:20 -0700233 linear_offset = y * fb->pitches[0] + x * pixel_size;
234 sprsurf_offset = intel_gen4_compute_page_offset(&x, &y,
235 obj->tiling_mode,
236 pixel_size,
237 fb->pitches[0]);
238 linear_offset -= sprsurf_offset;
239
Ville Syrjälä76eebda2014-08-05 11:26:52 +0530240 if (intel_plane->rotation == BIT(DRM_ROTATE_180)) {
241 sprctl |= SP_ROTATE_180;
242
243 x += src_w;
244 y += src_h;
245 linear_offset += src_h * fb->pitches[0] + src_w * pixel_size;
246 }
247
Ville Syrjälä8d7849d2014-04-29 13:35:46 +0300248 atomic_update = intel_pipe_update_start(intel_crtc, &start_vbl_count);
249
Ville Syrjälä5b633d62014-04-29 13:35:47 +0300250 intel_update_primary_plane(intel_crtc);
251
Ville Syrjäläca6ad022014-01-17 20:09:03 +0200252 I915_WRITE(SPSTRIDE(pipe, plane), fb->pitches[0]);
253 I915_WRITE(SPPOS(pipe, plane), (crtc_y << 16) | crtc_x);
254
Jesse Barnes7f1f3852013-04-02 11:22:20 -0700255 if (obj->tiling_mode != I915_TILING_NONE)
256 I915_WRITE(SPTILEOFF(pipe, plane), (y << 16) | x);
257 else
258 I915_WRITE(SPLINOFF(pipe, plane), linear_offset);
259
260 I915_WRITE(SPSIZE(pipe, plane), (crtc_h << 16) | crtc_w);
261 I915_WRITE(SPCNTR(pipe, plane), sprctl);
Daniel Vetter85ba7b72014-01-24 10:31:44 +0100262 I915_WRITE(SPSURF(pipe, plane), i915_gem_obj_ggtt_offset(obj) +
263 sprsurf_offset);
Ville Syrjälä5b633d62014-04-29 13:35:47 +0300264
265 intel_flush_primary_plane(dev_priv, intel_crtc->plane);
Ville Syrjälä8d7849d2014-04-29 13:35:46 +0300266
267 if (atomic_update)
268 intel_pipe_update_end(intel_crtc, start_vbl_count);
Jesse Barnes7f1f3852013-04-02 11:22:20 -0700269}
270
271static void
Ville Syrjäläb39d53f2013-08-06 22:24:09 +0300272vlv_disable_plane(struct drm_plane *dplane, struct drm_crtc *crtc)
Jesse Barnes7f1f3852013-04-02 11:22:20 -0700273{
274 struct drm_device *dev = dplane->dev;
275 struct drm_i915_private *dev_priv = dev->dev_private;
276 struct intel_plane *intel_plane = to_intel_plane(dplane);
Ville Syrjälä8d7849d2014-04-29 13:35:46 +0300277 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes7f1f3852013-04-02 11:22:20 -0700278 int pipe = intel_plane->pipe;
279 int plane = intel_plane->plane;
Ville Syrjälä8d7849d2014-04-29 13:35:46 +0300280 u32 start_vbl_count;
281 bool atomic_update;
282
283 atomic_update = intel_pipe_update_start(intel_crtc, &start_vbl_count);
Jesse Barnes7f1f3852013-04-02 11:22:20 -0700284
Ville Syrjälä5b633d62014-04-29 13:35:47 +0300285 intel_update_primary_plane(intel_crtc);
286
Jesse Barnes7f1f3852013-04-02 11:22:20 -0700287 I915_WRITE(SPCNTR(pipe, plane), I915_READ(SPCNTR(pipe, plane)) &
288 ~SP_ENABLE);
289 /* Activate double buffered register update */
Daniel Vetter85ba7b72014-01-24 10:31:44 +0100290 I915_WRITE(SPSURF(pipe, plane), 0);
Ville Syrjälä5b633d62014-04-29 13:35:47 +0300291
292 intel_flush_primary_plane(dev_priv, intel_crtc->plane);
Ville Syrjäläa95fd8c2013-08-06 22:24:12 +0300293
Ville Syrjälä8d7849d2014-04-29 13:35:46 +0300294 if (atomic_update)
295 intel_pipe_update_end(intel_crtc, start_vbl_count);
296
Damien Lespiaued57cb82014-07-15 09:21:24 +0200297 intel_update_sprite_watermarks(dplane, crtc, 0, 0, 0, false, false);
Jesse Barnes7f1f3852013-04-02 11:22:20 -0700298}
299
300static int
301vlv_update_colorkey(struct drm_plane *dplane,
302 struct drm_intel_sprite_colorkey *key)
303{
304 struct drm_device *dev = dplane->dev;
305 struct drm_i915_private *dev_priv = dev->dev_private;
306 struct intel_plane *intel_plane = to_intel_plane(dplane);
307 int pipe = intel_plane->pipe;
308 int plane = intel_plane->plane;
309 u32 sprctl;
310
311 if (key->flags & I915_SET_COLORKEY_DESTINATION)
312 return -EINVAL;
313
314 I915_WRITE(SPKEYMINVAL(pipe, plane), key->min_value);
315 I915_WRITE(SPKEYMAXVAL(pipe, plane), key->max_value);
316 I915_WRITE(SPKEYMSK(pipe, plane), key->channel_mask);
317
318 sprctl = I915_READ(SPCNTR(pipe, plane));
319 sprctl &= ~SP_SOURCE_KEY;
320 if (key->flags & I915_SET_COLORKEY_SOURCE)
321 sprctl |= SP_SOURCE_KEY;
322 I915_WRITE(SPCNTR(pipe, plane), sprctl);
323
324 POSTING_READ(SPKEYMSK(pipe, plane));
325
326 return 0;
327}
328
329static void
330vlv_get_colorkey(struct drm_plane *dplane,
331 struct drm_intel_sprite_colorkey *key)
332{
333 struct drm_device *dev = dplane->dev;
334 struct drm_i915_private *dev_priv = dev->dev_private;
335 struct intel_plane *intel_plane = to_intel_plane(dplane);
336 int pipe = intel_plane->pipe;
337 int plane = intel_plane->plane;
338 u32 sprctl;
339
340 key->min_value = I915_READ(SPKEYMINVAL(pipe, plane));
341 key->max_value = I915_READ(SPKEYMAXVAL(pipe, plane));
342 key->channel_mask = I915_READ(SPKEYMSK(pipe, plane));
343
344 sprctl = I915_READ(SPCNTR(pipe, plane));
345 if (sprctl & SP_SOURCE_KEY)
346 key->flags = I915_SET_COLORKEY_SOURCE;
347 else
348 key->flags = I915_SET_COLORKEY_NONE;
349}
350
351static void
Ville Syrjäläb39d53f2013-08-06 22:24:09 +0300352ivb_update_plane(struct drm_plane *plane, struct drm_crtc *crtc,
353 struct drm_framebuffer *fb,
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800354 struct drm_i915_gem_object *obj, int crtc_x, int crtc_y,
355 unsigned int crtc_w, unsigned int crtc_h,
356 uint32_t x, uint32_t y,
357 uint32_t src_w, uint32_t src_h)
358{
359 struct drm_device *dev = plane->dev;
360 struct drm_i915_private *dev_priv = dev->dev_private;
361 struct intel_plane *intel_plane = to_intel_plane(plane);
Ville Syrjälä8d7849d2014-04-29 13:35:46 +0300362 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800363 int pipe = intel_plane->pipe;
364 u32 sprctl, sprscale = 0;
Damien Lespiau5a35e992012-10-26 18:20:12 +0100365 unsigned long sprsurf_offset, linear_offset;
Ville Syrjälä2bd3c3c2012-10-31 17:50:20 +0200366 int pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
Ville Syrjälä8d7849d2014-04-29 13:35:46 +0300367 u32 start_vbl_count;
368 bool atomic_update;
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800369
370 sprctl = I915_READ(SPRCTL(pipe));
371
372 /* Mask out pixel format bits in case we change it */
373 sprctl &= ~SPRITE_PIXFORMAT_MASK;
374 sprctl &= ~SPRITE_RGB_ORDER_RGBX;
375 sprctl &= ~SPRITE_YUV_BYTE_ORDER_MASK;
Jesse Barnese86fe0d2012-06-26 13:10:11 -0700376 sprctl &= ~SPRITE_TILED;
Ville Syrjälä76eebda2014-08-05 11:26:52 +0530377 sprctl &= ~SPRITE_ROTATE_180;
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800378
379 switch (fb->pixel_format) {
380 case DRM_FORMAT_XBGR8888:
Vijay Purushothaman5ee36912012-08-23 12:08:57 +0530381 sprctl |= SPRITE_FORMAT_RGBX888 | SPRITE_RGB_ORDER_RGBX;
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800382 break;
383 case DRM_FORMAT_XRGB8888:
Vijay Purushothaman5ee36912012-08-23 12:08:57 +0530384 sprctl |= SPRITE_FORMAT_RGBX888;
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800385 break;
386 case DRM_FORMAT_YUYV:
387 sprctl |= SPRITE_FORMAT_YUV422 | SPRITE_YUV_ORDER_YUYV;
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800388 break;
389 case DRM_FORMAT_YVYU:
390 sprctl |= SPRITE_FORMAT_YUV422 | SPRITE_YUV_ORDER_YVYU;
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800391 break;
392 case DRM_FORMAT_UYVY:
393 sprctl |= SPRITE_FORMAT_YUV422 | SPRITE_YUV_ORDER_UYVY;
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800394 break;
395 case DRM_FORMAT_VYUY:
396 sprctl |= SPRITE_FORMAT_YUV422 | SPRITE_YUV_ORDER_VYUY;
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800397 break;
398 default:
Ville Syrjälä28d491d2012-10-31 17:50:21 +0200399 BUG();
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800400 }
401
Ville Syrjälä4ea67bc2013-11-18 18:32:38 -0800402 /*
403 * Enable gamma to match primary/cursor plane behaviour.
404 * FIXME should be user controllable via propertiesa.
405 */
406 sprctl |= SPRITE_GAMMA_ENABLE;
407
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800408 if (obj->tiling_mode != I915_TILING_NONE)
409 sprctl |= SPRITE_TILED;
410
Ville Syrjäläb42c6002013-11-03 13:47:27 +0200411 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Paulo Zanoni1f5d76d2013-08-23 19:51:28 -0300412 sprctl &= ~SPRITE_TRICKLE_FEED_DISABLE;
413 else
414 sprctl |= SPRITE_TRICKLE_FEED_DISABLE;
415
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800416 sprctl |= SPRITE_ENABLE;
417
Ville Syrjälä6bbfa1c2013-11-02 21:07:39 -0700418 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Ville Syrjälä86d3efc2013-01-18 19:11:38 +0200419 sprctl |= SPRITE_PIPE_CSC_ENABLE;
420
Damien Lespiaued57cb82014-07-15 09:21:24 +0200421 intel_update_sprite_watermarks(plane, crtc, src_w, src_h, pixel_size,
422 true,
Ville Syrjälä67ca28f2013-07-05 11:57:14 +0300423 src_w != crtc_w || src_h != crtc_h);
424
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800425 /* Sizes are 0 based */
426 src_w--;
427 src_h--;
428 crtc_w--;
429 crtc_h--;
430
Ville Syrjälä8553c182013-12-05 15:51:39 +0200431 if (crtc_w != src_w || crtc_h != src_h)
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800432 sprscale = SPRITE_SCALE_ENABLE | (src_w << 16) | src_h;
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800433
Chris Wilsonca320ac2012-12-19 12:14:22 +0000434 linear_offset = y * fb->pitches[0] + x * pixel_size;
Damien Lespiau5a35e992012-10-26 18:20:12 +0100435 sprsurf_offset =
Chris Wilsonbc752862013-02-21 20:04:31 +0000436 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
437 pixel_size, fb->pitches[0]);
Damien Lespiau5a35e992012-10-26 18:20:12 +0100438 linear_offset -= sprsurf_offset;
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800439
Ville Syrjälä76eebda2014-08-05 11:26:52 +0530440 if (intel_plane->rotation == BIT(DRM_ROTATE_180)) {
441 sprctl |= SPRITE_ROTATE_180;
442
443 /* HSW and BDW does this automagically in hardware */
444 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
445 x += src_w;
446 y += src_h;
447 linear_offset += src_h * fb->pitches[0] +
448 src_w * pixel_size;
449 }
450 }
451
Ville Syrjälä8d7849d2014-04-29 13:35:46 +0300452 atomic_update = intel_pipe_update_start(intel_crtc, &start_vbl_count);
453
Ville Syrjälä5b633d62014-04-29 13:35:47 +0300454 intel_update_primary_plane(intel_crtc);
455
Ville Syrjäläca6ad022014-01-17 20:09:03 +0200456 I915_WRITE(SPRSTRIDE(pipe), fb->pitches[0]);
457 I915_WRITE(SPRPOS(pipe), (crtc_y << 16) | crtc_x);
458
Damien Lespiau5a35e992012-10-26 18:20:12 +0100459 /* HSW consolidates SPRTILEOFF and SPRLINOFF into a single SPROFFSET
460 * register */
Paulo Zanonib3dc6852013-11-02 21:07:33 -0700461 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Damien Lespiau5a35e992012-10-26 18:20:12 +0100462 I915_WRITE(SPROFFSET(pipe), (y << 16) | x);
463 else if (obj->tiling_mode != I915_TILING_NONE)
464 I915_WRITE(SPRTILEOFF(pipe), (y << 16) | x);
465 else
466 I915_WRITE(SPRLINOFF(pipe), linear_offset);
Damien Lespiauc54173a2012-10-26 18:20:11 +0100467
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800468 I915_WRITE(SPRSIZE(pipe), (crtc_h << 16) | crtc_w);
Damien Lespiau2d354c32012-10-22 18:19:27 +0100469 if (intel_plane->can_scale)
470 I915_WRITE(SPRSCALE(pipe), sprscale);
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800471 I915_WRITE(SPRCTL(pipe), sprctl);
Daniel Vetter85ba7b72014-01-24 10:31:44 +0100472 I915_WRITE(SPRSURF(pipe),
473 i915_gem_obj_ggtt_offset(obj) + sprsurf_offset);
Ville Syrjälä5b633d62014-04-29 13:35:47 +0300474
475 intel_flush_primary_plane(dev_priv, intel_crtc->plane);
Ville Syrjälä8d7849d2014-04-29 13:35:46 +0300476
477 if (atomic_update)
478 intel_pipe_update_end(intel_crtc, start_vbl_count);
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800479}
480
481static void
Ville Syrjäläb39d53f2013-08-06 22:24:09 +0300482ivb_disable_plane(struct drm_plane *plane, struct drm_crtc *crtc)
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800483{
484 struct drm_device *dev = plane->dev;
485 struct drm_i915_private *dev_priv = dev->dev_private;
486 struct intel_plane *intel_plane = to_intel_plane(plane);
Ville Syrjälä8d7849d2014-04-29 13:35:46 +0300487 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800488 int pipe = intel_plane->pipe;
Ville Syrjälä8d7849d2014-04-29 13:35:46 +0300489 u32 start_vbl_count;
490 bool atomic_update;
491
492 atomic_update = intel_pipe_update_start(intel_crtc, &start_vbl_count);
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800493
Ville Syrjälä5b633d62014-04-29 13:35:47 +0300494 intel_update_primary_plane(intel_crtc);
495
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800496 I915_WRITE(SPRCTL(pipe), I915_READ(SPRCTL(pipe)) & ~SPRITE_ENABLE);
497 /* Can't leave the scaler enabled... */
Damien Lespiau2d354c32012-10-22 18:19:27 +0100498 if (intel_plane->can_scale)
499 I915_WRITE(SPRSCALE(pipe), 0);
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800500 /* Activate double buffered register update */
Daniel Vetter85ba7b72014-01-24 10:31:44 +0100501 I915_WRITE(SPRSURF(pipe), 0);
Ville Syrjälä5b633d62014-04-29 13:35:47 +0300502
503 intel_flush_primary_plane(dev_priv, intel_crtc->plane);
Chris Wilson828ed3e2012-04-18 17:12:26 +0100504
Ville Syrjälä8d7849d2014-04-29 13:35:46 +0300505 if (atomic_update)
506 intel_pipe_update_end(intel_crtc, start_vbl_count);
507
Ville Syrjälä1bd09ec2013-12-05 15:51:41 +0200508 /*
509 * Avoid underruns when disabling the sprite.
510 * FIXME remove once watermark updates are done properly.
511 */
512 intel_wait_for_vblank(dev, pipe);
513
Damien Lespiaued57cb82014-07-15 09:21:24 +0200514 intel_update_sprite_watermarks(plane, crtc, 0, 0, 0, false, false);
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800515}
516
Jesse Barnes8ea30862012-01-03 08:05:39 -0800517static int
518ivb_update_colorkey(struct drm_plane *plane,
519 struct drm_intel_sprite_colorkey *key)
520{
521 struct drm_device *dev = plane->dev;
522 struct drm_i915_private *dev_priv = dev->dev_private;
523 struct intel_plane *intel_plane;
524 u32 sprctl;
525 int ret = 0;
526
527 intel_plane = to_intel_plane(plane);
528
529 I915_WRITE(SPRKEYVAL(intel_plane->pipe), key->min_value);
530 I915_WRITE(SPRKEYMAX(intel_plane->pipe), key->max_value);
531 I915_WRITE(SPRKEYMSK(intel_plane->pipe), key->channel_mask);
532
533 sprctl = I915_READ(SPRCTL(intel_plane->pipe));
534 sprctl &= ~(SPRITE_SOURCE_KEY | SPRITE_DEST_KEY);
535 if (key->flags & I915_SET_COLORKEY_DESTINATION)
536 sprctl |= SPRITE_DEST_KEY;
537 else if (key->flags & I915_SET_COLORKEY_SOURCE)
538 sprctl |= SPRITE_SOURCE_KEY;
539 I915_WRITE(SPRCTL(intel_plane->pipe), sprctl);
540
541 POSTING_READ(SPRKEYMSK(intel_plane->pipe));
542
543 return ret;
544}
545
546static void
547ivb_get_colorkey(struct drm_plane *plane, struct drm_intel_sprite_colorkey *key)
548{
549 struct drm_device *dev = plane->dev;
550 struct drm_i915_private *dev_priv = dev->dev_private;
551 struct intel_plane *intel_plane;
552 u32 sprctl;
553
554 intel_plane = to_intel_plane(plane);
555
556 key->min_value = I915_READ(SPRKEYVAL(intel_plane->pipe));
557 key->max_value = I915_READ(SPRKEYMAX(intel_plane->pipe));
558 key->channel_mask = I915_READ(SPRKEYMSK(intel_plane->pipe));
559 key->flags = 0;
560
561 sprctl = I915_READ(SPRCTL(intel_plane->pipe));
562
563 if (sprctl & SPRITE_DEST_KEY)
564 key->flags = I915_SET_COLORKEY_DESTINATION;
565 else if (sprctl & SPRITE_SOURCE_KEY)
566 key->flags = I915_SET_COLORKEY_SOURCE;
567 else
568 key->flags = I915_SET_COLORKEY_NONE;
569}
570
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800571static void
Ville Syrjäläb39d53f2013-08-06 22:24:09 +0300572ilk_update_plane(struct drm_plane *plane, struct drm_crtc *crtc,
573 struct drm_framebuffer *fb,
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800574 struct drm_i915_gem_object *obj, int crtc_x, int crtc_y,
575 unsigned int crtc_w, unsigned int crtc_h,
576 uint32_t x, uint32_t y,
577 uint32_t src_w, uint32_t src_h)
578{
579 struct drm_device *dev = plane->dev;
580 struct drm_i915_private *dev_priv = dev->dev_private;
581 struct intel_plane *intel_plane = to_intel_plane(plane);
Ville Syrjälä8d7849d2014-04-29 13:35:46 +0300582 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjälä2bd3c3c2012-10-31 17:50:20 +0200583 int pipe = intel_plane->pipe;
Damien Lespiau5a35e992012-10-26 18:20:12 +0100584 unsigned long dvssurf_offset, linear_offset;
Chris Wilson8aaa81a2012-04-14 22:14:26 +0100585 u32 dvscntr, dvsscale;
Ville Syrjälä2bd3c3c2012-10-31 17:50:20 +0200586 int pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
Ville Syrjälä8d7849d2014-04-29 13:35:46 +0300587 u32 start_vbl_count;
588 bool atomic_update;
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800589
590 dvscntr = I915_READ(DVSCNTR(pipe));
591
592 /* Mask out pixel format bits in case we change it */
593 dvscntr &= ~DVS_PIXFORMAT_MASK;
Jesse Barnesab2f9df2012-02-27 12:40:10 -0800594 dvscntr &= ~DVS_RGB_ORDER_XBGR;
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800595 dvscntr &= ~DVS_YUV_BYTE_ORDER_MASK;
Ander Conselvan de Oliveira79626522012-07-13 15:50:33 +0300596 dvscntr &= ~DVS_TILED;
Ville Syrjälä76eebda2014-08-05 11:26:52 +0530597 dvscntr &= ~DVS_ROTATE_180;
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800598
599 switch (fb->pixel_format) {
600 case DRM_FORMAT_XBGR8888:
Jesse Barnesab2f9df2012-02-27 12:40:10 -0800601 dvscntr |= DVS_FORMAT_RGBX888 | DVS_RGB_ORDER_XBGR;
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800602 break;
603 case DRM_FORMAT_XRGB8888:
Jesse Barnesab2f9df2012-02-27 12:40:10 -0800604 dvscntr |= DVS_FORMAT_RGBX888;
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800605 break;
606 case DRM_FORMAT_YUYV:
607 dvscntr |= DVS_FORMAT_YUV422 | DVS_YUV_ORDER_YUYV;
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800608 break;
609 case DRM_FORMAT_YVYU:
610 dvscntr |= DVS_FORMAT_YUV422 | DVS_YUV_ORDER_YVYU;
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800611 break;
612 case DRM_FORMAT_UYVY:
613 dvscntr |= DVS_FORMAT_YUV422 | DVS_YUV_ORDER_UYVY;
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800614 break;
615 case DRM_FORMAT_VYUY:
616 dvscntr |= DVS_FORMAT_YUV422 | DVS_YUV_ORDER_VYUY;
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800617 break;
618 default:
Ville Syrjälä28d491d2012-10-31 17:50:21 +0200619 BUG();
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800620 }
621
Ville Syrjälä4ea67bc2013-11-18 18:32:38 -0800622 /*
623 * Enable gamma to match primary/cursor plane behaviour.
624 * FIXME should be user controllable via propertiesa.
625 */
626 dvscntr |= DVS_GAMMA_ENABLE;
627
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800628 if (obj->tiling_mode != I915_TILING_NONE)
629 dvscntr |= DVS_TILED;
630
Chris Wilsond1686ae2012-04-10 11:41:49 +0100631 if (IS_GEN6(dev))
632 dvscntr |= DVS_TRICKLE_FEED_DISABLE; /* must disable */
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800633 dvscntr |= DVS_ENABLE;
634
Damien Lespiaued57cb82014-07-15 09:21:24 +0200635 intel_update_sprite_watermarks(plane, crtc, src_w, src_h,
636 pixel_size, true,
Ville Syrjälä67ca28f2013-07-05 11:57:14 +0300637 src_w != crtc_w || src_h != crtc_h);
638
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800639 /* Sizes are 0 based */
640 src_w--;
641 src_h--;
642 crtc_w--;
643 crtc_h--;
644
Chris Wilson8aaa81a2012-04-14 22:14:26 +0100645 dvsscale = 0;
Ville Syrjälä8368f012013-12-05 15:51:31 +0200646 if (crtc_w != src_w || crtc_h != src_h)
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800647 dvsscale = DVS_SCALE_ENABLE | (src_w << 16) | src_h;
648
Chris Wilsonca320ac2012-12-19 12:14:22 +0000649 linear_offset = y * fb->pitches[0] + x * pixel_size;
Damien Lespiau5a35e992012-10-26 18:20:12 +0100650 dvssurf_offset =
Chris Wilsonbc752862013-02-21 20:04:31 +0000651 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
652 pixel_size, fb->pitches[0]);
Damien Lespiau5a35e992012-10-26 18:20:12 +0100653 linear_offset -= dvssurf_offset;
654
Ville Syrjälä76eebda2014-08-05 11:26:52 +0530655 if (intel_plane->rotation == BIT(DRM_ROTATE_180)) {
656 dvscntr |= DVS_ROTATE_180;
657
658 x += src_w;
659 y += src_h;
660 linear_offset += src_h * fb->pitches[0] + src_w * pixel_size;
661 }
662
Ville Syrjälä8d7849d2014-04-29 13:35:46 +0300663 atomic_update = intel_pipe_update_start(intel_crtc, &start_vbl_count);
664
Ville Syrjälä5b633d62014-04-29 13:35:47 +0300665 intel_update_primary_plane(intel_crtc);
666
Ville Syrjäläca6ad022014-01-17 20:09:03 +0200667 I915_WRITE(DVSSTRIDE(pipe), fb->pitches[0]);
668 I915_WRITE(DVSPOS(pipe), (crtc_y << 16) | crtc_x);
669
Damien Lespiau5a35e992012-10-26 18:20:12 +0100670 if (obj->tiling_mode != I915_TILING_NONE)
671 I915_WRITE(DVSTILEOFF(pipe), (y << 16) | x);
672 else
673 I915_WRITE(DVSLINOFF(pipe), linear_offset);
674
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800675 I915_WRITE(DVSSIZE(pipe), (crtc_h << 16) | crtc_w);
676 I915_WRITE(DVSSCALE(pipe), dvsscale);
677 I915_WRITE(DVSCNTR(pipe), dvscntr);
Daniel Vetter85ba7b72014-01-24 10:31:44 +0100678 I915_WRITE(DVSSURF(pipe),
679 i915_gem_obj_ggtt_offset(obj) + dvssurf_offset);
Ville Syrjälä5b633d62014-04-29 13:35:47 +0300680
681 intel_flush_primary_plane(dev_priv, intel_crtc->plane);
Ville Syrjälä8d7849d2014-04-29 13:35:46 +0300682
683 if (atomic_update)
684 intel_pipe_update_end(intel_crtc, start_vbl_count);
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800685}
686
687static void
Ville Syrjäläb39d53f2013-08-06 22:24:09 +0300688ilk_disable_plane(struct drm_plane *plane, struct drm_crtc *crtc)
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800689{
690 struct drm_device *dev = plane->dev;
691 struct drm_i915_private *dev_priv = dev->dev_private;
692 struct intel_plane *intel_plane = to_intel_plane(plane);
Ville Syrjälä8d7849d2014-04-29 13:35:46 +0300693 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800694 int pipe = intel_plane->pipe;
Ville Syrjälä8d7849d2014-04-29 13:35:46 +0300695 u32 start_vbl_count;
696 bool atomic_update;
697
698 atomic_update = intel_pipe_update_start(intel_crtc, &start_vbl_count);
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800699
Ville Syrjälä5b633d62014-04-29 13:35:47 +0300700 intel_update_primary_plane(intel_crtc);
701
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800702 I915_WRITE(DVSCNTR(pipe), I915_READ(DVSCNTR(pipe)) & ~DVS_ENABLE);
703 /* Disable the scaler */
704 I915_WRITE(DVSSCALE(pipe), 0);
705 /* Flush double buffered register updates */
Daniel Vetter85ba7b72014-01-24 10:31:44 +0100706 I915_WRITE(DVSSURF(pipe), 0);
Ville Syrjälä5b633d62014-04-29 13:35:47 +0300707
708 intel_flush_primary_plane(dev_priv, intel_crtc->plane);
Ville Syrjäläa95fd8c2013-08-06 22:24:12 +0300709
Ville Syrjälä8d7849d2014-04-29 13:35:46 +0300710 if (atomic_update)
711 intel_pipe_update_end(intel_crtc, start_vbl_count);
712
Ville Syrjälä1bd09ec2013-12-05 15:51:41 +0200713 /*
714 * Avoid underruns when disabling the sprite.
715 * FIXME remove once watermark updates are done properly.
716 */
717 intel_wait_for_vblank(dev, pipe);
718
Damien Lespiaued57cb82014-07-15 09:21:24 +0200719 intel_update_sprite_watermarks(plane, crtc, 0, 0, 0, false, false);
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800720}
721
Jesse Barnes175bd422011-12-13 13:19:39 -0800722static void
Ville Syrjälä5b633d62014-04-29 13:35:47 +0300723intel_post_enable_primary(struct drm_crtc *crtc)
Jesse Barnes175bd422011-12-13 13:19:39 -0800724{
725 struct drm_device *dev = crtc->dev;
Jesse Barnes175bd422011-12-13 13:19:39 -0800726 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjäläabae50e2013-10-01 18:02:16 +0300727
Ville Syrjälä20bc86732013-10-01 18:02:17 +0300728 /*
Ville Syrjälä33c3b0d2014-06-24 13:59:28 +0300729 * BDW signals flip done immediately if the plane
730 * is disabled, even if the plane enable is already
731 * armed to occur at the next vblank :(
732 */
733 if (IS_BROADWELL(dev))
734 intel_wait_for_vblank(dev, intel_crtc->pipe);
735
736 /*
Ville Syrjälä20bc86732013-10-01 18:02:17 +0300737 * FIXME IPS should be fine as long as one plane is
738 * enabled, but in practice it seems to have problems
739 * when going from primary only to sprite only and vice
740 * versa.
741 */
Ville Syrjäläcea165c2014-04-15 21:41:35 +0300742 hsw_enable_ips(intel_crtc);
Ville Syrjälä20bc86732013-10-01 18:02:17 +0300743
Ville Syrjälä82284b62013-10-01 18:02:12 +0300744 mutex_lock(&dev->struct_mutex);
Chris Wilson93314b52012-06-13 17:36:55 +0100745 intel_update_fbc(dev);
Ville Syrjälä82284b62013-10-01 18:02:12 +0300746 mutex_unlock(&dev->struct_mutex);
Jesse Barnes175bd422011-12-13 13:19:39 -0800747}
748
749static void
Ville Syrjälä5b633d62014-04-29 13:35:47 +0300750intel_pre_disable_primary(struct drm_crtc *crtc)
Jesse Barnes175bd422011-12-13 13:19:39 -0800751{
752 struct drm_device *dev = crtc->dev;
753 struct drm_i915_private *dev_priv = dev->dev_private;
754 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjälä82284b62013-10-01 18:02:12 +0300755
756 mutex_lock(&dev->struct_mutex);
Ville Syrjäläabae50e2013-10-01 18:02:16 +0300757 if (dev_priv->fbc.plane == intel_crtc->plane)
758 intel_disable_fbc(dev);
Ville Syrjälä82284b62013-10-01 18:02:12 +0300759 mutex_unlock(&dev->struct_mutex);
Ville Syrjäläabae50e2013-10-01 18:02:16 +0300760
Ville Syrjälä20bc86732013-10-01 18:02:17 +0300761 /*
762 * FIXME IPS should be fine as long as one plane is
763 * enabled, but in practice it seems to have problems
764 * when going from primary only to sprite only and vice
765 * versa.
766 */
767 hsw_disable_ips(intel_crtc);
Jesse Barnes175bd422011-12-13 13:19:39 -0800768}
769
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800770static int
Chris Wilsond1686ae2012-04-10 11:41:49 +0100771ilk_update_colorkey(struct drm_plane *plane,
Jesse Barnes8ea30862012-01-03 08:05:39 -0800772 struct drm_intel_sprite_colorkey *key)
773{
774 struct drm_device *dev = plane->dev;
775 struct drm_i915_private *dev_priv = dev->dev_private;
776 struct intel_plane *intel_plane;
777 u32 dvscntr;
778 int ret = 0;
779
780 intel_plane = to_intel_plane(plane);
781
782 I915_WRITE(DVSKEYVAL(intel_plane->pipe), key->min_value);
783 I915_WRITE(DVSKEYMAX(intel_plane->pipe), key->max_value);
784 I915_WRITE(DVSKEYMSK(intel_plane->pipe), key->channel_mask);
785
786 dvscntr = I915_READ(DVSCNTR(intel_plane->pipe));
787 dvscntr &= ~(DVS_SOURCE_KEY | DVS_DEST_KEY);
788 if (key->flags & I915_SET_COLORKEY_DESTINATION)
789 dvscntr |= DVS_DEST_KEY;
790 else if (key->flags & I915_SET_COLORKEY_SOURCE)
791 dvscntr |= DVS_SOURCE_KEY;
792 I915_WRITE(DVSCNTR(intel_plane->pipe), dvscntr);
793
794 POSTING_READ(DVSKEYMSK(intel_plane->pipe));
795
796 return ret;
797}
798
799static void
Chris Wilsond1686ae2012-04-10 11:41:49 +0100800ilk_get_colorkey(struct drm_plane *plane, struct drm_intel_sprite_colorkey *key)
Jesse Barnes8ea30862012-01-03 08:05:39 -0800801{
802 struct drm_device *dev = plane->dev;
803 struct drm_i915_private *dev_priv = dev->dev_private;
804 struct intel_plane *intel_plane;
805 u32 dvscntr;
806
807 intel_plane = to_intel_plane(plane);
808
809 key->min_value = I915_READ(DVSKEYVAL(intel_plane->pipe));
810 key->max_value = I915_READ(DVSKEYMAX(intel_plane->pipe));
811 key->channel_mask = I915_READ(DVSKEYMSK(intel_plane->pipe));
812 key->flags = 0;
813
814 dvscntr = I915_READ(DVSCNTR(intel_plane->pipe));
815
816 if (dvscntr & DVS_DEST_KEY)
817 key->flags = I915_SET_COLORKEY_DESTINATION;
818 else if (dvscntr & DVS_SOURCE_KEY)
819 key->flags = I915_SET_COLORKEY_SOURCE;
820 else
821 key->flags = I915_SET_COLORKEY_NONE;
822}
823
Ville Syrjälä17316932013-04-24 18:52:38 +0300824static bool
825format_is_yuv(uint32_t format)
826{
827 switch (format) {
828 case DRM_FORMAT_YUYV:
829 case DRM_FORMAT_UYVY:
830 case DRM_FORMAT_VYUY:
831 case DRM_FORMAT_YVYU:
832 return true;
833 default:
834 return false;
835 }
836}
837
Ville Syrjäläefb31d12013-12-05 15:51:40 +0200838static bool colorkey_enabled(struct intel_plane *intel_plane)
839{
840 struct drm_intel_sprite_colorkey key;
841
842 intel_plane->get_colorkey(&intel_plane->base, &key);
843
844 return key.flags != I915_SET_COLORKEY_NONE;
845}
846
Jesse Barnes8ea30862012-01-03 08:05:39 -0800847static int
Gustavo Padovan96d61a72014-09-05 17:04:47 -0300848intel_check_sprite_plane(struct drm_plane *plane,
849 struct intel_plane_state *state)
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800850{
Gustavo Padovan96d61a72014-09-05 17:04:47 -0300851 struct intel_crtc *intel_crtc = to_intel_crtc(state->crtc);
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800852 struct intel_plane *intel_plane = to_intel_plane(plane);
Gustavo Padovan96d61a72014-09-05 17:04:47 -0300853 struct drm_framebuffer *fb = state->fb;
Ville Syrjälä2afd9ef2013-10-01 18:02:14 +0300854 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
855 struct drm_i915_gem_object *obj = intel_fb->obj;
Gustavo Padovan96d61a72014-09-05 17:04:47 -0300856 int crtc_x, crtc_y;
857 unsigned int crtc_w, crtc_h;
858 uint32_t src_x, src_y, src_w, src_h;
859 struct drm_rect *src = &state->src;
860 struct drm_rect *dst = &state->dst;
861 struct drm_rect *orig_src = &state->orig_src;
862 const struct drm_rect *clip = &state->clip;
Ville Syrjälä17316932013-04-24 18:52:38 +0300863 int hscale, vscale;
864 int max_scale, min_scale;
865 int pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
Jesse Barnes5e1bac22013-03-26 09:25:43 -0700866
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800867 /* Don't modify another pipe's plane */
Ville Syrjälä17316932013-04-24 18:52:38 +0300868 if (intel_plane->pipe != intel_crtc->pipe) {
869 DRM_DEBUG_KMS("Wrong plane <-> crtc mapping\n");
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800870 return -EINVAL;
Ville Syrjälä17316932013-04-24 18:52:38 +0300871 }
872
873 /* FIXME check all gen limits */
874 if (fb->width < 3 || fb->height < 3 || fb->pitches[0] > 16384) {
875 DRM_DEBUG_KMS("Unsuitable framebuffer for plane\n");
876 return -EINVAL;
877 }
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800878
Damien Lespiau94c64192012-10-29 15:14:51 +0000879 /* Sprite planes can be linear or x-tiled surfaces */
880 switch (obj->tiling_mode) {
881 case I915_TILING_NONE:
882 case I915_TILING_X:
883 break;
884 default:
Ville Syrjälä17316932013-04-24 18:52:38 +0300885 DRM_DEBUG_KMS("Unsupported tiling mode\n");
Damien Lespiau94c64192012-10-29 15:14:51 +0000886 return -EINVAL;
887 }
888
Ville Syrjälä3c3686c2013-04-24 18:52:39 +0300889 /*
890 * FIXME the following code does a bunch of fuzzy adjustments to the
891 * coordinates and sizes. We probably need some way to decide whether
892 * more strict checking should be done instead.
893 */
Ville Syrjälä17316932013-04-24 18:52:38 +0300894 max_scale = intel_plane->max_downscale << 16;
895 min_scale = intel_plane->can_scale ? 1 : (1 << 16);
896
Gustavo Padovan96d61a72014-09-05 17:04:47 -0300897 drm_rect_rotate(src, fb->width << 16, fb->height << 16,
Ville Syrjälä76eebda2014-08-05 11:26:52 +0530898 intel_plane->rotation);
899
Gustavo Padovan96d61a72014-09-05 17:04:47 -0300900 hscale = drm_rect_calc_hscale_relaxed(src, dst, min_scale, max_scale);
Ville Syrjälä3c3686c2013-04-24 18:52:39 +0300901 BUG_ON(hscale < 0);
Ville Syrjälä17316932013-04-24 18:52:38 +0300902
Gustavo Padovan96d61a72014-09-05 17:04:47 -0300903 vscale = drm_rect_calc_vscale_relaxed(src, dst, min_scale, max_scale);
Ville Syrjälä3c3686c2013-04-24 18:52:39 +0300904 BUG_ON(vscale < 0);
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800905
Gustavo Padovan96d61a72014-09-05 17:04:47 -0300906 state->visible = drm_rect_clip_scaled(src, dst, clip, hscale, vscale);
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800907
Gustavo Padovan96d61a72014-09-05 17:04:47 -0300908 crtc_x = dst->x1;
909 crtc_y = dst->y1;
910 crtc_w = drm_rect_width(dst);
911 crtc_h = drm_rect_height(dst);
Damien Lespiau2d354c32012-10-22 18:19:27 +0100912
Gustavo Padovan96d61a72014-09-05 17:04:47 -0300913 if (state->visible) {
Ville Syrjälä3c3686c2013-04-24 18:52:39 +0300914 /* check again in case clipping clamped the results */
Gustavo Padovan96d61a72014-09-05 17:04:47 -0300915 hscale = drm_rect_calc_hscale(src, dst, min_scale, max_scale);
Ville Syrjälä3c3686c2013-04-24 18:52:39 +0300916 if (hscale < 0) {
917 DRM_DEBUG_KMS("Horizontal scaling factor out of limits\n");
Gustavo Padovan96d61a72014-09-05 17:04:47 -0300918 drm_rect_debug_print(src, true);
919 drm_rect_debug_print(dst, false);
Ville Syrjälä3c3686c2013-04-24 18:52:39 +0300920
921 return hscale;
922 }
923
Gustavo Padovan96d61a72014-09-05 17:04:47 -0300924 vscale = drm_rect_calc_vscale(src, dst, min_scale, max_scale);
Ville Syrjälä3c3686c2013-04-24 18:52:39 +0300925 if (vscale < 0) {
926 DRM_DEBUG_KMS("Vertical scaling factor out of limits\n");
Gustavo Padovan96d61a72014-09-05 17:04:47 -0300927 drm_rect_debug_print(src, true);
928 drm_rect_debug_print(dst, false);
Ville Syrjälä3c3686c2013-04-24 18:52:39 +0300929
930 return vscale;
931 }
932
Ville Syrjälä17316932013-04-24 18:52:38 +0300933 /* Make the source viewport size an exact multiple of the scaling factors. */
Gustavo Padovan96d61a72014-09-05 17:04:47 -0300934 drm_rect_adjust_size(src,
935 drm_rect_width(dst) * hscale - drm_rect_width(src),
936 drm_rect_height(dst) * vscale - drm_rect_height(src));
Ville Syrjälä17316932013-04-24 18:52:38 +0300937
Gustavo Padovan96d61a72014-09-05 17:04:47 -0300938 drm_rect_rotate_inv(src, fb->width << 16, fb->height << 16,
Ville Syrjälä76eebda2014-08-05 11:26:52 +0530939 intel_plane->rotation);
940
Ville Syrjälä17316932013-04-24 18:52:38 +0300941 /* sanity check to make sure the src viewport wasn't enlarged */
Gustavo Padovan96d61a72014-09-05 17:04:47 -0300942 WARN_ON(src->x1 < (int) orig_src->x1 ||
943 src->y1 < (int) orig_src->y1 ||
944 src->x2 > (int) orig_src->x2 ||
945 src->y2 > (int) orig_src->y2);
Ville Syrjälä17316932013-04-24 18:52:38 +0300946
947 /*
948 * Hardware doesn't handle subpixel coordinates.
949 * Adjust to (macro)pixel boundary, but be careful not to
950 * increase the source viewport size, because that could
951 * push the downscaling factor out of bounds.
Ville Syrjälä17316932013-04-24 18:52:38 +0300952 */
Gustavo Padovan96d61a72014-09-05 17:04:47 -0300953 src_x = src->x1 >> 16;
954 src_w = drm_rect_width(src) >> 16;
955 src_y = src->y1 >> 16;
956 src_h = drm_rect_height(src) >> 16;
Ville Syrjälä17316932013-04-24 18:52:38 +0300957
958 if (format_is_yuv(fb->pixel_format)) {
959 src_x &= ~1;
960 src_w &= ~1;
961
962 /*
963 * Must keep src and dst the
964 * same if we can't scale.
965 */
966 if (!intel_plane->can_scale)
967 crtc_w &= ~1;
968
969 if (crtc_w == 0)
Gustavo Padovan96d61a72014-09-05 17:04:47 -0300970 state->visible = false;
Ville Syrjälä17316932013-04-24 18:52:38 +0300971 }
972 }
973
974 /* Check size restrictions when scaling */
Gustavo Padovan96d61a72014-09-05 17:04:47 -0300975 if (state->visible && (src_w != crtc_w || src_h != crtc_h)) {
Ville Syrjälä17316932013-04-24 18:52:38 +0300976 unsigned int width_bytes;
977
978 WARN_ON(!intel_plane->can_scale);
979
980 /* FIXME interlacing min height is 6 */
981
982 if (crtc_w < 3 || crtc_h < 3)
Gustavo Padovan96d61a72014-09-05 17:04:47 -0300983 state->visible = false;
Ville Syrjälä17316932013-04-24 18:52:38 +0300984
985 if (src_w < 3 || src_h < 3)
Gustavo Padovan96d61a72014-09-05 17:04:47 -0300986 state->visible = false;
Ville Syrjälä17316932013-04-24 18:52:38 +0300987
Gustavo Padovan96d61a72014-09-05 17:04:47 -0300988 width_bytes = ((src_x * pixel_size) & 63) +
989 src_w * pixel_size;
Ville Syrjälä17316932013-04-24 18:52:38 +0300990
991 if (src_w > 2048 || src_h > 2048 ||
992 width_bytes > 4096 || fb->pitches[0] > 4096) {
993 DRM_DEBUG_KMS("Source dimensions exceed hardware limits\n");
994 return -EINVAL;
995 }
996 }
997
Gustavo Padovan96d61a72014-09-05 17:04:47 -0300998 if (state->visible) {
999 src->x1 = src_x;
1000 src->x2 = src_x + src_w;
1001 src->y1 = src_y;
1002 src->y2 = src_y + src_h;
1003 }
1004
1005 dst->x1 = crtc_x;
1006 dst->x2 = crtc_x + crtc_w;
1007 dst->y1 = crtc_y;
1008 dst->y2 = crtc_y + crtc_h;
1009
1010 return 0;
1011}
1012
1013static int
1014intel_commit_sprite_plane(struct drm_plane *plane,
1015 struct intel_plane_state *state)
1016{
1017 struct drm_device *dev = plane->dev;
1018 struct drm_crtc *crtc = state->crtc;
1019 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1020 struct intel_plane *intel_plane = to_intel_plane(plane);
1021 enum pipe pipe = intel_crtc->pipe;
1022 struct drm_framebuffer *fb = state->fb;
1023 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
1024 struct drm_i915_gem_object *obj = intel_fb->obj;
1025 struct drm_i915_gem_object *old_obj = intel_plane->obj;
1026 int crtc_x, crtc_y;
1027 unsigned int crtc_w, crtc_h;
1028 uint32_t src_x, src_y, src_w, src_h;
1029 struct drm_rect *dst = &state->dst;
1030 const struct drm_rect *clip = &state->clip;
1031 bool primary_enabled;
1032 int ret;
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001033
1034 /*
1035 * If the sprite is completely covering the primary plane,
1036 * we can disable the primary and save power.
1037 */
Gustavo Padovan96d61a72014-09-05 17:04:47 -03001038 primary_enabled = !drm_rect_equals(dst, clip) || colorkey_enabled(intel_plane);
1039 WARN_ON(!primary_enabled && !state->visible && intel_crtc->active);
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001040
1041 mutex_lock(&dev->struct_mutex);
1042
Chris Wilson693db182013-03-05 14:52:39 +00001043 /* Note that this will apply the VT-d workaround for scanouts,
1044 * which is more restrictive than required for sprites. (The
1045 * primary plane requires 256KiB alignment with 64 PTE padding,
1046 * the sprite planes only require 128KiB alignment and 32 PTE padding.
1047 */
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001048 ret = intel_pin_and_fence_fb_obj(dev, obj, NULL);
Ville Syrjälä82284b62013-10-01 18:02:12 +03001049
Daniel Vettera071fa02014-06-18 23:28:09 +02001050 i915_gem_track_fb(old_obj, obj,
1051 INTEL_FRONTBUFFER_SPRITE(pipe));
Ville Syrjälä82284b62013-10-01 18:02:12 +03001052 mutex_unlock(&dev->struct_mutex);
1053
Jesse Barnes00c2064b2012-01-13 15:48:39 -08001054 if (ret)
Ville Syrjälä82284b62013-10-01 18:02:12 +03001055 return ret;
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001056
Gustavo Padovan96d61a72014-09-05 17:04:47 -03001057 intel_plane->crtc_x = state->orig_dst.x1;
1058 intel_plane->crtc_y = state->orig_dst.y1;
1059 intel_plane->crtc_w = drm_rect_width(&state->orig_dst);
1060 intel_plane->crtc_h = drm_rect_height(&state->orig_dst);
1061 intel_plane->src_x = state->orig_src.x1;
1062 intel_plane->src_y = state->orig_src.y1;
1063 intel_plane->src_w = drm_rect_width(&state->orig_src);
1064 intel_plane->src_h = drm_rect_height(&state->orig_src);
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001065 intel_plane->obj = obj;
1066
Ville Syrjälä03c5b252013-10-01 18:02:11 +03001067 if (intel_crtc->active) {
Ville Syrjälä5b633d62014-04-29 13:35:47 +03001068 bool primary_was_enabled = intel_crtc->primary_enabled;
1069
1070 intel_crtc->primary_enabled = primary_enabled;
1071
Ville Syrjälä46a55d32014-05-21 14:04:46 +03001072 if (primary_was_enabled != primary_enabled)
1073 intel_crtc_wait_for_pending_flips(crtc);
1074
Ville Syrjälä5b633d62014-04-29 13:35:47 +03001075 if (primary_was_enabled && !primary_enabled)
1076 intel_pre_disable_primary(crtc);
Jesse Barnes175bd422011-12-13 13:19:39 -08001077
Gustavo Padovan96d61a72014-09-05 17:04:47 -03001078 if (state->visible) {
1079 crtc_x = state->dst.x1;
1080 crtc_y = state->dst.x2;
1081 crtc_w = drm_rect_width(&state->dst);
1082 crtc_h = drm_rect_height(&state->dst);
1083 src_x = state->src.x1;
1084 src_y = state->src.y1;
1085 src_w = drm_rect_width(&state->src);
1086 src_h = drm_rect_height(&state->src);
Ville Syrjälä03c5b252013-10-01 18:02:11 +03001087 intel_plane->update_plane(plane, crtc, fb, obj,
1088 crtc_x, crtc_y, crtc_w, crtc_h,
1089 src_x, src_y, src_w, src_h);
Gustavo Padovan96d61a72014-09-05 17:04:47 -03001090 } else {
Ville Syrjälä03c5b252013-10-01 18:02:11 +03001091 intel_plane->disable_plane(plane, crtc);
Gustavo Padovan96d61a72014-09-05 17:04:47 -03001092 }
1093
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001094
Daniel Vetterf99d7062014-06-19 16:01:59 +02001095 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_SPRITE(pipe));
1096
Ville Syrjälä5b633d62014-04-29 13:35:47 +03001097 if (!primary_was_enabled && primary_enabled)
1098 intel_post_enable_primary(crtc);
Ville Syrjälä03c5b252013-10-01 18:02:11 +03001099 }
Jesse Barnes175bd422011-12-13 13:19:39 -08001100
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001101 /* Unpin old obj after new one is active to avoid ugliness */
1102 if (old_obj) {
1103 /*
1104 * It's fairly common to simply update the position of
1105 * an existing object. In that case, we don't need to
1106 * wait for vblank to avoid ugliness, we only need to
1107 * do the pin & ref bookkeeping.
1108 */
Ville Syrjälä82284b62013-10-01 18:02:12 +03001109 if (old_obj != obj && intel_crtc->active)
Ville Syrjälä2afd9ef2013-10-01 18:02:14 +03001110 intel_wait_for_vblank(dev, intel_crtc->pipe);
Ville Syrjälä82284b62013-10-01 18:02:12 +03001111
1112 mutex_lock(&dev->struct_mutex);
Chris Wilson1690e1e2011-12-14 13:57:08 +01001113 intel_unpin_fb_obj(old_obj);
Ville Syrjälä82284b62013-10-01 18:02:12 +03001114 mutex_unlock(&dev->struct_mutex);
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001115 }
1116
Ville Syrjälä82284b62013-10-01 18:02:12 +03001117 return 0;
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001118}
1119
1120static int
Gustavo Padovan96d61a72014-09-05 17:04:47 -03001121intel_update_plane(struct drm_plane *plane, struct drm_crtc *crtc,
1122 struct drm_framebuffer *fb, int crtc_x, int crtc_y,
1123 unsigned int crtc_w, unsigned int crtc_h,
1124 uint32_t src_x, uint32_t src_y,
1125 uint32_t src_w, uint32_t src_h)
1126{
1127 struct intel_plane_state state;
1128 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1129 int ret;
1130
1131 state.crtc = crtc;
1132 state.fb = fb;
1133
1134 /* sample coordinates in 16.16 fixed point */
1135 state.src.x1 = src_x;
1136 state.src.x2 = src_x + src_w;
1137 state.src.y1 = src_y;
1138 state.src.y2 = src_y + src_h;
1139
1140 /* integer pixels */
1141 state.dst.x1 = crtc_x;
1142 state.dst.x2 = crtc_x + crtc_w;
1143 state.dst.y1 = crtc_y;
1144 state.dst.y2 = crtc_y + crtc_h;
1145
1146 state.clip.x1 = 0;
1147 state.clip.y1 = 0;
1148 state.clip.x2 = intel_crtc->active ? intel_crtc->config.pipe_src_w : 0;
1149 state.clip.y2 = intel_crtc->active ? intel_crtc->config.pipe_src_h : 0;
1150 state.orig_src = state.src;
1151 state.orig_dst = state.dst;
1152
1153 ret = intel_check_sprite_plane(plane, &state);
1154 if (ret)
1155 return ret;
1156
1157 return intel_commit_sprite_plane(plane, &state);
1158}
1159
1160static int
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001161intel_disable_plane(struct drm_plane *plane)
1162{
1163 struct drm_device *dev = plane->dev;
1164 struct intel_plane *intel_plane = to_intel_plane(plane);
Ville Syrjälä03c5b252013-10-01 18:02:11 +03001165 struct intel_crtc *intel_crtc;
Daniel Vettera071fa02014-06-18 23:28:09 +02001166 enum pipe pipe;
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001167
Ville Syrjälä88a94a52013-08-07 13:30:23 +03001168 if (!plane->fb)
1169 return 0;
1170
1171 if (WARN_ON(!plane->crtc))
1172 return -EINVAL;
1173
Ville Syrjälä03c5b252013-10-01 18:02:11 +03001174 intel_crtc = to_intel_crtc(plane->crtc);
Daniel Vettera071fa02014-06-18 23:28:09 +02001175 pipe = intel_crtc->pipe;
Ville Syrjälä03c5b252013-10-01 18:02:11 +03001176
1177 if (intel_crtc->active) {
Ville Syrjälä5b633d62014-04-29 13:35:47 +03001178 bool primary_was_enabled = intel_crtc->primary_enabled;
1179
1180 intel_crtc->primary_enabled = true;
1181
Ville Syrjälä03c5b252013-10-01 18:02:11 +03001182 intel_plane->disable_plane(plane, plane->crtc);
Ville Syrjälä5b633d62014-04-29 13:35:47 +03001183
1184 if (!primary_was_enabled && intel_crtc->primary_enabled)
1185 intel_post_enable_primary(plane->crtc);
Ville Syrjälä03c5b252013-10-01 18:02:11 +03001186 }
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001187
Ville Syrjälä5f3fb462013-10-01 18:02:13 +03001188 if (intel_plane->obj) {
1189 if (intel_crtc->active)
1190 intel_wait_for_vblank(dev, intel_plane->pipe);
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001191
Ville Syrjälä5f3fb462013-10-01 18:02:13 +03001192 mutex_lock(&dev->struct_mutex);
1193 intel_unpin_fb_obj(intel_plane->obj);
Daniel Vettera071fa02014-06-18 23:28:09 +02001194 i915_gem_track_fb(intel_plane->obj, NULL,
1195 INTEL_FRONTBUFFER_SPRITE(pipe));
Ville Syrjälä5f3fb462013-10-01 18:02:13 +03001196 mutex_unlock(&dev->struct_mutex);
Ville Syrjäläc626d312013-03-27 17:49:13 +02001197
Ville Syrjälä5f3fb462013-10-01 18:02:13 +03001198 intel_plane->obj = NULL;
1199 }
Ville Syrjälä82284b62013-10-01 18:02:12 +03001200
Ville Syrjälä5f3fb462013-10-01 18:02:13 +03001201 return 0;
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001202}
1203
1204static void intel_destroy_plane(struct drm_plane *plane)
1205{
1206 struct intel_plane *intel_plane = to_intel_plane(plane);
1207 intel_disable_plane(plane);
1208 drm_plane_cleanup(plane);
1209 kfree(intel_plane);
1210}
1211
Jesse Barnes8ea30862012-01-03 08:05:39 -08001212int intel_sprite_set_colorkey(struct drm_device *dev, void *data,
1213 struct drm_file *file_priv)
1214{
1215 struct drm_intel_sprite_colorkey *set = data;
Jesse Barnes8ea30862012-01-03 08:05:39 -08001216 struct drm_plane *plane;
1217 struct intel_plane *intel_plane;
1218 int ret = 0;
1219
Daniel Vetter1cff8f62012-04-24 09:55:08 +02001220 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1221 return -ENODEV;
Jesse Barnes8ea30862012-01-03 08:05:39 -08001222
1223 /* Make sure we don't try to enable both src & dest simultaneously */
1224 if ((set->flags & (I915_SET_COLORKEY_DESTINATION | I915_SET_COLORKEY_SOURCE)) == (I915_SET_COLORKEY_DESTINATION | I915_SET_COLORKEY_SOURCE))
1225 return -EINVAL;
1226
Daniel Vettera0e99e62012-12-02 01:05:46 +01001227 drm_modeset_lock_all(dev);
Jesse Barnes8ea30862012-01-03 08:05:39 -08001228
Rob Clark7707e652014-07-17 23:30:04 -04001229 plane = drm_plane_find(dev, set->plane_id);
1230 if (!plane) {
Ville Syrjälä3f2c2052013-10-17 13:35:03 +03001231 ret = -ENOENT;
Jesse Barnes8ea30862012-01-03 08:05:39 -08001232 goto out_unlock;
1233 }
1234
Jesse Barnes8ea30862012-01-03 08:05:39 -08001235 intel_plane = to_intel_plane(plane);
1236 ret = intel_plane->update_colorkey(plane, set);
1237
1238out_unlock:
Daniel Vettera0e99e62012-12-02 01:05:46 +01001239 drm_modeset_unlock_all(dev);
Jesse Barnes8ea30862012-01-03 08:05:39 -08001240 return ret;
1241}
1242
1243int intel_sprite_get_colorkey(struct drm_device *dev, void *data,
1244 struct drm_file *file_priv)
1245{
1246 struct drm_intel_sprite_colorkey *get = data;
Jesse Barnes8ea30862012-01-03 08:05:39 -08001247 struct drm_plane *plane;
1248 struct intel_plane *intel_plane;
1249 int ret = 0;
1250
Daniel Vetter1cff8f62012-04-24 09:55:08 +02001251 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1252 return -ENODEV;
Jesse Barnes8ea30862012-01-03 08:05:39 -08001253
Daniel Vettera0e99e62012-12-02 01:05:46 +01001254 drm_modeset_lock_all(dev);
Jesse Barnes8ea30862012-01-03 08:05:39 -08001255
Rob Clark7707e652014-07-17 23:30:04 -04001256 plane = drm_plane_find(dev, get->plane_id);
1257 if (!plane) {
Ville Syrjälä3f2c2052013-10-17 13:35:03 +03001258 ret = -ENOENT;
Jesse Barnes8ea30862012-01-03 08:05:39 -08001259 goto out_unlock;
1260 }
1261
Jesse Barnes8ea30862012-01-03 08:05:39 -08001262 intel_plane = to_intel_plane(plane);
1263 intel_plane->get_colorkey(plane, get);
1264
1265out_unlock:
Daniel Vettera0e99e62012-12-02 01:05:46 +01001266 drm_modeset_unlock_all(dev);
Jesse Barnes8ea30862012-01-03 08:05:39 -08001267 return ret;
1268}
1269
Sonika Jindal48404c12014-08-22 14:06:04 +05301270int intel_plane_set_property(struct drm_plane *plane,
1271 struct drm_property *prop,
1272 uint64_t val)
Ville Syrjälä7ed6eee2014-08-05 11:26:55 +05301273{
1274 struct drm_device *dev = plane->dev;
1275 struct intel_plane *intel_plane = to_intel_plane(plane);
1276 uint64_t old_val;
1277 int ret = -ENOENT;
1278
1279 if (prop == dev->mode_config.rotation_property) {
1280 /* exactly one rotation angle please */
1281 if (hweight32(val & 0xf) != 1)
1282 return -EINVAL;
1283
Ville Syrjälä09dba002014-09-01 18:08:25 +03001284 if (intel_plane->rotation == val)
1285 return 0;
1286
Ville Syrjälä7ed6eee2014-08-05 11:26:55 +05301287 old_val = intel_plane->rotation;
1288 intel_plane->rotation = val;
1289 ret = intel_plane_restore(plane);
1290 if (ret)
1291 intel_plane->rotation = old_val;
1292 }
1293
1294 return ret;
1295}
1296
Ville Syrjäläe57465f2014-08-05 11:26:53 +05301297int intel_plane_restore(struct drm_plane *plane)
Jesse Barnes5e1bac22013-03-26 09:25:43 -07001298{
1299 struct intel_plane *intel_plane = to_intel_plane(plane);
1300
1301 if (!plane->crtc || !plane->fb)
Ville Syrjäläe57465f2014-08-05 11:26:53 +05301302 return 0;
Jesse Barnes5e1bac22013-03-26 09:25:43 -07001303
Sonika Jindal48404c12014-08-22 14:06:04 +05301304 return plane->funcs->update_plane(plane, plane->crtc, plane->fb,
Ville Syrjäläe57465f2014-08-05 11:26:53 +05301305 intel_plane->crtc_x, intel_plane->crtc_y,
1306 intel_plane->crtc_w, intel_plane->crtc_h,
1307 intel_plane->src_x, intel_plane->src_y,
1308 intel_plane->src_w, intel_plane->src_h);
Jesse Barnes5e1bac22013-03-26 09:25:43 -07001309}
1310
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03001311void intel_plane_disable(struct drm_plane *plane)
1312{
1313 if (!plane->crtc || !plane->fb)
1314 return;
1315
1316 intel_disable_plane(plane);
1317}
1318
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001319static const struct drm_plane_funcs intel_plane_funcs = {
1320 .update_plane = intel_update_plane,
1321 .disable_plane = intel_disable_plane,
1322 .destroy = intel_destroy_plane,
Ville Syrjälä7ed6eee2014-08-05 11:26:55 +05301323 .set_property = intel_plane_set_property,
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001324};
1325
Chris Wilsond1686ae2012-04-10 11:41:49 +01001326static uint32_t ilk_plane_formats[] = {
1327 DRM_FORMAT_XRGB8888,
1328 DRM_FORMAT_YUYV,
1329 DRM_FORMAT_YVYU,
1330 DRM_FORMAT_UYVY,
1331 DRM_FORMAT_VYUY,
1332};
1333
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001334static uint32_t snb_plane_formats[] = {
1335 DRM_FORMAT_XBGR8888,
1336 DRM_FORMAT_XRGB8888,
1337 DRM_FORMAT_YUYV,
1338 DRM_FORMAT_YVYU,
1339 DRM_FORMAT_UYVY,
1340 DRM_FORMAT_VYUY,
1341};
1342
Jesse Barnes7f1f3852013-04-02 11:22:20 -07001343static uint32_t vlv_plane_formats[] = {
1344 DRM_FORMAT_RGB565,
1345 DRM_FORMAT_ABGR8888,
1346 DRM_FORMAT_ARGB8888,
1347 DRM_FORMAT_XBGR8888,
1348 DRM_FORMAT_XRGB8888,
1349 DRM_FORMAT_XBGR2101010,
1350 DRM_FORMAT_ABGR2101010,
1351 DRM_FORMAT_YUYV,
1352 DRM_FORMAT_YVYU,
1353 DRM_FORMAT_UYVY,
1354 DRM_FORMAT_VYUY,
1355};
1356
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001357int
Jesse Barnes7f1f3852013-04-02 11:22:20 -07001358intel_plane_init(struct drm_device *dev, enum pipe pipe, int plane)
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001359{
1360 struct intel_plane *intel_plane;
1361 unsigned long possible_crtcs;
Chris Wilsond1686ae2012-04-10 11:41:49 +01001362 const uint32_t *plane_formats;
1363 int num_plane_formats;
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001364 int ret;
1365
Chris Wilsond1686ae2012-04-10 11:41:49 +01001366 if (INTEL_INFO(dev)->gen < 5)
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001367 return -ENODEV;
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001368
Daniel Vetterb14c5672013-09-19 12:18:32 +02001369 intel_plane = kzalloc(sizeof(*intel_plane), GFP_KERNEL);
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001370 if (!intel_plane)
1371 return -ENOMEM;
1372
Chris Wilsond1686ae2012-04-10 11:41:49 +01001373 switch (INTEL_INFO(dev)->gen) {
1374 case 5:
1375 case 6:
Damien Lespiau2d354c32012-10-22 18:19:27 +01001376 intel_plane->can_scale = true;
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001377 intel_plane->max_downscale = 16;
Chris Wilsond1686ae2012-04-10 11:41:49 +01001378 intel_plane->update_plane = ilk_update_plane;
1379 intel_plane->disable_plane = ilk_disable_plane;
1380 intel_plane->update_colorkey = ilk_update_colorkey;
1381 intel_plane->get_colorkey = ilk_get_colorkey;
1382
1383 if (IS_GEN6(dev)) {
1384 plane_formats = snb_plane_formats;
1385 num_plane_formats = ARRAY_SIZE(snb_plane_formats);
1386 } else {
1387 plane_formats = ilk_plane_formats;
1388 num_plane_formats = ARRAY_SIZE(ilk_plane_formats);
1389 }
1390 break;
1391
1392 case 7:
Ben Widawsky4e0bbc32013-11-02 21:07:07 -07001393 case 8:
Damien Lespiaud49f7092013-04-25 15:15:00 +01001394 if (IS_IVYBRIDGE(dev)) {
Damien Lespiau2d354c32012-10-22 18:19:27 +01001395 intel_plane->can_scale = true;
Damien Lespiaud49f7092013-04-25 15:15:00 +01001396 intel_plane->max_downscale = 2;
1397 } else {
1398 intel_plane->can_scale = false;
1399 intel_plane->max_downscale = 1;
1400 }
Chris Wilsond1686ae2012-04-10 11:41:49 +01001401
Jesse Barnes7f1f3852013-04-02 11:22:20 -07001402 if (IS_VALLEYVIEW(dev)) {
Jesse Barnes7f1f3852013-04-02 11:22:20 -07001403 intel_plane->update_plane = vlv_update_plane;
1404 intel_plane->disable_plane = vlv_disable_plane;
1405 intel_plane->update_colorkey = vlv_update_colorkey;
1406 intel_plane->get_colorkey = vlv_get_colorkey;
1407
1408 plane_formats = vlv_plane_formats;
1409 num_plane_formats = ARRAY_SIZE(vlv_plane_formats);
1410 } else {
Jesse Barnes7f1f3852013-04-02 11:22:20 -07001411 intel_plane->update_plane = ivb_update_plane;
1412 intel_plane->disable_plane = ivb_disable_plane;
1413 intel_plane->update_colorkey = ivb_update_colorkey;
1414 intel_plane->get_colorkey = ivb_get_colorkey;
1415
1416 plane_formats = snb_plane_formats;
1417 num_plane_formats = ARRAY_SIZE(snb_plane_formats);
1418 }
Chris Wilsond1686ae2012-04-10 11:41:49 +01001419 break;
1420
1421 default:
Jesper Juhla8b0bba2012-06-27 00:55:37 +02001422 kfree(intel_plane);
Chris Wilsond1686ae2012-04-10 11:41:49 +01001423 return -ENODEV;
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001424 }
1425
1426 intel_plane->pipe = pipe;
Jesse Barnes7f1f3852013-04-02 11:22:20 -07001427 intel_plane->plane = plane;
Ville Syrjälä76eebda2014-08-05 11:26:52 +05301428 intel_plane->rotation = BIT(DRM_ROTATE_0);
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001429 possible_crtcs = (1 << pipe);
Derek Foreman8fe8a3f2014-09-03 10:38:20 -03001430 ret = drm_universal_plane_init(dev, &intel_plane->base, possible_crtcs,
1431 &intel_plane_funcs,
1432 plane_formats, num_plane_formats,
1433 DRM_PLANE_TYPE_OVERLAY);
Ville Syrjälä7ed6eee2014-08-05 11:26:55 +05301434 if (ret) {
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001435 kfree(intel_plane);
Ville Syrjälä7ed6eee2014-08-05 11:26:55 +05301436 goto out;
1437 }
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001438
Ville Syrjälä7ed6eee2014-08-05 11:26:55 +05301439 if (!dev->mode_config.rotation_property)
1440 dev->mode_config.rotation_property =
1441 drm_mode_create_rotation_property(dev,
1442 BIT(DRM_ROTATE_0) |
1443 BIT(DRM_ROTATE_180));
1444
1445 if (dev->mode_config.rotation_property)
1446 drm_object_attach_property(&intel_plane->base.base,
1447 dev->mode_config.rotation_property,
1448 intel_plane->rotation);
1449
1450 out:
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001451 return ret;
1452}