blob: b859f944b53bdd2319c35e12b21cc7dd9fea9c7c [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
40static void
Ville Syrjäläb39d53f2013-08-06 22:24:09 +030041vlv_update_plane(struct drm_plane *dplane, struct drm_crtc *crtc,
42 struct drm_framebuffer *fb,
Jesse Barnes7f1f3852013-04-02 11:22:20 -070043 struct drm_i915_gem_object *obj, int crtc_x, int crtc_y,
44 unsigned int crtc_w, unsigned int crtc_h,
45 uint32_t x, uint32_t y,
46 uint32_t src_w, uint32_t src_h)
47{
48 struct drm_device *dev = dplane->dev;
49 struct drm_i915_private *dev_priv = dev->dev_private;
50 struct intel_plane *intel_plane = to_intel_plane(dplane);
51 int pipe = intel_plane->pipe;
52 int plane = intel_plane->plane;
53 u32 sprctl;
54 unsigned long sprsurf_offset, linear_offset;
55 int pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
56
57 sprctl = I915_READ(SPCNTR(pipe, plane));
58
59 /* Mask out pixel format bits in case we change it */
60 sprctl &= ~SP_PIXFORMAT_MASK;
61 sprctl &= ~SP_YUV_BYTE_ORDER_MASK;
62 sprctl &= ~SP_TILED;
63
64 switch (fb->pixel_format) {
65 case DRM_FORMAT_YUYV:
66 sprctl |= SP_FORMAT_YUV422 | SP_YUV_ORDER_YUYV;
67 break;
68 case DRM_FORMAT_YVYU:
69 sprctl |= SP_FORMAT_YUV422 | SP_YUV_ORDER_YVYU;
70 break;
71 case DRM_FORMAT_UYVY:
72 sprctl |= SP_FORMAT_YUV422 | SP_YUV_ORDER_UYVY;
73 break;
74 case DRM_FORMAT_VYUY:
75 sprctl |= SP_FORMAT_YUV422 | SP_YUV_ORDER_VYUY;
76 break;
77 case DRM_FORMAT_RGB565:
78 sprctl |= SP_FORMAT_BGR565;
79 break;
80 case DRM_FORMAT_XRGB8888:
81 sprctl |= SP_FORMAT_BGRX8888;
82 break;
83 case DRM_FORMAT_ARGB8888:
84 sprctl |= SP_FORMAT_BGRA8888;
85 break;
86 case DRM_FORMAT_XBGR2101010:
87 sprctl |= SP_FORMAT_RGBX1010102;
88 break;
89 case DRM_FORMAT_ABGR2101010:
90 sprctl |= SP_FORMAT_RGBA1010102;
91 break;
92 case DRM_FORMAT_XBGR8888:
93 sprctl |= SP_FORMAT_RGBX8888;
94 break;
95 case DRM_FORMAT_ABGR8888:
96 sprctl |= SP_FORMAT_RGBA8888;
97 break;
98 default:
99 /*
100 * If we get here one of the upper layers failed to filter
101 * out the unsupported plane formats
102 */
103 BUG();
104 break;
105 }
106
107 if (obj->tiling_mode != I915_TILING_NONE)
108 sprctl |= SP_TILED;
109
110 sprctl |= SP_ENABLE;
111
Ville Syrjäläadf3d352013-08-06 22:24:11 +0300112 intel_update_sprite_watermarks(dplane, crtc, src_w, pixel_size, true,
Ville Syrjälä67ca28f2013-07-05 11:57:14 +0300113 src_w != crtc_w || src_h != crtc_h);
114
Jesse Barnes7f1f3852013-04-02 11:22:20 -0700115 /* Sizes are 0 based */
116 src_w--;
117 src_h--;
118 crtc_w--;
119 crtc_h--;
120
Jesse Barnes7f1f3852013-04-02 11:22:20 -0700121 I915_WRITE(SPSTRIDE(pipe, plane), fb->pitches[0]);
122 I915_WRITE(SPPOS(pipe, plane), (crtc_y << 16) | crtc_x);
123
124 linear_offset = y * fb->pitches[0] + x * pixel_size;
125 sprsurf_offset = intel_gen4_compute_page_offset(&x, &y,
126 obj->tiling_mode,
127 pixel_size,
128 fb->pitches[0]);
129 linear_offset -= sprsurf_offset;
130
131 if (obj->tiling_mode != I915_TILING_NONE)
132 I915_WRITE(SPTILEOFF(pipe, plane), (y << 16) | x);
133 else
134 I915_WRITE(SPLINOFF(pipe, plane), linear_offset);
135
136 I915_WRITE(SPSIZE(pipe, plane), (crtc_h << 16) | crtc_w);
137 I915_WRITE(SPCNTR(pipe, plane), sprctl);
Ben Widawskyf343c5f2013-07-05 14:41:04 -0700138 I915_MODIFY_DISPBASE(SPSURF(pipe, plane), i915_gem_obj_ggtt_offset(obj) +
Jesse Barnes7f1f3852013-04-02 11:22:20 -0700139 sprsurf_offset);
140 POSTING_READ(SPSURF(pipe, plane));
141}
142
143static void
Ville Syrjäläb39d53f2013-08-06 22:24:09 +0300144vlv_disable_plane(struct drm_plane *dplane, struct drm_crtc *crtc)
Jesse Barnes7f1f3852013-04-02 11:22:20 -0700145{
146 struct drm_device *dev = dplane->dev;
147 struct drm_i915_private *dev_priv = dev->dev_private;
148 struct intel_plane *intel_plane = to_intel_plane(dplane);
149 int pipe = intel_plane->pipe;
150 int plane = intel_plane->plane;
151
152 I915_WRITE(SPCNTR(pipe, plane), I915_READ(SPCNTR(pipe, plane)) &
153 ~SP_ENABLE);
154 /* Activate double buffered register update */
155 I915_MODIFY_DISPBASE(SPSURF(pipe, plane), 0);
156 POSTING_READ(SPSURF(pipe, plane));
Ville Syrjäläa95fd8c2013-08-06 22:24:12 +0300157
158 intel_update_sprite_watermarks(dplane, crtc, 0, 0, false, false);
Jesse Barnes7f1f3852013-04-02 11:22:20 -0700159}
160
161static int
162vlv_update_colorkey(struct drm_plane *dplane,
163 struct drm_intel_sprite_colorkey *key)
164{
165 struct drm_device *dev = dplane->dev;
166 struct drm_i915_private *dev_priv = dev->dev_private;
167 struct intel_plane *intel_plane = to_intel_plane(dplane);
168 int pipe = intel_plane->pipe;
169 int plane = intel_plane->plane;
170 u32 sprctl;
171
172 if (key->flags & I915_SET_COLORKEY_DESTINATION)
173 return -EINVAL;
174
175 I915_WRITE(SPKEYMINVAL(pipe, plane), key->min_value);
176 I915_WRITE(SPKEYMAXVAL(pipe, plane), key->max_value);
177 I915_WRITE(SPKEYMSK(pipe, plane), key->channel_mask);
178
179 sprctl = I915_READ(SPCNTR(pipe, plane));
180 sprctl &= ~SP_SOURCE_KEY;
181 if (key->flags & I915_SET_COLORKEY_SOURCE)
182 sprctl |= SP_SOURCE_KEY;
183 I915_WRITE(SPCNTR(pipe, plane), sprctl);
184
185 POSTING_READ(SPKEYMSK(pipe, plane));
186
187 return 0;
188}
189
190static void
191vlv_get_colorkey(struct drm_plane *dplane,
192 struct drm_intel_sprite_colorkey *key)
193{
194 struct drm_device *dev = dplane->dev;
195 struct drm_i915_private *dev_priv = dev->dev_private;
196 struct intel_plane *intel_plane = to_intel_plane(dplane);
197 int pipe = intel_plane->pipe;
198 int plane = intel_plane->plane;
199 u32 sprctl;
200
201 key->min_value = I915_READ(SPKEYMINVAL(pipe, plane));
202 key->max_value = I915_READ(SPKEYMAXVAL(pipe, plane));
203 key->channel_mask = I915_READ(SPKEYMSK(pipe, plane));
204
205 sprctl = I915_READ(SPCNTR(pipe, plane));
206 if (sprctl & SP_SOURCE_KEY)
207 key->flags = I915_SET_COLORKEY_SOURCE;
208 else
209 key->flags = I915_SET_COLORKEY_NONE;
210}
211
212static void
Ville Syrjäläb39d53f2013-08-06 22:24:09 +0300213ivb_update_plane(struct drm_plane *plane, struct drm_crtc *crtc,
214 struct drm_framebuffer *fb,
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800215 struct drm_i915_gem_object *obj, int crtc_x, int crtc_y,
216 unsigned int crtc_w, unsigned int crtc_h,
217 uint32_t x, uint32_t y,
218 uint32_t src_w, uint32_t src_h)
219{
220 struct drm_device *dev = plane->dev;
221 struct drm_i915_private *dev_priv = dev->dev_private;
222 struct intel_plane *intel_plane = to_intel_plane(plane);
223 int pipe = intel_plane->pipe;
224 u32 sprctl, sprscale = 0;
Damien Lespiau5a35e992012-10-26 18:20:12 +0100225 unsigned long sprsurf_offset, linear_offset;
Ville Syrjälä2bd3c3c2012-10-31 17:50:20 +0200226 int pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
Ville Syrjälä2c6602d2013-02-08 23:13:35 +0200227 bool scaling_was_enabled = dev_priv->sprite_scaling_enabled;
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800228
229 sprctl = I915_READ(SPRCTL(pipe));
230
231 /* Mask out pixel format bits in case we change it */
232 sprctl &= ~SPRITE_PIXFORMAT_MASK;
233 sprctl &= ~SPRITE_RGB_ORDER_RGBX;
234 sprctl &= ~SPRITE_YUV_BYTE_ORDER_MASK;
Jesse Barnese86fe0d2012-06-26 13:10:11 -0700235 sprctl &= ~SPRITE_TILED;
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800236
237 switch (fb->pixel_format) {
238 case DRM_FORMAT_XBGR8888:
Vijay Purushothaman5ee36912012-08-23 12:08:57 +0530239 sprctl |= SPRITE_FORMAT_RGBX888 | SPRITE_RGB_ORDER_RGBX;
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800240 break;
241 case DRM_FORMAT_XRGB8888:
Vijay Purushothaman5ee36912012-08-23 12:08:57 +0530242 sprctl |= SPRITE_FORMAT_RGBX888;
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800243 break;
244 case DRM_FORMAT_YUYV:
245 sprctl |= SPRITE_FORMAT_YUV422 | SPRITE_YUV_ORDER_YUYV;
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800246 break;
247 case DRM_FORMAT_YVYU:
248 sprctl |= SPRITE_FORMAT_YUV422 | SPRITE_YUV_ORDER_YVYU;
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800249 break;
250 case DRM_FORMAT_UYVY:
251 sprctl |= SPRITE_FORMAT_YUV422 | SPRITE_YUV_ORDER_UYVY;
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800252 break;
253 case DRM_FORMAT_VYUY:
254 sprctl |= SPRITE_FORMAT_YUV422 | SPRITE_YUV_ORDER_VYUY;
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800255 break;
256 default:
Ville Syrjälä28d491d2012-10-31 17:50:21 +0200257 BUG();
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800258 }
259
260 if (obj->tiling_mode != I915_TILING_NONE)
261 sprctl |= SPRITE_TILED;
262
Paulo Zanoni1f5d76d2013-08-23 19:51:28 -0300263 if (IS_HASWELL(dev))
264 sprctl &= ~SPRITE_TRICKLE_FEED_DISABLE;
265 else
266 sprctl |= SPRITE_TRICKLE_FEED_DISABLE;
267
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800268 sprctl |= SPRITE_ENABLE;
269
Ville Syrjälä86d3efc2013-01-18 19:11:38 +0200270 if (IS_HASWELL(dev))
271 sprctl |= SPRITE_PIPE_CSC_ENABLE;
272
Ville Syrjäläadf3d352013-08-06 22:24:11 +0300273 intel_update_sprite_watermarks(plane, crtc, src_w, pixel_size, true,
Ville Syrjälä67ca28f2013-07-05 11:57:14 +0300274 src_w != crtc_w || src_h != crtc_h);
275
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800276 /* Sizes are 0 based */
277 src_w--;
278 src_h--;
279 crtc_w--;
280 crtc_h--;
281
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800282 /*
283 * IVB workaround: must disable low power watermarks for at least
284 * one frame before enabling scaling. LP watermarks can be re-enabled
285 * when scaling is disabled.
286 */
287 if (crtc_w != src_w || crtc_h != src_h) {
Ville Syrjälä2c6602d2013-02-08 23:13:35 +0200288 dev_priv->sprite_scaling_enabled |= 1 << pipe;
289
290 if (!scaling_was_enabled) {
Ville Syrjälä46ba6142013-09-10 11:40:40 +0300291 intel_update_watermarks(crtc);
Chris Wilson828ed3e2012-04-18 17:12:26 +0100292 intel_wait_for_vblank(dev, pipe);
293 }
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800294 sprscale = SPRITE_SCALE_ENABLE | (src_w << 16) | src_h;
Ville Syrjälä2c6602d2013-02-08 23:13:35 +0200295 } else
296 dev_priv->sprite_scaling_enabled &= ~(1 << pipe);
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800297
298 I915_WRITE(SPRSTRIDE(pipe), fb->pitches[0]);
299 I915_WRITE(SPRPOS(pipe), (crtc_y << 16) | crtc_x);
Damien Lespiauc54173a2012-10-26 18:20:11 +0100300
Chris Wilsonca320ac2012-12-19 12:14:22 +0000301 linear_offset = y * fb->pitches[0] + x * pixel_size;
Damien Lespiau5a35e992012-10-26 18:20:12 +0100302 sprsurf_offset =
Chris Wilsonbc752862013-02-21 20:04:31 +0000303 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
304 pixel_size, fb->pitches[0]);
Damien Lespiau5a35e992012-10-26 18:20:12 +0100305 linear_offset -= sprsurf_offset;
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800306
Damien Lespiau5a35e992012-10-26 18:20:12 +0100307 /* HSW consolidates SPRTILEOFF and SPRLINOFF into a single SPROFFSET
308 * register */
309 if (IS_HASWELL(dev))
310 I915_WRITE(SPROFFSET(pipe), (y << 16) | x);
311 else if (obj->tiling_mode != I915_TILING_NONE)
312 I915_WRITE(SPRTILEOFF(pipe), (y << 16) | x);
313 else
314 I915_WRITE(SPRLINOFF(pipe), linear_offset);
Damien Lespiauc54173a2012-10-26 18:20:11 +0100315
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800316 I915_WRITE(SPRSIZE(pipe), (crtc_h << 16) | crtc_w);
Damien Lespiau2d354c32012-10-22 18:19:27 +0100317 if (intel_plane->can_scale)
318 I915_WRITE(SPRSCALE(pipe), sprscale);
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800319 I915_WRITE(SPRCTL(pipe), sprctl);
Ben Widawskyf343c5f2013-07-05 14:41:04 -0700320 I915_MODIFY_DISPBASE(SPRSURF(pipe),
321 i915_gem_obj_ggtt_offset(obj) + sprsurf_offset);
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800322 POSTING_READ(SPRSURF(pipe));
Ville Syrjälä2c6602d2013-02-08 23:13:35 +0200323
324 /* potentially re-enable LP watermarks */
325 if (scaling_was_enabled && !dev_priv->sprite_scaling_enabled)
Ville Syrjälä46ba6142013-09-10 11:40:40 +0300326 intel_update_watermarks(crtc);
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800327}
328
329static void
Ville Syrjäläb39d53f2013-08-06 22:24:09 +0300330ivb_disable_plane(struct drm_plane *plane, struct drm_crtc *crtc)
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800331{
332 struct drm_device *dev = plane->dev;
333 struct drm_i915_private *dev_priv = dev->dev_private;
334 struct intel_plane *intel_plane = to_intel_plane(plane);
335 int pipe = intel_plane->pipe;
Ville Syrjälä2c6602d2013-02-08 23:13:35 +0200336 bool scaling_was_enabled = dev_priv->sprite_scaling_enabled;
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800337
338 I915_WRITE(SPRCTL(pipe), I915_READ(SPRCTL(pipe)) & ~SPRITE_ENABLE);
339 /* Can't leave the scaler enabled... */
Damien Lespiau2d354c32012-10-22 18:19:27 +0100340 if (intel_plane->can_scale)
341 I915_WRITE(SPRSCALE(pipe), 0);
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800342 /* Activate double buffered register update */
Armin Reese446f2542012-03-30 16:20:16 -0700343 I915_MODIFY_DISPBASE(SPRSURF(pipe), 0);
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800344 POSTING_READ(SPRSURF(pipe));
Chris Wilson828ed3e2012-04-18 17:12:26 +0100345
Ville Syrjälä2c6602d2013-02-08 23:13:35 +0200346 dev_priv->sprite_scaling_enabled &= ~(1 << pipe);
347
Ville Syrjäläadf3d352013-08-06 22:24:11 +0300348 intel_update_sprite_watermarks(plane, crtc, 0, 0, false, false);
Paulo Zanoni4c4ff432013-05-24 11:59:17 -0300349
Ville Syrjälä2c6602d2013-02-08 23:13:35 +0200350 /* potentially re-enable LP watermarks */
351 if (scaling_was_enabled && !dev_priv->sprite_scaling_enabled)
Ville Syrjälä46ba6142013-09-10 11:40:40 +0300352 intel_update_watermarks(crtc);
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800353}
354
Jesse Barnes8ea30862012-01-03 08:05:39 -0800355static int
356ivb_update_colorkey(struct drm_plane *plane,
357 struct drm_intel_sprite_colorkey *key)
358{
359 struct drm_device *dev = plane->dev;
360 struct drm_i915_private *dev_priv = dev->dev_private;
361 struct intel_plane *intel_plane;
362 u32 sprctl;
363 int ret = 0;
364
365 intel_plane = to_intel_plane(plane);
366
367 I915_WRITE(SPRKEYVAL(intel_plane->pipe), key->min_value);
368 I915_WRITE(SPRKEYMAX(intel_plane->pipe), key->max_value);
369 I915_WRITE(SPRKEYMSK(intel_plane->pipe), key->channel_mask);
370
371 sprctl = I915_READ(SPRCTL(intel_plane->pipe));
372 sprctl &= ~(SPRITE_SOURCE_KEY | SPRITE_DEST_KEY);
373 if (key->flags & I915_SET_COLORKEY_DESTINATION)
374 sprctl |= SPRITE_DEST_KEY;
375 else if (key->flags & I915_SET_COLORKEY_SOURCE)
376 sprctl |= SPRITE_SOURCE_KEY;
377 I915_WRITE(SPRCTL(intel_plane->pipe), sprctl);
378
379 POSTING_READ(SPRKEYMSK(intel_plane->pipe));
380
381 return ret;
382}
383
384static void
385ivb_get_colorkey(struct drm_plane *plane, struct drm_intel_sprite_colorkey *key)
386{
387 struct drm_device *dev = plane->dev;
388 struct drm_i915_private *dev_priv = dev->dev_private;
389 struct intel_plane *intel_plane;
390 u32 sprctl;
391
392 intel_plane = to_intel_plane(plane);
393
394 key->min_value = I915_READ(SPRKEYVAL(intel_plane->pipe));
395 key->max_value = I915_READ(SPRKEYMAX(intel_plane->pipe));
396 key->channel_mask = I915_READ(SPRKEYMSK(intel_plane->pipe));
397 key->flags = 0;
398
399 sprctl = I915_READ(SPRCTL(intel_plane->pipe));
400
401 if (sprctl & SPRITE_DEST_KEY)
402 key->flags = I915_SET_COLORKEY_DESTINATION;
403 else if (sprctl & SPRITE_SOURCE_KEY)
404 key->flags = I915_SET_COLORKEY_SOURCE;
405 else
406 key->flags = I915_SET_COLORKEY_NONE;
407}
408
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800409static void
Ville Syrjäläb39d53f2013-08-06 22:24:09 +0300410ilk_update_plane(struct drm_plane *plane, struct drm_crtc *crtc,
411 struct drm_framebuffer *fb,
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800412 struct drm_i915_gem_object *obj, int crtc_x, int crtc_y,
413 unsigned int crtc_w, unsigned int crtc_h,
414 uint32_t x, uint32_t y,
415 uint32_t src_w, uint32_t src_h)
416{
417 struct drm_device *dev = plane->dev;
418 struct drm_i915_private *dev_priv = dev->dev_private;
419 struct intel_plane *intel_plane = to_intel_plane(plane);
Ville Syrjälä2bd3c3c2012-10-31 17:50:20 +0200420 int pipe = intel_plane->pipe;
Damien Lespiau5a35e992012-10-26 18:20:12 +0100421 unsigned long dvssurf_offset, linear_offset;
Chris Wilson8aaa81a2012-04-14 22:14:26 +0100422 u32 dvscntr, dvsscale;
Ville Syrjälä2bd3c3c2012-10-31 17:50:20 +0200423 int pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800424
425 dvscntr = I915_READ(DVSCNTR(pipe));
426
427 /* Mask out pixel format bits in case we change it */
428 dvscntr &= ~DVS_PIXFORMAT_MASK;
Jesse Barnesab2f9df2012-02-27 12:40:10 -0800429 dvscntr &= ~DVS_RGB_ORDER_XBGR;
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800430 dvscntr &= ~DVS_YUV_BYTE_ORDER_MASK;
Ander Conselvan de Oliveira79626522012-07-13 15:50:33 +0300431 dvscntr &= ~DVS_TILED;
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800432
433 switch (fb->pixel_format) {
434 case DRM_FORMAT_XBGR8888:
Jesse Barnesab2f9df2012-02-27 12:40:10 -0800435 dvscntr |= DVS_FORMAT_RGBX888 | DVS_RGB_ORDER_XBGR;
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800436 break;
437 case DRM_FORMAT_XRGB8888:
Jesse Barnesab2f9df2012-02-27 12:40:10 -0800438 dvscntr |= DVS_FORMAT_RGBX888;
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800439 break;
440 case DRM_FORMAT_YUYV:
441 dvscntr |= DVS_FORMAT_YUV422 | DVS_YUV_ORDER_YUYV;
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800442 break;
443 case DRM_FORMAT_YVYU:
444 dvscntr |= DVS_FORMAT_YUV422 | DVS_YUV_ORDER_YVYU;
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800445 break;
446 case DRM_FORMAT_UYVY:
447 dvscntr |= DVS_FORMAT_YUV422 | DVS_YUV_ORDER_UYVY;
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800448 break;
449 case DRM_FORMAT_VYUY:
450 dvscntr |= DVS_FORMAT_YUV422 | DVS_YUV_ORDER_VYUY;
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800451 break;
452 default:
Ville Syrjälä28d491d2012-10-31 17:50:21 +0200453 BUG();
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800454 }
455
456 if (obj->tiling_mode != I915_TILING_NONE)
457 dvscntr |= DVS_TILED;
458
Chris Wilsond1686ae2012-04-10 11:41:49 +0100459 if (IS_GEN6(dev))
460 dvscntr |= DVS_TRICKLE_FEED_DISABLE; /* must disable */
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800461 dvscntr |= DVS_ENABLE;
462
Ville Syrjäläadf3d352013-08-06 22:24:11 +0300463 intel_update_sprite_watermarks(plane, crtc, src_w, pixel_size, true,
Ville Syrjälä67ca28f2013-07-05 11:57:14 +0300464 src_w != crtc_w || src_h != crtc_h);
465
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800466 /* Sizes are 0 based */
467 src_w--;
468 src_h--;
469 crtc_w--;
470 crtc_h--;
471
Chris Wilson8aaa81a2012-04-14 22:14:26 +0100472 dvsscale = 0;
473 if (IS_GEN5(dev) || crtc_w != src_w || crtc_h != src_h)
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800474 dvsscale = DVS_SCALE_ENABLE | (src_w << 16) | src_h;
475
476 I915_WRITE(DVSSTRIDE(pipe), fb->pitches[0]);
477 I915_WRITE(DVSPOS(pipe), (crtc_y << 16) | crtc_x);
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800478
Chris Wilsonca320ac2012-12-19 12:14:22 +0000479 linear_offset = y * fb->pitches[0] + x * pixel_size;
Damien Lespiau5a35e992012-10-26 18:20:12 +0100480 dvssurf_offset =
Chris Wilsonbc752862013-02-21 20:04:31 +0000481 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
482 pixel_size, fb->pitches[0]);
Damien Lespiau5a35e992012-10-26 18:20:12 +0100483 linear_offset -= dvssurf_offset;
484
485 if (obj->tiling_mode != I915_TILING_NONE)
486 I915_WRITE(DVSTILEOFF(pipe), (y << 16) | x);
487 else
488 I915_WRITE(DVSLINOFF(pipe), linear_offset);
489
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800490 I915_WRITE(DVSSIZE(pipe), (crtc_h << 16) | crtc_w);
491 I915_WRITE(DVSSCALE(pipe), dvsscale);
492 I915_WRITE(DVSCNTR(pipe), dvscntr);
Ben Widawskyf343c5f2013-07-05 14:41:04 -0700493 I915_MODIFY_DISPBASE(DVSSURF(pipe),
494 i915_gem_obj_ggtt_offset(obj) + dvssurf_offset);
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800495 POSTING_READ(DVSSURF(pipe));
496}
497
498static void
Ville Syrjäläb39d53f2013-08-06 22:24:09 +0300499ilk_disable_plane(struct drm_plane *plane, struct drm_crtc *crtc)
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800500{
501 struct drm_device *dev = plane->dev;
502 struct drm_i915_private *dev_priv = dev->dev_private;
503 struct intel_plane *intel_plane = to_intel_plane(plane);
504 int pipe = intel_plane->pipe;
505
506 I915_WRITE(DVSCNTR(pipe), I915_READ(DVSCNTR(pipe)) & ~DVS_ENABLE);
507 /* Disable the scaler */
508 I915_WRITE(DVSSCALE(pipe), 0);
509 /* Flush double buffered register updates */
Armin Reese446f2542012-03-30 16:20:16 -0700510 I915_MODIFY_DISPBASE(DVSSURF(pipe), 0);
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800511 POSTING_READ(DVSSURF(pipe));
Ville Syrjäläa95fd8c2013-08-06 22:24:12 +0300512
513 intel_update_sprite_watermarks(plane, crtc, 0, 0, false, false);
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800514}
515
Jesse Barnes175bd422011-12-13 13:19:39 -0800516static void
517intel_enable_primary(struct drm_crtc *crtc)
518{
519 struct drm_device *dev = crtc->dev;
520 struct drm_i915_private *dev_priv = dev->dev_private;
521 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
522 int reg = DSPCNTR(intel_crtc->plane);
523
Chris Wilson93314b52012-06-13 17:36:55 +0100524 if (!intel_crtc->primary_disabled)
525 return;
526
527 intel_crtc->primary_disabled = false;
Ville Syrjälä82284b62013-10-01 18:02:12 +0300528
529 mutex_lock(&dev->struct_mutex);
Chris Wilson93314b52012-06-13 17:36:55 +0100530 intel_update_fbc(dev);
Ville Syrjälä82284b62013-10-01 18:02:12 +0300531 mutex_unlock(&dev->struct_mutex);
Chris Wilson93314b52012-06-13 17:36:55 +0100532
Jesse Barnes175bd422011-12-13 13:19:39 -0800533 I915_WRITE(reg, I915_READ(reg) | DISPLAY_PLANE_ENABLE);
534}
535
536static void
537intel_disable_primary(struct drm_crtc *crtc)
538{
539 struct drm_device *dev = crtc->dev;
540 struct drm_i915_private *dev_priv = dev->dev_private;
541 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
542 int reg = DSPCNTR(intel_crtc->plane);
543
Chris Wilson93314b52012-06-13 17:36:55 +0100544 if (intel_crtc->primary_disabled)
545 return;
546
Jesse Barnes175bd422011-12-13 13:19:39 -0800547 I915_WRITE(reg, I915_READ(reg) & ~DISPLAY_PLANE_ENABLE);
Chris Wilson93314b52012-06-13 17:36:55 +0100548
549 intel_crtc->primary_disabled = true;
Ville Syrjälä82284b62013-10-01 18:02:12 +0300550
551 mutex_lock(&dev->struct_mutex);
Chris Wilson93314b52012-06-13 17:36:55 +0100552 intel_update_fbc(dev);
Ville Syrjälä82284b62013-10-01 18:02:12 +0300553 mutex_unlock(&dev->struct_mutex);
Jesse Barnes175bd422011-12-13 13:19:39 -0800554}
555
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800556static int
Chris Wilsond1686ae2012-04-10 11:41:49 +0100557ilk_update_colorkey(struct drm_plane *plane,
Jesse Barnes8ea30862012-01-03 08:05:39 -0800558 struct drm_intel_sprite_colorkey *key)
559{
560 struct drm_device *dev = plane->dev;
561 struct drm_i915_private *dev_priv = dev->dev_private;
562 struct intel_plane *intel_plane;
563 u32 dvscntr;
564 int ret = 0;
565
566 intel_plane = to_intel_plane(plane);
567
568 I915_WRITE(DVSKEYVAL(intel_plane->pipe), key->min_value);
569 I915_WRITE(DVSKEYMAX(intel_plane->pipe), key->max_value);
570 I915_WRITE(DVSKEYMSK(intel_plane->pipe), key->channel_mask);
571
572 dvscntr = I915_READ(DVSCNTR(intel_plane->pipe));
573 dvscntr &= ~(DVS_SOURCE_KEY | DVS_DEST_KEY);
574 if (key->flags & I915_SET_COLORKEY_DESTINATION)
575 dvscntr |= DVS_DEST_KEY;
576 else if (key->flags & I915_SET_COLORKEY_SOURCE)
577 dvscntr |= DVS_SOURCE_KEY;
578 I915_WRITE(DVSCNTR(intel_plane->pipe), dvscntr);
579
580 POSTING_READ(DVSKEYMSK(intel_plane->pipe));
581
582 return ret;
583}
584
585static void
Chris Wilsond1686ae2012-04-10 11:41:49 +0100586ilk_get_colorkey(struct drm_plane *plane, struct drm_intel_sprite_colorkey *key)
Jesse Barnes8ea30862012-01-03 08:05:39 -0800587{
588 struct drm_device *dev = plane->dev;
589 struct drm_i915_private *dev_priv = dev->dev_private;
590 struct intel_plane *intel_plane;
591 u32 dvscntr;
592
593 intel_plane = to_intel_plane(plane);
594
595 key->min_value = I915_READ(DVSKEYVAL(intel_plane->pipe));
596 key->max_value = I915_READ(DVSKEYMAX(intel_plane->pipe));
597 key->channel_mask = I915_READ(DVSKEYMSK(intel_plane->pipe));
598 key->flags = 0;
599
600 dvscntr = I915_READ(DVSCNTR(intel_plane->pipe));
601
602 if (dvscntr & DVS_DEST_KEY)
603 key->flags = I915_SET_COLORKEY_DESTINATION;
604 else if (dvscntr & DVS_SOURCE_KEY)
605 key->flags = I915_SET_COLORKEY_SOURCE;
606 else
607 key->flags = I915_SET_COLORKEY_NONE;
608}
609
Ville Syrjälä17316932013-04-24 18:52:38 +0300610static bool
611format_is_yuv(uint32_t format)
612{
613 switch (format) {
614 case DRM_FORMAT_YUYV:
615 case DRM_FORMAT_UYVY:
616 case DRM_FORMAT_VYUY:
617 case DRM_FORMAT_YVYU:
618 return true;
619 default:
620 return false;
621 }
622}
623
Jesse Barnes8ea30862012-01-03 08:05:39 -0800624static int
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800625intel_update_plane(struct drm_plane *plane, struct drm_crtc *crtc,
626 struct drm_framebuffer *fb, int crtc_x, int crtc_y,
627 unsigned int crtc_w, unsigned int crtc_h,
628 uint32_t src_x, uint32_t src_y,
629 uint32_t src_w, uint32_t src_h)
630{
631 struct drm_device *dev = plane->dev;
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800632 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
633 struct intel_plane *intel_plane = to_intel_plane(plane);
634 struct intel_framebuffer *intel_fb;
635 struct drm_i915_gem_object *obj, *old_obj;
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800636 int ret = 0;
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800637 bool disable_primary = false;
Ville Syrjälä17316932013-04-24 18:52:38 +0300638 bool visible;
639 int hscale, vscale;
640 int max_scale, min_scale;
641 int pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
642 struct drm_rect src = {
643 /* sample coordinates in 16.16 fixed point */
644 .x1 = src_x,
645 .x2 = src_x + src_w,
646 .y1 = src_y,
647 .y2 = src_y + src_h,
648 };
649 struct drm_rect dst = {
650 /* integer pixels */
651 .x1 = crtc_x,
652 .x2 = crtc_x + crtc_w,
653 .y1 = crtc_y,
654 .y2 = crtc_y + crtc_h,
655 };
656 const struct drm_rect clip = {
Ville Syrjälä03c5b252013-10-01 18:02:11 +0300657 .x2 = intel_crtc->active ? intel_crtc->config.pipe_src_w : 0,
658 .y2 = intel_crtc->active ? intel_crtc->config.pipe_src_h : 0,
Ville Syrjälä17316932013-04-24 18:52:38 +0300659 };
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800660
661 intel_fb = to_intel_framebuffer(fb);
662 obj = intel_fb->obj;
663
664 old_obj = intel_plane->obj;
665
Jesse Barnes5e1bac22013-03-26 09:25:43 -0700666 intel_plane->crtc_x = crtc_x;
667 intel_plane->crtc_y = crtc_y;
668 intel_plane->crtc_w = crtc_w;
669 intel_plane->crtc_h = crtc_h;
670 intel_plane->src_x = src_x;
671 intel_plane->src_y = src_y;
672 intel_plane->src_w = src_w;
673 intel_plane->src_h = src_h;
674
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800675 /* Don't modify another pipe's plane */
Ville Syrjälä17316932013-04-24 18:52:38 +0300676 if (intel_plane->pipe != intel_crtc->pipe) {
677 DRM_DEBUG_KMS("Wrong plane <-> crtc mapping\n");
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800678 return -EINVAL;
Ville Syrjälä17316932013-04-24 18:52:38 +0300679 }
680
681 /* FIXME check all gen limits */
682 if (fb->width < 3 || fb->height < 3 || fb->pitches[0] > 16384) {
683 DRM_DEBUG_KMS("Unsuitable framebuffer for plane\n");
684 return -EINVAL;
685 }
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800686
Damien Lespiau94c64192012-10-29 15:14:51 +0000687 /* Sprite planes can be linear or x-tiled surfaces */
688 switch (obj->tiling_mode) {
689 case I915_TILING_NONE:
690 case I915_TILING_X:
691 break;
692 default:
Ville Syrjälä17316932013-04-24 18:52:38 +0300693 DRM_DEBUG_KMS("Unsupported tiling mode\n");
Damien Lespiau94c64192012-10-29 15:14:51 +0000694 return -EINVAL;
695 }
696
Ville Syrjälä3c3686c2013-04-24 18:52:39 +0300697 /*
698 * FIXME the following code does a bunch of fuzzy adjustments to the
699 * coordinates and sizes. We probably need some way to decide whether
700 * more strict checking should be done instead.
701 */
Ville Syrjälä17316932013-04-24 18:52:38 +0300702 max_scale = intel_plane->max_downscale << 16;
703 min_scale = intel_plane->can_scale ? 1 : (1 << 16);
704
Ville Syrjälä3c3686c2013-04-24 18:52:39 +0300705 hscale = drm_rect_calc_hscale_relaxed(&src, &dst, min_scale, max_scale);
706 BUG_ON(hscale < 0);
Ville Syrjälä17316932013-04-24 18:52:38 +0300707
Ville Syrjälä3c3686c2013-04-24 18:52:39 +0300708 vscale = drm_rect_calc_vscale_relaxed(&src, &dst, min_scale, max_scale);
709 BUG_ON(vscale < 0);
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800710
Ville Syrjälä17316932013-04-24 18:52:38 +0300711 visible = drm_rect_clip_scaled(&src, &dst, &clip, hscale, vscale);
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800712
Ville Syrjälä17316932013-04-24 18:52:38 +0300713 crtc_x = dst.x1;
714 crtc_y = dst.y1;
715 crtc_w = drm_rect_width(&dst);
716 crtc_h = drm_rect_height(&dst);
Damien Lespiau2d354c32012-10-22 18:19:27 +0100717
Ville Syrjälä17316932013-04-24 18:52:38 +0300718 if (visible) {
Ville Syrjälä3c3686c2013-04-24 18:52:39 +0300719 /* check again in case clipping clamped the results */
720 hscale = drm_rect_calc_hscale(&src, &dst, min_scale, max_scale);
721 if (hscale < 0) {
722 DRM_DEBUG_KMS("Horizontal scaling factor out of limits\n");
723 drm_rect_debug_print(&src, true);
724 drm_rect_debug_print(&dst, false);
725
726 return hscale;
727 }
728
729 vscale = drm_rect_calc_vscale(&src, &dst, min_scale, max_scale);
730 if (vscale < 0) {
731 DRM_DEBUG_KMS("Vertical scaling factor out of limits\n");
732 drm_rect_debug_print(&src, true);
733 drm_rect_debug_print(&dst, false);
734
735 return vscale;
736 }
737
Ville Syrjälä17316932013-04-24 18:52:38 +0300738 /* Make the source viewport size an exact multiple of the scaling factors. */
739 drm_rect_adjust_size(&src,
740 drm_rect_width(&dst) * hscale - drm_rect_width(&src),
741 drm_rect_height(&dst) * vscale - drm_rect_height(&src));
742
743 /* sanity check to make sure the src viewport wasn't enlarged */
744 WARN_ON(src.x1 < (int) src_x ||
745 src.y1 < (int) src_y ||
746 src.x2 > (int) (src_x + src_w) ||
747 src.y2 > (int) (src_y + src_h));
748
749 /*
750 * Hardware doesn't handle subpixel coordinates.
751 * Adjust to (macro)pixel boundary, but be careful not to
752 * increase the source viewport size, because that could
753 * push the downscaling factor out of bounds.
Ville Syrjälä17316932013-04-24 18:52:38 +0300754 */
755 src_x = src.x1 >> 16;
756 src_w = drm_rect_width(&src) >> 16;
757 src_y = src.y1 >> 16;
758 src_h = drm_rect_height(&src) >> 16;
759
760 if (format_is_yuv(fb->pixel_format)) {
761 src_x &= ~1;
762 src_w &= ~1;
763
764 /*
765 * Must keep src and dst the
766 * same if we can't scale.
767 */
768 if (!intel_plane->can_scale)
769 crtc_w &= ~1;
770
771 if (crtc_w == 0)
772 visible = false;
773 }
774 }
775
776 /* Check size restrictions when scaling */
777 if (visible && (src_w != crtc_w || src_h != crtc_h)) {
778 unsigned int width_bytes;
779
780 WARN_ON(!intel_plane->can_scale);
781
782 /* FIXME interlacing min height is 6 */
783
784 if (crtc_w < 3 || crtc_h < 3)
785 visible = false;
786
787 if (src_w < 3 || src_h < 3)
788 visible = false;
789
790 width_bytes = ((src_x * pixel_size) & 63) + src_w * pixel_size;
791
792 if (src_w > 2048 || src_h > 2048 ||
793 width_bytes > 4096 || fb->pitches[0] > 4096) {
794 DRM_DEBUG_KMS("Source dimensions exceed hardware limits\n");
795 return -EINVAL;
796 }
797 }
798
799 dst.x1 = crtc_x;
800 dst.x2 = crtc_x + crtc_w;
801 dst.y1 = crtc_y;
802 dst.y2 = crtc_y + crtc_h;
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800803
804 /*
805 * If the sprite is completely covering the primary plane,
806 * we can disable the primary and save power.
807 */
Ville Syrjälä17316932013-04-24 18:52:38 +0300808 disable_primary = drm_rect_equals(&dst, &clip);
Ville Syrjälä03c5b252013-10-01 18:02:11 +0300809 WARN_ON(disable_primary && !visible && intel_crtc->active);
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800810
811 mutex_lock(&dev->struct_mutex);
812
Chris Wilson693db182013-03-05 14:52:39 +0000813 /* Note that this will apply the VT-d workaround for scanouts,
814 * which is more restrictive than required for sprites. (The
815 * primary plane requires 256KiB alignment with 64 PTE padding,
816 * the sprite planes only require 128KiB alignment and 32 PTE padding.
817 */
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800818 ret = intel_pin_and_fence_fb_obj(dev, obj, NULL);
Ville Syrjälä82284b62013-10-01 18:02:12 +0300819
820 mutex_unlock(&dev->struct_mutex);
821
Jesse Barnes00c2064b2012-01-13 15:48:39 -0800822 if (ret)
Ville Syrjälä82284b62013-10-01 18:02:12 +0300823 return ret;
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800824
825 intel_plane->obj = obj;
826
Ville Syrjälä03c5b252013-10-01 18:02:11 +0300827 if (intel_crtc->active) {
828 /*
829 * Be sure to re-enable the primary before the sprite is no longer
830 * covering it fully.
831 */
832 if (!disable_primary)
833 intel_enable_primary(crtc);
Jesse Barnes175bd422011-12-13 13:19:39 -0800834
Ville Syrjälä03c5b252013-10-01 18:02:11 +0300835 if (visible)
836 intel_plane->update_plane(plane, crtc, fb, obj,
837 crtc_x, crtc_y, crtc_w, crtc_h,
838 src_x, src_y, src_w, src_h);
839 else
840 intel_plane->disable_plane(plane, crtc);
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800841
Ville Syrjälä03c5b252013-10-01 18:02:11 +0300842 if (disable_primary)
843 intel_disable_primary(crtc);
844 }
Jesse Barnes175bd422011-12-13 13:19:39 -0800845
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800846 /* Unpin old obj after new one is active to avoid ugliness */
847 if (old_obj) {
848 /*
849 * It's fairly common to simply update the position of
850 * an existing object. In that case, we don't need to
851 * wait for vblank to avoid ugliness, we only need to
852 * do the pin & ref bookkeeping.
853 */
Ville Syrjälä82284b62013-10-01 18:02:12 +0300854 if (old_obj != obj && intel_crtc->active)
855 intel_wait_for_vblank(dev, to_intel_crtc(crtc)->pipe);
856
857 mutex_lock(&dev->struct_mutex);
Chris Wilson1690e1e2011-12-14 13:57:08 +0100858 intel_unpin_fb_obj(old_obj);
Ville Syrjälä82284b62013-10-01 18:02:12 +0300859 mutex_unlock(&dev->struct_mutex);
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800860 }
861
Ville Syrjälä82284b62013-10-01 18:02:12 +0300862 return 0;
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800863}
864
865static int
866intel_disable_plane(struct drm_plane *plane)
867{
868 struct drm_device *dev = plane->dev;
869 struct intel_plane *intel_plane = to_intel_plane(plane);
Ville Syrjälä03c5b252013-10-01 18:02:11 +0300870 struct intel_crtc *intel_crtc;
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800871 int ret = 0;
872
Ville Syrjälä88a94a52013-08-07 13:30:23 +0300873 if (!plane->fb)
874 return 0;
875
876 if (WARN_ON(!plane->crtc))
877 return -EINVAL;
878
Ville Syrjälä03c5b252013-10-01 18:02:11 +0300879 intel_crtc = to_intel_crtc(plane->crtc);
880
881 if (intel_crtc->active) {
882 intel_enable_primary(plane->crtc);
883 intel_plane->disable_plane(plane, plane->crtc);
884 }
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800885
886 if (!intel_plane->obj)
887 goto out;
888
Ville Syrjälä03c5b252013-10-01 18:02:11 +0300889 if (intel_crtc->active)
890 intel_wait_for_vblank(dev, intel_plane->pipe);
Ville Syrjäläc626d312013-03-27 17:49:13 +0200891
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800892 mutex_lock(&dev->struct_mutex);
Chris Wilson1690e1e2011-12-14 13:57:08 +0100893 intel_unpin_fb_obj(intel_plane->obj);
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800894 mutex_unlock(&dev->struct_mutex);
Ville Syrjälä82284b62013-10-01 18:02:12 +0300895
896 intel_plane->obj = NULL;
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800897out:
898
899 return ret;
900}
901
902static void intel_destroy_plane(struct drm_plane *plane)
903{
904 struct intel_plane *intel_plane = to_intel_plane(plane);
905 intel_disable_plane(plane);
906 drm_plane_cleanup(plane);
907 kfree(intel_plane);
908}
909
Jesse Barnes8ea30862012-01-03 08:05:39 -0800910int intel_sprite_set_colorkey(struct drm_device *dev, void *data,
911 struct drm_file *file_priv)
912{
913 struct drm_intel_sprite_colorkey *set = data;
Jesse Barnes8ea30862012-01-03 08:05:39 -0800914 struct drm_mode_object *obj;
915 struct drm_plane *plane;
916 struct intel_plane *intel_plane;
917 int ret = 0;
918
Daniel Vetter1cff8f62012-04-24 09:55:08 +0200919 if (!drm_core_check_feature(dev, DRIVER_MODESET))
920 return -ENODEV;
Jesse Barnes8ea30862012-01-03 08:05:39 -0800921
922 /* Make sure we don't try to enable both src & dest simultaneously */
923 if ((set->flags & (I915_SET_COLORKEY_DESTINATION | I915_SET_COLORKEY_SOURCE)) == (I915_SET_COLORKEY_DESTINATION | I915_SET_COLORKEY_SOURCE))
924 return -EINVAL;
925
Daniel Vettera0e99e62012-12-02 01:05:46 +0100926 drm_modeset_lock_all(dev);
Jesse Barnes8ea30862012-01-03 08:05:39 -0800927
928 obj = drm_mode_object_find(dev, set->plane_id, DRM_MODE_OBJECT_PLANE);
929 if (!obj) {
930 ret = -EINVAL;
931 goto out_unlock;
932 }
933
934 plane = obj_to_plane(obj);
935 intel_plane = to_intel_plane(plane);
936 ret = intel_plane->update_colorkey(plane, set);
937
938out_unlock:
Daniel Vettera0e99e62012-12-02 01:05:46 +0100939 drm_modeset_unlock_all(dev);
Jesse Barnes8ea30862012-01-03 08:05:39 -0800940 return ret;
941}
942
943int intel_sprite_get_colorkey(struct drm_device *dev, void *data,
944 struct drm_file *file_priv)
945{
946 struct drm_intel_sprite_colorkey *get = data;
Jesse Barnes8ea30862012-01-03 08:05:39 -0800947 struct drm_mode_object *obj;
948 struct drm_plane *plane;
949 struct intel_plane *intel_plane;
950 int ret = 0;
951
Daniel Vetter1cff8f62012-04-24 09:55:08 +0200952 if (!drm_core_check_feature(dev, DRIVER_MODESET))
953 return -ENODEV;
Jesse Barnes8ea30862012-01-03 08:05:39 -0800954
Daniel Vettera0e99e62012-12-02 01:05:46 +0100955 drm_modeset_lock_all(dev);
Jesse Barnes8ea30862012-01-03 08:05:39 -0800956
957 obj = drm_mode_object_find(dev, get->plane_id, DRM_MODE_OBJECT_PLANE);
958 if (!obj) {
959 ret = -EINVAL;
960 goto out_unlock;
961 }
962
963 plane = obj_to_plane(obj);
964 intel_plane = to_intel_plane(plane);
965 intel_plane->get_colorkey(plane, get);
966
967out_unlock:
Daniel Vettera0e99e62012-12-02 01:05:46 +0100968 drm_modeset_unlock_all(dev);
Jesse Barnes8ea30862012-01-03 08:05:39 -0800969 return ret;
970}
971
Jesse Barnes5e1bac22013-03-26 09:25:43 -0700972void intel_plane_restore(struct drm_plane *plane)
973{
974 struct intel_plane *intel_plane = to_intel_plane(plane);
975
976 if (!plane->crtc || !plane->fb)
977 return;
978
979 intel_update_plane(plane, plane->crtc, plane->fb,
980 intel_plane->crtc_x, intel_plane->crtc_y,
981 intel_plane->crtc_w, intel_plane->crtc_h,
982 intel_plane->src_x, intel_plane->src_y,
983 intel_plane->src_w, intel_plane->src_h);
984}
985
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +0300986void intel_plane_disable(struct drm_plane *plane)
987{
988 if (!plane->crtc || !plane->fb)
989 return;
990
991 intel_disable_plane(plane);
992}
993
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800994static const struct drm_plane_funcs intel_plane_funcs = {
995 .update_plane = intel_update_plane,
996 .disable_plane = intel_disable_plane,
997 .destroy = intel_destroy_plane,
998};
999
Chris Wilsond1686ae2012-04-10 11:41:49 +01001000static uint32_t ilk_plane_formats[] = {
1001 DRM_FORMAT_XRGB8888,
1002 DRM_FORMAT_YUYV,
1003 DRM_FORMAT_YVYU,
1004 DRM_FORMAT_UYVY,
1005 DRM_FORMAT_VYUY,
1006};
1007
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001008static uint32_t snb_plane_formats[] = {
1009 DRM_FORMAT_XBGR8888,
1010 DRM_FORMAT_XRGB8888,
1011 DRM_FORMAT_YUYV,
1012 DRM_FORMAT_YVYU,
1013 DRM_FORMAT_UYVY,
1014 DRM_FORMAT_VYUY,
1015};
1016
Jesse Barnes7f1f3852013-04-02 11:22:20 -07001017static uint32_t vlv_plane_formats[] = {
1018 DRM_FORMAT_RGB565,
1019 DRM_FORMAT_ABGR8888,
1020 DRM_FORMAT_ARGB8888,
1021 DRM_FORMAT_XBGR8888,
1022 DRM_FORMAT_XRGB8888,
1023 DRM_FORMAT_XBGR2101010,
1024 DRM_FORMAT_ABGR2101010,
1025 DRM_FORMAT_YUYV,
1026 DRM_FORMAT_YVYU,
1027 DRM_FORMAT_UYVY,
1028 DRM_FORMAT_VYUY,
1029};
1030
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001031int
Jesse Barnes7f1f3852013-04-02 11:22:20 -07001032intel_plane_init(struct drm_device *dev, enum pipe pipe, int plane)
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001033{
1034 struct intel_plane *intel_plane;
1035 unsigned long possible_crtcs;
Chris Wilsond1686ae2012-04-10 11:41:49 +01001036 const uint32_t *plane_formats;
1037 int num_plane_formats;
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001038 int ret;
1039
Chris Wilsond1686ae2012-04-10 11:41:49 +01001040 if (INTEL_INFO(dev)->gen < 5)
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001041 return -ENODEV;
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001042
Daniel Vetterb14c5672013-09-19 12:18:32 +02001043 intel_plane = kzalloc(sizeof(*intel_plane), GFP_KERNEL);
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001044 if (!intel_plane)
1045 return -ENOMEM;
1046
Chris Wilsond1686ae2012-04-10 11:41:49 +01001047 switch (INTEL_INFO(dev)->gen) {
1048 case 5:
1049 case 6:
Damien Lespiau2d354c32012-10-22 18:19:27 +01001050 intel_plane->can_scale = true;
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001051 intel_plane->max_downscale = 16;
Chris Wilsond1686ae2012-04-10 11:41:49 +01001052 intel_plane->update_plane = ilk_update_plane;
1053 intel_plane->disable_plane = ilk_disable_plane;
1054 intel_plane->update_colorkey = ilk_update_colorkey;
1055 intel_plane->get_colorkey = ilk_get_colorkey;
1056
1057 if (IS_GEN6(dev)) {
1058 plane_formats = snb_plane_formats;
1059 num_plane_formats = ARRAY_SIZE(snb_plane_formats);
1060 } else {
1061 plane_formats = ilk_plane_formats;
1062 num_plane_formats = ARRAY_SIZE(ilk_plane_formats);
1063 }
1064 break;
1065
1066 case 7:
Damien Lespiaud49f7092013-04-25 15:15:00 +01001067 if (IS_IVYBRIDGE(dev)) {
Damien Lespiau2d354c32012-10-22 18:19:27 +01001068 intel_plane->can_scale = true;
Damien Lespiaud49f7092013-04-25 15:15:00 +01001069 intel_plane->max_downscale = 2;
1070 } else {
1071 intel_plane->can_scale = false;
1072 intel_plane->max_downscale = 1;
1073 }
Chris Wilsond1686ae2012-04-10 11:41:49 +01001074
Jesse Barnes7f1f3852013-04-02 11:22:20 -07001075 if (IS_VALLEYVIEW(dev)) {
Jesse Barnes7f1f3852013-04-02 11:22:20 -07001076 intel_plane->update_plane = vlv_update_plane;
1077 intel_plane->disable_plane = vlv_disable_plane;
1078 intel_plane->update_colorkey = vlv_update_colorkey;
1079 intel_plane->get_colorkey = vlv_get_colorkey;
1080
1081 plane_formats = vlv_plane_formats;
1082 num_plane_formats = ARRAY_SIZE(vlv_plane_formats);
1083 } else {
Jesse Barnes7f1f3852013-04-02 11:22:20 -07001084 intel_plane->update_plane = ivb_update_plane;
1085 intel_plane->disable_plane = ivb_disable_plane;
1086 intel_plane->update_colorkey = ivb_update_colorkey;
1087 intel_plane->get_colorkey = ivb_get_colorkey;
1088
1089 plane_formats = snb_plane_formats;
1090 num_plane_formats = ARRAY_SIZE(snb_plane_formats);
1091 }
Chris Wilsond1686ae2012-04-10 11:41:49 +01001092 break;
1093
1094 default:
Jesper Juhla8b0bba2012-06-27 00:55:37 +02001095 kfree(intel_plane);
Chris Wilsond1686ae2012-04-10 11:41:49 +01001096 return -ENODEV;
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001097 }
1098
1099 intel_plane->pipe = pipe;
Jesse Barnes7f1f3852013-04-02 11:22:20 -07001100 intel_plane->plane = plane;
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001101 possible_crtcs = (1 << pipe);
1102 ret = drm_plane_init(dev, &intel_plane->base, possible_crtcs,
Chris Wilsond1686ae2012-04-10 11:41:49 +01001103 &intel_plane_funcs,
1104 plane_formats, num_plane_formats,
1105 false);
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001106 if (ret)
1107 kfree(intel_plane);
1108
1109 return ret;
1110}