blob: 03cfd62d17e274638b270ed9cd28e7a740711345 [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>
Jesse Barnesb840d907f2011-12-13 13:19:38 -080035#include "intel_drv.h"
David Howells760285e2012-10-02 18:01:07 +010036#include <drm/i915_drm.h>
Jesse Barnesb840d907f2011-12-13 13:19:38 -080037#include "i915_drv.h"
38
39static void
40ivb_update_plane(struct drm_plane *plane, struct drm_framebuffer *fb,
41 struct drm_i915_gem_object *obj, int crtc_x, int crtc_y,
42 unsigned int crtc_w, unsigned int crtc_h,
43 uint32_t x, uint32_t y,
44 uint32_t src_w, uint32_t src_h)
45{
46 struct drm_device *dev = plane->dev;
47 struct drm_i915_private *dev_priv = dev->dev_private;
48 struct intel_plane *intel_plane = to_intel_plane(plane);
49 int pipe = intel_plane->pipe;
50 u32 sprctl, sprscale = 0;
Damien Lespiau5a35e992012-10-26 18:20:12 +010051 unsigned long sprsurf_offset, linear_offset;
Ville Syrjälä2bd3c3c2012-10-31 17:50:20 +020052 int pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
Ville Syrjälä2c6602d2013-02-08 23:13:35 +020053 bool scaling_was_enabled = dev_priv->sprite_scaling_enabled;
Jesse Barnesb840d907f2011-12-13 13:19:38 -080054
55 sprctl = I915_READ(SPRCTL(pipe));
56
57 /* Mask out pixel format bits in case we change it */
58 sprctl &= ~SPRITE_PIXFORMAT_MASK;
59 sprctl &= ~SPRITE_RGB_ORDER_RGBX;
60 sprctl &= ~SPRITE_YUV_BYTE_ORDER_MASK;
Jesse Barnese86fe0d2012-06-26 13:10:11 -070061 sprctl &= ~SPRITE_TILED;
Jesse Barnesb840d907f2011-12-13 13:19:38 -080062
63 switch (fb->pixel_format) {
64 case DRM_FORMAT_XBGR8888:
Vijay Purushothaman5ee36912012-08-23 12:08:57 +053065 sprctl |= SPRITE_FORMAT_RGBX888 | SPRITE_RGB_ORDER_RGBX;
Jesse Barnesb840d907f2011-12-13 13:19:38 -080066 break;
67 case DRM_FORMAT_XRGB8888:
Vijay Purushothaman5ee36912012-08-23 12:08:57 +053068 sprctl |= SPRITE_FORMAT_RGBX888;
Jesse Barnesb840d907f2011-12-13 13:19:38 -080069 break;
70 case DRM_FORMAT_YUYV:
71 sprctl |= SPRITE_FORMAT_YUV422 | SPRITE_YUV_ORDER_YUYV;
Jesse Barnesb840d907f2011-12-13 13:19:38 -080072 break;
73 case DRM_FORMAT_YVYU:
74 sprctl |= SPRITE_FORMAT_YUV422 | SPRITE_YUV_ORDER_YVYU;
Jesse Barnesb840d907f2011-12-13 13:19:38 -080075 break;
76 case DRM_FORMAT_UYVY:
77 sprctl |= SPRITE_FORMAT_YUV422 | SPRITE_YUV_ORDER_UYVY;
Jesse Barnesb840d907f2011-12-13 13:19:38 -080078 break;
79 case DRM_FORMAT_VYUY:
80 sprctl |= SPRITE_FORMAT_YUV422 | SPRITE_YUV_ORDER_VYUY;
Jesse Barnesb840d907f2011-12-13 13:19:38 -080081 break;
82 default:
Ville Syrjälä28d491d2012-10-31 17:50:21 +020083 BUG();
Jesse Barnesb840d907f2011-12-13 13:19:38 -080084 }
85
86 if (obj->tiling_mode != I915_TILING_NONE)
87 sprctl |= SPRITE_TILED;
88
89 /* must disable */
90 sprctl |= SPRITE_TRICKLE_FEED_DISABLE;
91 sprctl |= SPRITE_ENABLE;
92
93 /* Sizes are 0 based */
94 src_w--;
95 src_h--;
96 crtc_w--;
97 crtc_h--;
98
99 intel_update_sprite_watermarks(dev, pipe, crtc_w, pixel_size);
100
101 /*
102 * IVB workaround: must disable low power watermarks for at least
103 * one frame before enabling scaling. LP watermarks can be re-enabled
104 * when scaling is disabled.
105 */
106 if (crtc_w != src_w || crtc_h != src_h) {
Ville Syrjälä2c6602d2013-02-08 23:13:35 +0200107 dev_priv->sprite_scaling_enabled |= 1 << pipe;
108
109 if (!scaling_was_enabled) {
Chris Wilson828ed3e2012-04-18 17:12:26 +0100110 intel_update_watermarks(dev);
111 intel_wait_for_vblank(dev, pipe);
112 }
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800113 sprscale = SPRITE_SCALE_ENABLE | (src_w << 16) | src_h;
Ville Syrjälä2c6602d2013-02-08 23:13:35 +0200114 } else
115 dev_priv->sprite_scaling_enabled &= ~(1 << pipe);
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800116
117 I915_WRITE(SPRSTRIDE(pipe), fb->pitches[0]);
118 I915_WRITE(SPRPOS(pipe), (crtc_y << 16) | crtc_x);
Damien Lespiauc54173a2012-10-26 18:20:11 +0100119
Chris Wilsonca320ac2012-12-19 12:14:22 +0000120 linear_offset = y * fb->pitches[0] + x * pixel_size;
Damien Lespiau5a35e992012-10-26 18:20:12 +0100121 sprsurf_offset =
122 intel_gen4_compute_offset_xtiled(&x, &y,
Chris Wilsonca320ac2012-12-19 12:14:22 +0000123 pixel_size, fb->pitches[0]);
Damien Lespiau5a35e992012-10-26 18:20:12 +0100124 linear_offset -= sprsurf_offset;
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800125
Damien Lespiau5a35e992012-10-26 18:20:12 +0100126 /* HSW consolidates SPRTILEOFF and SPRLINOFF into a single SPROFFSET
127 * register */
128 if (IS_HASWELL(dev))
129 I915_WRITE(SPROFFSET(pipe), (y << 16) | x);
130 else if (obj->tiling_mode != I915_TILING_NONE)
131 I915_WRITE(SPRTILEOFF(pipe), (y << 16) | x);
132 else
133 I915_WRITE(SPRLINOFF(pipe), linear_offset);
Damien Lespiauc54173a2012-10-26 18:20:11 +0100134
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800135 I915_WRITE(SPRSIZE(pipe), (crtc_h << 16) | crtc_w);
Damien Lespiau2d354c32012-10-22 18:19:27 +0100136 if (intel_plane->can_scale)
137 I915_WRITE(SPRSCALE(pipe), sprscale);
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800138 I915_WRITE(SPRCTL(pipe), sprctl);
Damien Lespiau5a35e992012-10-26 18:20:12 +0100139 I915_MODIFY_DISPBASE(SPRSURF(pipe), obj->gtt_offset + sprsurf_offset);
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800140 POSTING_READ(SPRSURF(pipe));
Ville Syrjälä2c6602d2013-02-08 23:13:35 +0200141
142 /* potentially re-enable LP watermarks */
143 if (scaling_was_enabled && !dev_priv->sprite_scaling_enabled)
144 intel_update_watermarks(dev);
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800145}
146
147static void
148ivb_disable_plane(struct drm_plane *plane)
149{
150 struct drm_device *dev = plane->dev;
151 struct drm_i915_private *dev_priv = dev->dev_private;
152 struct intel_plane *intel_plane = to_intel_plane(plane);
153 int pipe = intel_plane->pipe;
Ville Syrjälä2c6602d2013-02-08 23:13:35 +0200154 bool scaling_was_enabled = dev_priv->sprite_scaling_enabled;
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800155
156 I915_WRITE(SPRCTL(pipe), I915_READ(SPRCTL(pipe)) & ~SPRITE_ENABLE);
157 /* Can't leave the scaler enabled... */
Damien Lespiau2d354c32012-10-22 18:19:27 +0100158 if (intel_plane->can_scale)
159 I915_WRITE(SPRSCALE(pipe), 0);
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800160 /* Activate double buffered register update */
Armin Reese446f2542012-03-30 16:20:16 -0700161 I915_MODIFY_DISPBASE(SPRSURF(pipe), 0);
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800162 POSTING_READ(SPRSURF(pipe));
Chris Wilson828ed3e2012-04-18 17:12:26 +0100163
Ville Syrjälä2c6602d2013-02-08 23:13:35 +0200164 dev_priv->sprite_scaling_enabled &= ~(1 << pipe);
165
166 /* potentially re-enable LP watermarks */
167 if (scaling_was_enabled && !dev_priv->sprite_scaling_enabled)
168 intel_update_watermarks(dev);
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800169}
170
Jesse Barnes8ea30862012-01-03 08:05:39 -0800171static int
172ivb_update_colorkey(struct drm_plane *plane,
173 struct drm_intel_sprite_colorkey *key)
174{
175 struct drm_device *dev = plane->dev;
176 struct drm_i915_private *dev_priv = dev->dev_private;
177 struct intel_plane *intel_plane;
178 u32 sprctl;
179 int ret = 0;
180
181 intel_plane = to_intel_plane(plane);
182
183 I915_WRITE(SPRKEYVAL(intel_plane->pipe), key->min_value);
184 I915_WRITE(SPRKEYMAX(intel_plane->pipe), key->max_value);
185 I915_WRITE(SPRKEYMSK(intel_plane->pipe), key->channel_mask);
186
187 sprctl = I915_READ(SPRCTL(intel_plane->pipe));
188 sprctl &= ~(SPRITE_SOURCE_KEY | SPRITE_DEST_KEY);
189 if (key->flags & I915_SET_COLORKEY_DESTINATION)
190 sprctl |= SPRITE_DEST_KEY;
191 else if (key->flags & I915_SET_COLORKEY_SOURCE)
192 sprctl |= SPRITE_SOURCE_KEY;
193 I915_WRITE(SPRCTL(intel_plane->pipe), sprctl);
194
195 POSTING_READ(SPRKEYMSK(intel_plane->pipe));
196
197 return ret;
198}
199
200static void
201ivb_get_colorkey(struct drm_plane *plane, struct drm_intel_sprite_colorkey *key)
202{
203 struct drm_device *dev = plane->dev;
204 struct drm_i915_private *dev_priv = dev->dev_private;
205 struct intel_plane *intel_plane;
206 u32 sprctl;
207
208 intel_plane = to_intel_plane(plane);
209
210 key->min_value = I915_READ(SPRKEYVAL(intel_plane->pipe));
211 key->max_value = I915_READ(SPRKEYMAX(intel_plane->pipe));
212 key->channel_mask = I915_READ(SPRKEYMSK(intel_plane->pipe));
213 key->flags = 0;
214
215 sprctl = I915_READ(SPRCTL(intel_plane->pipe));
216
217 if (sprctl & SPRITE_DEST_KEY)
218 key->flags = I915_SET_COLORKEY_DESTINATION;
219 else if (sprctl & SPRITE_SOURCE_KEY)
220 key->flags = I915_SET_COLORKEY_SOURCE;
221 else
222 key->flags = I915_SET_COLORKEY_NONE;
223}
224
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800225static void
Chris Wilsond1686ae2012-04-10 11:41:49 +0100226ilk_update_plane(struct drm_plane *plane, struct drm_framebuffer *fb,
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800227 struct drm_i915_gem_object *obj, int crtc_x, int crtc_y,
228 unsigned int crtc_w, unsigned int crtc_h,
229 uint32_t x, uint32_t y,
230 uint32_t src_w, uint32_t src_h)
231{
232 struct drm_device *dev = plane->dev;
233 struct drm_i915_private *dev_priv = dev->dev_private;
234 struct intel_plane *intel_plane = to_intel_plane(plane);
Ville Syrjälä2bd3c3c2012-10-31 17:50:20 +0200235 int pipe = intel_plane->pipe;
Damien Lespiau5a35e992012-10-26 18:20:12 +0100236 unsigned long dvssurf_offset, linear_offset;
Chris Wilson8aaa81a2012-04-14 22:14:26 +0100237 u32 dvscntr, dvsscale;
Ville Syrjälä2bd3c3c2012-10-31 17:50:20 +0200238 int pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800239
240 dvscntr = I915_READ(DVSCNTR(pipe));
241
242 /* Mask out pixel format bits in case we change it */
243 dvscntr &= ~DVS_PIXFORMAT_MASK;
Jesse Barnesab2f9df2012-02-27 12:40:10 -0800244 dvscntr &= ~DVS_RGB_ORDER_XBGR;
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800245 dvscntr &= ~DVS_YUV_BYTE_ORDER_MASK;
Ander Conselvan de Oliveira79626522012-07-13 15:50:33 +0300246 dvscntr &= ~DVS_TILED;
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800247
248 switch (fb->pixel_format) {
249 case DRM_FORMAT_XBGR8888:
Jesse Barnesab2f9df2012-02-27 12:40:10 -0800250 dvscntr |= DVS_FORMAT_RGBX888 | DVS_RGB_ORDER_XBGR;
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800251 break;
252 case DRM_FORMAT_XRGB8888:
Jesse Barnesab2f9df2012-02-27 12:40:10 -0800253 dvscntr |= DVS_FORMAT_RGBX888;
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800254 break;
255 case DRM_FORMAT_YUYV:
256 dvscntr |= DVS_FORMAT_YUV422 | DVS_YUV_ORDER_YUYV;
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800257 break;
258 case DRM_FORMAT_YVYU:
259 dvscntr |= DVS_FORMAT_YUV422 | DVS_YUV_ORDER_YVYU;
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800260 break;
261 case DRM_FORMAT_UYVY:
262 dvscntr |= DVS_FORMAT_YUV422 | DVS_YUV_ORDER_UYVY;
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800263 break;
264 case DRM_FORMAT_VYUY:
265 dvscntr |= DVS_FORMAT_YUV422 | DVS_YUV_ORDER_VYUY;
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800266 break;
267 default:
Ville Syrjälä28d491d2012-10-31 17:50:21 +0200268 BUG();
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800269 }
270
271 if (obj->tiling_mode != I915_TILING_NONE)
272 dvscntr |= DVS_TILED;
273
Chris Wilsond1686ae2012-04-10 11:41:49 +0100274 if (IS_GEN6(dev))
275 dvscntr |= DVS_TRICKLE_FEED_DISABLE; /* must disable */
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800276 dvscntr |= DVS_ENABLE;
277
278 /* Sizes are 0 based */
279 src_w--;
280 src_h--;
281 crtc_w--;
282 crtc_h--;
283
284 intel_update_sprite_watermarks(dev, pipe, crtc_w, pixel_size);
285
Chris Wilson8aaa81a2012-04-14 22:14:26 +0100286 dvsscale = 0;
287 if (IS_GEN5(dev) || crtc_w != src_w || crtc_h != src_h)
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800288 dvsscale = DVS_SCALE_ENABLE | (src_w << 16) | src_h;
289
290 I915_WRITE(DVSSTRIDE(pipe), fb->pitches[0]);
291 I915_WRITE(DVSPOS(pipe), (crtc_y << 16) | crtc_x);
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800292
Chris Wilsonca320ac2012-12-19 12:14:22 +0000293 linear_offset = y * fb->pitches[0] + x * pixel_size;
Damien Lespiau5a35e992012-10-26 18:20:12 +0100294 dvssurf_offset =
295 intel_gen4_compute_offset_xtiled(&x, &y,
Chris Wilsonca320ac2012-12-19 12:14:22 +0000296 pixel_size, fb->pitches[0]);
Damien Lespiau5a35e992012-10-26 18:20:12 +0100297 linear_offset -= dvssurf_offset;
298
299 if (obj->tiling_mode != I915_TILING_NONE)
300 I915_WRITE(DVSTILEOFF(pipe), (y << 16) | x);
301 else
302 I915_WRITE(DVSLINOFF(pipe), linear_offset);
303
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800304 I915_WRITE(DVSSIZE(pipe), (crtc_h << 16) | crtc_w);
305 I915_WRITE(DVSSCALE(pipe), dvsscale);
306 I915_WRITE(DVSCNTR(pipe), dvscntr);
Damien Lespiau5a35e992012-10-26 18:20:12 +0100307 I915_MODIFY_DISPBASE(DVSSURF(pipe), obj->gtt_offset + dvssurf_offset);
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800308 POSTING_READ(DVSSURF(pipe));
309}
310
311static void
Chris Wilsond1686ae2012-04-10 11:41:49 +0100312ilk_disable_plane(struct drm_plane *plane)
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800313{
314 struct drm_device *dev = plane->dev;
315 struct drm_i915_private *dev_priv = dev->dev_private;
316 struct intel_plane *intel_plane = to_intel_plane(plane);
317 int pipe = intel_plane->pipe;
318
319 I915_WRITE(DVSCNTR(pipe), I915_READ(DVSCNTR(pipe)) & ~DVS_ENABLE);
320 /* Disable the scaler */
321 I915_WRITE(DVSSCALE(pipe), 0);
322 /* Flush double buffered register updates */
Armin Reese446f2542012-03-30 16:20:16 -0700323 I915_MODIFY_DISPBASE(DVSSURF(pipe), 0);
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800324 POSTING_READ(DVSSURF(pipe));
325}
326
Jesse Barnes175bd422011-12-13 13:19:39 -0800327static void
328intel_enable_primary(struct drm_crtc *crtc)
329{
330 struct drm_device *dev = crtc->dev;
331 struct drm_i915_private *dev_priv = dev->dev_private;
332 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
333 int reg = DSPCNTR(intel_crtc->plane);
334
Chris Wilson93314b52012-06-13 17:36:55 +0100335 if (!intel_crtc->primary_disabled)
336 return;
337
338 intel_crtc->primary_disabled = false;
339 intel_update_fbc(dev);
340
Jesse Barnes175bd422011-12-13 13:19:39 -0800341 I915_WRITE(reg, I915_READ(reg) | DISPLAY_PLANE_ENABLE);
342}
343
344static void
345intel_disable_primary(struct drm_crtc *crtc)
346{
347 struct drm_device *dev = crtc->dev;
348 struct drm_i915_private *dev_priv = dev->dev_private;
349 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
350 int reg = DSPCNTR(intel_crtc->plane);
351
Chris Wilson93314b52012-06-13 17:36:55 +0100352 if (intel_crtc->primary_disabled)
353 return;
354
Jesse Barnes175bd422011-12-13 13:19:39 -0800355 I915_WRITE(reg, I915_READ(reg) & ~DISPLAY_PLANE_ENABLE);
Chris Wilson93314b52012-06-13 17:36:55 +0100356
357 intel_crtc->primary_disabled = true;
358 intel_update_fbc(dev);
Jesse Barnes175bd422011-12-13 13:19:39 -0800359}
360
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800361static int
Chris Wilsond1686ae2012-04-10 11:41:49 +0100362ilk_update_colorkey(struct drm_plane *plane,
Jesse Barnes8ea30862012-01-03 08:05:39 -0800363 struct drm_intel_sprite_colorkey *key)
364{
365 struct drm_device *dev = plane->dev;
366 struct drm_i915_private *dev_priv = dev->dev_private;
367 struct intel_plane *intel_plane;
368 u32 dvscntr;
369 int ret = 0;
370
371 intel_plane = to_intel_plane(plane);
372
373 I915_WRITE(DVSKEYVAL(intel_plane->pipe), key->min_value);
374 I915_WRITE(DVSKEYMAX(intel_plane->pipe), key->max_value);
375 I915_WRITE(DVSKEYMSK(intel_plane->pipe), key->channel_mask);
376
377 dvscntr = I915_READ(DVSCNTR(intel_plane->pipe));
378 dvscntr &= ~(DVS_SOURCE_KEY | DVS_DEST_KEY);
379 if (key->flags & I915_SET_COLORKEY_DESTINATION)
380 dvscntr |= DVS_DEST_KEY;
381 else if (key->flags & I915_SET_COLORKEY_SOURCE)
382 dvscntr |= DVS_SOURCE_KEY;
383 I915_WRITE(DVSCNTR(intel_plane->pipe), dvscntr);
384
385 POSTING_READ(DVSKEYMSK(intel_plane->pipe));
386
387 return ret;
388}
389
390static void
Chris Wilsond1686ae2012-04-10 11:41:49 +0100391ilk_get_colorkey(struct drm_plane *plane, struct drm_intel_sprite_colorkey *key)
Jesse Barnes8ea30862012-01-03 08:05:39 -0800392{
393 struct drm_device *dev = plane->dev;
394 struct drm_i915_private *dev_priv = dev->dev_private;
395 struct intel_plane *intel_plane;
396 u32 dvscntr;
397
398 intel_plane = to_intel_plane(plane);
399
400 key->min_value = I915_READ(DVSKEYVAL(intel_plane->pipe));
401 key->max_value = I915_READ(DVSKEYMAX(intel_plane->pipe));
402 key->channel_mask = I915_READ(DVSKEYMSK(intel_plane->pipe));
403 key->flags = 0;
404
405 dvscntr = I915_READ(DVSCNTR(intel_plane->pipe));
406
407 if (dvscntr & DVS_DEST_KEY)
408 key->flags = I915_SET_COLORKEY_DESTINATION;
409 else if (dvscntr & DVS_SOURCE_KEY)
410 key->flags = I915_SET_COLORKEY_SOURCE;
411 else
412 key->flags = I915_SET_COLORKEY_NONE;
413}
414
415static int
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800416intel_update_plane(struct drm_plane *plane, struct drm_crtc *crtc,
417 struct drm_framebuffer *fb, int crtc_x, int crtc_y,
418 unsigned int crtc_w, unsigned int crtc_h,
419 uint32_t src_x, uint32_t src_y,
420 uint32_t src_w, uint32_t src_h)
421{
422 struct drm_device *dev = plane->dev;
423 struct drm_i915_private *dev_priv = dev->dev_private;
424 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
425 struct intel_plane *intel_plane = to_intel_plane(plane);
426 struct intel_framebuffer *intel_fb;
427 struct drm_i915_gem_object *obj, *old_obj;
428 int pipe = intel_plane->pipe;
Paulo Zanoni702e7a52012-10-23 18:29:59 -0200429 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
430 pipe);
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800431 int ret = 0;
432 int x = src_x >> 16, y = src_y >> 16;
433 int primary_w = crtc->mode.hdisplay, primary_h = crtc->mode.vdisplay;
434 bool disable_primary = false;
435
436 intel_fb = to_intel_framebuffer(fb);
437 obj = intel_fb->obj;
438
439 old_obj = intel_plane->obj;
440
Jesse Barnesb4db1e32012-03-20 10:59:09 -0700441 src_w = src_w >> 16;
442 src_h = src_h >> 16;
443
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800444 /* Pipe must be running... */
Paulo Zanoni702e7a52012-10-23 18:29:59 -0200445 if (!(I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_ENABLE))
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800446 return -EINVAL;
447
448 if (crtc_x >= primary_w || crtc_y >= primary_h)
449 return -EINVAL;
450
451 /* Don't modify another pipe's plane */
452 if (intel_plane->pipe != intel_crtc->pipe)
453 return -EINVAL;
454
Damien Lespiau94c64192012-10-29 15:14:51 +0000455 /* Sprite planes can be linear or x-tiled surfaces */
456 switch (obj->tiling_mode) {
457 case I915_TILING_NONE:
458 case I915_TILING_X:
459 break;
460 default:
461 return -EINVAL;
462 }
463
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800464 /*
465 * Clamp the width & height into the visible area. Note we don't
466 * try to scale the source if part of the visible region is offscreen.
467 * The caller must handle that by adjusting source offset and size.
468 */
469 if ((crtc_x < 0) && ((crtc_x + crtc_w) > 0)) {
470 crtc_w += crtc_x;
471 crtc_x = 0;
472 }
473 if ((crtc_x + crtc_w) <= 0) /* Nothing to display */
474 goto out;
475 if ((crtc_x + crtc_w) > primary_w)
476 crtc_w = primary_w - crtc_x;
477
478 if ((crtc_y < 0) && ((crtc_y + crtc_h) > 0)) {
479 crtc_h += crtc_y;
480 crtc_y = 0;
481 }
482 if ((crtc_y + crtc_h) <= 0) /* Nothing to display */
483 goto out;
484 if (crtc_y + crtc_h > primary_h)
485 crtc_h = primary_h - crtc_y;
486
487 if (!crtc_w || !crtc_h) /* Again, nothing to display */
488 goto out;
489
490 /*
Damien Lespiau2d354c32012-10-22 18:19:27 +0100491 * We may not have a scaler, eg. HSW does not have it any more
492 */
493 if (!intel_plane->can_scale && (crtc_w != src_w || crtc_h != src_h))
494 return -EINVAL;
495
496 /*
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800497 * We can take a larger source and scale it down, but
498 * only so much... 16x is the max on SNB.
499 */
500 if (((src_w * src_h) / (crtc_w * crtc_h)) > intel_plane->max_downscale)
501 return -EINVAL;
502
503 /*
504 * If the sprite is completely covering the primary plane,
505 * we can disable the primary and save power.
506 */
507 if ((crtc_x == 0) && (crtc_y == 0) &&
508 (crtc_w == primary_w) && (crtc_h == primary_h))
509 disable_primary = true;
510
511 mutex_lock(&dev->struct_mutex);
512
513 ret = intel_pin_and_fence_fb_obj(dev, obj, NULL);
Jesse Barnes00c2064b2012-01-13 15:48:39 -0800514 if (ret)
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800515 goto out_unlock;
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800516
517 intel_plane->obj = obj;
518
Jesse Barnes175bd422011-12-13 13:19:39 -0800519 /*
520 * Be sure to re-enable the primary before the sprite is no longer
521 * covering it fully.
522 */
Chris Wilson93314b52012-06-13 17:36:55 +0100523 if (!disable_primary)
Jesse Barnes175bd422011-12-13 13:19:39 -0800524 intel_enable_primary(crtc);
Jesse Barnes175bd422011-12-13 13:19:39 -0800525
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800526 intel_plane->update_plane(plane, fb, obj, crtc_x, crtc_y,
527 crtc_w, crtc_h, x, y, src_w, src_h);
528
Chris Wilson93314b52012-06-13 17:36:55 +0100529 if (disable_primary)
Jesse Barnes175bd422011-12-13 13:19:39 -0800530 intel_disable_primary(crtc);
Jesse Barnes175bd422011-12-13 13:19:39 -0800531
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800532 /* Unpin old obj after new one is active to avoid ugliness */
533 if (old_obj) {
534 /*
535 * It's fairly common to simply update the position of
536 * an existing object. In that case, we don't need to
537 * wait for vblank to avoid ugliness, we only need to
538 * do the pin & ref bookkeeping.
539 */
540 if (old_obj != obj) {
541 mutex_unlock(&dev->struct_mutex);
542 intel_wait_for_vblank(dev, to_intel_crtc(crtc)->pipe);
543 mutex_lock(&dev->struct_mutex);
544 }
Chris Wilson1690e1e2011-12-14 13:57:08 +0100545 intel_unpin_fb_obj(old_obj);
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800546 }
547
548out_unlock:
549 mutex_unlock(&dev->struct_mutex);
550out:
551 return ret;
552}
553
554static int
555intel_disable_plane(struct drm_plane *plane)
556{
557 struct drm_device *dev = plane->dev;
558 struct intel_plane *intel_plane = to_intel_plane(plane);
559 int ret = 0;
560
Chris Wilson93314b52012-06-13 17:36:55 +0100561 if (plane->crtc)
Jesse Barnes175bd422011-12-13 13:19:39 -0800562 intel_enable_primary(plane->crtc);
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800563 intel_plane->disable_plane(plane);
564
565 if (!intel_plane->obj)
566 goto out;
567
568 mutex_lock(&dev->struct_mutex);
Chris Wilson1690e1e2011-12-14 13:57:08 +0100569 intel_unpin_fb_obj(intel_plane->obj);
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800570 intel_plane->obj = NULL;
571 mutex_unlock(&dev->struct_mutex);
572out:
573
574 return ret;
575}
576
577static void intel_destroy_plane(struct drm_plane *plane)
578{
579 struct intel_plane *intel_plane = to_intel_plane(plane);
580 intel_disable_plane(plane);
581 drm_plane_cleanup(plane);
582 kfree(intel_plane);
583}
584
Jesse Barnes8ea30862012-01-03 08:05:39 -0800585int intel_sprite_set_colorkey(struct drm_device *dev, void *data,
586 struct drm_file *file_priv)
587{
588 struct drm_intel_sprite_colorkey *set = data;
Jesse Barnes8ea30862012-01-03 08:05:39 -0800589 struct drm_mode_object *obj;
590 struct drm_plane *plane;
591 struct intel_plane *intel_plane;
592 int ret = 0;
593
Daniel Vetter1cff8f62012-04-24 09:55:08 +0200594 if (!drm_core_check_feature(dev, DRIVER_MODESET))
595 return -ENODEV;
Jesse Barnes8ea30862012-01-03 08:05:39 -0800596
597 /* Make sure we don't try to enable both src & dest simultaneously */
598 if ((set->flags & (I915_SET_COLORKEY_DESTINATION | I915_SET_COLORKEY_SOURCE)) == (I915_SET_COLORKEY_DESTINATION | I915_SET_COLORKEY_SOURCE))
599 return -EINVAL;
600
Daniel Vettera0e99e62012-12-02 01:05:46 +0100601 drm_modeset_lock_all(dev);
Jesse Barnes8ea30862012-01-03 08:05:39 -0800602
603 obj = drm_mode_object_find(dev, set->plane_id, DRM_MODE_OBJECT_PLANE);
604 if (!obj) {
605 ret = -EINVAL;
606 goto out_unlock;
607 }
608
609 plane = obj_to_plane(obj);
610 intel_plane = to_intel_plane(plane);
611 ret = intel_plane->update_colorkey(plane, set);
612
613out_unlock:
Daniel Vettera0e99e62012-12-02 01:05:46 +0100614 drm_modeset_unlock_all(dev);
Jesse Barnes8ea30862012-01-03 08:05:39 -0800615 return ret;
616}
617
618int intel_sprite_get_colorkey(struct drm_device *dev, void *data,
619 struct drm_file *file_priv)
620{
621 struct drm_intel_sprite_colorkey *get = data;
Jesse Barnes8ea30862012-01-03 08:05:39 -0800622 struct drm_mode_object *obj;
623 struct drm_plane *plane;
624 struct intel_plane *intel_plane;
625 int ret = 0;
626
Daniel Vetter1cff8f62012-04-24 09:55:08 +0200627 if (!drm_core_check_feature(dev, DRIVER_MODESET))
628 return -ENODEV;
Jesse Barnes8ea30862012-01-03 08:05:39 -0800629
Daniel Vettera0e99e62012-12-02 01:05:46 +0100630 drm_modeset_lock_all(dev);
Jesse Barnes8ea30862012-01-03 08:05:39 -0800631
632 obj = drm_mode_object_find(dev, get->plane_id, DRM_MODE_OBJECT_PLANE);
633 if (!obj) {
634 ret = -EINVAL;
635 goto out_unlock;
636 }
637
638 plane = obj_to_plane(obj);
639 intel_plane = to_intel_plane(plane);
640 intel_plane->get_colorkey(plane, get);
641
642out_unlock:
Daniel Vettera0e99e62012-12-02 01:05:46 +0100643 drm_modeset_unlock_all(dev);
Jesse Barnes8ea30862012-01-03 08:05:39 -0800644 return ret;
645}
646
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800647static const struct drm_plane_funcs intel_plane_funcs = {
648 .update_plane = intel_update_plane,
649 .disable_plane = intel_disable_plane,
650 .destroy = intel_destroy_plane,
651};
652
Chris Wilsond1686ae2012-04-10 11:41:49 +0100653static uint32_t ilk_plane_formats[] = {
654 DRM_FORMAT_XRGB8888,
655 DRM_FORMAT_YUYV,
656 DRM_FORMAT_YVYU,
657 DRM_FORMAT_UYVY,
658 DRM_FORMAT_VYUY,
659};
660
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800661static uint32_t snb_plane_formats[] = {
662 DRM_FORMAT_XBGR8888,
663 DRM_FORMAT_XRGB8888,
664 DRM_FORMAT_YUYV,
665 DRM_FORMAT_YVYU,
666 DRM_FORMAT_UYVY,
667 DRM_FORMAT_VYUY,
668};
669
670int
671intel_plane_init(struct drm_device *dev, enum pipe pipe)
672{
673 struct intel_plane *intel_plane;
674 unsigned long possible_crtcs;
Chris Wilsond1686ae2012-04-10 11:41:49 +0100675 const uint32_t *plane_formats;
676 int num_plane_formats;
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800677 int ret;
678
Chris Wilsond1686ae2012-04-10 11:41:49 +0100679 if (INTEL_INFO(dev)->gen < 5)
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800680 return -ENODEV;
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800681
682 intel_plane = kzalloc(sizeof(struct intel_plane), GFP_KERNEL);
683 if (!intel_plane)
684 return -ENOMEM;
685
Chris Wilsond1686ae2012-04-10 11:41:49 +0100686 switch (INTEL_INFO(dev)->gen) {
687 case 5:
688 case 6:
Damien Lespiau2d354c32012-10-22 18:19:27 +0100689 intel_plane->can_scale = true;
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800690 intel_plane->max_downscale = 16;
Chris Wilsond1686ae2012-04-10 11:41:49 +0100691 intel_plane->update_plane = ilk_update_plane;
692 intel_plane->disable_plane = ilk_disable_plane;
693 intel_plane->update_colorkey = ilk_update_colorkey;
694 intel_plane->get_colorkey = ilk_get_colorkey;
695
696 if (IS_GEN6(dev)) {
697 plane_formats = snb_plane_formats;
698 num_plane_formats = ARRAY_SIZE(snb_plane_formats);
699 } else {
700 plane_formats = ilk_plane_formats;
701 num_plane_formats = ARRAY_SIZE(ilk_plane_formats);
702 }
703 break;
704
705 case 7:
Damien Lespiau4d8d71b2012-10-25 18:06:19 +0100706 if (IS_HASWELL(dev) || IS_VALLEYVIEW(dev))
Damien Lespiau2d354c32012-10-22 18:19:27 +0100707 intel_plane->can_scale = false;
708 else
709 intel_plane->can_scale = true;
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800710 intel_plane->max_downscale = 2;
711 intel_plane->update_plane = ivb_update_plane;
712 intel_plane->disable_plane = ivb_disable_plane;
Jesse Barnes8ea30862012-01-03 08:05:39 -0800713 intel_plane->update_colorkey = ivb_update_colorkey;
714 intel_plane->get_colorkey = ivb_get_colorkey;
Chris Wilsond1686ae2012-04-10 11:41:49 +0100715
716 plane_formats = snb_plane_formats;
717 num_plane_formats = ARRAY_SIZE(snb_plane_formats);
718 break;
719
720 default:
Jesper Juhla8b0bba2012-06-27 00:55:37 +0200721 kfree(intel_plane);
Chris Wilsond1686ae2012-04-10 11:41:49 +0100722 return -ENODEV;
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800723 }
724
725 intel_plane->pipe = pipe;
726 possible_crtcs = (1 << pipe);
727 ret = drm_plane_init(dev, &intel_plane->base, possible_crtcs,
Chris Wilsond1686ae2012-04-10 11:41:49 +0100728 &intel_plane_funcs,
729 plane_formats, num_plane_formats,
730 false);
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800731 if (ret)
732 kfree(intel_plane);
733
734 return ret;
735}